diff --git a/exp/kerokero/prepare_data.py b/exp/kerokero/prepare_data.py --- a/exp/kerokero/prepare_data.py +++ b/exp/kerokero/prepare_data.py @@ -20,7 +20,7 @@ class Sample: SIDE=224 def __init__(self,img,grid): - """:param img: a greyscale image as a 2D np.uint8 + """:param img: an image as a 3D np.uint8, channels-last :param grid: iterable of 4 EPoints, ie. Corners""" self.img=img self.grid=grid @@ -38,7 +38,7 @@ class Sample: m=np.matmul(mi,m) m=np.matmul(self._computeCrop(m),m) img=cv.warpPerspective(self.img,m,(self.SIDE,self.SIDE)) - img=np.float32(img)/255 + img=cv.cvtColor(img,cv.COLOR_BGR2RGB) grid=Corners(c.transform(m) for c in self.grid) grid=list(map(lambda p: 2*p/self.SIDE-EPoint(1,1), grid)) return (img,grid,list(itertools.chain.from_iterable(grid))) @@ -59,7 +59,7 @@ class Sample: return np.matmul(scale,t2) def show(self): - img=cv.cvtColor(self.img,cv.COLOR_GRAY2BGR) + img=np.copy(self.img) for c in self.grid: cv.circle(img,(int(c.x),int(c.y)),3,[0,255,0],-1) show(img) @@ -80,15 +80,15 @@ def traverseDirs(root): 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) + files=sorted(filter(imgFilter,os.scandir(path)),key=lambda f: f.name)[::3] boards=annotations["."] for f in files: img=cv.imread(f.path) - img=cv.cvtColor(img,cv.COLOR_BGR2GRAY) for b in boards: sample=Sample(img,b.grid) + # sample.show() (transformedImg,transformedGrid,label)=sample.transform() - # Sample(np.uint8(transformedImg*255),map(lambda c: (c+EPoint(1,1))*Sample.SIDE/2,transformedGrid)).show() + # Sample(transformedImg,map(lambda c: (c+EPoint(1,1))*Sample.SIDE/2,transformedGrid)).show() yield (transformedImg,label) @@ -108,8 +108,8 @@ def loadDataset(root): labels=[labels[k] for k in keys] m=int(n*trainRatio) return ( - (np.float32(images[:m]),np.float32(labels[:m])), - (np.float32(images[m:]),np.float32(labels[m:])) + (images[:m],np.float32(labels[:m])), + (images[m:],np.float32(labels[m:])) )