Пример #1
0
def one_pred(img_path):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    result = ocr.ocr(img_path, cls=True)
    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='/doc/simfang.ttf')
    im_show = Image.fromarray(im_show)

    n = 0
    stR = ""
    for line in result:
        #print(line[1])
        n += 1
        texts = line[1][0]
        stR = stR + "," + str(texts)
        #print(texts)
    print(stR)
    im_show.show()
    return stR
Пример #2
0
def ocr_once(path):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    img = cv2.imread(path)
    result = ocr.ocr(img, cls=True)
    # print(type(result))
    print(result)
    return check_c1(result)
Пример #3
0
def img_word_translate(img_path):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    result = ocr.ocr(img_path, cls=True)
    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='/doc/simfang.ttf')
    im_show = Image.fromarray(im_show)

    n = 0
    stR = ""
    for line in result:
        # print(line[1])
        n += 1
        texts = line[1][0]
        if re.findall('[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]', texts):
            stR = stR  + str(texts)
        else:
            stR = stR + str(texts) + ","
        # print(texts)
    print(stR)
    translate(stR, "en", "zh-CN")
    im_show.show()
    return stR
def ocr_once():
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    img_path = '/home/jiantang/桌面/tmp/certification/认证合一/归档/道路运输证/c1_001.png'
    image = cv2.imread(img_path)
    result = ocr.ocr(image, cls=True)

    # 保存框框结果
    boxes = [line[0] for line in result]
    txts = [line[1][0] for line in result]
    scores = [line[1][1] for line in result]
    for i in range(len(boxes)):
        pts = np.array(boxes[i], np.int32)
        pts = pts.reshape((-1, 1, 2))
        cv2.polylines(image, [pts], True, (0, 255, 255))
    cv2.imwrite('tmp_img.png', image)

    # 保存文字结果
    img = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)
    for i in range(len(boxes)):
        pts = np.array(boxes[i], np.int32)
        pts = pts.reshape((-1, 1, 2))
        cv2.polylines(img, [pts], True, (0, 255, 255))
        img = cv2ImgAddText(img, txts[i], int(boxes[i][0][0] - 1),
                            int(boxes[i][0][1] - 1), (0, 255, 255), 20)
    cv2.imwrite('tmp_txt.png', img)
    cv2.imshow('line', img)
    cv2.waitKey()
Пример #5
0
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
Пример #6
0
def convert_from_url(img_url):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    response = requests.get(img_url)
    image = np.array(Image.open(BytesIO(response.content)))
    result = ocr.ocr(image, cls=True)
    text = [line[1][0] for line in result]
    return " ".join(text)
Пример #7
0
def for_not_rec_test():
    ocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=False)
    en_ocr = PaddleOCR(use_angle_cls=True, lang="en", use_gpu=False)

    dst_root_file_dir = "C:/Users/FH/Desktop/ocrtupianjieya/icpr_mtwi_task1/result20201230"
    transfer_not_rec_dir = os.path.join(dst_root_file_dir, "transfer_not_rec")

    file_names = os.listdir(transfer_not_rec_dir)
    test_result_path = os.path.join(dst_root_file_dir, "rec_test.txt")
    already_files = []
    if os.path.exists(test_result_path):
        reader = open(test_result_path, 'r', encoding='utf8')
        already_files = [i.strip().split(" ")[0] for i in reader.readlines()]
        reader.close()
    writer = open(test_result_path, 'a+', encoding='utf8')
    begin_time = time.time()
    count = 0
    for index, file_name in enumerate(file_names):
        if file_name in already_files:
            continue
        count += 1
        out = ""
        try:
            file_path = os.path.join(transfer_not_rec_dir, file_name)
            result = ocr.ocr(file_path, det=False, cls=True)
            en_result = en_ocr.ocr(file_path, det=False, cls=True)
            if result:
                out = "".join(i[0] for i in result)
            if en_result:
                en_out = "".join(i[0] for i in en_result)
                if len(en_out) > len(out):
                    out = en_out
            writer.write(file_name + " " + out + "\n")
            if count <= 10:
                print(out)
            if (index + 1) % 500 == 0:
                print("当前已处理{}条数据,目前处理平均速度为{:.2f}条/s".format(
                    index + 1, count / (time.time() - begin_time)))
                writer.flush()
            # if index == 5000:
            #     break
        except Exception as e:
            print("============error===========")
            print(e)
            print(file_name)
            writer.write(file_name + " " + out + "\n")
    writer.close()
