Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -202,7 +202,7 @@ ocr_lang=[
|
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
-
def blur_im(img,bounds):
|
| 206 |
im = cv2.imread(img)
|
| 207 |
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
| 208 |
for bound in bounds:
|
|
@@ -216,7 +216,6 @@ def blur_im(img,bounds):
|
|
| 216 |
im[y:y+h, x:x+w] = cv2.GaussianBlur(im[y:y+h, x:x+w],(51,51),0)
|
| 217 |
|
| 218 |
|
| 219 |
-
text = "New Text"
|
| 220 |
|
| 221 |
im = Image.fromarray(im)
|
| 222 |
for bound in bounds:
|
|
@@ -228,11 +227,8 @@ def blur_im(img,bounds):
|
|
| 228 |
draw = ImageDraw.Draw(im)
|
| 229 |
font_size=int(int(w)*0.2)
|
| 230 |
font = ImageFont.truetype("./fonts/unifont-15.0.01.ttf", int(font_size))
|
|
|
|
| 231 |
draw.text((x, y),text, font = font, fill=(0,0,0))
|
| 232 |
-
#kernel = np.ones((1, 1), np.uint8)
|
| 233 |
-
#im[y:y+h, x:x+w] = cv2.dilate(im[y:y+h, x:x+w], kernel, iterations=1)
|
| 234 |
-
#im[y:y+h, x:x+w] = cv2.erode(im[y:y+h, x:x+w], kernel, iterations=1)
|
| 235 |
-
|
| 236 |
return im
|
| 237 |
|
| 238 |
def draw_boxes(image, bounds, color='blue', width=1):
|
|
@@ -242,7 +238,7 @@ def draw_boxes(image, bounds, color='blue', width=1):
|
|
| 242 |
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
|
| 243 |
return image
|
| 244 |
|
| 245 |
-
def detect(img, target_lang,target_lang2=None):
|
| 246 |
if target_lang2 != None and target_lang2 != "":
|
| 247 |
lang=f"{lang_id[target_lang]}"
|
| 248 |
lang2=f"{lang_id[target_lang2]}"
|
|
@@ -257,7 +253,7 @@ def detect(img, target_lang,target_lang2=None):
|
|
| 257 |
im_out=draw_boxes(im, bounds)
|
| 258 |
#im.save('result.jpg')
|
| 259 |
|
| 260 |
-
blr_out=blur_im(img,bounds)
|
| 261 |
return im_out,blr_out,pd.DataFrame(bounds),pd.DataFrame(bounds).iloc[:,1:]
|
| 262 |
|
| 263 |
|
|
@@ -269,8 +265,9 @@ with gr.Blocks() as robot:
|
|
| 269 |
with gr.Column():
|
| 270 |
im=gr.Image(type="filepath")
|
| 271 |
with gr.Column():
|
| 272 |
-
target_lang = gr.Dropdown(label="Detect language", choices=list(lang_id.keys()),value="English")
|
| 273 |
-
target_lang2 = gr.Dropdown(label="Detect language2", choices=list(lang_id.keys()),value="")
|
|
|
|
| 274 |
go_btn=gr.Button()
|
| 275 |
with gr.Row():
|
| 276 |
with gr.Column():
|
|
@@ -282,6 +279,6 @@ with gr.Blocks() as robot:
|
|
| 282 |
out_txt=gr.Textbox(lines=8)
|
| 283 |
data_f=gr.Dataframe()
|
| 284 |
|
| 285 |
-
|
| 286 |
-
go_btn.click(detect,[im,target_lang,target_lang2],[out_im,trans_im,out_txt,data_f])
|
| 287 |
robot.queue(concurrency_count=10).launch()
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
+
def blur_im(img,bounds,target_lang,trans_lang):
|
| 206 |
im = cv2.imread(img)
|
| 207 |
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
| 208 |
for bound in bounds:
|
|
|
|
| 216 |
im[y:y+h, x:x+w] = cv2.GaussianBlur(im[y:y+h, x:x+w],(51,51),0)
|
| 217 |
|
| 218 |
|
|
|
|
| 219 |
|
| 220 |
im = Image.fromarray(im)
|
| 221 |
for bound in bounds:
|
|
|
|
| 227 |
draw = ImageDraw.Draw(im)
|
| 228 |
font_size=int(int(w)*0.2)
|
| 229 |
font = ImageFont.truetype("./fonts/unifont-15.0.01.ttf", int(font_size))
|
| 230 |
+
text = this(bound[1],target_lang,trans_lang)
|
| 231 |
draw.text((x, y),text, font = font, fill=(0,0,0))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
return im
|
| 233 |
|
| 234 |
def draw_boxes(image, bounds, color='blue', width=1):
|
|
|
|
| 238 |
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
|
| 239 |
return image
|
| 240 |
|
| 241 |
+
def detect(img, target_lang,trans_lang,target_lang2=None):
|
| 242 |
if target_lang2 != None and target_lang2 != "":
|
| 243 |
lang=f"{lang_id[target_lang]}"
|
| 244 |
lang2=f"{lang_id[target_lang2]}"
|
|
|
|
| 253 |
im_out=draw_boxes(im, bounds)
|
| 254 |
#im.save('result.jpg')
|
| 255 |
|
| 256 |
+
blr_out=blur_im(img,bounds,target_lang,trans_lang)
|
| 257 |
return im_out,blr_out,pd.DataFrame(bounds),pd.DataFrame(bounds).iloc[:,1:]
|
| 258 |
|
| 259 |
|
|
|
|
| 265 |
with gr.Column():
|
| 266 |
im=gr.Image(type="filepath")
|
| 267 |
with gr.Column():
|
| 268 |
+
target_lang = gr.Dropdown(label="Detect language:", choices=list(lang_id.keys()),value="English")
|
| 269 |
+
#target_lang2 = gr.Dropdown(label="Detect language2", choices=list(lang_id.keys()),value="")
|
| 270 |
+
trans_lang = gr.Dropdown(label="Translate to:", choices=list(lang_id.keys()),value="Chinese")
|
| 271 |
go_btn=gr.Button()
|
| 272 |
with gr.Row():
|
| 273 |
with gr.Column():
|
|
|
|
| 279 |
out_txt=gr.Textbox(lines=8)
|
| 280 |
data_f=gr.Dataframe()
|
| 281 |
|
| 282 |
+
go_btn.click(detect,[im,target_lang,trans_lang],[out_im,trans_im,out_txt,data_f])
|
| 283 |
+
#go_btn.click(detect,[im,target_lang,target_lang2],[out_im,trans_im,out_txt,data_f])
|
| 284 |
robot.queue(concurrency_count=10).launch()
|