def crnnRec(im, boxes, leftAdjust=False, rightAdjust=False, alph=0.2, f=1.0): """ crnn模型,ocr识别 @@model, @@converter, @@im:Array @@text_recs:text box @@ifIm:是否输出box对应的img """ results = [] im = Image.fromarray(im) for index, box in enumerate(boxes): degree, w, h, cx, cy = solve(box) partImg, newW, newH = rotate_cut_img(im, degree, box, w, h, leftAdjust, rightAdjust, alph) newBox = xy_rotate_box(cx, cy, newW, newH, degree) # partImg_ = partImg.convert('L') # simPred = crnnOcr(partImg_)##识别的文本 # if simPred.strip()!=u'': # results.append({'cx':cx*f,'cy':cy*f,'text':simPred,'w':newW*f,'h':newH*f,'degree':degree*180.0/np.pi}) results.append({ 'cx': cx * f, 'cy': cy * f, 'text': '', 'w': newW * f, 'h': newH * f, 'degree': degree * 180.0 / np.pi }) return results
def genGT(file) : with open(file, 'r', encoding = 'utf-8') as f : gt = [] lines = f.readlines() for line in lines : line = line.split(' ') points = line[0] text = re.sub(r'\n', '', ''.join(line[1 :])) box = [float(p) for p in points.split(',')] box = np.array(box).astype('int32') box = box.reshape(4, 2) box = order_point(box) box = box.reshape(-1) angle, w, h, cx, cy = solve(box) gt.append({'angle' : angle, 'w' : w, 'h' : h, 'cx' : cx, 'cy' : cy, 'text' : text}) basic = getBasics(gt) buyer = getBuyer(gt) content = getContent(gt) seller = getSeller(gt) summation = getSummation(gt) groundtruth = [{'title' : r'发票基本信息', 'items' : basic}, {'title' : r'购买方', 'items' : buyer}, {'title' : r'销售方', 'items' : seller}, {'title' : r'货物或应税劳务、服务', 'items' : content}, {'title' : r'合计', 'items' : summation}] return groundtruth
def rotate_box(box): angle, w, h, cx, cy = solve(box) angle = round(angle, 4) w = round(w, 4) h = round(h, 4) cx = round(cx, 4) cy = round(cy, 4) return ((cx, cy), (w, h), angle)
def evaluate(file, errfile, canfile, predict = None) : with open(file, 'r', encoding = 'utf-8') as f : gt = [] precision = [] lines = f.readlines() # print("file:",file) # print("len:",len(lines)) # i = 1 for line in lines : line = line.split(',') flag = len(line) if flag==8:# 空格 points = line[:7] line = line[7].split(' ') points.append(line[0]) text = '' for i in range(1,len(line)): text += line[i] text = re.sub(r'\n', '', ''.join(text)) # print("points:",points) box = [float(p) for p in points] else:#逗号 points = line[:8] text = line[8:] text = re.sub(r'\n','',''.join(text)) box = [float(p) for p in points] pass box = np.array(box).astype('int32') box = box.reshape(4, 2) box = order_point(box) box = box.reshape(-1) angle, w, h, cx, cy = solve(box) gt.append({'angle' : angle, 'w' : w, 'h' : h, 'cx' : cx, 'cy' : cy, 'text' : text}) basic = getBasics(gt) buyer = getBuyer(gt) content = getContent(gt) seller = getSeller(gt) summation = getSummation(gt) gt_len = len(gt) groundtruth = [{'title' : r'发票基本信息', 'items' : basic}, {'title' : r'购买方', 'items' : buyer}, {'title' : r'销售方', 'items' : seller}, {'title' : r'货物或应税劳务、服务', 'items' : content}, {'title' : r'合计', 'items' : summation}] precision1 = calc_precision1(predict, groundtruth, errfile, canfile) # precision2 = calc_precision2(gt_len, errfile) # 不准 precision.append(precision1) # precision.append(precision2) return precision
def crnnRec(im, boxes, leftAdjust=False, rightAdjust=False, alph=0.2, f=1.0): """ crnn模型,ocr识别 leftAdjust,rightAdjust 是否左右调整box 边界误差,解决文字漏检 """ results = [] im = Image.fromarray(im) for index, box in enumerate(boxes): degree, w, h, cx, cy = solve(box) partImg, newW, newH = rotate_cut_img(im, degree, box, w, h, leftAdjust, rightAdjust, alph) text = crnnOcr(partImg.convert('L')) if text.strip() != u'': results.append({'cx': cx * f, 'cy': cy * f, 'text': text, 'w': newW * f, 'h': newH * f, 'degree': degree * 180.0 / np.pi}) return results
def crnnRec(im, boxes, leftAdjust=False, rightAdjust=False, alph=0.2, f=1.0, save=True): """ crnn模型,ocr识别 leftAdjust, rightAdjust 是否左右调整box 边界误差,解决文字漏检 """ results = [] # im = Image.fromarray(im) im = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)) for index, box in enumerate(boxes): degree, w, h, cx, cy = solve(box) # 按照box大小,裁剪图片 partImg, newW, newH = rotate_cut_img(im, degree, box, w, h, leftAdjust, rightAdjust, alph) # partImg, newW, newH = cut_img = () image_cv = cv2.cvtColor(numpy.asarray(partImg), cv2.COLOR_RGB2BGR) cv2.imshow("crnnRec", image_cv) # partImg = Image.fromarray(cv2.cvtColor(partImg, cv2.COLOR_BGR2RGB)) # 图片会转灰度图,进行识别 # print('crnnRec', partImg.size) text, w = crnnOcr(partImg.convert('L')) # text, w = crnnOcr(partImg) if save: image = partImg.resize((w, 32), Image.BILINEAR) save_image(image, text) # text = crnnOcr(partImg) if text.strip() != u'': results.append({ 'cx': cx * f, 'cy': cy * f, 'text': text, 'w': newW * f, 'h': newH * f, 'degree': degree * 180.0 / np.pi }) return results
def crnnRec(im, boxes, leftAdjust=False, rightAdjust=False, alph=0.2, f=1.0, tp_groups=None, boxAll=None, scoreAll=None): """ crnn模型,ocr识别 leftAdjust,rightAdjust 是否左右调整box 边界误差,解决文字漏检 """ results = [] im = Image.fromarray(im) for index, box in enumerate(boxes): degree, w, h, cx, cy = solve(box) partImg, newW, newH = rotate_cut_img(im, degree, box, w, h, leftAdjust, rightAdjust, alph) text = crnnOcr(partImg.convert('L')) detailbox = boxAll[tp_groups[index]] detailscore = scoreAll[tp_groups[index]] detaildex = tp_groups[index] if text.strip() != u'': results.append({ 'cx': cx * f, 'cy': cy * f, 'text': text, 'w': newW * f, 'h': newH * f, 'degree': degree * 180.0 / np.pi, 'detailbox': detailbox, 'detailscore': detailscore, 'detaildex': detaildex }) #degree表示顺时针转多少度. return results
for imgPath in imglist: # print('img_count: ', img_count) img = Image.open(imgPath).convert("RGB") config['img'] = img text_recs = text_detect(**config) # print(text_recs) boxes = sorted(text_recs, key=lambda x: sum([x[1], x[3], x[5], x[7]])) i = 0 filename = ntpath.basename(imgPath) ori_filename = romvChinese(filename) for index, box in enumerate(boxes): filename = ori_filename degree, w, h, cx, cy = solve(box) partImg, newW, newH = rotate_cut_img(img, degree, box, w, h, leftAdjust=True, rightAdjust=True, alph=0.2) if partImg.size[1] < 32: scale = partImg.size[1] * 1.0 / 32 w = partImg.size[0] / scale w = int(w) partImg = partImg.resize((w, 32), Image.BILINEAR) filename = filename[:-4] + '_' + str(i) + '_.jpg' partImgPath = os.path.join(subdir, filename)