Пример #8
0
def one_pred(img_path):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    result = ocr.ocr(img_path, cls=False)
    # print(result)
    n = 0
    stR = ""
    phone = ""  #电话
    shopName = ""  #店铺名
    describe = ""  #描述
    otherinfo = "没有其他信息"
    print("+++", result)
    for line in result:
        n += 1
        texts = line[1][0]
        if texts == "":
            continue
        if len(texts) < 2:
            continue
        r = test.pre(texts)
        if len(texts) < 3:
            continue
        if r == "商铺名":
            shopName = shopName + texts
            continue
        s = ms.pre(texts)
        if s == "描述":
            describe = describe + texts
            continue
        stR = stR + str(line[1][0]) + ":" + str(line[1][1]) + "\n"
        phones = texts.split("1")
        # print("++",phone)
        for i in phones:
            # print(i)
            if re.match(r'^[1-9]\d{9}$', i):
                a = "1" + i
                # print(a)
                phone = phone + ";" + a
            else:
                phone = " 图片中没有电话信息"
    phone = phone[1:]
    print("****", shopName, phone, describe, otherinfo)
    #return im_show.show()

    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='/doc/simfang.ttf')
    im_show = Image.fromarray(im_show)
    # im_show.show()
    # im_show.save是保存识别后的图片
    fanhui_img = 'result.jpg'
    im_show.save(fanhui_img)

    return shopName, phone, describe, otherinfo, fanhui_img
Пример #9
0
def init():
    ocr = PaddleOCR(
        use_angle_cls=True, lang="ch"
    )  # need to run only once to download and load model into memory
    img_path = './data/3.jpg'
    result = ocr.ocr(img_path, cls=True)
    for line in result:
        print(line)
Пример #10
0
    def ocr_run():
        global ocr_init
        ocr = PaddleOCR(use_angle_cls=True, use_gpu=True, use_tensorrt=False, gpu_mem=3000, det_max_side_len=960, rec_batch_num=2)
        cnt = 0
        last_result = []
        while not b_exit:
            try:
                frame = ocr_queue.get(block=True, timeout=0.1)
            except:
                continue 
            
            print('ocr got one frame ....')
            t0 = time.time()
            result = ocr.ocr(frame, cls=True)
            t1 = time.time()

            #try:
            #    speech_que.put_nowait(result)
            #except:
            #    pass
            if len(result) < 1:
                continue 

            try:
                if last_result:
                    likehood = samelike(last_result, result)

                    if likehood >= 0.6: # and len(last_result) == len(result):
                        Speech.Block_Speech_text("请翻页")
                        last_result = result
                        continue

                last_result = result

            except:
                traceback.print_exc()
                pass

            if 1:
                text_lst = []
                for line in result:
                    text = line[1][0]
                    text_lst.append(text)
                    print(line)

                for text in text_lst:
                    Speech.Block_Speech_text(text)

            cnt += 1

            if cnt % 1 == 0:
                print("frame cnt [%d] ocr detect delay = %.1fms" % (cnt, (t1 - t0) * 1000))
Пример #11
0
    def convertText2(self, img):

        ocr = PaddleOCR(
        )  # need to run only once to download and load model into memory
        result = ocr.ocr(
            img)  # return a list with coordinates, content and accuracy

        index = 0
        done = False
        for line in result:
            if done: break
            [corList, (txt, _)] = line
            [[_, y1], [x2, _], [_, _], [_, y4]] = corList
            if '期货品种' in txt:
                self.findContent2(index, y1, y4, x2, result, '期货品种')
            elif '现货头寸' in txt:
                self.findContent2(index, y1, y4, x2, result, '现货头寸')
            elif '期货头寸' in txt:
                self.findContent2(index, y1, y4, x2, result, '期货头寸')
            elif '现货数量' in txt:
                self.findContent2(index, y1, y4, x2, result, '现货数量')
            elif '手' in txt:
                self.findContent2(index, y1, y4, x2, result, '合约数量')
            elif '计划套保' in txt:
                [corListPrev, (txtP, _)] = result[index - 1]
                [corListAft, (txtA, _)] = result[index + 1]
                [[_, yp1], _, _, _] = corListPrev
                [[_, ya1], _, _, _] = corListAft
                if abs(yp1 - y1) < abs(ya1 - y1):
                    self.textDict['hedgeRatio'] = txtP
                else:
                    self.textDict['hedgeRatio'] = txtA
            elif '保证金规模' in txt:
                self.findContent2(index, y1, y4, x2, result, '保证金规模')
            elif txt == '最大亏损限额':
                self.findContent2(index, y1, y4, x2, result, '最大亏损')
            elif txt == '行情分析':
                self.get_multi_lines(index, result, '行情分析')
            elif txt == '持仓预案':
                self.get_multi_lines(index, result, '持仓预案')
            elif txt == '平仓预案':
                self.get_multi_lines(index, result, '平仓预案')
            elif '止盈位' in txt:
                self.get_multi_lines(index, result, '止盈')
            elif '止损位' in txt:
                self.get_multi_lines(index, result, '止损')
            elif '备注' in txt:
                self.get_multi_lines(index, result, '备注')
                done = True

            index += 1
