diff --git a/LICENSE b/LICENSE.txt similarity index 100% rename from LICENSE rename to LICENSE.txt diff --git a/README.md b/README.md index 14e06b5..6f7bd2b 100644 --- a/README.md +++ b/README.md @@ -117,15 +117,15 @@ res = requests.post(url=url, data={'img': img_b64}) ![验证码识别](https://images.alisen39.com/20200501173211.png) ## 更新记录 +* 2022年01月16日 + 更新接口,增加不返回图片参数 + * 2020年08月17日 更新Dockerfile,docker镜像支持tr2.3 * 2020年07月30日 支持启动命令选择GPU/CPU -* 2020年07月26日 - 更新tr2.0版,支持GPU - [更多记录 >>>](https://github.com/alisen39/TrWebOCR/blob/master/updateHistory.md) diff --git a/backend/webInterface/tr_run.py b/backend/webInterface/tr_run.py index 30800b3..724bdcc 100644 --- a/backend/webInterface/tr_run.py +++ b/backend/webInterface/tr_run.py @@ -47,6 +47,7 @@ def post(self): img_up = self.request.files.get('file', None) img_b64 = self.get_argument('img', None) compress_size = self.get_argument('compress', None) + is_draw = self.get_argument("is_draw", None) # 判断是上传的图片还是base64 self.set_header('content-type', 'application/json') @@ -65,6 +66,7 @@ def post(self): self.finish(json.dumps({'code': 400, 'msg': '没有传入参数'}, cls=NpEncoder)) return + # 旋转图片 try: if hasattr(img, '_getexif') and img._getexif() is not None: orientation = 274 @@ -109,43 +111,46 @@ def post(self): new_height = int(img.height / scale + 0.5) img = img.resize((new_width, new_height), Image.ANTIALIAS) + # 进行ocr res = tr.run(img.copy().convert("L"), flag=tr.FLAG_ROTATED_RECT) - 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 - ''' - cx: 中心点x - xy: 中心点y - w: 宽度 - h: 高度 - a: 旋转角度 - ''' - cx, cy, w, h, a = rect - 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) - - output_buffer = BytesIO() - img_detected.save(output_buffer, format='JPEG') - byte_data = output_buffer.getvalue() - img_detected_b64 = base64.b64encode(byte_data).decode('utf8') - + response_data = {'code': 200, 'msg': '成功', + 'data': {'raw_out': res, + 'speed_time': round(time.time() - start_time, 2)}} + if is_draw != '0': + 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 + ''' + cx: 中心点x + xy: 中心点y + w: 宽度 + h: 高度 + a: 旋转角度 + ''' + cx, cy, w, h, a = rect + 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) + + output_buffer = BytesIO() + img_detected.save(output_buffer, format='JPEG') + byte_data = output_buffer.getvalue() + img_detected_b64 = base64.b64encode(byte_data).decode('utf8') + + response_data['data']['img_detected'] = 'data:image/jpeg;base64,' + img_detected_b64 log_info = { 'ip': self.request.host, - 'return': res, + 'return': response_data, '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)) + self.finish(json.dumps(response_data, + cls=NpEncoder)) return diff --git a/updateHistory.md b/updateHistory.md index 65d3431..1b30860 100644 --- a/updateHistory.md +++ b/updateHistory.md @@ -1,4 +1,7 @@ ## 更新记录 +* 2022年01月16日 + 更新接口,增加不返回图片参数 + * 2020年08月17日 更新Dockerfile,docker镜像支持tr2.3