def __ocr_check(self, file_path, save_path, option=None, change_image=True): """ 选择百度ocr识别,还是tesseract识别;以及图片二值化是否开启, :param file_path: ocr识别图片路径 :param save_path: ocr结果保存路径,建议使用txt跟tesseract同一 :param option: 对tesseract的命令传递 :param change_image:是否进行二值化,默认使用二值化 :return: """ global enable_api if change_image: binarization_image(file_path) if enable_api: try: ocr(file_path, save_path + ".txt") except ConnectionError: self.shell_color.failure_text("[!] 百度API无法连接") enable_api = False self.shell_color.helper_text("继续使用tesseract") if option is not None: option = " " + option os.popen( "tesseract {} {}{}".format(file_path, save_path, option) + self.__rebase_to_null ) self.__wait(3) else: if option is not None: option = " " + option os.popen( "tesseract {} {}{}".format(file_path, save_path, option) + self.__rebase_to_null ) self.__wait(3)
def __ocr_check(self, file_path, # 输入文件路径 save_path, # 输出文件路径 option=None, # 附加选项 change_image=True): # 是否预处理图片 self.shell_log.debug_text("base.__ocr_check") global enable_baidu_api if change_image: if enable_baidu_api: if enable_help_baidu: binarization_image(filepath=file_path, save_backup=True) else: self.shell_log.info_text("不对百度ocr进行图像处理") else: binarization_image(filepath=file_path, save_backup=True) if enable_baidu_api: try: ocr(file_path, save_path + ".txt") except ConnectionError: self.shell_log.failure_text("[!] 百度API无法连接") enable_baidu_api = False self.shell_log.helper_text("继续使用tesseract") if not enable_baidu_api: self.shell_log.debug_text("使用 tesseract OCR") if option is not None: option = " " + option os.popen( 'tesseract "{}" "{}" {}'.format(file_path, save_path, option) + self.__rebase_to_null ) self.__wait(3) else: self.shell_log.debug_text("使用 baidu api")
def __ocr_check(self, file_path, save_path, option=None, change_image=True): # 对图形进行二值化操作 if change_image: binarization_image(file_path) if config.Enable_api: try: ocr(file_path, save_path + ".txt") # Baidu_OCR.ocr(file_path, save_path+".txt") except ConnectionError: self.shell_color.failure_text("[!] 百度API无法连接") config.Enable_api = False self.shell_color.helper_text("继续使用tesseract") if option is not None: option = " " + option os.popen( "tesseract {} {}{}".format(file_path, save_path, option) + self.__rebase_to_null) self.__wait(3) else: if option is not None: option = " " + option os.popen("tesseract {} {}{}".format(file_path, save_path, option) + self.__rebase_to_null) self.__wait(3)
def get_loacation(filepath, option="makebox", change_image=False): if change_image: # image = Image.open(filepath) # 对图片进行阈值过滤,然后保存 # image = image.point(lambda x: 0 if x < 143 else 255) # image.save(filepath) binarization_image(filepath) os.popen('tesseract "{}" "{}" {}'.format(filepath, filepath, option))