Пример #12
0
class Vehicle_License_Plate_Recognition(nn.Layer):
    def __init__(self):
        super(Vehicle_License_Plate_Recognition, self).__init__()
        self.vlpr = PaddleOCR(
            det_model_dir=os.path.join(self.directory, 'det_vlpr'),
            rec_model_dir=os.path.join(self.directory,
                                       'ch_ppocr_server_v2.0_rec_infer'))

    @staticmethod
    def base64_to_cv2(b64str):
        data = base64.b64decode(b64str.encode('utf8'))
        data = np.frombuffer(data, np.uint8)
        data = cv2.imdecode(data, cv2.IMREAD_COLOR)
        return data

    def plate_recognition(self, images=None):
        assert isinstance(images, (list, str, np.ndarray))
        results = []

        if isinstance(images, list):
            for item in images:
                for bbox, text in self.vlpr.ocr(item):
                    results.append({'license': text[0], 'bbox': bbox})

        elif isinstance(images, (str, np.ndarray)):
            for bbox, text in self.vlpr.ocr(images):
                results.append({'license': text[0], 'bbox': bbox})

        return results

    @serving
    def serving_method(self, images):
        if isinstance(images, list):
            images_decode = [self.base64_to_cv2(image) for image in images]
        elif isinstance(images, str):
            images_decode = self.base64_to_cv2(images)

        return self.plate_recognition(images_decode)
Пример #13
0
def one_pred(img_path):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    result = ocr.ocr(img_path, cls=True)
    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='/doc/simfang.ttf')
    im_show = Image.fromarray(im_show)
    im_show.show()
Пример #14
0
def ocr_preprocess(img_dir):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=True)
    ocr_reses = []
    img_names = sorted(
        os.listdir(img_dir), key=lambda x: int(x.split("_")[1].split(".")[0]))
    for img_name in img_names:
        img_path = os.path.join(img_dir, img_name)
        parsing_res = ocr.ocr(img_path, cls=True)
        ocr_res = []
        for para in parsing_res:
            ocr_res.append({"text": para[1][0], "bbox": para[0]})
        ocr_reses.append((img_name, ocr_res))

    return ocr_reses
Пример #15
0
def demo_use():
    # Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改lang参数进行切换,参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`。

    ocr = PaddleOCR(use_angle_cls=True, lang="en", use_gpu=False)

    # 输入待识别图片路径

    img_path = "C:/Users/FH/Desktop/ocrtupianjieya/icpr_mtwi_task1/test_line_image_transfer/line_101415.jpg"
    # img_path = "C:/Users/FH/Desktop/line_101415_angel.jpg"
    # img_path = "../pillow_toolkits/line_100031_higher_gray.jpg"

    # 输出结果保存路径
    result = ocr.ocr(img_path, det=False, cls=True)
    print(result)
Пример #16
0
    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
Пример #17
0
def demo_use2():
    ocr = PaddleOCR(use_angle_cls=True, lang="ch")
    file_dir = "C:/Users/FH/Desktop/ocrtupianjieya/icpr_mtwi_task1/test_line_image_transfer"
    file_names = os.listdir(file_dir)
    reader = open(
        "C:/Users/FH/Desktop/ocrtupianjieya/icpr_mtwi_task1/result_transfer.txt",
        'r',
        encoding='utf8')
    already_files = [i.strip().split(" ")[0] for i in reader.readlines()]
    reader.close()
    writer = open(
        "C:/Users/FH/Desktop/ocrtupianjieya/icpr_mtwi_task1/result_transfer.txt",
        'a+',
        encoding='utf8')
    import time
    begin_time = time.time()
    count = 0
    for index, file_name in enumerate(file_names):
        if file_name in already_files:
            continue
        count += 1
        out = ""
        try:
            file_path = os.path.join(file_dir, file_name)
            result = ocr.ocr(file_path, det=False, cls=False)
            if result:
                out = result[0][0]
            writer.write(file_name + " " + out + "\n")
            if count <= 10:
                print(result)
            if (index + 1) % 500 == 0:
                print("当前已处理{}条数据,目前处理平均速度为{}条/s".format(
                    index + 1, count / (time.time() - begin_time)))
                writer.flush()
            # if index == 5000:
            #     break
        except Exception as e:
            print("============error===========")
            print(e)
            print(file_name)
            writer.write(file_name + " " + out + "\n")
    writer.close()
