def process(buffer): logger.debug("从web读取数据len:%r", len(buffer)) if len(buffer) == 0: return False, "Image is null" data_array = np.frombuffer(buffer, dtype=np.uint8) image = cv2.imdecode(data_array, cv2.IMREAD_COLOR) if image is None: logger.error("图像解析失败") #有可能从字节数组解析成图片失败 return "图像角度探测失败" logger.debug("从字节数组变成图像的shape:%r", image.shape) global input_images, sess _classes = pred.pred(sess, classes, input_images, np.array([image])) return pred.CLASS_NAME[_classes[0]]
def process(buffer, image_name): logger.debug("从web读取数据len:%r", len(buffer)) if len(buffer) == 0: return False, "Image is null" #先给他转成ndarray(numpy的) data_array = np.frombuffer(buffer, dtype=np.uint8) #从ndarray中读取图片,有raw数据变成一个图片GBR数据 #出来的数据,其实就是有维度了,就是原图的尺寸,如160x70 image = cv2.imdecode(data_array, cv2.IMREAD_COLOR)[:, :, ::-1] # cv2.imwrite("test1.png",image) BGR # cv2.imwrite("test2.png",image[:,:,::-1]) RGB if image is None: logger.error("图像解析失败") #有可能从字节数组解析成图片失败 return False, None logger.debug("从字节数组变成图像的shape:%r", image.shape) result = ctpn.pred(sess_ctpn, [image], [image_name]) for r in result: # 从opencv的np array格式,转成原始图像,再转成base64 r['image'] = ocr_utils.tobase64(r['image']) # logger.debug("预测返回结果:%r",result[0]) small_images = ocr_utils.crop_small_images(image, result[0]['boxes']) # small_images = small_images[:2] # 测试用,为了提高测试速度,只处理2个 all_txt = crnn.pred(small_images, sess_crnn) # 小框们的文本们 result[0]['text'] = all_txt # 小框们的图片的base64 result[0]['small_images'] = ocr_utils.tobase64(small_images) logger.debug("最终的预测结果为:%r", all_txt) # logger.debug("最终的预测的子图:%r",result[0]['small_images']) return True, result[0]
def process(image, image_name="test.jpg", is_verbose=False): # result:[{ # name: 'xxx.png', # 'box': { # [1, 1, 1, 1], # [2, 2, 2, 2] # }, # image : <draw image numpy array>, # 'f1': 0.78 # } # }, ] #global crnn_sess,ctpn_sess result = ctpn.pred(ctpn_sess, [image], [image_name], ctpn_graph) # logger.debug("预测返回结果:%r",result[0]) small_images = ocr_utils.crop_small_images(image, result[0]['boxes']) result[0]['text'] = process_crnn(small_images) # 小框们的文本们 # 仅仅调试CTPN # all_txt = ['']*len(small_images) # 返回原图和切出来的小图,这个是为了调试用 if is_verbose: # 小框们的图片的base64 result[0]['small_images'] = ocr_utils.nparray2base64(small_images) # 这个是为了,把图片再回显到网页上用 for r in result: # 从opencv的np array格式,转成原始图像,再转成base64 if r.get('image', None) is not None: logger.debug("返回的大图:%r", r['image'].shape) r['image'] = ocr_utils.nparray2base64(r['image']) else: logger.debug("返回大图为空") # logger.debug("最终的预测的子图:%r",result[0]['small_images']) return True, result[0]
def main(): input_images, classes = pred.init_model() sess = pred.restore_session() image_name_list = pred.get_images() pbar = tqdm(total=len(image_name_list)) i = 0 for image_name in image_name_list: start = time.time() logger.info("探测图片[%s]开始", image_name) pbar.update(1) try: img = cv2.imread(image_name) _classes = pred.pred(sess, classes, input_images, np.array([img])) if _classes[0] != 0: select_image(image_name, _classes[0]) except Exception as e: traceback.print_exc() logger.error("处理图片[%s]发生错误:%s", image_name, str(e)) continue i += 1 logger.debug("处理完第%d张图片,耗时:%f", i, time.time() - start) pbar.close()