import os import sys import re import random import cv2 as cv sys.path.append("../exp") from annotations import DataFile,computeBoundingBox random.seed(361) def traverseDirs(root): stack=[root] while len(stack)>0: d=stack.pop() contents=sorted(os.scandir(d),key=lambda f: f.name,reverse=True) if any(f.name=="annotations.json.gz" for f in contents): print(d) yield d for f in contents: if f.is_dir(): stack.append(f.path) def harvestDir(path): annotations=DataFile(os.path.join(path,"annotations.json.gz")) imgFilter=lambda f: f.is_file() and re.match(r".*\.(jpg|jpeg|png|gif)$", f.name.lower()) files=sorted(filter(imgFilter,os.scandir(path)),key=lambda f: f.name) boards=annotations["."] for f in files: img=cv.imread(f.path) for b in boards: crop(img,b) def crop(img,board): margin=0.2 (hi,wi)=img.shape[:2] (x1,y1,x2,y2)=computeBoundingBox(board.board) (wb,hb)=(x2-x1,y2-y1) dx1=min(int(wb*margin),x1) dx2=min(int(wb*margin),wi-x2) dy1=min(int(hb*margin),y1) dy2=min(int(hb*margin),hi-y2) xa=x1-random.randint(0,dx1) xb=x2+random.randint(0,dx2) ya=y1-random.randint(0,dy1) yb=y2+random.randint(0,dy2) show(img[ya:yb,xa:xb]) return img[ya:yb,xa:xb] def show(img,filename="x"): cv.imshow(filename,img) cv.waitKey(0) cv.destroyAllWindows() if __name__=="__main__": root=sys.argv[1] for d in traverseDirs(root): harvestDir(d)