def test(): os.chdir(_BASEDIR) print("recognize", tr.recognize("imgs/line.png")) img_path = "imgs/id_card.jpeg" # img_path = "imgs/name_card.jpg" img_pil = Image.open(img_path) print(img_pil.size) color_pil = img_pil.convert("RGB") gray_pil = img_pil.convert("L") img_draw = ImageDraw.Draw(color_pil) colors = ['red', 'green', 'blue', "purple"] t = time.time() n = 1 for _ in range(n): tr.detect(gray_pil, flag=tr.FLAG_RECT) print("time", (time.time() - t) / n) results = tr.run(gray_pil, flag=tr.FLAG_ROTATED_RECT) for i, rect in enumerate(results): cx, cy, w, h, a = tuple(rect[0]) print(i, "\t", rect[1], rect[2]) box = cv2.boxPoints(((cx, cy), (w, h), a)) box = np.int0(np.round(box)) for p1, p2 in [(0, 1), (1, 2), (2, 3), (3, 0)]: img_draw.line(xy=(box[p1][0], box[p1][1], box[p2][0], box[p2][1]), fill=colors[i % len(colors)], width=2) color_pil.show()
def test(): print("recognize", tr.recognize("imgs/line.png")) img_path = "imgs/id_card.jpeg" # img_path = "imgs/name_card.jpg" img_pil = Image.open(img_path) try: if hasattr(img_pil, '_getexif'): # from PIL import ExifTags # for orientation in ExifTags.TAGS.keys(): # if ExifTags.TAGS[orientation] == 'Orientation': # break orientation = 274 exif = dict(img_pil._getexif().items()) if exif[orientation] == 3: img_pil = img_pil.rotate(180, expand=True) elif exif[orientation] == 6: img_pil = img_pil.rotate(270, expand=True) elif exif[orientation] == 8: img_pil = img_pil.rotate(90, expand=True) except: pass MAX_SIZE = 1600 if img_pil.height > MAX_SIZE or img_pil.width > MAX_SIZE: scale = max(img_pil.height / MAX_SIZE, img_pil.width / MAX_SIZE) new_width = int(img_pil.width / scale + 0.5) new_height = int(img_pil.height / scale + 0.5) img_pil = img_pil.resize((new_width, new_height), Image.ANTIALIAS) color_pil = img_pil.convert("RGB") gray_pil = img_pil.convert("L") img_draw = ImageDraw.Draw(color_pil) colors = ['red', 'green', 'blue', "purple"] t = time.time() n = 1 for _ in range(n): tr.detect(gray_pil, flag=tr.FLAG_RECT) print("time", (time.time() - t) / n) results = tr.run(gray_pil, flag=tr.FLAG_ROTATED_RECT) for i, rect in enumerate(results): cx, cy, w, h, a = tuple(rect[0]) print(i, "\t", rect[1], rect[2]) box = cv2.boxPoints(((cx, cy), (w, h), a)) box = np.int0(np.round(box)) for p1, p2 in [(0, 1), (1, 2), (2, 3), (3, 0)]: img_draw.line(xy=(box[p1][0], box[p1][1], box[p2][0], box[p2][1]), fill=colors[i % len(colors)], width=2) color_pil.show()
def local_ocr2(): # 接收图片 二进制流 upload_file = request.files.get('file') # print(upload_file) if upload_file: upload_file = Image.open(BytesIO(upload_file.read())) upload_file = upload_file.convert("RGB") # print(tr.run(img.copy().convert("L"), flag=tr.FLAG_ROTATED_RECT)) result = tr.recognize(upload_file.copy().convert("L")) # print(result) return {"res": result} return 400
def plate_recognize(self): dirs = PlateRecognize().os_path() # print(dirs) plate_list = {} for filename in dirs: pth = self.path + os.sep + filename plate = tr.recognize(pth)[0].upper() last_num = re.findall(r"[0-9A-Z]{4}$", plate) if plate[:3].isalpha(): char = [] for i in last_num[0]: if i == "I": char.append("1") elif i == "O": char.append("0") elif i == "Z": char.append("2") elif i == "J": char.append("1") elif i == "L": char.append("2") else: char.append(i) #print(char) real_plate = "" for i in plate[:3]: real_plate += i for i in char: real_plate += i #real_plate = real_plate + char plate_list.update({filename: real_plate}) else: # x = plate[:2].isalpha() # print(x) old_plate = "" for i in plate: if i != "-": old_plate += i plate_list.update({filename: old_plate}) return plate_list
def do_process(frame): vehicle, LpImg, cor = get_plate(frame) if (len(LpImg)): #check if there is at least one license image plate_image = cv2.convertScaleAbs(LpImg[0], alpha=(170.0)) gray = cv2.cvtColor(plate_image, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (7, 7), 0) binary = cv2.threshold(gray, 195, 255, 4)[1] binary = binary[30:-30, :] binary = cv2.resize(binary, (int(binary.shape[1]), int(binary.shape[0] * 0.6))) # plate_image = plate_image[30:-30,:] # plate_image=cv2.resize(plate_image,(int(plate_image.shape[1]),int(plate_image.shape[0]*0.6))) res = tr.recognize(binary) return res
def image_to_text(image_path, index, actual_filename): """ :param image_path: pdf 切割后每个小图片的路径 :param index: 索引 :param serial_num: pdf 序列号,也叫车辆架构号 :return: """ image = Image.open(image_path) # index=0 表头信息 if index == 0: scope = constants.header_scope result = {} for k, v in scope.items(): # 序列号从文件名中取,不在图片中进行识别 if k == 'serialnum': continue img = cut_img(img=image, coordinate=v) # 表头高度有轻微变化,用run_angle检测识别出范围 tr_result = tr.run_angle(img) text = None for item in tr_result: if item[1] == 'r': continue else: text = correct_text(k, item[1]) result[k] = text log.info('index:{0}, text:{1}'.format(*(index, result))) redis.hash_set(actual_filename, index, json.dumps(result)) else: scope = constants.normal_scope result = {} for k, v in scope.items(): img = cut_img(img=image, coordinate=v) # 列名有很多重复的,判断是否和最新识别过的相同,避免在ORC的时候浪费时间 if k == 'column_name': np_column = np.array(img) global np_base_columns, text_base_columns # 判断和之前的列名是否一样,避免重复识别,提高速度 if (np_column.shape == np_base_columns.shape) and (not np.any( cv2.subtract(np_base_columns, np_column))): result[k] = text_base_columns continue else: result[k] = correct_text(k, tr.recognize(img)[0]) # 更新要对比的基础列名 text_base_columns = result[k] np_base_columns = np_column # 这种需要单独处理 如果用 tr.recognize 有些测试值空格识别不出来,会粘在一起 # 如果title1 里面有K,则显示用title1 elif k == 'test_value' and 'K' in result['title1']: text = '' tr_result = tr.run_angle(img) for i, item in enumerate(tr_result): # 第四列是-TOL,填充一个空置 if i == 3: text = text + ' ' + 'null' + ' ' + item[1] else: text = text + ' ' + item[1] # 末尾填充null,为了防止BONUS有些pdf里没有值,没法一一对应 text = text + ' ' + 'null' text = text.strip() # 如果加了 'null' 之后,column_name 和test_value 长度不同,说明不需要null占位,需要去除 if len(result['column_name'].split(' ')) != len( text.split(' ')): text = text.replace(' null', '') result[k] = text print(result) else: result[k] = correct_text(k, tr.recognize(img)[0]) log.info('index:{0}, text:{1}'.format(*(index, result))) redis.hash_set(actual_filename, index, json.dumps(result))
import tr # detect text lines, return list of (x, y, w, h) print(tr.detect("imgs/web.png")) # recognize text line, return (text, confidence) print(tr.recognize("imgs/line.png")) # detect and recognize, return list of ((x, y, w, h), text, confidence) print(tr.run("imgs/name_card.jpg"))
np.set_printoptions(suppress=True) # 取消科学计数法输出 # # Use TR package # In[ ]: # coding: utf-8 import tr import sys, cv2, time, os from PIL import Image, ImageDraw, ImageFont import numpy as np #print("recognize", tr.recognize("imgs/line.png")) print("recognize", tr.recognize("E:/work/code/check_web/data/icon/icon_chima4.png")) # img_path = "imgs/id_card.jpeg" img_path = "E:/work/code/ocr/新增-商品手册/784381.jpg" # img_path = "imgs/name_card.jpg" # img_path = "E:/work/code/check_web/data/icon/temp_tishi41.png" img_pil = Image.open(img_path) try: if hasattr(img_pil, '_getexif'): # from PIL import ExifTags # for orientation in ExifTags.TAGS.keys(): # if ExifTags.TAGS[orientation] == 'Orientation': # break orientation = 274 exif = dict(img_pil._getexif().items())
def test(): os.chdir(_BASEDIR) print("recognize", tr.recognize("imgs/line.png")) txt = tr.run("imgs/line.png")[0][1] print(txt)