def crnnRec(im, text_recs, adjust=False): index = 0 results = {} xDim, yDim = im.shape[1], im.shape[0] for index, rec in enumerate(text_recs): results[index] = [ rec, ] xlength = int((rec[6] - rec[0]) * 0.1) ylength = int((rec[7] - rec[1]) * 0.2) if adjust: pt1 = (max(1, rec[0] - xlength), max(1, rec[1] - ylength)) pt2 = (rec[2], rec[3]) pt3 = (min(rec[6] + xlength, xDim - 2), min(yDim - 2, rec[7] + ylength)) pt4 = (rec[4], rec[5]) else: pt1 = (max(1, rec[0]), max(1, rec[1])) pt2 = (rec[2], rec[3]) pt3 = (min(rec[6], xDim - 2), min(yDim - 2, rec[7])) pt4 = (rec[4], rec[5]) degree = degrees(atan2(pt2[1] - pt1[1], pt2[0] - pt1[0])) partImg = dumpRotateImage(im, degree, pt1, pt2, pt3, pt4) image = Image.fromarray(partImg).convert('L') sim_pred = ocr(image) results[index].append(sim_pred) return results
def crnnRec(im, text_recs, ocr_mode='keras', adjust=False): """ crnn模型,ocr识别 @@model, @@converter, @@im:Array @@text_recs:text box """ index = 0 results = {} xDim, yDim = im.shape[1], im.shape[0] for index, rec in enumerate(text_recs): results[index] = [ rec, ] xlength = int((rec[6] - rec[0]) * 0.1) ylength = int((rec[7] - rec[1]) * 0.2) if adjust: pt1 = (max(1, rec[0] - xlength), max(1, rec[1] - ylength)) pt2 = (rec[2], rec[3]) pt3 = (min(rec[6] + xlength, xDim - 2), min(yDim - 2, rec[7] + ylength)) pt4 = (rec[4], rec[5]) else: pt1 = (max(1, rec[0]), max(1, rec[1])) pt2 = (rec[2], rec[3]) pt3 = (min(rec[6], xDim - 2), min(yDim - 2, rec[7])) pt4 = (rec[4], rec[5]) # 图像倾斜角度 degree = degrees(atan2(pt2[1] - pt1[1], pt2[0] - pt1[0])) part_img = dump_rotate_image(im, degree, pt1, pt2, pt3, pt4) image = Image.fromarray(part_img).convert('L') if ocr_mode == 'keras': sim_pred = ocr(image) else: sim_pred = crnnOcr(image) # 识别文字 results[index].append(sim_pred) return results
def crnnRec(im, ocrMode='keras'): """ crnn模型,ocr识别 @@model, @@converter, @@im:Array @@text_recs:text box """ index = 0 results = {} results[index] = list() image = Image.fromarray(im).convert('L') # 进行识别出的文字识别 if ocrMode == 'keras': sim_pred = ocr(image) else: sim_pred = crnnOcr(image) results[index].append(sim_pred) # 识别文字 return results
def draw_boxes(img, image_name, boxes, opt, adjust): base_name = image_name.split('/')[-1] i = 0 j = 0 boxes = sorted(boxes, key=lambda x: x[1]) boxesX0 = sorted(boxes, key=lambda x: x[0]) xStart = boxesX0[0][0] iXStart = int(xStart) if (iXStart < 0): iXStart = 0 boxesX2 = sorted(boxes, key=lambda x: x[2]) xEnd = boxesX2[-1][2] previousY = 0 resultMap = { '1_shopName': 'none', '2_city': 'none', '3_tel': 'none', '4_year': 'none', '5_total': 0, '6_receiptNO': 'none', '7_staffNO': 'none', '8_pointcard': 'none', '9_category': 0, 'a_goods': 'none', 'a_tax': 0, 'b_subtotal': 0, 'suffix_catPrice': [], 'tel_before': '', 'pos_tel_before': 0, 'pos_tel_after': 0, 'origin_result': [], 'pos_shop': 0, 'pos_time': 0, 'pos_time_after': 0, 'pos_tax_after': 0, 'pos_staff': 0, 'pos_ling': 0, 'pos_ling_after': 0, 'pos_category': 0, 'pos_total': 0, 'pos_subtotal': 0, 'pos_tax': 0, 'pos_card': 0, 'pos_mny_after': 0, 'pos_card_after': 0, 'type_shop': 0 } for box in boxes: if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm(box[3] - box[0]) < 5: j += 1 continue if (i > 0 and int(box[1]) - previousY <= 11): previousY = int(box[1]) continue previousY = int(box[1]) i += 1 crop_img = img[int(box[1]):int(box[5]), iXStart:int(xEnd)] # print('Y0={}, Y1={}, X0={}, X1={}'.format(int(box[1]),int(box[5]),iXStart,int(xEnd))) if opt.develop and i <= 5: cv2.imwrite( os.path.join("test/results", base_name.split('.')[0] + "_" + str(i) + ".jpg"), crop_img) try: if opt.torch: from crnn.crnn import crnnOcr sim_pred = crnnOcr( Image.fromarray(crop_img).convert('L')).strip() else: from ocr.model import predict as ocr sim_pred = ocr(Image.fromarray(crop_img).convert('L')).strip() except Exception as e: print('Exception for step {}--{}'.format(i, e)) continue sim_pred = sim_pred.strip() sim_pred = numberUtils.removeQuote(sim_pred) sim_pred = jaconv.z2h(sim_pred, digit=True, ascii=True) resultMap['origin_result'].append(sim_pred) if opt.develop and i <= 5: with open( os.path.join( "test/results", base_name.split('.')[0] + "_" + str(i) + ".txt"), 'a+') as f: f.write(sim_pred) commonUtils.getDrawboxResult(resultMap['origin_result'], resultMap) return resultMap