def text_ocr(img, scale, maxScale, TEXT_LINE_SCORE, filePath, fileName): try: boxes, scores = textModel(img, scale=scale, maxScale=maxScale) result = [] drawBoxes = [] draw_filename = fileName.split('.')[0] + '_drawed.' + fileName.split( '.')[1] drawPath = os.path.join(filePath, draw_filename) drawUrl = settings.FILE_URL + settings.MEDIA_URL + 'photos' + '/' + draw_filename im = Image.fromarray(img) for i, box in enumerate(boxes): if scores[i] > TEXT_LINE_SCORE: drawBoxes.append(box) tmpImg = rotate_cut_img(im, box, leftAdjust=0.01, rightAdjust=0.01) # tmpPath = os.path.join(os.getcwd(),"backend","api","handwrite","test", str(i) + '.jpg') # tmpImg.save(tmpPath, quality=95) text = ocrModel(tmpImg) if text['text'] != '': text['box'] = [int(x) for x in box] text['textprob'] = round(float(scores[i]), 2) result.append(text) result = sorted(result, key=lambda x: sum(x['box'][1::2])) #draw box in to original image if len(drawBoxes) > 0: drawImg = draw_boxes(img, drawBoxes) # drawPath = os.path.join(os.getcwd(),"backend","api","handwrite","test","draw.jpg") cv2.imwrite(drawPath, drawImg) except: result = [{"text": '不支持的文件内容', "box": [], "drawUrl": ''}] return result, drawUrl
def text_ocr(img, scale, maxScale, TEXT_LINE_SCORE): boxes, scores = textModel(img, scale=scale, maxScale=maxScale) result = [] im = Image.fromarray(img) for i, box in enumerate(boxes): if scores[i] > TEXT_LINE_SCORE: tmpImg = rotate_cut_img(im, box, leftAdjust=0.01, rightAdjust=0.01) text = ocrModel(tmpImg) result.append({ 'text': text, 'box': [int(x) for x in box], 'prob': round(float(scores[i]), 2) }) return result
def text_ocr(img, scale, maxScale, TEXT_LINE_SCORE): boxes, scores = textModel(img, scale=scale, maxScale=maxScale) result = [] im = Image.fromarray(img) for i, box in enumerate(boxes): if scores[i] > TEXT_LINE_SCORE: tmpImg = rotate_cut_img(im, box, leftAdjust=0.01, rightAdjust=0.01) text = ocrModel(tmpImg) if text['text'] != '': text['box'] = [int(x) for x in box] text['textprob'] = round(float(scores[i]), 2) result.append(text) result = sorted(result, key=lambda x: sum(x['box'][1::2])) return result
def text_ocr(img, scale, maxScale, TEXT_LINE_SCORE): boxes, scores = detect_lines(img, scale=scale, maxScale=maxScale) result = [] if len(boxes) == 0: return result im = Image.fromarray(img) for i, box in enumerate(boxes): if scores[i] < TEXT_LINE_SCORE: continue tmpImg = rotate_cut_img(im, box, leftAdjust=0.01, rightAdjust=0.01) text = ocrModel(tmpImg) if len(text) == 0: continue result.append({'text': text, 'box': [int(x) for x in box], 'prob': round(float(scores[i]), 2)}) result = sorted(result, key=lambda x: sum(x['box'][1::2])) return result