def test_multiple_instances(model_name): global INSTANCE_ID print('test multiple instances for model_name: %s' % model_name) img_fp = os.path.join(example_dir, 'beauty2.jpg') INSTANCE_ID += 1 print('instance id: %d' % INSTANCE_ID) std1 = CnStd(model_name, name='instance-%d' % INSTANCE_ID) box_info_list = std1.detect(img_fp) INSTANCE_ID += 1 std2 = CnStd(model_name, name='instance-%d' % INSTANCE_ID) box_info_list2 = std2.detect(img_fp) assert len(box_info_list) == len(box_info_list2)
def __init__(self, path: str, show_img: bool = False): self.__ext = {'jpg', 'jpeg'} self.__ocr = CnOcr(model_name='densenet-lite-gru', cand_alphabet=string.digits, name=path) self.__std = CnStd(name=path) self.__info_dict = {} self.__dup_name_dict = {} # 先对路径进行替换 path = self.__format_path(path) # 根据传入的路径判断操作 if os.path.isdir(path) or os.path.isfile(path): files = [self.__format_path(os.path.join(path, f)) for f in os.listdir(path) if (os.path.isfile(os.path.join(path, f)) and self.__is_image(f))] \ if os.path.isdir(path) \ else [path] for file in tqdm(files): self.__handle_info( file, self.__ocr_number( self.__std_number(self.__cutter(file, show_img)))) else: print(f'获取数据错误,“{path}”既不是文件也不是文件夹')
def start_engine(model_path='models'): if OCR_MODEL_NAME + '.zip' in os.listdir( model_path) and STD_MODEL_NAME + '.zip' in os.listdir(model_path): print('初始化模型中...') ocr_root = os.path.join(data_dir(mode='cnocr'), ocr_version) ocr_root = os.path.join(ocr_root, OCR_MODEL_NAME) os.makedirs(ocr_root, exist_ok=True) ocr_zip_file_path = os.path.join(ocr_root, OCR_MODEL_NAME + '.zip') shutil.copyfile(os.path.join('models', OCR_MODEL_NAME + '.zip'), ocr_zip_file_path) with zipfile.ZipFile(ocr_zip_file_path) as zf: zf.extractall(os.path.dirname(ocr_root)) os.remove(ocr_zip_file_path) std_root = os.path.join(data_dir(mode='cnstd'), std_version) std_root = os.path.join(std_root, STD_MODEL_NAME) os.makedirs(std_root, exist_ok=True) std_zip_file_path = os.path.join(std_root, STD_MODEL_NAME + '.zip') shutil.copyfile(os.path.join('models', STD_MODEL_NAME + '.zip'), std_zip_file_path) with zipfile.ZipFile(std_zip_file_path) as zf: zf.extractall(os.path.dirname(std_root)) os.remove(std_zip_file_path) ocr_core = CnOcr() std_core = CnStd() print('初始化完成') return ocr_core, std_core else: print('初始化失败,未找到预加载模型') return None
def get_list(self): std=CnStd() ocr=CnOcr() ocr._model_name='conv-lite-fc' print(ocr._model_name) ocr_res2=ocr.ocr(img_fp=self.filepath) box_info_list=std.detect(self.filepath,pse_threshold=0.7,pse_min_area=150,context='gpu',height_border=0.10) image=Image.open(self.filepath) fontstyle=ImageFont.truetype('./simhei.ttf',13,encoding='utf-8') draw=ImageDraw.Draw(image) for box_info in box_info_list: print('a') print('a') print(box_info) info_box=box_info['box'] crp_img=box_info['cropped_img'] ocr_res1=ocr.ocr_for_single_line(crp_img) print('result: %s' % ''.join(str(ocr_res1))) x1,y1=info_box[0,0],info_box[0,1] x2,y2=info_box[1,0],info_box[1,1] x3,y3=info_box[2,0],info_box[2,1] x4,y4=info_box[3,0],info_box[3,1] draw.polygon([(x1,y1),(x4,y4),(x3,y3),(x2,y2)],outline=(255,0,0)) draw.text((x4,y4),str(ocr_res1),(200,0,0),font=fontstyle) image.show() print(ocr_res2) return box_info_list
def test_resnet50_v1b(): model_name = 'resnet50_v1b' img_fp = os.path.join(example_dir, 'beauty2.jpg') std = CnStd(model_name) box_info_list = std.detect(img_fp) img = mx.image.imread(img_fp, 1) box_info_list2 = std.detect(img) assert len(box_info_list) == len(box_info_list2)
def identify(image): std = CnStd(model_name='resnet50_v1b', root='C:/Users/HP/AppData/Roaming/Python/Python36/site-packages/cnstd') global cn_ocr box_info_list = std.detect(image) ocr_res=[] for box_info in box_info_list: cropped_img = box_info['cropped_img'] ocr_res.extend(cn_ocr.ocr_for_single_line(cropped_img)) print('ocr result1: %s' % ''.join(ocr_res)) return ocr_res
def identify(image): std = CnStd(model_name='resnet50_v1b', root='C:/Users/dell/AppData/Roaming/cnstd/0.1.0/mobilenetv3') global cn_ocr ocr_res = [] box_info_list = std.detect(image) ocr_res = [] for box_info in box_info_list: cropped_img = box_info['cropped_img'] ocr_res.extend(cn_ocr.ocr_for_single_line(cropped_img)) print('ocr result1: %s' % ''.join(ocr_res)) return ocr_res
def __init__(self): super(picture, self).__init__() self.resize(350, 350) self.setWindowTitle("图片转文字") self.label = QLabel(self) # self.label.setText("显示图片") self.label.setScaledContents(True) self.label.setFixedSize(300, 200) self.label.move(25, 60) self.label.setStyleSheet( "QLabel{background:white;}" "QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}" ) btn = QPushButton(self) btn.setText("打开图片") btn.move(135, 20) btn.clicked.connect(self.openimage) self.label_text = QLabel(self) self.label_text.setFixedSize(300, 30) self.label_text.move(25, 270) self.label_text.setTextInteractionFlags( Qt.TextSelectableByMouse) ###可复制 self.label_wait = QLabel(self) self.label_wait.setFixedSize(300, 30) self.label_wait.move(25, 300) # 标签1的背景填充更改为True,否则无法显示背景 self.label_wait.setAutoFillBackground(True) # # 实例化背景对象,进行相关背景颜色属性设置 # palette = QPalette() # palette.setColor(QPalette.Window, Qt.green) # # 标签1加载背景 # self.label_wait.setPalette(palette) # 设置文本居中显示 self.label_wait.setAlignment(Qt.AlignCenter) self.label_wait.setText('tips:识别过程可能会卡住,需几秒到几十秒不等') self.std = CnStd() self.cn_ocr = CnOcr()
def extract_content(save_dir_path): std = CnStd() cn_ocr = CnOcr() base_path = os.path.abspath(os.path.dirname(__file__)) pic_base_dir = os.path.join(base_path, "received") pic_path_list = (glob.glob(os.path.join(pic_base_dir, "*.png")) + glob.glob(os.path.join(pic_base_dir, "*.jpg")) + glob.glob(os.path.join(pic_base_dir, "*.jpeg"))) workbook = xlwt.Workbook() for index, pic_path in enumerate(pic_path_list): sheet = workbook.add_sheet('sheet{}'.format(index), cell_overwrite_ok=True) box_info_list = std.detect(pic_path) for box_info in box_info_list: x_list, y_list = [], [] for (x, y) in box_info['box']: x_list.append(x) y_list.append(y) top, bottom, left, right = min(y_list), max(y_list), min( x_list), max(x_list) top_row, bottom_row, left_column, right_column = int( top // 80), int(bottom // 80), int(left // 60), int(right // 60) cropped_img = box_info['cropped_img'] # 检测出的文本框 ocr_res = ''.join(cn_ocr.ocr_for_single_line(cropped_img)) try: logger.info( "top_row:{}, bottom_row:{}, left_column:{}, right_column:{}, ocr_res:{}", top_row, bottom_row, left_column, right_column, ocr_res, feature="f-strings") sheet.write_merge(top_row, bottom_row, left_column, right_column, ocr_res) except Exception as e: print(e) xls_base_dir = os.path.join(base_path, save_dir_path) xls_path = os.path.join(xls_base_dir, "res.xls") workbook.save(xls_path)
def ocr(): if 'file' not in request.files: return jsonify(code=-1, message='no file error'), 400 file = request.files['file'] _uuid = str(uuid.uuid1()) file_name = '/tmp/ocr/' + _uuid file.save(file_name) ocr = CnOcr(name=_uuid) std = CnStd(name=_uuid) box_info_list = std.detect(file_name) lines = [] for box_info in box_info_list: cropped_img = box_info['cropped_img'] # 检测出的文本框 ocr_res = ocr.ocr_for_single_line(cropped_img) lines.append(''.join(ocr_res)) return jsonify(code=0, message='ok', data=lines)
def target_function(idx, ls, prefix): # 使得每一张卡都能被充分用到 # 提交的时候由于只有一张卡,需要全部都改成0 os.environ['CUDA_VISIBLE_DEVICES'] = '0' std = CnStd(context='gpu') cn_ocr = CnOcr(context='gpu') result = dict() for file_name in tqdm(ls): file_path = os.path.join(prefix, file_name) box_info_list = std.detect(file_path) output = '' for box_info in box_info_list: cropped_img = box_info['cropped_img'] # 检测出的文本框 if type(cropped_img) != type(None): ocr_res = cn_ocr.ocr_for_single_line(cropped_img) output += ''.join(ocr_res) # print('ocr result: %s' % ''.join(ocr_res)) output = output.replace(' ', '') output = re.sub("[^\u4e00-\u9fa5]", "", output) result[file_name] = output with open('./output_%d.json' % (idx), 'w', encoding='utf-8') as w: w.write(json.dumps(result, ensure_ascii=False, indent=2))
import numpy as np from cnocr import CnOcr import cv2 from cnstd import CnStd from identify import identify global std, cn_ocr kernel = np.ones((10, 10), np.uint8) std = CnStd( model_name='resnet50_v1b', root='C:/Users/HP/AppData/Roaming/Python/Python36/site-packages/cnstd') cn_ocr = CnOcr(model_name="conv-lite-fc") #颜色提取方法 def color_identify(name): #颜色范围 lower = np.array([70, 110, 110]) upper = np.array([130, 255, 255]) raw = cv2.imread(name) original = cv2.imread(name) #中值滤波 hsv_one = cv2.medianBlur(raw, 5) hsv = cv2.cvtColor(hsv_one, cv2.COLOR_BGR2HSV) #颜色提取 mask_blue = cv2.inRange(hsv, lower, upper) res = cv2.bitwise_and(raw, raw, mask=mask_blue)
import cv2 import numpy as np import pytesseract from cnocr import CnOcr import cv2 from cnstd import CnStd #rawImage = cv2.imread("3.jpg") kernel = np.ones((10, 10), np.uint8) global std global cn_ocr std = CnStd(model_name='resnet50_v1b', root='C:/Users/dell/AppData/Roaming/cnstd/0.1.0/mobilenetv3') cn_ocr = CnOcr(model_name="conv-lite-fc") #颜色提取方法 def color_identify(name): rawImage = cv2.imread(name) original = cv2.imread(name) img = cv2.imread(name) # 蓝色的范围 lower_blue = np.array([70, 110, 110]) upper_blue = np.array([130, 255, 255]) #中值滤波 hsv = cv2.medianBlur(rawImage, 5) #转为hsv hsv = cv2.cvtColor(hsv, cv2.COLOR_BGR2HSV)
from cnocr import CnOcr import cnocr from cnstd import CnStd std=CnStd() ocr=CnOcr() box_info_list=std.detect('E:\\Work Place\\pocr\\pic\\2.png') res=ocr.ocr('E:\\Work Place\\pocr\\pic\\1.png') for box_info in box_info_list: crp_img=box_info['cropped_img'] ocr_res=ocr.ocr_for_single_line(crp_img) print('result: %s' % ''.join(ocr_res))
import string import random from cnstd import CnStd from cnocr import CnOcr from divide import divide __std = CnStd(model_name='resnet50_v1b', context='cpu') __ocr = CnOcr(model_name='densenet-lite-gru', context='cpu') def OCRProcess(image_name=None): assert isinstance(image_name, str), '请输入有效的图片路径' box_info_list = __std.detect(image_name) for box_info in box_info_list: cropped_img = box_info['cropped_img'] ocr_res = __ocr.ocr_for_single_line(cropped_img) print(ocr_res) # print('ocr result: %s' % ''.join(ocr_res)) pass OCRProcess('000000001960.jpg') OCRProcess('000000002000.jpg') OCRProcess('000000002040.jpg') OCRProcess('000000002080.jpg') OCRProcess('000000002120.jpg')