7/24/2021

yolo data coordinate format, draw rectangle by cv2

yolo v5 data coordinate format


 




ex)

str_v = "32 0.262 0.7878 0.314 0.385"



#read image

cvmat = cv2.imread(img_path)



#get height, width

h,w,_ = cvmat.shape



#extract x1, y1 <- center, width, height

x1 = int( float(str_v.split(' ')[1]) * w )

y1 = int( float(str_v.split(' ')[2]) * h )

xw = int( float(str_v.split(' ')[3]) * w /2)

yw = int( float(str_v.split(' ')[4]) * h /2)



#make x1,y1, x2,y2

start_point = (x1 - xw, y1 - yw )

end_point = (x1 + xw, y1 + yw )



#draw rectangle

cvmat = cv2.rectangle(cvmat, start_point, end_point, (255, 0, 0), 2)



Thank you.

https://study.marearts.com


find all txt file in dir and read line by line in txt. python example source code

 

python code

.

import glob

files = []
start_dir = '/MareArts/dev/dataset/coco_yolov5pytorch/train/labels/'
# output_path = '/Volumes/output/'
pattern = "*.txt"
search_dir = os.path.join(start_dir,pattern)

total = 0
txt_list = glob.glob(search_dir)
for i,v in enumerate(txt_list):
print(i,v)
file1 = open(v, 'r')
Lines = file1.readlines()
count = 0
# Strips the newline character
for line in Lines:
count += 1
print("Line{}: {}".format(count, line.strip()))

.


result

0 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000253429_jpg.rf.7f12980ae02c096d1b9ac0b574d3ac1b.txt
Line1: 10 0.6768149882903981 0.2203125 0.6463700234192038 0.196875
Line2: 36 0.32786885245901637 0.67265625 0.6557377049180327 0.6359375
Line3: 48 0.34543325526932084 0.57578125 0.36533957845433257 0.6765625
1 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009465_jpg.rf.3d5a6b94f8c1afdc004f2641cd578912.txt
Line1: 36 0.6703125 0.8002309468822171 0.2109375 0.22170900692840648
Line2: 73 0.06015625 0.684757505773672 0.1171875 0.42494226327944573
Line3: 48 0.16484375 0.7621247113163973 0.04375 0.16166281755196305
Line4: 48 0.24921875 0.7829099307159353 0.0296875 0.16628175519630484
Line5: 48 0.2234375 0.7736720554272517 0.0359375 0.17090069284064666
Line6: 48 0.19765625 0.76905311778291 0.03125 0.16628175519630484
Line7: 48 0.1265625 0.7840646651270208 0.075 0.20554272517321015
2 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009466_jpg.rf.5b49bdd7c037ed299dd3b6fd2531c34d.txt
Line1: 20 0.2625 0.45433255269320844 0.3078125 0.4496487119437939
3 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009469_jpg.rf.38d5d254d972459fba2431d6ed2baffe.txt
Line1: 27 0.262 0.7878048780487805 0.314 0.3853658536585366
Line2: 48 0.304 0.5146341463414634 0.282 0.9170731707317074
4 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009474_jpg.rf.fe5b0057cd113427b333ccd60c2d10d5.txt
Line1: 10 0.3203125 0.5530660377358491 0.2953125 0.8702830188679245
5 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009478_jpg.rf.1584a7148b62cc3612da3407115b14b7.txt
Line1: 33 0.52421875 0.5612745098039216 0.21875 0.44607843137254904
...
...


Thank you.
https://study.marearts.com

check pytorch, Tensorflow can use GPU

 

test tensorflow which can use GPU

#method 1
import tensorflow as tf
tf.test.is_built_with_cuda()
> Ture

#method 2
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
> ..

test pytorch can use GPU

#method 3
import torch
torch.cuda.is_available()
>>> True

torch.cuda.current_device()
>>> 0

torch.cuda.device(0)
>>> <torch.cuda.device at 0x7efce0b03be0>

torch.cuda.device_count()
>>> 1

torch.cuda.get_device_name(0)
>>> 'GeForce GTX 950M'



Thank you.
www.MareArts.com

7/11/2021

Hide current working directory in terminal

 

do it on your terminal

$ export OLD_PS1=$PS1                    # save Long Prompt to OLD_PS1
$ export PS1="\u > "export PS1="\u $ "   # change to PS1 to Short Prompt
$ export PS1=$OLD_PS1                    # restore Long Prompt to PS1

7/09/2021

curlpp/cURLpp.hpp: No such file or directory

issue: 

curlpp/cURLpp.hpp: No such file or directory


solution:

install package which related with curl

> sudo apt-get install pkg-config libcurlpp-dev libcurl4-openssl-dev


* Check header file location

> dpkg -S cURLpp.hpp

libcurlpp-dev:amd64: /usr/include/curlpp/cURLpp.hpp


Thank you.


study.marearts.com

marearts



7/08/2021

C linux get Mac Address

 In my case, I failed to get Mac from eth0 devices.

So I need to check all devices to get Mac address.

This is my solution example code:

(study.marearts.com)


..

#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <sstream>
#include <string>
#include <iostream>
std::string getMac(){
struct ifreq ifr;
struct ifconf ifc;
char buf[1024];
int success = 0;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock == -1) { /* handle error*/ };
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) { /* handle error */ }
struct ifreq* it = ifc.ifc_req;
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
int a=0;
for (; it != end; ++it) {
strcpy(ifr.ifr_name, it->ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
success = 1;
break;
}
}
}
else {
/* handle error */
return "0";
}
}
unsigned char mac_address[6];
if (success)
{
memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
std::stringstream ss;
std::string target;
char buffer [50];
sprintf (buffer, "%x-%x-%x-%x-%x-%x", mac_address[0], mac_address[1], mac_address[2], mac_address[3], mac_address[4], mac_address[5]);
ss << buffer;
ss >> target;
return target;
}
return "0";
}
int main()
{
std::cout<< getMac() << std::endl;
}

