TungDuong commited on
Commit
1aac82a
·
verified ·
1 Parent(s): eb20997
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import os
4
+ import json
5
+ import cv2
6
+ import sys
7
+ import torch
8
+ import torch.nn as nn
9
+ import torchvision
10
+
11
+ sys.path.append(os.getcwd())
12
+ from predict import *
13
+
14
+ def visualize_image(image, detections):
15
+ for bbox, detected_class, conf, text, _ in detections:
16
+ x1, y1, x2, y2 = bbox
17
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
18
+
19
+ image = cv2.rectangle(image, (x1, y1), (x2, y2), color=(255, 0, 0), thickness=2)
20
+ image = cv2.putText(image, f"{conf:.2f} {text}", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
21
+
22
+ return image
23
+
24
+ def pipeline(image):
25
+ image = np.array(image)
26
+
27
+ predictions = prediction(image)
28
+
29
+ # Filter low conf boxes
30
+ filter_predictions = []
31
+ for bbox, cls, conf, text, encoded_text in predictions:
32
+ if conf > 0.7:
33
+ filter_predictions.append([bbox, cls, conf, text, encoded_text])
34
+
35
+ image = visualize_image(image, filter_predictions)
36
+ return image
37
+
38
+ demo = gr.Interface(
39
+ fn=pipeline,
40
+ inputs=gr.Image(type="pil", label="Input Image"),
41
+ outputs="image",
42
+ title="Scene Text Recognization",
43
+ description="Recognize text in scene images"
44
+ )
45
+
46
+ demo.launch()