Пример #18
0
def text_detection(input_file='../data/input/30800.jpg',
                   output_file='../data/output',
                   show=False,
                   method='google',
                   paddle_model=None):
    '''
    :param method: google or paddle
    :param paddle_model: the preload paddle model for paddle ocr
    '''
    start = time.clock()
    name = input_file.split('/')[-1][:-4]
    ocr_root = pjoin(output_file, 'ocr')
    img = cv2.imread(input_file)

    if method == 'google':
        print('*** Detect Text through Google OCR ***')
        ocr_result = ocr.ocr_detection_google(input_file)
        texts = text_cvt_orc_format(ocr_result)
        texts = merge_intersected_texts(texts)
        texts = text_filter_noise(texts)
        texts = text_sentences_recognition(texts)
    elif method == 'paddle':
        # The import of the paddle ocr can be separate to the beginning of the program if you decide to use this method
        from paddleocr import PaddleOCR
        print('*** Detect Text through Paddle OCR ***')
        if paddle_model is None:
            paddle_model = PaddleOCR(use_angle_cls=True, lang="ch")
        result = paddle_model.ocr(input_file, cls=True)
        texts = text_cvt_orc_format_paddle(result)
    else:
        raise ValueError('Method has to be "google" or "paddle"')

    visualize_texts(img,
                    texts,
                    shown_resize_height=800,
                    show=show,
                    write_path=pjoin(ocr_root, name + '.png'))
    save_detection_json(pjoin(ocr_root, name + '.json'), texts, img.shape)
    print("[Text Detection Completed in %.3f s] Input: %s Output: %s" %
          (time.clock() - start, input_file, pjoin(ocr_root, name + '.json')))
Пример #19
0
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')
def ocr():
    upload = request.files.get('upload')
    lang = request.forms.get('lang')
    print(lang)
    global current_lang
    global ocr
    if current_lang!=lang:
        current_lang=lang
        ocr = PaddleOCR(lang=current_lang)
        
    name, ext = os.path.splitext(upload.filename)
    print(ext.lower())
    if ext.lower() not in ('.png','.jpg','.jpeg'):
        return "File extension not allowed."
    timestamp=str(int(time.time()*1000))
    savedName=timestamp+ext
    save_path = "./uploaded/"
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    file_path = "{path}/{file}".format(path=save_path, file=savedName)
    if os.path.exists(file_path)==True:
        os.remove(file_path)
    upload.save(file_path)        
    ret = {}
    result = ocr.ocr(file_path)
    text_lines=[]
    for line in result:
        text_line={}
        index=0
        for coord in line[0]:
            text_line["x"+str(index)]=int(coord[0])
            text_line["y"+str(index)]=int(coord[1])
            index=index+1
        text_line["text"]=line[1][0]
        text_lines.append(text_line)
    os.remove(file_path)
    ret["text_lines"]=text_lines
    return ret    
Пример #21
0
    def convertText1(self, img):

        ocr = PaddleOCR(
        )  # need to run only once to download and load model into memory
        result = ocr.ocr(
            img)  # return a list with coordinates, content and accuracy

        index = 0
        done = False
        for line in result:
            if done: break
            [corList, (txt, _)] = line
            [[x1, y1], [x2, y2], [x3, y3], [x4, y4]] = corList
            if txt == '委托主体名称':
                [corListPrev, (txtP, _)] = result[index - 1]
                [corListAft, (txtA, _)] = result[index + 1]
                [[_, yp1], _, _, _] = corListPrev
                [[_, ya1], _, _, _] = corListAft
                if abs(yp1 - y1) < abs(ya1 - y1):
                    self.textDict['entrustSubject'] = txtP
                else:
                    self.textDict['entrustSubject'] = txtA
            elif txt == '操作主体名称':
                [corListPrev, (txtP, _)] = result[index - 1]
                [corListAft, (txtA, _)] = result[index + 1]
                [[_, yp1], _, _, _] = corListPrev
                [[_, ya1], _, _, _] = corListAft
                if abs(yp1 - y1) < abs(ya1 - y1):
                    self.textDict['operatorName'] = txtP
                else:
                    self.textDict['operatorName'] = txtA
            elif '现货品种' in txt:
                self.findContent1(index, y1, y4, x2, result, '现货品种')
            elif '开仓价格' in txt:
                self.findContent1(index, y1, y4, x2, result, '开仓价格')
                done = True

            index += 1
