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 @@ -62,6 +62,29 @@ class Sample: grid=list(map(lambda p: list(2*p/self.SIDE-EPoint(1,1)), grid)) return (img,grid) + def cut(self): + width=max(p.x for p in self.grid)-min(p.x for p in self.grid) + height=max(p.y for p in self.grid)-min(p.y for p in self.grid) + kx=width/4 + ky=height/4 + n=self.SIDE + for p in self.grid: + shift=self._createNoise(0.2) + abcd=[[p.x-kx,p.y-ky],[p.x-kx,p.y+ky],[p.x+kx,p.y+ky],[p.x+kx,p.y-ky]] + abcd_=[[shift.x,shift.y],[shift.x,n+shift.y],[n+shift.x,n+shift.y],[n+shift.x,shift.y]] + m=cv.getPerspectiveTransform(np.float32(abcd),np.float32(abcd_)) + t1=getTranslation(-n/2,-n/2) + mir=getMirroring() + proj=getProjection() + rot=getRotation() + t2=getTranslation(n/2,n/2) + for mi in [t1,mir,proj,rot,t2]: + m=np.matmul(mi,m) + img=cv.warpPerspective(self.img,m,(self.SIDE,self.SIDE)) + img=np.uint8(img) + point=p.transform(m)*2/self.SIDE-EPoint(1,1) + yield (img,[point.x,point.y]) + def _getCenter(self): (a,b,c,d)=self.grid p=Line.fromPoints(a,c) @@ -77,9 +100,9 @@ class Sample: scale=getScale(self.SIDE/(wg*(1+left+right)), self.SIDE/(hg*(1+top+bottom))) return np.matmul(scale,t2) - def _createNoise(self): + def _createNoise(self,mag=0.05): alpha=random.uniform(0,math.pi*2) - d=random.uniform(0,self.SIDE*0.05) + d=random.uniform(0,self.SIDE*mag) return EPoint(math.cos(alpha)*d, math.sin(alpha)*d) def show(self): @@ -117,9 +140,10 @@ def harvestDir(path): sample=Sample(img,b.grid) # sample.show() # (transformedImg,label)=sample.transform() - (transformedImg,label)=sample.rectify() - # Sample(np.uint8(transformedImg),map(lambda c: (EPoint(*c)+EPoint(1,1))*Sample.SIDE/2,label)).show() - yield (transformedImg,label) + # (transformedImg,label)=sample.rectify() + for (transformedImg,label) in sample.cut(): + Sample(np.uint8(transformedImg),[(EPoint(*label)+EPoint(1,1))*Sample.SIDE/2]).show() + yield (transformedImg,label) def loadDataset(root):