def model(img, detectAngle=False, config={}, leftAdjust=False, rightAdjust=False, alph=0.2, ifadjustDegree=False): """ @@param:img, @@param:ifadjustDegree 调整文字识别倾斜角度 @@param:detectAngle,是否检测文字朝向 """ angle, degree, img = eval_angle(img, detectAngle=detectAngle, ifadjustDegree=ifadjustDegree) if opencvFlag != 'keras': img, f = letterbox_image(img, IMGSIZE) else: f = 1.0 ##解决box在原图坐标不一致问题 config['img'] = img timeTake = time.time() text_recs = text_detect(**config) timeTake = time.time() - timeTake print('Detect take:{}s'.format(timeTake)) newBox = sort_box(text_recs) result = crnnRec(np.array(img), newBox, leftAdjust, rightAdjust, alph, 1.0 / f) return img, result, angle
def text_detect(img,scale,maxScale,prob = 0.05): thresh = prob img_height,img_width = img.shape[:2] inputBlob,f = letterbox_image(img,(scale,scale)) inputBlob = cv2.dnn.blobFromImage(inputBlob, scalefactor=1.0, size=(scale,scale),swapRB=True ,crop=False) textNet.setInput(inputBlob/255.0) outputName = textNet.getUnconnectedOutLayersNames() outputs = textNet.forward(outputName) class_ids = [] confidences = [] boxes = [] for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > thresh: center_x = int(detection[0] * scale/f) center_y = int(detection[1] * scale/f) width = int(detection[2] * scale/f) height = int(detection[3] * scale/f) left = int(center_x - width / 2) top = int(center_y - height / 2) if class_id==1: class_ids.append(class_id) confidences.append(float(confidence)) boxes.append([left, top,left+width, top+height ]) boxes = np.array(boxes) confidences = np.array(confidences) return boxes,confidences
def model(img, detectAngle=False, config={}, leftAdjust=False, rightAdjust=False, alph=0.2): """ @@param:img, @@param:ifadjustDegree 调整文字识别倾斜角度 @@param:detectAngle,是否检测文字朝向 """ angle, img = eval_angle(img, detectAngle=detectAngle) ##文字方向检测 if opencvFlag != 'keras': img, f = letterbox_image(Image.fromarray(img), IMGSIZE) ## pad img = np.array(img) else: f = 1.0 ##解决box在原图坐标不一致问题 config['img'] = img text_recs = text_detect(**config) ##文字检测 newBox = sort_box(text_recs) ##行文本识别 result = crnnRec(np.array(img), newBox, leftAdjust, rightAdjust, alph, 1.0 / f) return img, result, angle
def model(img, file_name, detectAngle=False, config={}, leftAdjust=False, rightAdjust=False, alpha=0.2): """ @@param:img, @@param:ifadjustDegree 调整文字识别倾斜角度 @@param:detectAngle,是否检测文字朝向 """ angle, img = eval_angle(img, detectAngle=detectAngle) # 进行文字方向检测 if opencvFlag != 'keras': img, f = letterbox_image(Image.fromarray(img), IMGSIZE) # pad img = np.array(img) else: f = 1.0 # 解决box在原图坐标不一致问题 config['img'] = img image_cv = copy.copy(img) cv2.imshow("model", image_cv) text_recs = text_detect(**config) # 文字检测 # print('text_recs', text_recs) # draw_boxes(image_cv, text_recs, "text_recs", (255, 255, 0)) newBox = sort_box(text_recs) # 文本进行排序(从上到下进行排序) print('newBox', newBox) draw_boxes(image_cv, text_recs, file_name, (255, 0, 0), "./output") # print('model', img.shape) result = crnnRec(np.array(img), newBox, leftAdjust, rightAdjust, alpha, 1.0 / f) # ocr识别 print('result', result) return img, result, angle
def model(img, detectAngle=False, config={}, leftAdjust=False, rightAdjust=False, alph=0.2, bili=1.2): """ @@param:img, @@param:ifadjustDegree 调整文字识别倾斜角度 @@param:detectAngle,是否检测文字朝向 """ print(22222222222) ''' 这个地方需要加入图片预处理!!!!!!滤波等. 确实这个算法没有加入,需要加入!!!!!!!!!!! ''' angle, img = eval_angle(img, detectAngle=detectAngle) ##文字方向检测 if opencvFlag != 'keras': img, f = letterbox_image(Image.fromarray(img), IMGSIZE) ## pad img = np.array(img) else: f = 1.0 ##解决box在原图坐标不一致问题 config['img'] = img config['bili'] = bili text_recs, scores, boxForSingleAfterNMS, scoresForSingle, keepIndForSingle, tp_groups, Allboxes, Allscores = text_detect( **config) ##文字检测 newBox, tp_groups = sort_box(text_recs, tp_groups) #按照列高排序,符合我们阅读顺序! ##下行行文本识别 print(newBox) result = crnnRec(np.array(img), newBox, leftAdjust, rightAdjust, alph, 1.0 / f, tp_groups, boxForSingleAfterNMS, scoresForSingle) return img, result, angle, scores, text_recs, newBox, boxForSingleAfterNMS, scoresForSingle, keepIndForSingle, tp_groups, Allboxes, Allscores