Пример #22
0
def detect(image):
    # PATH_IMG_IN = './in'
    # filename = os.path.join(PATH_IMG_IN, '1.png')
    # filename = 'FSRCNNp2x2.jpg'
    # image = cv2.imread("picture2.jpg")
    # cv2.imshow("a", image)
    image1 = copy.deepcopy(image[0:int(image.shape[0]/2), 0:int(image.shape[1]/2), :])
    # print(image.shape)
    # cv2.imshow("b", image1)
    # cv2.waitKey()
    ocr = PaddleOCR()  # need to run only once to download and load model into memory
    start = time.perf_counter()
    result = ocr.ocr(image1, rec=True)
    # 第一个坐标表示的是第几个识别的字,第二个表示的识别的字中的是字的坐标还是还是字的内容或者置信度,第三个坐标表示的是四个坐
    # 标中的第几个坐标,第四个坐标是是一个具体的坐标的x或者y
    # print(result)
    end = time.perf_counter()
    print('检测文字区域 耗时{}'.format(end - start))
    # 每个矩形,从左上角顺时针排列
    for rect1 in result:
        if 'XODTC' in rect1[1][0]:
            # print(rect1[1][0])
            return rect1[1][0]
Пример #23
0
    plt.savefig('./img.png')
    plt.show()


if __name__ == '__main__':
    # 模型路径下必须含有model和params文件
    ocr = PaddleOCR(
        det_model_dir='./PaddleOCR/output/ch_db_mv3_inference/inference',
        use_angle_cls=True)
    lac = Taskflow("pos_tagging")

    enti_list = []
    pdfFolder = './ResearchReport'
    for p in os.listdir(pdfFolder):
        if os.path.isdir(os.path.join(pdfFolder, p)):
            print('Processing folder:', p)
            imgPath = pdfFolder + '/' + p
            res_list = []
            for i in os.listdir(imgPath):
                img_path = os.path.join(imgPath, i)
                result = ocr.ocr(img_path, cls=True)
                res_list.append(result)

            enti = LAC(lac, res_list)
            enti_list += enti

    counter = Counter(enti_list)
    print('Entity results:', counter)

    PlotHist(counter)
Пример #24
0
from appzoo.utils.streamlit_utils import *

ocr = PaddleOCR(use_angle_cls=True, lang="ch")

# side
st.sidebar.markdown('**OCR SideBar**')
biz = st.sidebar.selectbox('输入方式', ('ImageUrl', 'ImageFile'), index=0)


if biz == 'ImageUrl':
    ImageUrl = st.text_input("ImageUrl",
                             "https://i1.mifile.cn/f/i/mioffice/img/slogan_5.png?1604383825042")
    input_image = 'image.png'
    # import wget
    # wget.download(ImageUrl, img_path)
    os.system(f"wget {ImageUrl} -O {input_image}")
    result = ocr.ocr(input_image, cls=True)
    output_image = ocr_result_image(result, input_image)
    st.image(output_image)
    st.json(result)


elif biz == 'ImageFile':
    input_image = file_uploader(st)
    if input_image:
        result = ocr.ocr(input_image, cls=True)
        output_image = ocr_result_image(result, input_image)
        st.image(output_image)
        st.json(result)
import sys
from paddleocr import PaddleOCR, draw_ocr

reader = PaddleOCR(use_angle_cls=True, lang="en")
input_list = ["view_5c261ffd2ff010ed8595940026e53cfd.png","view_5f4ff95b432cfa28b166053bda27c097.png","view_6a6be836c131f3f7021e87fe47a1d00d.png","view_5ecdf5e7309ea11e4d58e182ad889634.png","view_6a5d46d99a2945eb892655451e2a960a.png"]
#input_list = ["view_3c561a0409fc2ea64de5ea0c80fe0b8e.png","view_3c62f950dfe5a3b38f57532fd5c3d9e5.png","view_3c6810045d2cbb14533f99223d9a379c.png","view_3c9da8fdd9e884323150e16ff2b1d2c4.png","view_3ce096c75172bb12bc2c9bae21de2a26.png","view_3cee3d41cf42711eda8f9304588eadfc.png","view_3d30d1c286f18b956903439dfa84c484.png","view_3d8c722a35db5edb098cf5d9f39ab0d8.png","view_3e88a3a99b8cf04353c80c953dcbb38d.png","view_3eb1c7df278c46060f91932e182cfc18.png","view_3eeb214ece2d1f76bba0db3978e7246e.png","view_3f6adc786346c113b236065fe7f457fc.png","view_4a1030eea495bcd57615f9ab795322a7.png","view_4b1494d7a7c34e92b6e13e00abeab862.png","view_4d53fb6e03e464619029ba18872a8774.png","view_4f3bfd2c2445e93b4e19fc9a90c29c15.png","view_4f7a04d575274c1481dbbeb7dd94dfe1.png","view_4f7ba3a89e52793167224517ef8bdbb7.png"] 
for input_item in input_list:
        print(input_item)
        result = reader.ocr(input_item,cls=True)
        print('>----\n',result,'----<')
