def on_send_button_clicked(self, event): if self.monitor is None: img_data = self.painter.get_image_data() #prepare an image w, h, d = img_data.shape img = Image.fromstring( "RGBA", ( w ,h ), img_data.tostring() ) img_gs = img.convert('L') # thumbnail_size = (28, 28) # img_gs.thumbnail(thumbnail_size) img_gs_inv = ImageOps.invert(img_gs) else: img_data = self.monitor.get_image_data() # img_gs_inv = 255 - img_data img_gs_inv = img_data img_gs_inv_thumbnail, bound_rect = utils.get_char_img_thumbnail_helper(np.asarray(img_gs_inv)) # img.show() self.img_data = np.asarray(img_gs_inv_thumbnail).flatten().astype(np.float32) * 1./255. if self.on_send_usr_callback is not None: self.on_send_usr_callback(self) #update the target canvas self.ax_target.imshow(self.img_data.reshape((28, 28))) self.target_canvas.draw() return
def on_send_incomplete_button_clicked(self, event): if self.monitor is None: img_data = self.painter.get_image_data() else: img_data = self.monitor.get_image_data() #prepare an image w, h, d = img_data.shape img = Image.fromstring( "RGBA", ( w ,h ), img_data.tostring() ) img_gs = img.convert('L') # thumbnail_size = (28, 28) # img_gs.thumbnail(thumbnail_size) img_gs_inv = ImageOps.invert(img_gs) img_gs_inv_thumbnail, bound_rect = utils.get_char_img_thumbnail_helper(np.asarray(img_gs_inv)) # img.show() self.img_data = np.asarray(img_gs_inv_thumbnail).flatten().astype(np.float32) * 1./255. #cover a fraction of the image to produce incomplete data self.fraction_idx = (np.random.rand(2) > 0.5).astype(int) self.img_incomplete_data = self.img_data.reshape((28, 28)) self.img_incomplete_data[14*self.fraction_idx[0]:14*(self.fraction_idx[0]+1), 14*self.fraction_idx[1]:14*(self.fraction_idx[1]+1)] = 0 self.img_incomplete_data = self.img_incomplete_data.flatten() if self.on_send_incomplete_usr_callback is not None: self.on_send_incomplete_usr_callback(self) #update the target canvas self.ax_target.imshow(self.img_data.reshape((28, 28))) self.target_canvas.draw() return
def main(record=True): if record: img_reader = CameraImageReader(camera_idx=0) #print information # img_reader.print_cap_info() #wait until enter to capture an image raw_input('ENTER to capture an image...') img = img_reader.capture_image() cv2.imshow("capture", img) cv2.waitKey() timestr = time.strftime("%Y%m%d-%H%M%S") cv2.imwrite(os.path.join('img_reader', timestr+'.png'),img) else: curr_dir = os.path.dirname(os.path.realpath(__file__)) fdir = os.path.join(curr_dir, 'img_reader') fname = '20160808-131404.png' #load the colored image in grayscale img = cv2.imread(os.path.join(fdir, fname), cv2.IMREAD_GRAYSCALE) cv2.imshow("file", img) cv2.waitKey() img_processor = ImageLetterProcessor(img=img) img_processed = img_processor.binarize_img() img_processed = img_processor.localize_img() img_processed = np.array(img_processed, dtype=np.uint8) img_gs_inv_thumbnail, bound_rect = utils.get_char_img_thumbnail_helper(np.asarray(img_processed)) cv2.imshow("processed", img_gs_inv_thumbnail) cv2.waitKey() return