コード例 #1
0
ファイル: module.py プロジェクト: omar16100/PaddleOCR
    def merge_configs(self, ):
        # deafult cfg
        backup_argv = copy.deepcopy(sys.argv)
        sys.argv = sys.argv[:1]
        cfg = parse_args()

        update_cfg_map = vars(read_params())

        for key in update_cfg_map:
            cfg.__setattr__(key, update_cfg_map[key])

        sys.argv = copy.deepcopy(backup_argv)
        return cfg
コード例 #2
0
        if self.e2e_algorithm == 'PGNet':
            preds['f_border'] = outputs[0]
            preds['f_char'] = outputs[1]
            preds['f_direction'] = outputs[2]
            preds['f_score'] = outputs[3]
        else:
            raise NotImplementedError
        post_result = self.postprocess_op(preds, shape_list)
        points, strs = post_result['points'], post_result['strs']
        dt_boxes = self.filter_tag_det_res_only_clip(points, ori_im.shape)
        elapse = time.time() - starttime
        return dt_boxes, strs, elapse


if __name__ == "__main__":
    args = utility.parse_args()
    image_file_list = get_image_file_list(args.image_dir)
    text_detector = TextE2E(args)
    count = 0
    total_time = 0
    draw_img_save = "./inference_results"
    if not os.path.exists(draw_img_save):
        os.makedirs(draw_img_save)
    for image_file in image_file_list:
        img, flag = check_and_read_gif(image_file)
        if not flag:
            img = cv2.imread(image_file)
        if img is None:
            logger.info("error in loading image:{}".format(image_file))
            continue
        points, strs, elapse = text_detector(img)
コード例 #3
0
        dt_num = len(dt_boxes)
        for dno in range(dt_num):
            text, score = rec_res[dno]
            if score >= drop_score:
                text_str = "%s, %.3f" % (text, score)
                print(text_str)

        if is_visualize:
            image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
            boxes = dt_boxes
            txts = [rec_res[i][0] for i in range(len(rec_res))]
            scores = [rec_res[i][1] for i in range(len(rec_res))]

            draw_img = draw_ocr(image,
                                boxes,
                                txts,
                                scores,
                                drop_score=drop_score)
            draw_img_save = "./inference_results/"
            if not os.path.exists(draw_img_save):
                os.makedirs(draw_img_save)
            cv2.imwrite(
                os.path.join(draw_img_save, os.path.basename(image_file)),
                draw_img[:, :, ::-1])
            print("The visualized image saved in {}".format(
                os.path.join(draw_img_save, os.path.basename(image_file))))


if __name__ == "__main__":
    main(utility.parse_args())
