def GetText(path=None,result_pic=False): """ 文字识别 :param path:图片路径 :return: """ if not path: path = "F:/git/wxbot/dm/pic/222.png" # ocr = PaddleOCR(use_gpu=False,det_model_dir="./inference/ch_det_mv3_db/",rec_model_dir="./inference/ch_det_mv3_db/") # need to run only once to download and load model into memory ocr = PaddleOCR(gpu=False, det_model_dir="./inference/ch_det_r50_vd_db", rec_model_dir="./inference/ch_rec_r34_vd_crnn_infer") # need to run only once to download and load model into memory result = ocr.ocr(path) txts = [line[1][0] for line in result] # 展示图片 if result_pic: image = Image.open(path).convert('RGB') boxes = [line[0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores) im_show = Image.fromarray(im_show) TMP_FILE = os.path.join(conf.TMP_PATH, "result.png") im_show.save(TMP_FILE) # print(txts) return txts
async def tt_ocr(request): try: # 获取数据 req_json = request.json imgbase64 = req_json['imageBase64'] do_img = base64_to_image(imgbase64) # 处理图片 img_result = ocr.ocr(do_img) # 画图 boxes = [line[0] for line in img_result] txts = [line[1][0] for line in img_result] scores = [line[1][1] for line in img_result] done_img = draw_ocr( do_img, boxes, txts, scores, font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf') # out图片转base64 imgbase64 = image_to_base64(done_img) ret_data = {'out_imageBase64': imgbase64} return response.json(ret_data) except: traceback.print_exc()
def ocr_result_image(result, input_image, output_image='output_image.png'): if not os.path.exists('./simfang.ttf'): os.system(f"wget -q {fds_url}/data/simfang.ttf") image = Image.open(input_image).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path="./simfang.ttf") # im_show = draw_ocr(image, boxes, txts, scores, font_path=get_module_path("../data/simfang.ttf", __file__)) im_show = Image.fromarray(im_show) im_show.save(output_image) return output_image
def drawImg(result, image): image = safeShape(image) boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path="/code/doc/simfang.ttf") im_show = Image.fromarray(im_show) im_show.save(base_path + '/static/result.jpg') return toPngStr(im_show)
def ocr_result_image(result, input_image, output_image='output_image.png'): # https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/develop/doc/simfang.ttf image = Image.open(input_image).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path=get_module_path("../data/simfang.ttf", __file__)) im_show = Image.fromarray(im_show) im_show.save(output_image) return output_image
def imageOcrOperator(): image_path = "./static/images/input.jpg" out_path = './static/images/orc_result.jpg' result = OCR.ocr(image_path) image = Image.open(image_path).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path='./static/tools/simfang.ttf') im_show = Image.fromarray(im_show) im_show.save(out_path) return out_path
def detect_OCR(self, img): # Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换 # 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。 ocr = PaddleOCR(use_angle_cls=True, use_gpu=False, lang="ch", use_space_char=True,det_db_unclip_ratio=2.0) # det_db_unclip_ratio=2.5这是参数定义位置,这个参数是检测后处理时控制文本框大小的,默认2.0,可以尝试改成2.5或者更大,反之,如果觉得文本框不够紧凑,也可以把该参数调小。 need to run only once to download and load model into memory img_path = img result = ocr.ocr(img_path,cls=False) for line in result: print(line) # 显示结果 from PIL import Image image = Image.open(img_path).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path='/path/to/PaddleOCR/doc/simfang.ttf') im_show = Image.fromarray(im_show) im_show.save('result.jpg') return boxes, txts
def ocr_once(): # Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换 # 参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。 ocr = PaddleOCR( use_angle_cls=True, lang="ch" ) # need to run only once to download and load model into memory img_path = 'test/3.jpg' img = cv2.imread(img_path) start = time.time() result = ocr.ocr(img, cls=True) print(result) print(time.time() - start) # 显示结果 from PIL import Image image = Image.open(img_path).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path='simfang.ttf') im_show = Image.fromarray(im_show) im_show.save('result/result3.jpg')
import os from paddleocr import PaddleOCR, draw_ocr # 模型路径下必须含有model和params文件 ocr = PaddleOCR(det_model_dir='models/li_det_infer', use_gpu=False) dir_path="D:/iiiiii/img_50/" dir_path2="D:/result02/" files = os.listdir(dir_path) print(files) for file in files : img_path = dir_path+str(file) result = ocr.ocr(img_path, rec=False) fo = open(dir_path2+str(file)[:-4]+".txt", "w") for line in result: fo.write(str(line) + "\n") fo.close() from PIL import Image image = Image.open(img_path).convert('RGB') # boxes = [line[0] for line in result] # txts = [line[1][0] for line in result] # scores = [line[1][1] for line in result] im_show = draw_ocr(image, result,txts=None, scores=None,font_path='D:/paddle_pp/PaddleOCR/doc/simfang.ttf') im_show = Image.fromarray(im_show)
from paddleocr import PaddleOCR, draw_ocr # 模型路径下必须含有model和params文件 ocr = PaddleOCR(rec_model_dir='models/li_rec_infer', use_angle_cls=True, use_gpu=False) img_path = 'imgs/xxxxxxxx.png' result = ocr.ocr(img_path, cls=True) for line in result: print(line) # 显示结果 from PIL import Image image = Image.open(img_path).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf') im_show = Image.fromarray(im_show) im_show.save('result.jpg')
from paddleocr import PaddleOCR, draw_ocr ocr = PaddleOCR(use_angle_cls=True, lang="ch") # your own path img_path = "../img/test2.jpg" result = ocr.ocr(img_path, cls=True) for line in result: print(line) from PIL import Image image = Image.open(img_path).convert('RGB') boxes = [line[0] for line in result] txts = [line[1][0] for line in result] scores = [line[1][1] for line in result] im_show = draw_ocr(image, boxes, txts, scores, font_path='simfang.ttf') im_show = Image.fromarray(im_show) im_show.save('result2.jpg')