Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -180,10 +180,15 @@ ocr_id = {
|
|
| 180 |
}
|
| 181 |
|
| 182 |
|
| 183 |
-
def blur_im(img,bounds,target_lang,trans_lang,ocr_sens,font_fac):
|
| 184 |
im = cv2.imread(img)
|
| 185 |
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
for bound in bounds:
|
| 188 |
if bound[2]>=(ocr_sens):
|
| 189 |
p0, p1, p2, p3 = bound[0]
|
|
@@ -192,7 +197,12 @@ def blur_im(img,bounds,target_lang,trans_lang,ocr_sens,font_fac):
|
|
| 192 |
w = int(p2[0]) - int(x)
|
| 193 |
h = int(p2[1]) - int(y)
|
| 194 |
kernel = np.ones((3, 3), np.uint8)
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
im[y:y+h, x:x+w] = cv2.GaussianBlur(im[y:y+h, x:x+w],(51,51),0)
|
| 197 |
else:
|
| 198 |
pass
|
|
@@ -208,7 +218,7 @@ def blur_im(img,bounds,target_lang,trans_lang,ocr_sens,font_fac):
|
|
| 208 |
text = this(bound[1],target_lang,trans_lang)
|
| 209 |
font_size=int(int(h)*font_fac)
|
| 210 |
font = ImageFont.truetype("./fonts/unifont-15.0.01.ttf", int(font_size))
|
| 211 |
-
draw.text((x, y),text, font = font, fill=
|
| 212 |
else:
|
| 213 |
pass
|
| 214 |
return im
|
|
@@ -274,7 +284,9 @@ with gr.Blocks() as robot:
|
|
| 274 |
with gr.Column():
|
| 275 |
ocr_sens=gr.Slider(0.1, 1, step=0.05,value=0.25,label="Detect Min Confidence")
|
| 276 |
font_fac=gr.Slider(0.1, 1, step =0.1,value=0.4,label="Font Scale")
|
| 277 |
-
ocr_space=gr.Slider(1,10, step=1,value=5,label="Future Function")
|
|
|
|
|
|
|
| 278 |
go_btn=gr.Button("Go")
|
| 279 |
with gr.Row():
|
| 280 |
with gr.Column():
|
|
@@ -286,6 +298,6 @@ with gr.Blocks() as robot:
|
|
| 286 |
out_txt=gr.Textbox(lines=8)
|
| 287 |
data_f=gr.Dataframe()
|
| 288 |
|
| 289 |
-
go_btn.click(detect,[im,target_lang,trans_lang,ocr_sens,font_fac],[out_im,trans_im,out_txt,data_f])
|
| 290 |
#go_btn.click(detect,[im,target_lang,target_lang2],[out_im,trans_im,out_txt,data_f])
|
| 291 |
robot.queue(concurrency_count=10).launch()
|
|
|
|
| 180 |
}
|
| 181 |
|
| 182 |
|
| 183 |
+
def blur_im(img,bounds,target_lang,trans_lang,ocr_sens,font_fac,t_color):
|
| 184 |
im = cv2.imread(img)
|
| 185 |
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
| 186 |
+
if t_color == "Black":
|
| 187 |
+
t_fill = (0,0,0)
|
| 188 |
+
pass
|
| 189 |
+
elif t_color == "White":
|
| 190 |
+
t_fill = (255,255,255)
|
| 191 |
+
pass
|
| 192 |
for bound in bounds:
|
| 193 |
if bound[2]>=(ocr_sens):
|
| 194 |
p0, p1, p2, p3 = bound[0]
|
|
|
|
| 197 |
w = int(p2[0]) - int(x)
|
| 198 |
h = int(p2[1]) - int(y)
|
| 199 |
kernel = np.ones((3, 3), np.uint8)
|
| 200 |
+
if t_color=="Black":
|
| 201 |
+
im[y:y+h, x:x+w] = cv2.dilate(im[y:y+h, x:x+w], kernel, iterations=3)
|
| 202 |
+
pass
|
| 203 |
+
elif t_color=="White":
|
| 204 |
+
im[y:y+h, x:x+w] = cv2.erode(im[y:y+h, x:x+w], kernel, iterations=3)
|
| 205 |
+
pass
|
| 206 |
im[y:y+h, x:x+w] = cv2.GaussianBlur(im[y:y+h, x:x+w],(51,51),0)
|
| 207 |
else:
|
| 208 |
pass
|
|
|
|
| 218 |
text = this(bound[1],target_lang,trans_lang)
|
| 219 |
font_size=int(int(h)*font_fac)
|
| 220 |
font = ImageFont.truetype("./fonts/unifont-15.0.01.ttf", int(font_size))
|
| 221 |
+
draw.text((x, y),text, font = font, fill=t_fill)
|
| 222 |
else:
|
| 223 |
pass
|
| 224 |
return im
|
|
|
|
| 284 |
with gr.Column():
|
| 285 |
ocr_sens=gr.Slider(0.1, 1, step=0.05,value=0.25,label="Detect Min Confidence")
|
| 286 |
font_fac=gr.Slider(0.1, 1, step =0.1,value=0.4,label="Font Scale")
|
| 287 |
+
ocr_space=gr.Slider(1,10, step=1,value=5,label="Future Function")
|
| 288 |
+
text_color=gr.Radio(["Black", "White"])
|
| 289 |
+
|
| 290 |
go_btn=gr.Button("Go")
|
| 291 |
with gr.Row():
|
| 292 |
with gr.Column():
|
|
|
|
| 298 |
out_txt=gr.Textbox(lines=8)
|
| 299 |
data_f=gr.Dataframe()
|
| 300 |
|
| 301 |
+
go_btn.click(detect,[im,target_lang,trans_lang,ocr_sens,font_fac,text_color],[out_im,trans_im,out_txt,data_f])
|
| 302 |
#go_btn.click(detect,[im,target_lang,target_lang2],[out_im,trans_im,out_txt,data_f])
|
| 303 |
robot.queue(concurrency_count=10).launch()
|