コード例 #4
0
    def OnStart2(self):
        print(self.path.GetLabelText())

        args = utility.parse_args()
        args.image_dir = self.path.GetLabelText()
        args.det_model_dir = "./config/ch_det_mv3_db/"
        args.rec_model_dir = "./config/ch_rec_mv3_crnn/"
        args.use_gpu = False
        print("初始化系统")
        image_file_list = self.get_image_file_list(self.path.GetLabelText())
        text_sys = ocr_sys.TextSystem(args)

        if not os.path.exists(self.path.GetLabelText() + "/temp"):
            os.makedirs(self.path.GetLabelText() + "/temp")
        print("完成初始化")

        textNoDict = {}
        for n in range(len(image_file_list)):
            try:
                self.tipCurtLabel.SetLabelText("当前第" + str(n + 1) + "个")
                print(self.tipCurtLabel.GetLabelText())
                pdf_file = image_file_list[n]

                if pdf_file.endswith(('pdf')) == False:
                    continue

                (filePath, filename) = os.path.split(pdf_file)
                (filePathAndName, ext) = os.path.splitext(pdf_file)
                # 将pdf文件转为图片
                popplerPath = "./config/poppler"
                pages = convert_from_path(pdf_file,
                                          500,
                                          poppler_path=popplerPath)
                for page in pages:
                    page.save(self.path.GetLabelText() + "/temp/temp.jpg",
                              'JPEG')
                    img = self.cv_imread(self.path.GetLabelText() +
                                         "/temp/temp.jpg")
                    # cv2.imshow('img', img)
                    # cv2.waitKey()
                    if img is None:
                        print("error in loading temp image")
                        continue

                    startTime = time.time()
                    finalText = ''

                    # 对受理方式进行ocr识别 先裁切图片
                    img2 = self.cropImg2(img)
                    # img2 = cv2.resize(img2, (0, 0), fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
                    cv2.imshow('img2', img2)
                    cv2.waitKey()
                    dt_boxes, rec_res = text_sys(img2)
                    dt_num = len(dt_boxes)
                    for dno in range(dt_num):
                        text, score = rec_res[dno]
                        if score >= 0.5:
                            text_str = "%s, %.3f" % (text, score)
                            print(text_str)
                            if text.find("理号:") != -1:
                                print(1)

                elapse = time.time() - startTime
                print("Predict time of %s: %.3fs" % (pdf_file, elapse))

                # 对出现的字符串进行出现次数编号
                num = textNoDict.get(finalText, 0)
                if num == 0:
                    textNoDict[finalText] = 1
                else:
                    textNoDict[finalText] = (num + 1)
                print("finalText:", finalText, "append num:" + str(num))

                try:
                    print(1)
                except Exception as e:
                    print(e)

                if len(image_file_list) == n + 1:
                    self.tipCurtLabel.SetLabelText("处理完成")
                print("处理完成")
            except Exception as e:
                print(e)

        self.selectBtn.Enable()
        self.startBtn.Enable()
