Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,9 @@ except Exception as e:
|
|
| 33 |
raise
|
| 34 |
|
| 35 |
def detect_and_read_plate(image):
|
|
|
|
|
|
|
|
|
|
| 36 |
results = model(image)
|
| 37 |
for result in results:
|
| 38 |
boxes = result.boxes.xyxy.cpu().numpy() # [x1, y1, x2, y2]
|
|
@@ -46,18 +49,27 @@ def detect_and_read_plate(image):
|
|
| 46 |
ocr_result = reader.readtext(plate_img)
|
| 47 |
if ocr_result:
|
| 48 |
text = " ".join([res[1] for res in ocr_result])
|
|
|
|
| 49 |
print(f"Detected Plate: {text}")
|
| 50 |
-
# Draw bounding box
|
| 51 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
demo = gr.Interface(
|
| 57 |
fn=detect_and_read_plate,
|
| 58 |
-
inputs="image",
|
| 59 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 60 |
title="Automatic Number Plate Recognition (ANPR)",
|
| 61 |
-
description="Upload an image of a car to detect and read its license plate."
|
| 62 |
)
|
| 63 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 33 |
raise
|
| 34 |
|
| 35 |
def detect_and_read_plate(image):
|
| 36 |
+
# Initialize an empty list to store detected plate texts
|
| 37 |
+
detected_texts = []
|
| 38 |
+
|
| 39 |
results = model(image)
|
| 40 |
for result in results:
|
| 41 |
boxes = result.boxes.xyxy.cpu().numpy() # [x1, y1, x2, y2]
|
|
|
|
| 49 |
ocr_result = reader.readtext(plate_img)
|
| 50 |
if ocr_result:
|
| 51 |
text = " ".join([res[1] for res in ocr_result])
|
| 52 |
+
detected_texts.append(text)
|
| 53 |
print(f"Detected Plate: {text}")
|
| 54 |
+
# Draw bounding box (optional, kept for visualization)
|
| 55 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 56 |
+
# Optionally remove text on image
|
| 57 |
+
# cv2.putText(image, text, (x1, y1 - 10),
|
| 58 |
+
# cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
| 59 |
+
|
| 60 |
+
# Join detected texts (in case multiple plates are detected)
|
| 61 |
+
output_text = "\n".join(detected_texts) if detected_texts else "No license plate detected"
|
| 62 |
+
return image, output_text
|
| 63 |
|
| 64 |
+
# Create Gradio interface
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=detect_and_read_plate,
|
| 67 |
+
inputs=gr.Image(type="numpy", label="Upload an image of a car"),
|
| 68 |
+
outputs=[
|
| 69 |
+
gr.Image(type="numpy", label="Detected License Plate Image"),
|
| 70 |
+
gr.Textbox(label="Detected License Plate Number")
|
| 71 |
+
],
|
| 72 |
title="Automatic Number Plate Recognition (ANPR)",
|
| 73 |
+
description="Upload an image of a car to detect and read its license plate. The detected plate number will be shown below the image."
|
| 74 |
)
|
| 75 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|