Пример #26
0
def pre_save(img_path,save_path,csv_path,m_filePicker11=None):
    ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
    i = 0
    names =[]
    #abels = []
    imgs=[]
    one_qian_pic_path = ""
    one_hou_pic_path = ""
    #存放所有的图片路径
    n_qian_pic_path = []
    n_hou_pic_path = []

    j=0
    stR = ""

    # 启动窗口
    window.Show(True)

    jindu_len = len(os.listdir(img_path)) + 2
    print("共:"+str(jindu_len-2))
    window.m_gauge1.SetRange(jindu_len)
    dangqian_jindu = 1
    n=0

    for img in os.listdir(img_path):
        n+=1
        window.m_staticText11.SetLabel("正在识别:"+img_path+'/'+img)
        print("当前正在识别"+str(dangqian_jindu)+"-->"+"正在识别:"+img_path+'/'+img)
        window.m_gauge1.SetValue(dangqian_jindu)
        dangqian_jindu = dangqian_jindu + 1

        #动态显示进度条
        if dangqian_jindu==jindu_len:
            window.m_gauge1.SetValue(dangqian_jindu)
        print(img_path+'/'+img)
        i+=1
        result = ocr.ocr(img_path+'/'+img, cls=True)

        image = Image.open(img_path+'/'+img).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='/doc/simfang.ttf')
        im_show = Image.fromarray(im_show)
        # im_show.show()
        # im_show.save是保存识别后的图片
        sp = save_path+"/"+img
        im_show.save(sp)
        print("图片保存的位置:"+sp)
        #存储所有的图片路径
        n_qian_pic_path.append(img_path+'/'+img)
        n_hou_pic_path.append(save_path+"/"+img)
        if j==0:
            one_hou_pic_path = save_path+"/"+img
            one_qian_pic_path = img_path+'/'+img
        j = j + 1
        if str(result) == "None":
            continue
        for line in result:
            texts = line[1][0]
            if texts == "":
                continue

            if texts in names:
                continue
            texts = re.findall('[\u4e00-\u9fa50-9]+', texts, re.S)  # 只要字符串中的中文,字母,数字
            texts = "".join(texts)

            label = test.pre(texts)
            if len(texts) < 3:
                label = "非商铺名"
            if label == "商铺名":
                #print(texts, " :", label)
                stR = stR + str(img) + ":" + str(texts) + "\n"
                imgs.append(img)
                #labels.append(label)
                names.append(texts)
        dataframe = pd.DataFrame({'图片名': imgs, '商铺名称': names})
        if csv_path[-3:]=="xls":
            dataframe.to_excel(csv_path, index=True)
        elif csv_path[-3:]=="csv":
            dataframe.to_csv(csv_path, index=True,sep=',')
        else:
            print("请选择csv或者xls文件")
    stR = stR + "\n" + "共识别了" + str(n) + "张图片" + "\n" + "共检测出商铺名" + str(i) + "个\n"
    dangqian_jindu = dangqian_jindu + 1
    window.m_gauge1.SetValue(dangqian_jindu)
    print("当前正在识别" + str(dangqian_jindu))
    print(i)

    return stR,one_qian_pic_path,one_hou_pic_path,n_qian_pic_path,n_hou_pic_path
Пример #27
0
ocr = PaddleOCR(rec_model_dir=rec_model_dir, lang='en')
# ocr = PaddleOCR(use_space_char=True)
#det_model_dir='{your_det_model_dir}', rec_model_dir='{your_rec_model_dir}', rec_char_dict_path='{your_rec_char_dict_path}', cls_model_dir='{your_cls_model_dir}', use_angle_cls=True
# img_path = 'https://image.jiandan100.cn/images/cqaimages/42/255/2817944_q.jpg'
# img_path = 'https://image.jiandan100.cn/images/cqaimages/3/73/215296_q.jpg'

