def find_shapes(img): markupImage = Image(img) bwImage = markupImage.binarize() blobs = bwImage.findBlobs() rectangles = [] for b in blobs: info = b.boundingBox() x = info[0] y = info[1] w = info[2] h = info[3] c = find_color(x, y, markupImage) markupImage.drawRectangle(x, y, w, h) rectangles.append(Rectangle(*((x, y, w, h) + c))) max_rectangle = max(rectangles, key=lambda rect: rect.w * rect.h) #markupImage.drawRectangle(max_rectangle.x, max_rectangle.y, max_rectangle.w, max_rectangle.h, Color.ORANGE) return rectangles, markupImage
def constructMask(w, h, offset, expf=1.2): # Create an alpha blur on the left followed by white # using some exponential value to get better results mask = Image((w, h)) offset = int(offset) for i in range(0, offset): factor = np.clip((float(i)**expf) / float(offset), 0.0, 1.0) c = int(factor * 255.0) #this is oddness in slice, need to submit bug report mask[i:i + 1, 0:h] = (c, c, c) mask.drawRectangle(offset, 0, w - offset, h, color=(255, 255, 255), width=-1) mask = mask.applyLayers() return mask
def __call__(self, image): params = util.utf8convert(self.inspection.parameters) retVal = [] mask = Image((image.width,image.height)) if( params.has_key('w') and params.has_key('h') and params.has_key('x') and params.has_key('y') ): #rectangle if( params['x'] + params['w'] < image.width and params['y'] + params['h'] < image.height and params['y'] >= 0 and params['x'] >= 0 ): mask.drawRectangle(params['x'],params['y'],params['w'],params['h'],width=-1,color=Color.WHITE) mask = mask.applyLayers() fs = image.findBlobsFromMask(mask) ff = M.FrameFeature() if( fs is not None and len(fs) > 0 ): #fs[-1].draw() b = fs[-1] b.__class__ = BlobRegion c = b.meanColor() b.mColor = (int(c[0]),int(c[1]),int(c[2])) ff.setFeature(b) # a little hacky but I am sure that it works retVal = [ff] elif( params.has_key('x') and params.has_key('y') and params.has_key('r') ): # circle if( params['x'] + params['r'] < image.width and params['y'] + params['r'] < image.height and params['x'] - params['r'] >= 0 and params['y'] - params['r'] >= 0 ): r = params['r'] x = params['x'] y = params['y'] mask.drawCircle((x,y),r,thickness=-1,color=Color.WHITE) mask = mask.applyLayers() fs = image.findBlobsFromMask(mask) ff = M.FrameFeature() if( fs is not None and len(fs) > 0 ): #fs[-1].draw() b = fs[-1] b.__class__ = BlobRegion c = b.meanColor() b.mColor = (int(c[0]),int(c[1]),int(c[2])) ff.setFeature(b) retVal = [ff] elif( params.has_key('contour') ): contour = params['contour'] # this may bail out if( len(contour) >= 3 ): mask.dl().polygon(contour,filled=True,color=Color.WHITE) mask = mask.applyLayers() fs = image.findBlobsFromMask(mask) ff = M.FrameFeature() if( fs is not None and len(fs) > 0 ): #fs[-1].draw() b = fs[-1] b.__class__ = BlobRegion c = b.meanColor() b.mColor = (int(c[0]),int(c[1]),int(c[2])) ff.setFeature(b) retVal = [ff] if( params.has_key("saveFile") ): image.save(params["saveFile"]) return retVal
def run(self): cam_mode=self.cam_mode wsh = self.wsh js = self.js wsh2 = self.wsh2 d = "n" acu = int(1) acd = int(1) acl = int(1) acr = int(1) c2=self.c2 c=self.c sqx=self.sqx sqy=self.sqy x=0 y=0 stat="centering" if cam_mode == 3: img1 = c2.getImage() if cam_mode==1: img1 = c.getImage() ## with picamera.PiCamera() as camera: ## camera.resolution = (544, 288) ## camera.capture('imagesmall.jpg') ## img1 = Image('imagesmall.jpg') if cam_mode==2: with picamera.PiCamera() as camera: camera.resolution = (544, 288) camera.capture('imagesmall.jpg') img1 = Image('imagesmall.jpg') self.img1 = img1 blobs = img1.findBlobs() if blobs : ##blobs.draw() img1.drawCircle((blobs[-1].x,blobs[-1].y),30,color=(255,255,255)) img1.drawCircle((blobs[-1].centroid()),10,color=(255,100,100)) blobx1 = blobs[-1].x bloby1 = blobs[-1].y print blobx1 print bloby1 print "sensor" s.write('n') time.sleep(1) position = "altaz_ " + str(s.readline()) wsh.write_message(wsh2, str(position)) img1.drawText("ogp: chasing", 10, 5, fontsize=40) img1.drawText(str(blobx1), blobx1, 250, color=(255,255,255), fontsize=20) img1.drawText(str(bloby1), 10, bloby1, color=(255,255,255), fontsize=20) img1.drawText(str(position), 10, 50, color=(255,255,255), fontsize=25) sqx2=sqx+20 sqy2=sqy+20 img1.drawRectangle(sqx,sqy,20,20,color=(100,100,255)) img1.save(js.framebuffer) if blobx1 > sqx2: d = 'r' s.write('s') time.sleep(.5) wsh.write_message(wsh2, "g_"+ str(d)) if blobx1 < sqx: d = 'l' s.write('q') time.sleep(.5) wsh.write_message(wsh2, "g_"+ str(d)) if bloby1 > sqy2: d = 'd' s.write('a') time.sleep(.5) wsh.write_message(wsh2, "g_"+ str(d)) if bloby1 < sqy: d = 'u' s.write('w') time.sleep(.5) wsh.write_message(wsh2, "g_"+ str(d)) time.sleep(.5) wsh.write_message(wsh2, "c") else: wsh.write_message(wsh2, "c" ) wsh.write_message(wsh2, "CAPTURE" )