fasdfsa commited on
Commit
f9e9fd7
·
1 Parent(s): e1c7103

add flask api

Browse files
Files changed (2) hide show
  1. main.py +165 -2
  2. requirements.txt +1 -0
main.py CHANGED
@@ -1,4 +1,22 @@
1
- # see huggingface\BallonsTranslator\main.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import json, os, sys, time, io
3
  import os.path as osp
4
 
@@ -7,6 +25,48 @@ import PIL
7
  import cv2
8
  import numpy as np
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # see utils\io_utils.py
11
  def imread(imgpath, read_type=cv2.IMREAD_COLOR, max_retry_limit=5, retry_interval=0.1):
12
  if not osp.exists(imgpath):
@@ -43,7 +103,106 @@ def chunks(lst, n):
43
  for i in range(0, len(lst), n):
44
  yield lst[i:i + n]
45
 
46
- if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  from modules.textdetector.ctd.inference import TextDetector as CTDModel
49
  from modules.ocr.mit48px import Model48pxOCR
@@ -144,3 +303,7 @@ if __name__ == '__main__':
144
  pass
145
 
146
 
 
 
 
 
 
1
+ # see huggingface/BallonsTranslator/main.py
2
+ # see huggingface/project/flask_auto_selection.py
3
+
4
+
5
+
6
+ from modules.textdetector.ctd.inference import TextDetector as CTDModel
7
+ from modules.ocr.mit48px import Model48pxOCR
8
+
9
+ CTD_ONNX_PATH = 'data/models/comictextdetector.pt.onnx'
10
+ device = 'cpu'
11
+ detect_size = 1280
12
+
13
+ ctd_model = CTDModel(CTD_ONNX_PATH, detect_size=detect_size, device=device)
14
+
15
+
16
+ OCR48PXMODEL_PATH = 'data/models/ocr_ar_48px.ckpt'
17
+ ocr_model = Model48pxOCR(OCR48PXMODEL_PATH, device)
18
+
19
+
20
  import json, os, sys, time, io
21
  import os.path as osp
22
 
 
25
  import cv2
26
  import numpy as np
27
 
28
+ is_debug = True
29
+
30
+ dic_cache = {}
31
+
32
+ from flask import Flask, request, jsonify
33
+
34
+ app = Flask(__name__)
35
+
36
+ import base64
37
+ import math, re
38
+
39
+ def save_json(filename, dics):
40
+ with open(filename, 'w', encoding='utf-8') as fp:
41
+ json.dump(dics, fp, indent=4, ensure_ascii=False)
42
+ fp.close()
43
+
44
+ def load_json(filename):
45
+ with open(filename, encoding='utf-8') as fp:
46
+ js = json.load(fp)
47
+ fp.close()
48
+ return js
49
+
50
+ def jsonparse(s):
51
+ return json.loads(s, strict=False)
52
+
53
+ def jsonstring(d):
54
+ return json.dumps(d, ensure_ascii=False)
55
+
56
+ def show_img(image, target_width=400):
57
+ # 获取原始图片的宽度和高度
58
+ original_height, original_width = image.shape[:2]
59
+
60
+ # 计算缩放比例和目标高度
61
+ scale = target_width / original_width
62
+ target_height = int(original_height * scale)
63
+
64
+ # 等比例缩放图片
65
+ resized_image = cv2.resize(image, (target_width, target_height), interpolation=cv2.INTER_AREA)
66
+ cv2.imshow("green", resized_image)
67
+ cv2.waitKey(0)
68
+ return resized_image
69
+
70
  # see utils\io_utils.py
71
  def imread(imgpath, read_type=cv2.IMREAD_COLOR, max_retry_limit=5, retry_interval=0.1):
72
  if not osp.exists(imgpath):
 
103
  for i in range(0, len(lst), n):
104
  yield lst[i:i + n]
105
 
