def run(self): read_color_data() self.cam=WebCam(info=self.cam_info) if self.debug: self.debug_thread.start() try: while self.pipe.recv() == True: im=self.cam.get_image() small_im=cv.CreateImage((im.width/2, im.height/2), cv.IPL_DEPTH_8U, 3) cv.PyrDown(im, small_im); smaller_im=cv.CreateImage((im.width/4, im.height/4), cv.IPL_DEPTH_8U, 3) cv.PyrDown(small_im, smaller_im); smallerer_im=cv.CreateImage((im.width/8, im.height/8), cv.IPL_DEPTH_8U, 3) cv.PyrDown(smaller_im, smallerer_im); colors=convert_to_colors(smallerer_im) closest_ball=self.find_closest_ball(colors) self.pipe.send({"closest_ball": closest_ball}) self.colors=colors #time.sleep(0.01) except KeyboardInterrupt: pass finally: if self.debug: self.debug_thread.stop() self.cam.stop()
class VisionProc(multiprocessing.Process): def __init__(self, cam_info, pipe, debug=False): multiprocessing.Process.__init__(self) self.cam_info=cam_info self.pipe=pipe self.debug=debug if self.debug: self.debug_thread=DebugThread(self) def run(self): read_color_data() self.cam=WebCam(info=self.cam_info) if self.debug: self.debug_thread.start() try: while self.pipe.recv() == True: im=self.cam.get_image() small_im=cv.CreateImage((im.width/2, im.height/2), cv.IPL_DEPTH_8U, 3) cv.PyrDown(im, small_im); smaller_im=cv.CreateImage((im.width/4, im.height/4), cv.IPL_DEPTH_8U, 3) cv.PyrDown(small_im, smaller_im); smallerer_im=cv.CreateImage((im.width/8, im.height/8), cv.IPL_DEPTH_8U, 3) cv.PyrDown(smaller_im, smallerer_im); colors=convert_to_colors(smallerer_im) closest_ball=self.find_closest_ball(colors) self.pipe.send({"closest_ball": closest_ball}) self.colors=colors #time.sleep(0.01) except KeyboardInterrupt: pass finally: if self.debug: self.debug_thread.stop() self.cam.stop() def find_closest_ball(self, im): for b in find_blobs(im, color=RED, reverse=True): if self.is_ball(b, im): return self.cam.info.get_vector(b[0], im) return None def is_ball(self, blob, im): """Check if a list of pixels is a ball""" size=self.cam.info.get_pixel_size(blob[0], im)*len(blob) return size>15 and size<45