rishabh5752 commited on
Commit
2c4fd20
1 Parent(s): 61647e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import easyocr
4
+ import numpy as np
5
+
6
+ # Initialize the EasyOCR reader
7
+ reader = easyocr.Reader(['en'], gpu=False)
8
+
9
+ # Function to perform text detection and return the image with bounding boxes
10
+ def detect_text(image):
11
+ # Convert the image to OpenCV format
12
+ image_cv = cv2.imdecode(np.frombuffer(image.read(), np.uint8), cv2.IMREAD_COLOR)
13
+
14
+ # Detect text on the image
15
+ text_results = reader.readtext(image_cv)
16
+
17
+ # Draw bounding boxes and text on the image
18
+ for result in text_results:
19
+ bbox, text, score = result
20
+ if score > 0.25:
21
+ cv2.rectangle(image_cv, bbox[0], bbox[2], (0, 255, 0), 5)
22
+ cv2.putText(image_cv, text, bbox[0], cv2.FONT_HERSHEY_COMPLEX, 0.65, (255, 0, 0), 2)
23
+
24
+ # Convert the processed image back to a format that Gradio can display
25
+ image_with_boxes = cv2.imencode('.jpg', image_cv)[1].tobytes()
26
+
27
+ return image_with_boxes
28
+
29
+ # Create a Gradio interface
30
+ iface = gr.Interface(
31
+ fn=detect_text,
32
+ inputs=gr.inputs.Image(type="file", label="Upload an image"),
33
+ outputs=gr.outputs.Image(type="pil"),
34
+ title="EasyOCR Text Detection",
35
+ description="Upload an image and detect text with EasyOCR",
36
+ live=True,
37
+ )
38
+
39
+ # Launch the Gradio app
40
+ iface.launch()