106
+ def ocr(img):
107
+
108
+ # All text detectors only support 3 channels input
109
+ if img.ndim == 3 and img.shape[2] == 4:
110
+ img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
111
+
112
+ _, mask, blk_list = ctd_model(img)
113
+
114
+ fnt_rsz = 1.0
115
+ fnt_max = -1
116
+ fnt_min = -1
117
+ for blk in blk_list:
118
+ sz = blk._detected_font_size * fnt_rsz
119
+ if fnt_max > 0:
120
+ sz = min(fnt_max, sz)
121
+ if fnt_min > 0:
122
+ sz = max(fnt_min, sz)
123
+ blk.font_size = sz
124
+ blk._detected_font_size = sz
125
+
126
+ ksize = 2
127
+ if ksize > 0:
128
+ element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * ksize + 1, 2 * ksize + 1),(ksize, ksize)) # 创建一个椭圆形的结构元素(kernel),用于后续的形态学操作 # 元素的尺寸 # (ksize, ksize) :椭圆的锚点(中心点)
129
+ mask = cv2.dilate(mask, element) # 对 mask 图像进行膨胀操作(dilate),使用上面创建的椭圆结构元素。膨胀操作可以让白色区域(通常是前景或目标区域)变大,常用于去除小的黑洞、连接断开的区域等。
130
+
131
+ for blk in blk_list:
132
+ blk.det_model = 'ctd'
133
+
134
+ need_save_mask = True
135
+ detect_counter = 0
136
+
137
+ detect_counter += 1
138
+
139
+ for blk in blk_list:
140
+ blk.text = []
141
+
142
+ split_textblk = False
143
+ seg_func = None
144
+
145
+ model_text_height = 48
146
+ model_maxwidth = 8100
147
+
148
+ from utils.textblock import collect_textblock_regions
149
+
150
+ chunk_size = 16
151
+
152
+ regions, textblk_lst_indices = collect_textblock_regions(img, blk_list, model_text_height, model_maxwidth, split_textblk, seg_func)
153
+
154
+
155
+ ocr_model(blk_list, regions, textblk_lst_indices, chunk_size=chunk_size)
156
+
157
+
158
+ img_draw = img.copy()
159
+
160
+
161
+ # ui\mainwindow.py
162
+ for blk in blk_list:
163
+ text = blk.get_text()
164
+ for line in blk.lines:
165
+ img_draw = cv2.rectangle(img_draw, line[0], line[3], (0, 0, 255), 2) # 在一行坚排文字的左边画一条红线
166
+
167
+
168
+ return blk_list, img_draw
169
+
170
+ @app.route('/comicocr', methods=['post'])
171
+ def comicocr():
172
+ global dic_cache
173
+
174
+ # request.json 只能够接受方法为POST、Body为raw,header 内容为 application/json类型的数据
175
+ print(request.json, type(request.json))
176
+
177
+
178
+ img_b64_str = request.json['img']
179
+ img_bytes = base64.b64decode(img_b64_str)
180
+
181
+ imgData = np.frombuffer(img_bytes, dtype=np.uint8)
182
+ img = cv2.imdecode(imgData, -1)
183
+
184
+ # All text detectors only support 3 channels input
185
+ if img.ndim == 3 and img.shape[2] == 4:
186
+ img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
187
+
188
+
189
+ cv2.imshow('test', img)
190
+ cv2.waitKey()
191
+
192
+ return jsonify({})
193
+
194
+
195
+ def main():
196
+ if is_debug:
197
+ img = imread('E:/huggingface/BallonsTranslator/assets/kcc-0010.jpg')
198
+ blk_list, img_draw = ocr(img)
199
+
200
+ cv2.imwrite("E:/xxxxxxxxxxxxxxxx.jpg", img_draw)
201
+
202
+ else:
203
+ app.run(host="0.0.0.0", port=2393, debug=True)
204
+
205
+ return
206
 
207
  from modules.textdetector.ctd.inference import TextDetector as CTDModel
208
  from modules.ocr.mit48px import Model48pxOCR
 
303
  pass
304
 
305
 
306
+ if __name__ == '__main__':
307
+ main()
308
+
309
+
requirements.txt CHANGED
@@ -24,4 +24,5 @@ httpcore==1.0.5
24
  openai==2.6.0
25
  openapi-schema-validator==0.6.2
26
  openapi-spec-validator==0.7.1
 
27
  # winsdk==1.0.0b10
 
24
  openai==2.6.0
25
  openapi-schema-validator==0.6.2
26
  openapi-spec-validator==0.7.1
27
+ flask==3.1.0
28
  # winsdk==1.0.0b10