abhicodes commited on
Commit
d053f30
1 Parent(s): b74d405

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import easyocr
3
+ import gradio as gr
4
+ import base64
5
+ import json
6
+
7
+ def text_extraction(image):
8
+ # Convert base64 image to OpenCV format
9
+ image = base64.b64decode(image.split(",")[1])
10
+ nparr = np.frombuffer(image, np.uint8)
11
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
12
+
13
+ # Instance text detector
14
+ reader = easyocr.Reader(['en'], gpu=False)
15
+
16
+ # Detect text on image
17
+ text_ = reader.readtext(img)
18
+
19
+ threshold = 0.25
20
+ # Draw bbox and text
21
+ for t_, t in enumerate(text_):
22
+ bbox, text, score = t
23
+
24
+ if score > threshold:
25
+ cv2.rectangle(img, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (255, 0, 0), 2)
26
+
27
+ # Encode image to base64
28
+ retval, buffer = cv2.imencode('.jpg', img)
29
+ img_base64 = base64.b64encode(buffer).decode('utf-8')
30
+
31
+ # Create JSON response
32
+ response_json = {
33
+ 'annotated_image_base64': img_base64,
34
+ 'text_data': text_
35
+ }
36
+
37
+ # Convert the dictionary to a JSON string
38
+ response_json_str = json.dumps(response_json, default=str)
39
+
40
+ return response_json_str
41
+
42
+ # Define Gradio interface
43
+ iface = gr.Interface(
44
+ fn=text_extraction,
45
+ inputs=gr.Image(),
46
+ outputs=["image", "json"]
47
+ )
48
+
49
+ # Launch the Gradio interface
50
+ iface.launch()