response = requests.get(
    'http://8.210.115.9/img.php?num=NjMyMzgy&x=MzM5NDgwNTgzNTk=&s=77600120640')
img_fp = Image.open(BytesIO(response.content))
# img_fp = Image.open('img.png')
np_image = np.array(img_fp)

gray = cv2.cvtColor(np_image, cv2.COLOR_BGR2GRAY)

# print(gray)

t, rst = cv2.threshold(gray, 168, 255, cv2.THRESH_BINARY_INV)

result = ocr.ocr(rst, det=False, rec=True, cls=False)

print(result[0][0])
print(result)

# for line in result:
#     print(line)
#     print(line[1][0])

# text = '\n'.join([a[1][0] for a in result])

# print(text)
Пример #28
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project      : tql-App.
# @File         : ocr_demo
# @Time         : 2020/11/4 6:00 下午
# @Author       : yuanjie
# @Email        : [email protected]
# @Software     : PyCharm
# @Description  :

# https://github.com/PaddlePaddle/PaddleOCR/blob/develop/doc/doc_ch/whl.md
from paddleocr import PaddleOCR, draw_ocr
# 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 = '/Users/yuanjie/Desktop/image.png'
result = ocr.ocr(img_path, cls=True)
for line in result:
    print(line)

from tql.pipe import *
rs = [img_path] * 100 | xThreadPoolExecutor(lambda p: ocr.ocr(p, cls=True), 10)

print(list(rs))
Пример #29
0
from paddleocr import PaddleOCR
from tools.infer.utility import draw_ocr
ocr = PaddleOCR(
)  # need to run only once to download and load model into memory
img_path = r'F:\chrome\zrbdata\data\images\33.jpg'
result = ocr.ocr(img_path, rec=False)
print("+++", result)
for line in result:
    print(line)

# 显示结果
from PIL import Image

image = Image.open(img_path).convert('RGB')
im_show = draw_ocr(image,
                   result,
                   txts=None,
                   scores=None,
                   font_path='/path/to/PaddleOCR/doc/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')
im_show.show()
Пример #30
0
class Identity:
    def __init__(self):
        self.ocr_engine = PaddleOCR(use_gpu=Config.is_gpu,
                                    use_angle_cls=True,
                                    use_space_char=False,
                                    rec_model_dir=Config.rec_model_dir,
                                    det=Config.det_mode_dir)

    def front(self, path):
        result = self.ocr_engine.ocr(path, det=True, rec=True, cls=False)

        id = self.getId(result)
        name = self.getName(result)
        gender = self.getGender(id)
        nation = self.getNation(result)
        if nation in ["备"]:
            nation = "畲"

        birthday = self.getBirthday(id)

        os.remove(path)

        return {
            "name": name,
            "gender": gender,
            "nation": nation,
            "birthday": birthday,
            "id": id
        }

    def back(self, path):
        result = self.ocr_engine.ocr(path, det=True, rec=True, cls=False)
        # for line in result:
        #     print(line)
        sign = self.getSign(result)

        (start_date, end_date) = self.getExpired(result)

        return {"sign": sign, "start_date": start_date, "end_date": end_date}

    def getExpired(self, result):
        time = None

        pos = None
        for idx in range(len(result)):
            line = result[idx]
            val = str(line[1][0])
            if not val.startswith("有效期限"):
                continue
            pos = line[0][1]
            if len(val) > 4:
                time = val[4:]
                break

        if pos is None:
            return (None, None)

        if time is None:
            min_pos = 200000000

            for line in result:
                val = str(line[1][0])
                if val.startswith("签发机关") or val.startswith("有效期限"):
                    continue

                if pos[0] > line[0][1][0]:
                    continue

                interval_pixel = abs(line[0][1][1] - pos[1])
                if interval_pixel < min_pos:
                    min_pos = interval_pixel
                    time = line[1][0]

        if time is None:
            return (None, None)

        result = time.replace("-",
                              "").replace(".",
                                          "").replace("_",
                                                      "").replace("长期", "")
        # print(result)
        if len(result) != 16 and len(result) != 8:
            return (None, None)

        if len(result) == 16:
            return (result[0:4] + "." + result[4:6] + "." + result[6:8],
                    result[8:12] + "." + result[12:14] + "." + result[14:])

        if len(result) == 8:
            return (result[0:4] + "." + result[4:6] + "." + result[6:8], "长期")

        return (None, None)

    def getSign(self, result):
        pos = None
        for idx in range(len(result)):
            line = result[idx]
            val = str(line[1][0])
            if not val.startswith("签发机关"):
                continue
            pos = line[0][1]
            if len(val) > 4:
                return val[4:]

        if pos is None:
            return None

        min_pos = 200000000
        name = None
        for line in result:
            val = str(line[1][0])
            if val.startswith("签发机关") or val.startswith("有效期限"):
                continue

            if pos[0] > line[0][1][0]:
                continue

            interval_pixel = abs(line[0][1][1] - pos[1])
            if interval_pixel < min_pos:
                min_pos = interval_pixel
                name = line[1][0]
        return name

    def getName(self, result):
        pos = None
        for idx in range(len(result)):
            line = result[idx]
            val = str(line[1][0])
            if not val.startswith("姓名"):
                continue
            pos = line[0][1]
            if len(val) > 2:
                return val[2:]

        if pos is None:
            return None

        min_pos = 200000000
        name = None
        for line in result:
            val = str(line[1][0])
            if val.startswith("姓名") or val.startswith("性别") or val.startswith("民族") or val.startswith("住址")\
                    or val.startswith("公民身份号码"):
                continue

            if pos[0] > line[0][1][0]:
                continue

            interval_pixel = abs(line[0][1][1] - pos[1])
            if interval_pixel < min_pos:
                min_pos = interval_pixel
                name = line[1][0]
        return name

    def getGender(self, id):
        # id=self.getId(result)
        if id is None:
            return None
        if len(id) < 15:
            return None

        if int(id[15]) % 2 == 1:
            return "男"
        else:
            return '女'

    def getNation(self, result):
        pos = None
        for idx in range(len(result)):
            line = result[idx]
            val = str(line[1][0])
            if not val.startswith("民族"):
                continue

            pos = line[0][1]

            if len(val) > 2:
                return val[2:]
        if pos is None:
            return None

        min_pos = 200000000
        nation = None
        for line in result:
            val = str(line[1][0])
            if val.startswith("姓名") or val.startswith("性别") or val.startswith("民族") or val.startswith("住址") \
                    or val.startswith("公民身份号码"):
                continue

            if pos[0] > line[0][1][0]:
                continue

            interval_pixel = abs(line[0][1][1] - pos[1])
            if interval_pixel < min_pos:
                min_pos = interval_pixel
                nation = line[1][0]

        return nation

    def getBirthday(self, id):
        # id=self.getId(result)
        if id is None:
            return None

        year = id[6:10]
        month = id[10:12]
        day = id[12:14]

        return "%s年%d月%d日" % (year, int(month), int(day))

    def getAddress(self, result):
        pos1 = None
        for line in result:
            val = str(line[1][0])
            if not val.startswith('出生'):
                continue

            pos1 = line[0][3]

        address = ''

        for line in result:
            if pos1[0] > line[0][0][0]:
                continue
            if pos1[1] > line[0][0][1]:
                continue
            val = str(line[1][0])
            if re.findall("\d{15,}", val):
                continue

            address += val
        if address == '':
            return None
        return address

    def getId(self, result):
        for line in result:
            val = str(line[1][0])
            if not val.startswith("公民身份号码"):
                continue

            if len(val) >= 6:
                id = str(val[6:])
                id = self.modify_id(id)
                if re.findall("\d{15,}", id):
                    if not self.check_id(id):
                        return None
                    return id

        for line in result:
            id = str(line[1][0])
            id = self.modify_id(id)
            if re.findall("\d{15,}", id):
                if not self.check_id(id):
                    return None
                return id

        return None

    def check_id(self, id):
        if len(id) < 17:
            return False

        if len(id) == 18:
            if not id[:-1].isdigit():
                return False

            fact = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
            sum = 0
            for i in range(17):
                sum += fact[i] * int(id[i])
            m = sum % 11
            chk = '10X98765432'
            if chk[m] != id[-1]:
                return False

        year = int(id[6:10])
        month = int(id[10:12])
        day = int(id[12:14])

        if not (year >= 1890 and year <= datetime.datetime.now().year):
            return False

        if not (month >= 1 and month <= 12):
            return False

        if not (day >= 1 and day <= 31):
            return False

        return True

    def modify_id(self, id):
        id = id.replace("i", "1").replace("I", "")
        id = id.replace("z", "2").replace("Z", "2")
        id = id.replace("o", "0").replace("O", "0")
        return id.replace("x", "X")