コード例 #5
0
    def OnStart2(self):
        print(self.path.GetLabelText())

        args = utility.parse_args()
        args.image_dir = self.path.GetLabelText()
        args.det_model_dir = "./config/ch_det_mv3_db/"
        args.rec_model_dir = "./config/ch_rec_mv3_crnn/"
        args.use_gpu = False
        print("初始化系统")
        image_file_list = self.get_image_file_list(self.path.GetLabelText())
        text_sys = ocr_sys.TextSystem(args)

        if not os.path.exists(self.path.GetLabelText()+"/ticket_no"):
            os.makedirs(self.path.GetLabelText()+"/ticket_no")
        print("完成初始化")
        imgFileDict = {}
        for image_file in image_file_list:
            imgFileDict[image_file] = image_file

        textNoDict = {}
        for n in range(len(image_file_list)):
            try:
                self.tipCurtLabel.SetLabelText("当前第" + str(n + 1) + "个")
                print(self.tipCurtLabel.GetLabelText())
                image_file = image_file_list[n]
                image_file = imgFileDict.get(image_file, "")
                if image_file.endswith(('jpg', 'png', 'jpeg', 'JPEG', 'JPG', 'bmp')) == False:
                    continue
                (filePath, filename) = os.path.split(image_file)
                (filePathAndName, ext) = os.path.splitext(image_file)

                img = self.cv_imread(image_file)
                # cv2.imshow('img', img1)
                # cv2.waitKey()
                if img is None:
                    print("error in loading image:{}".format(image_file))
                    continue
                img1 = self.cropImg(img)

                startTime = time.time()
                txt_list = pyzbar.decode(img1)
                if len(txt_list) == 0:
                    img1 = cv2.resize(img1, (0, 0), fx=4, fy=4, interpolation=cv2.INTER_LINEAR)
                    txt_list = pyzbar.decode(img1)

                if len(txt_list) == 0:
                    continue

                finalText = str(txt_list[0].data, encoding="utf-8")
                # print(finalText)
                if finalText == "":
                    continue

                textArr = finalText.split(',')
                if len(textArr) < 7:
                    continue
                finalText = '-' + textArr[3] + '-' + textArr[6].split(".")[0]

                file1 = Path(self.path.GetLabelText()+"/ticket_no/"+textArr[3]+".jpg")
                if not file1.exists():
                    cv2.imencode('.jpg', img)[1].tofile(self.path.GetLabelText()+"/ticket_no/"+textArr[3]+".jpg")

                # 对受理方式进行ocr识别 先裁切图片
                img2 = self.cropImg2(img)
                img2 = cv2.resize(img2, (0, 0), fx=4, fy=4, interpolation=cv2.INTER_LINEAR)
                # cv2.imshow('img2', img2)
                # cv2.waitKey()
                dt_boxes, rec_res = text_sys(img2)
                dt_num = len(dt_boxes)
                for dno in range(dt_num):
                    text, score = rec_res[dno]
                    if score >= 0.5:
                        text_str = "%s, %.3f" % (text, score)
                        # print(text_str)
                        if text.find("理号:") != -1:
                            index = text.find("理号:")
                            shouliNum = text[int(index + 3):]
                            finalText = shouliNum + finalText
                        if text.find("理号:") != -1:
                            index = text.find("理号:")
                            shouliNum = text[int(index + 3):]
                            finalText = shouliNum + finalText
                elapse = time.time() - startTime
                print("Predict time of %s: %.3fs" % (image_file, elapse))

                # 对出现的字符串进行出现次数编号
                num = textNoDict.get(finalText, 0)
                if num == 0:
                    textNoDict[finalText] = 1
                else:
                    textNoDict[finalText] = (num + 1)
                print("finalText:", finalText, "append num:" + str(num))

                try:
                    if num == 0:
                        finalFileName = filePath + "\\" + finalText + ext
                        if finalFileName == image_file:
                            continue
                        file1 = Path(finalFileName)
                        if file1.exists():
                            newName = filePath + "\\" + finalText + self.getRandom(4) + ext
                            imgFileDict[finalFileName] = newName
                            os.rename(finalFileName, newName)
                        else:
                            os.rename(image_file, filePath + "\\" + finalText + ext)
                    else:
                        finalFileName = filePath + "\\" + finalText + "(" + str(num) + ")" + ext
                        if finalFileName == image_file:
                            continue
                        file1 = Path(finalFileName)
                        if file1.exists():
                            newName = filePath + "\\" + finalText + self.getRandom(4) + ext
                            imgFileDict[finalFileName] = newName
                            os.rename(finalFileName, newName)
                        else:
                            os.rename(image_file, filePath + "\\" + finalText + "(" + str(num) + ")" + ext)
                except Exception as e:
                    print(e)

                if len(image_file_list) == n + 1:
                    self.tipCurtLabel.SetLabelText("处理完成")
                print("处理完成")
            except Exception as e:
                print(e)

        self.selectBtn.Enable()
        self.startBtn.Enable()
