Skip to content

Instantly share code, notes, and snippets.

@aliyevorkhan
Created May 28, 2021 12:25
Show Gist options
  • Save aliyevorkhan/08fc5b2a09364f3f000b010796b28735 to your computer and use it in GitHub Desktop.
Save aliyevorkhan/08fc5b2a09364f3f000b010796b28735 to your computer and use it in GitHub Desktop.
import cv2
# import matplotlib.pyplot as plt
from glob import glob
import sys
import uuid
from tqdm import tqdm
import os
num_of_files = len([x for x in os.listdir(str(sys.argv[1])) if (x.endswith('.txt'))])
pbar = tqdm(total=num_of_files,desc ="Converting")
for f in glob(str(sys.argv[1])+'/*.txt'):
img = cv2.imread(f[:-4]+'.jpg')
dh, dw, _ = img.shape
fl = open(f, 'r')
data = fl.readlines()
fl.close()
for dt in data:
c, x, y, w, h = map(float, dt.split(' '))
l = int((x - w / 2) * dw)
r = int((x + w / 2) * dw)
t = int((y - h / 2) * dh)
b = int((y + h / 2) * dh)
if l < 0:
l = 0
if r > dw - 1:
r = dw - 1
if t < 0:
t = 0
if b > dh - 1:
b = dh - 1
crop_img = img[t:b, l:r]
if c==0:
cv2.imwrite("empty_hand/"+str(uuid.uuid4())+".jpg", crop_img)
else:
cv2.imwrite("full_hand/"+str(uuid.uuid4())+".jpg", crop_img)
pbar.update(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment