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 ocr_batch(self, img, boxes, leftAdjustAlph=0.0, rightAdjustAlph=0.0): """ batch for ocr """ im = Image.fromarray(img) newBoxes = [] for index, box in enumerate(boxes): partImg, box = rotate_cut_img(im, box, leftAdjustAlph, rightAdjustAlph) box['img'] = partImg.convert('L') newBoxes.append(box) res = self.ocrModel(newBoxes) return res
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 ocr_batch(self, img, boxes, leftAdjustAlph=0.0, rightAdjustAlph=0.0): """ batch for ocr """ im = Image.fromarray(img) newBoxes = [] for index, box in enumerate(boxes): partImg, box = rotate_cut_img(im, box, leftAdjustAlph, rightAdjustAlph) # 旋转裁切图片。 box['img'] = partImg.convert( 'L' ) # L = R * 299/1000 + G * 587/1000+ B * 114/1000 转换。看下面这个,我明明转换成L了,为什么输出的图像还有颜色呢?不是应该是灰度图吗? newBoxes.append(box) res = self.ocrModel(newBoxes) return res
def ocr_batch(self, img, boxes, leftAdjustAlph=0.0, rightAdjustAlph=0.0): """ batch for ocr """ im = Image.fromarray(img) newBoxes = [] for index, box in enumerate(boxes): partImg, box = rotate_cut_img(im, box, leftAdjustAlph, rightAdjustAlph) img = np.array(partImg) _, img_bright = cv.threshold(img, 200, 255, cv.THRESH_BINARY) # cv.imshow('mg0',img_bright) # cv.waitKey() box['img'] = partImg.convert('L') newBoxes.append(box) res = self.ocrModel(newBoxes) return res
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
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) partImg.save(partImgPath) i += 1 img_count += 1 # print('img_count: ', img_count) if img_count % subdir_interval == 0: