Skip to content

Commit

Permalink
DEV 增加不返回图片功能
Browse files Browse the repository at this point in the history
  • Loading branch information
alisen39 committed Jan 16, 2022
1 parent e04e788 commit 25a1689
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
69 changes: 37 additions & 32 deletions backend/webInterface/tr_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions updateHistory.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 更新记录
* 2022年01月16日
更新接口,增加不返回图片参数

* 2020年08月17日
更新Dockerfile,docker镜像支持tr2.3

Expand Down

0 comments on commit 25a1689

Please sign in to comment.