Update app.py
Browse files
app.py
CHANGED
|
@@ -29,24 +29,27 @@ def process_image(image, confidence_threshold=0.5):
|
|
| 29 |
# Perform license plate detection
|
| 30 |
results = yolo_model(image, conf=confidence_threshold)
|
| 31 |
annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
license_plate_text = []
|
| 35 |
cropped_images = []
|
| 36 |
-
|
|
|
|
| 37 |
for result in results:
|
| 38 |
boxes = result.boxes.xyxy.cpu().numpy().astype(int)
|
| 39 |
confidences = result.boxes.conf.cpu().numpy()
|
| 40 |
-
|
| 41 |
if len(boxes) == 0:
|
| 42 |
st.warning("No license plate detected!")
|
| 43 |
return [], [], None
|
|
|
|
| 44 |
for i, (box, conf) in enumerate(zip(boxes, confidences)):
|
| 45 |
x1, y1, x2, y2 = box
|
| 46 |
# Draw bounding box on the annotated image
|
| 47 |
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 48 |
cv2.putText(annotated_image, f"{conf:.2f}", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 49 |
|
|
|
|
| 50 |
cropped_image = image[y1:y2, x1:x2]
|
| 51 |
cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
|
| 52 |
cropped_images.append(cropped_image_rgb)
|
|
@@ -55,11 +58,11 @@ def process_image(image, confidence_threshold=0.5):
|
|
| 55 |
text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
|
| 56 |
detected_text = " ".join(text_results)
|
| 57 |
license_plate_text.append(detected_text)
|
| 58 |
-
|
| 59 |
return license_plate_text, cropped_images, annotated_image
|
| 60 |
|
| 61 |
# Sidebar input for file upload
|
| 62 |
-
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 63 |
|
| 64 |
if uploaded_file is not None:
|
| 65 |
# Read and process the image
|
|
@@ -70,11 +73,13 @@ if uploaded_file is not None:
|
|
| 70 |
c1, c2, c3 = st.columns(3)
|
| 71 |
|
| 72 |
with c1:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
with c2:
|
|
|
|
| 78 |
if cropped_images:
|
| 79 |
for i, cropped_image in enumerate(cropped_images):
|
| 80 |
st.image(cropped_image, caption=f'Cropped License Plate {i+1}', use_container_width=True)
|
|
@@ -82,14 +87,12 @@ if uploaded_file is not None:
|
|
| 82 |
st.write('No License Plate Detected')
|
| 83 |
|
| 84 |
with c3:
|
|
|
|
| 85 |
if license_plate_text:
|
| 86 |
st.success(', '.join(license_plate_text))
|
| 87 |
st.write('License Plate Text')
|
| 88 |
else:
|
| 89 |
st.write('No text detected')
|
| 90 |
|
| 91 |
-
# Display the annotated image with bounding boxes
|
| 92 |
-
st.image(annotated_image, caption='Annotated Image with Bounding Boxes', use_container_width=True)
|
| 93 |
-
|
| 94 |
st.markdown("---")
|
| 95 |
st.info("This application uses Fine Tuned YOLOv8 for detection and EasyOCR for text recognition.")
|
|
|
|
| 29 |
# Perform license plate detection
|
| 30 |
results = yolo_model(image, conf=confidence_threshold)
|
| 31 |
annotated_image = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
|
| 32 |
+
|
| 33 |
+
# Prepare the cropped images and detected text for each plate
|
| 34 |
license_plate_text = []
|
| 35 |
cropped_images = []
|
| 36 |
+
|
| 37 |
+
# Loop through detections and perform OCR
|
| 38 |
for result in results:
|
| 39 |
boxes = result.boxes.xyxy.cpu().numpy().astype(int)
|
| 40 |
confidences = result.boxes.conf.cpu().numpy()
|
| 41 |
+
|
| 42 |
if len(boxes) == 0:
|
| 43 |
st.warning("No license plate detected!")
|
| 44 |
return [], [], None
|
| 45 |
+
|
| 46 |
for i, (box, conf) in enumerate(zip(boxes, confidences)):
|
| 47 |
x1, y1, x2, y2 = box
|
| 48 |
# Draw bounding box on the annotated image
|
| 49 |
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 50 |
cv2.putText(annotated_image, f"{conf:.2f}", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 51 |
|
| 52 |
+
# Crop the license plate from the image
|
| 53 |
cropped_image = image[y1:y2, x1:x2]
|
| 54 |
cropped_image_rgb = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
|
| 55 |
cropped_images.append(cropped_image_rgb)
|
|
|
|
| 58 |
text_results = ocr_reader.readtext(cropped_image_rgb, detail=0)
|
| 59 |
detected_text = " ".join(text_results)
|
| 60 |
license_plate_text.append(detected_text)
|
| 61 |
+
|
| 62 |
return license_plate_text, cropped_images, annotated_image
|
| 63 |
|
| 64 |
# Sidebar input for file upload
|
| 65 |
+
uploaded_file = st.file_uploader("Upload an Image or Video", type=["mp4", "avi", "mov", "jpg", "jpeg", "png"])
|
| 66 |
|
| 67 |
if uploaded_file is not None:
|
| 68 |
# Read and process the image
|
|
|
|
| 73 |
c1, c2, c3 = st.columns(3)
|
| 74 |
|
| 75 |
with c1:
|
| 76 |
+
# Display uploaded image with bounding boxes
|
| 77 |
+
license_plate_text, cropped_images, annotated_image = process_image(image, confidence_threshold)
|
| 78 |
+
if annotated_image is not None:
|
| 79 |
+
st.image(annotated_image, caption='Uploaded Image with Bounding Boxes', use_container_width=True)
|
| 80 |
|
| 81 |
with c2:
|
| 82 |
+
# Display cropped license plates
|
| 83 |
if cropped_images:
|
| 84 |
for i, cropped_image in enumerate(cropped_images):
|
| 85 |
st.image(cropped_image, caption=f'Cropped License Plate {i+1}', use_container_width=True)
|
|
|
|
| 87 |
st.write('No License Plate Detected')
|
| 88 |
|
| 89 |
with c3:
|
| 90 |
+
# Display the extracted text for license plates
|
| 91 |
if license_plate_text:
|
| 92 |
st.success(', '.join(license_plate_text))
|
| 93 |
st.write('License Plate Text')
|
| 94 |
else:
|
| 95 |
st.write('No text detected')
|
| 96 |
|
|
|
|
|
|
|
|
|
|
| 97 |
st.markdown("---")
|
| 98 |
st.info("This application uses Fine Tuned YOLOv8 for detection and EasyOCR for text recognition.")
|