コード例 #6
0
ファイル: OcrTool.py プロジェクト: iuoon/RecognizeText
    def OnStart2(self):
        print(self.path.GetLabelText())
        #fileNames = os.listdir(self.path.GetLabelText())

        #if os.path.exists(self.path.GetLabelText()+"\\temp") == False:
        #    os.makedirs(self.path.GetLabelText()+"\\temp")

        args = utility.parse_args()
        args.image_dir = self.path.GetLabelText()
        args.det_model_dir = "./config/ch_det_mv3_db/"
        args.rec_model_dir = "./config/ch_rec_mv3_crnn/"
        args.use_gpu = False
        print("初始化系统")
        image_file_list = ocr_sys.get_image_file_list(args.image_dir)
        text_sys = ocr_sys.TextSystem(args)
        print("完成初始化")
        #for image_file in image_file_list:

        for n in range(len(image_file_list)):
            try:
                self.tipCurtLabel.SetLabelText("当前第" + str(n + 1) + "个")
                print(self.tipCurtLabel.GetLabelText())
                image_file = image_file_list[n]
                if image_file.endswith(
                    ('jpg', 'png', 'jpeg', 'JPEG', 'JPG', 'bmp')) == False:
                    continue
                (filePath, filename) = os.path.split(image_file)
                (filePathAndName, ext) = os.path.splitext(image_file)

                img = self.cv_imread(image_file)
                if img is None:
                    print("error in loading image:{}".format(image_file))
                    continue
                sp = img.shape
                height = sp[0]
                width = sp[1]

                if width > height:
                    img = cv2.flip(img, 0)
                    img = cv2.transpose(img)
                    sp = img.shape
                    height = sp[0]
                    width = sp[1]

                crop_img = img[int(height * 2 / 3):height,
                               int(width * 2 / 3):width]
                crop_img = cv2.resize(crop_img, ((int(width / 3)) * 2,
                                                 (int(height / 3)) * 2),
                                      cv2.INTER_LINEAR)
                # 逆时针旋转90度
                crop_img = cv2.flip(crop_img, 1)
                crop_img = cv2.transpose(crop_img)
                #cv2.imshow('img', crop_img)
                #cv2.waitKey()
                starttime = time.time()
                dt_boxes, rec_res = text_sys(crop_img)
                elapse = time.time() - starttime
                print("Predict time of %s: %.3fs" % (image_file, elapse))
                dt_num = len(dt_boxes)
                textList = []
                dict = {}
                for dno in range(dt_num):
                    text, score = rec_res[dno]
                    if score >= 0.5:
                        text_str = "%s, %.3f" % (text, score)
                        print(text_str)
                        if text.isdigit() and len(text) == 8:
                            textList.append(text)
                            dict[text] = score
                finalArr = Counter(textList).most_common(1)[0]
                finalNo = finalArr[0]
                finalCount = finalArr[1]
                print(finalNo)
                try:
                    if finalCount == 1:
                        dic1SortList = sorted(dict.items(),
                                              key=lambda x: x[1],
                                              reverse=True)
                        finalNo = dic1SortList[0][0]
                        os.rename(image_file, filePath + "/" + finalNo + ext)
                    else:
                        os.rename(image_file, filePath + "/" + finalNo + ext)
                except Exception as e:
                    print(e)
                    os.rename(
                        image_file,
                        filePath + "/" + finalNo + "(标识码" + str(n) + ")" + ext)

                # 清除临时目录
                # filelist = os.listdir(self.path.GetLabelText()+"\\temp\\")
                # for f in filelist:
                #    filepath = os.path.join(self.path.GetLabelText()+"\\temp\\", f)
                #    if os.path.isfile(filepath):
                #      os.remove(filepath)
                #      print(str(filepath)+" removed!")
                if len(image_file_list) == n + 1:
                    self.tipCurtLabel.SetLabelText("处理完成")
                print("处理完成")
            except Exception as e:
                print(e)
                os.rename(image_file, filePathAndName + "(不能识别)" + ext)
        self.selectBtn.Enable()
        self.startBtn.Enable()
