323Ahmed commited on
Commit
80fd898
·
verified ·
1 Parent(s): 8260163

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import easyocr
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+
7
+ # Create an EasyOCR Reader
8
+ reader = easyocr.Reader(['en'])
9
+
10
+ def process_image(image):
11
+ # Convert the PIL image to a numpy array (compatible with OpenCV)
12
+ image_np = np.array(image)
13
+
14
+ # Convert the image to RGB (OpenCV loads as BGR, EasyOCR expects RGB)
15
+ image_rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
16
+
17
+ # Use EasyOCR to read text from the image
18
+ result = reader.readtext(image_rgb)
19
+
20
+ # Draw bounding boxes around detected text
21
+ for (bbox, text, prob) in result:
22
+ (top_left, top_right, bottom_right, bottom_left) = bbox
23
+ top_left = tuple(map(int, top_left))
24
+ bottom_right = tuple(map(int, bottom_right))
25
+ cv2.rectangle(image_np, top_left, bottom_right, (0, 255, 0), 2)
26
+
27
+ # Convert back to RGB for display
28
+ result_image = Image.fromarray(cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
29
+
30
+ # Combine detected text and their confidence scores
31
+ detected_text = "\n".join([f"Detected text: {text}, Confidence: {prob:.2f}" for (_, text, prob) in result])
32
+
33
+ return result_image, detected_text
34
+
35
+ # Gradio Interface
36
+ interface = gr.Interface(
37
+ fn=process_image,
38
+ inputs="image",
39
+ outputs=["image", "text"],
40
+ title="OCR with EasyOCR",
41
+ description="Upload an image, and the system will detect text using EasyOCR and display it."
42
+ )
43
+
44
+ # Launch the interface
45
+ interface.launch()