venom11/cardetection13232
0
1import gradio as gr
2import easyocr
3import cv2
4import numpy as np
5from PIL import Image
6
7# Create an EasyOCR Reader
8reader = easyocr.Reader(['en'])
9
10def 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
36interface = 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 wi ll detect text using EasyOCR and display it."
42)
43
44# Launch the interface
45interface.launch()
46 