コード例 #7
0
    def OnStart2(self):
        print(self.path.GetLabelText())
        # fileNames = os.listdir(self.path.GetLabelText())

        # if os.path.exists(self.path.GetLabelText()+"\\temp") == False:
        #    os.makedirs(self.path.GetLabelText()+"\\temp")

        args = utility.parse_args()
        args.image_dir = self.path.GetLabelText()
        args.det_model_dir = "./config/ch_det_mv3_db/"
        args.rec_model_dir = "./config/ch_rec_mv3_crnn/"
        args.use_gpu = False
        print("初始化系统")
        config = configparser.ConfigParser()
        config.read("./config/config.ini", encoding='UTF-8')
        roadMapList = config.items('RoadWord')
        image_file_list = ocr_sys.get_image_file_list(args.image_dir)
        text_sys = ocr_sys.TextSystem(args)
        print("完成初始化")
        imgFileDict = {}
        for image_file in image_file_list:
            imgFileDict[image_file] = image_file

        textNoDict = {}
        for n in range(len(image_file_list)):
            try:
                self.tipCurtLabel.SetLabelText("当前第" + str(n + 1) + "个")
                print(self.tipCurtLabel.GetLabelText())
                image_file = image_file_list[n]
                image_file = imgFileDict.get(image_file, "")
                if image_file.endswith(('jpg', 'png', 'jpeg', 'JPEG', 'JPG', 'bmp')) == False:
                    continue
                (filePath, filename) = os.path.split(image_file)
                (filePathAndName, ext) = os.path.splitext(image_file)

                img = self.cv_imread(image_file)
                if img is None:
                    print("error in loading image:{}".format(image_file))
                    continue
                sp = img.shape
                height = sp[0]
                width = sp[1]
                # cv2.imshow('img', img)
                # cv2.waitKey()
                starttime = time.time()
                dt_boxes, rec_res = text_sys(img)
                elapse = time.time() - starttime
                print("Predict time of %s: %.3fs" % (image_file, elapse))
                dt_num = len(dt_boxes)

                # finalTextDict = {}
                finalText = ""
                for dno in range(dt_num):
                    text, score = rec_res[dno]
                    if score >= 0.5:
                        text_str = "%s, %.3f" % (text, score)
                        # print(text_str)
                        tempText = self.removeNum(text)
                        tempText2 = self.removeNum(finalText)
                        # finalTextDict[tempText] = finalText
                        if len(tempText) > len(tempText2):
                            finalText = text
                if finalText == "":
                    continue
                for i in range(len(roadMapList)):
                    roadWord = roadMapList[i][0]
                    if finalText.find(roadWord) != -1:
                        finalText = finalText.replace(roadWord, roadMapList[i][1])

                # finalText = finalTextDict.get(finalText, "")

                # 对出现的字符串进行出现次数编号
                num = textNoDict.get(finalText, 0)
                if num == 0:
                    textNoDict[finalText] = 1
                else:
                    textNoDict[finalText] = (num + 1)
                print("finalText:", finalText, "append num:" + str(num))

                try:
                    if num == 0:
                        finalFileName = filePath + "\\" + finalText + ext
                        if finalFileName == image_file:
                            continue
                        file1 = Path(finalFileName)
                        if file1.exists():
                            newName = filePath + "\\" + finalText + self.getRandom(4) + ext
                            imgFileDict[finalFileName] = newName
                            os.rename(finalFileName, newName)
                        else:
                            os.rename(image_file, filePath + "\\" + finalText + ext)
                    else:
                        finalFileName = filePath + "\\" + finalText + str(num) + ext
                        if finalFileName == image_file:
                            continue
                        file1 = Path(finalFileName)
                        if file1.exists():
                            newName = filePath + "\\" + finalText + self.getRandom(4) + ext
                            imgFileDict[finalFileName] = newName
                            os.rename(finalFileName, newName)
                        else:
                            os.rename(image_file, filePath + "\\" + finalText + str(num) + ext)
                except Exception as e:
                    print(e)
                    # os.rename(image_file, filePath+"/"+finalNo+"(标识码"+str(n)+")"+ext)

                # 清除临时目录
                # filelist = os.listdir(self.path.GetLabelText()+"\\temp\\")
                # for f in filelist:
                #    filepath = os.path.join(self.path.GetLabelText()+"\\temp\\", f)
                #    if os.path.isfile(filepath):
                #      os.remove(filepath)
                #      print(str(filepath)+" removed!")
                if len(image_file_list) == n + 1:
                    self.tipCurtLabel.SetLabelText("处理完成")
                print("处理完成")
            except Exception as e:
                print(e)
                # os.rename(image_file, filePathAndName+"(不能识别)"+ext)
        self.selectBtn.Enable()
        self.startBtn.Enable()