def recog(imgstr, ext='.png'): if not isinstance(imgstr, str) and len(imgstr) > 0: return {'ok': False, 'error': 'Invalid Image: not string or too small'} try: img = base64.b64decode(imgstr) except: return {'ok': False, 'error': 'Invalid Image: not valid encode'} path = '/tmp/' path += str(uuid.uuid4()) + ext with open(path, 'wb') as fp: fp.write(img) try: r = tr.run(path) r = [{ 'x': int(a[0]), 'y': int(a[1]), 'w': int(a[2]), 'h': int(a[3]), 'text': b, 'c': c } for a, b, c in r] try: os.remove(path) except: pass return {'ok': True, 'data': r} except: try: os.remove(path) except: pass traceback.print_exc() return {'ok': False, 'error': 'Parse Error'}
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 job_ocr(self, image_bytes): result = '' try: path = "./test.jpg" pyBytes = bytes(image_bytes.data()) with open(path, 'wb') as f: f.write(pyBytes) result_list = tr.run(path) if (len(result_list) != 0): for result_single in result_list: result += result_single[1] finally: self.signal_response.emit('') self.signal_response.emit(result) pyperclip.copy(result)
def job_ocr2(self, image_bytes): result = '' try: path = "./test.jpg" # pyBytes=bytes(image_bytes.data()) pyBytes = image_bytes with open(path, 'wb') as f: f.write(pyBytes) result_list = tr.run(path) result_list_final = get_title_from_reg_text(result_list) if (len(result_list) != 0): for result_single in result_list_final: result += result_single + "\n" finally: self.signal_response.emit('') self.signal_response.emit(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"))
def post(self): ''' :return: 报错: 400 没有请求参数 ''' start_time = time.time() MAX_SIZE = 1600 img_up = self.request.files.get('file', None) img_b64 = self.get_argument('img', None) compress_size = self.get_argument('compress', None) # 判断是上传的图片还是base64 self.set_header('content-type', 'application/json') up_image_type = None if img_up is not None and len(img_up) > 0: img_up = img_up[0] up_image_type = img_up.content_type up_image_name = img_up.filename img = Image.open(BytesIO(img_up.body)) elif img_b64 is not None: raw_image = base64.b64decode(img_b64.encode('utf8')) img = Image.open(BytesIO(raw_image)) else: self.set_status(400) logger.error(json.dumps({'code': 400, 'msg': '没有传入参数'}, cls=NpEncoder)) self.finish(json.dumps({'code': 400, 'msg': '没有传入参数'}, cls=NpEncoder)) return try: if hasattr(img, '_getexif') and img._getexif() is not None: orientation = 274 exif = dict(img._getexif().items()) if exif[orientation] == 3: img = img.rotate(180, expand=True) elif exif[orientation] == 6: img = img.rotate(270, expand=True) elif exif[orientation] == 8: img = img.rotate(90, expand=True) except Exception as ex: error_log = json.dumps({'code': 400, 'msg': '产生了一点错误,请检查日志', 'err': str(ex)}, cls=NpEncoder) logger.error(error_log, exc_info=True) self.finish(error_log) return img = img.convert("RGB") ''' 是否开启图片压缩 默认为1600px 值为 0 时表示不开启压缩 非 0 时则压缩到该值的大小 ''' if compress_size is not None: try: compress_size = int(compress_size) except ValueError as ex: logger.error(exc_info=True) self.finish(json.dumps({'code': 400, 'msg': 'compress参数类型有误,只能是int类型'}, cls=NpEncoder)) return if compress_size < 1: MAX_SIZE = max(img.height, img.width) else: MAX_SIZE = compress_size if img.height > MAX_SIZE or img.width > MAX_SIZE: scale = max(img.height / MAX_SIZE, img.width / MAX_SIZE) new_width = int(img.width / scale + 0.5) new_height = int(img.height / scale + 0.5) img = img.resize((new_width, new_height), Image.BICUBIC) res = tr.run(img) img_detected = img.copy() img_draw = ImageDraw.Draw(img_detected) colors = ['red', 'green', 'blue', "purple"] for i, r in enumerate(res): rect, txt, confidence = r x, y, w, h = rect for xy in [(x, y, x + w, y), (x + w, y, x + w, y + h), (x + w, y + h, x, y + h), (x, y + h, x, y)]: img_draw.line(xy=xy, fill=colors[i % len(colors)], width=2) output_buffer = BytesIO() img_detected.save(output_buffer, format='JPEG') byte_data = output_buffer.getvalue() img_detected_b64 = base64.b64encode(byte_data).decode('utf8') log_info = { 'ip': self.request.host, 'return': res, 'time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') } logger.info(json.dumps(log_info, cls=NpEncoder)) self.finish(json.dumps( {'code': 200, 'msg': '成功', 'data': {'img_detected': 'data:image/jpeg;base64,' + img_detected_b64, 'raw_out': res, 'speed_time': round(time.time() - start_time, 2)}}, cls=NpEncoder)) return
def run_task(i): x = tr.run(imgs[i % len(imgs)]) return i, len(x)
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() # In[ ]:
def test(): os.chdir(_BASEDIR) print("recognize", tr.recognize("imgs/line.png")) txt = tr.run("imgs/line.png")[0][1] print(txt)
def tr_run(path, return_dict): try: return_dict['ret'] = tr.run(path) except: return_dict['ret'] = None