..

Output is

44-a5-6e-63-a4-bb


Thank you.

marearts



python date time add or subtract

datetime package is very useful.

see the example code..

..
import datetime
print(datetime.datetime.now())
print(datetime.datetime.now().date())
lastHourDateTime = datetime.datetime.now() + datetime.timedelta(days = +10)
print(lastHourDateTime.date())
print(lastHourDateTime.time())
lastHourDateTime = datetime.datetime.now() + datetime.timedelta(hours = 10)
print(lastHourDateTime.date())
print(lastHourDateTime.time())
lastHourDateTime = datetime.datetime.now() + datetime.timedelta(hours = -10)
print(lastHourDateTime.date())
print(lastHourDateTime.time())
..
result
2021-07-08 10:19:44.969082
2021-07-08
2021-07-18
10:19:44.969146
2021-07-08
20:19:44.969164

Thank you.

study.marearts.com


7/07/2021

opencv Vector CameraParams save(yaml file store) and load(yaml file restore ) using FileNode, FileNodeIterator


Refer to load & save function.

..

std::vector<cv::detail::CameraParams> params;

bool loadCameraParams(std::string fileName){

cv::FileStorage fs(fileName+".yaml", cv::FileStorage::READ);

if(!fs.isOpened())
return false;

/////
int nObjects= 0;
fs["NC"] >> nObjects;
params.clear();
params.resize(nObjects);
/////
cv::FileNode fn = fs["cameras"];
int id=0;
for (cv::FileNodeIterator it = fn.begin(); it != fn.end(); it++,id++)
{
cv::FileNode item = *it;

cv::Mat K, R, t;
double ppx, ppy, focal, aspect;
item["K"] >> K;
item["R"] >> R;
item["t"] >> t;
item["ppx"] >> ppx;
item["ppy"] >> ppy;
item["focal"] >> focal;
item["aspect"] >> aspect;

mg_params[id].K() = (cv::Mat)K;
mg_params[id].R = R;
mg_params[id].t = t;
mg_params[id].ppx = (double)ppx;
mg_params[id].ppy = (double)ppy;
mg_params[id].focal = (double)focal;
mg_params[id].aspect = (double)aspect;
}

return true;
}

bool saveCameraParams(std::string fileName){

cv::FileStorage fs(fileName+".yaml", cv::FileStorage::WRITE);
cv::detail::CameraParams cameras = mg_params[0];
fs << "NC" << int(params.size());

fs << "cameras" << "[";
for (const auto& param : mg_params)
{
fs << "{";
fs << "K" << param.K();
fs << "R" << param.R;
fs << "t" << param.t;
fs << "ppx" << param.ppx;
fs << "ppy" << param.ppy;
fs << "focal" << param.focal;
fs << "aspect" << param.aspect;
fs << "}";
}
fs << "]";
fs.release();
return true;
}

..


Thank you!!

☕️

7/04/2021

bash: g++: command not found

 > sudo apt-get install g++




main.cpp:(.text+0x3f): undefined reference to `std::ios_base::Init::Init()'

 errors like that:

> gcc -o test main.cpp 

/usr/bin/ld: /tmp/ccoMmwaL.o: in function `__static_initialization_and_destruction_0(int, int)':

main.cpp:(.text+0x3f): undefined reference to `std::ios_base::Init::Init()'

/usr/bin/ld: main.cpp:(.text+0x54): undefined reference to `std::ios_base::Init::~Init()'

collect2: error: ld returned 1 exit status


just replace g++ instead of gcc

> g++ -o test main.cpp 


Then it would be fine!.



Thank you!


bash: objdump: command not found

> apt-get update

> apt-get upgrade

> apt-get install binutils


That's all!!


docker stop and remove all container, remove all images

 




stop all containers:
docker kill $(docker ps -q)

remove all containers
docker rm $(docker ps -a -q)

remove all docker images
docker rmi $(docker images -q)

7/03/2021

ubuntu copy all dependencies to certain folder of your executable file.

 Make copyall.sh file and make sure chmod is 775.

//

#!/bin/bash
# Author : Hemanth.HM
# Email : hemanth[dot]hm[at]gmail[dot]com
# License : GNU GPLv3
#

function useage()
{
cat << EOU
Useage: bash $0 <path to the binary> <path to copy the dependencies>
EOU
exit 1
}

#Validate the inputs
[[ $# < 2 ]] && useage

#Check if the paths are vaild
[[ ! -e $1 ]] && echo "Not a vaild input $1" && exit 1
[[ -d $2 ]] || echo "No such directory $2 creating..."&& mkdir -p "$2"

#Get the library dependencies
echo "Collecting the shared library dependencies for $1..."
deps=$(ldd $1 | awk 'BEGIN{ORS=" "}$1~/^\//{print $1}$3~/^\//{print $3}' | sed 's/,$/\n/')
echo "Copying the dependencies to $2"

#Copy the deps
for dep in $deps
do
echo "Copying $dep to $2"
cp "$dep" "$2"
done

echo "Done!"

//

And run it like this

./copy_dll.sh ./executableFileName ./copyToHere


That's all


Thank you.