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 @@ -9,7 +9,7 @@ import cv2 as cv sys.path.append("..") sys.path.append("../../src") -from annotations import DataFile,computeBoundingBox,Corners +from annotations import DataFile,computeBoundingBox,Corners,EPoint from geometry import Line from kerokero.transformation_matrices import getIdentity,getRotation,getTranslation,getScale,getMirroring,getProjection @@ -20,10 +20,14 @@ class Sample: SIDE=224 def __init__(self,img,grid): + """:param img: a greyscale image as a 2D np.uint8 + :param grid: iterable of 4 EPoints, ie. Corners""" self.img=img self.grid=grid def transform(self): + """:return: (img, grid), where img is a 2D np.float32 with values in (0,1), + grid [(float) x, (float) y, ...], with x, y in (-1,1)""" center=self._getCenter() m=getIdentity() t1=getTranslation(-center.x,-center.y) @@ -34,8 +38,10 @@ 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 grid=Corners(c.transform(m) for c in self.grid) - return (img,list(itertools.chain.from_iterable(grid))) + grid=list(map(lambda p: 2*p/self.SIDE-EPoint(1,1), grid)) + return (img,grid,list(itertools.chain.from_iterable(grid))) def _getCenter(self): (a,b,c,d)=self.grid @@ -53,7 +59,7 @@ class Sample: return np.matmul(scale,t2) def show(self): - img=np.copy(self.img) + img=cv.cvtColor(self.img,cv.COLOR_GRAY2BGR) for c in self.grid: cv.circle(img,(int(c.x),int(c.y)),3,[0,255,0],-1) show(img) @@ -81,7 +87,8 @@ def harvestDir(path): img=cv.cvtColor(img,cv.COLOR_BGR2GRAY) for b in boards: sample=Sample(img,b.grid) - (transformedImg,label)=sample.transform() + (transformedImg,transformedGrid,label)=sample.transform() + # Sample(np.uint8(transformedImg*255),map(lambda c: (c+EPoint(1,1))*Sample.SIDE/2,transformedGrid)).show() yield (transformedImg,label) @@ -101,8 +108,8 @@ def loadDataset(root): labels=[labels[k] for k in keys] m=int(n*trainRatio) return ( - (np.uint8(images[:m]),np.float32(labels[:m])), - (np.uint8(images[m:]),np.float32(labels[m:])) + (np.float32(images[:m]),np.float32(labels[:m])), + (np.float32(images[m:]),np.float32(labels[m:])) )