Update app.py
Browse files
app.py
CHANGED
|
@@ -43,28 +43,28 @@ def detect_and_crop_license_plate(image):
|
|
| 43 |
img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 44 |
results = full_plate_model(img_bgr)
|
| 45 |
|
|
|
|
| 46 |
cropped_images = []
|
| 47 |
for result in results:
|
| 48 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
| 49 |
for box in result.boxes.xyxy:
|
| 50 |
x1, y1, x2, y2 = map(int, box)
|
|
|
|
| 51 |
cropped_image = img_bgr[y1:y2, x1:x2]
|
| 52 |
cropped_images.append(cropped_image)
|
| 53 |
|
| 54 |
-
return cropped_images
|
| 55 |
|
| 56 |
-
# Function to detect
|
| 57 |
-
def
|
| 58 |
-
results = character_model(
|
| 59 |
character_crops = []
|
| 60 |
-
bounding_boxes = []
|
| 61 |
for result in results:
|
| 62 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
| 63 |
for box in result.boxes.xyxy:
|
| 64 |
x1, y1, x2, y2 = map(int, box)
|
| 65 |
-
character_crops.append(
|
| 66 |
-
|
| 67 |
-
return character_crops, bounding_boxes
|
| 68 |
|
| 69 |
# Function to recognize characters
|
| 70 |
def recognize_characters(character_crops):
|
|
@@ -95,26 +95,29 @@ if uploaded_file is not None:
|
|
| 95 |
|
| 96 |
# Detect license plates
|
| 97 |
with st.spinner("Processing image..."):
|
| 98 |
-
cropped_plates = detect_and_crop_license_plate(image)
|
| 99 |
|
| 100 |
if cropped_plates:
|
|
|
|
|
|
|
|
|
|
| 101 |
for idx, cropped_plate in enumerate(cropped_plates, 1):
|
| 102 |
st.write(f"Processing License Plate {idx}:")
|
| 103 |
-
character_crops
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
recognized_characters = recognize_characters(character_crops)
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
# Show the annotated license plate with bounding boxes
|
| 114 |
-
st.image(cv2.cvtColor(cropped_plate, cv2.COLOR_BGR2RGB), caption=f"License Plate {idx} with Bounding Boxes", use_container_width=True)
|
| 115 |
-
|
| 116 |
-
# Display recognized characters
|
| 117 |
-
st.write("Recognized Characters: ", "".join(recognized_characters))
|
| 118 |
-
else:
|
| 119 |
-
st.write("No license plates detected.")
|
| 120 |
st.success("Processing complete!")
|
|
|
|
| 43 |
img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 44 |
results = full_plate_model(img_bgr)
|
| 45 |
|
| 46 |
+
detected_image = img_bgr.copy()
|
| 47 |
cropped_images = []
|
| 48 |
for result in results:
|
| 49 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
| 50 |
for box in result.boxes.xyxy:
|
| 51 |
x1, y1, x2, y2 = map(int, box)
|
| 52 |
+
cv2.rectangle(detected_image, (x1, y1), (x2, y2), (255, 0, 0), 2) # Draw bounding box
|
| 53 |
cropped_image = img_bgr[y1:y2, x1:x2]
|
| 54 |
cropped_images.append(cropped_image)
|
| 55 |
|
| 56 |
+
return cropped_images, detected_image
|
| 57 |
|
| 58 |
+
# Function to detect and crop characters
|
| 59 |
+
def detect_and_crop_characters(image):
|
| 60 |
+
results = character_model(image)
|
| 61 |
character_crops = []
|
|
|
|
| 62 |
for result in results:
|
| 63 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
| 64 |
for box in result.boxes.xyxy:
|
| 65 |
x1, y1, x2, y2 = map(int, box)
|
| 66 |
+
character_crops.append(image[y1:y2, x1:x2])
|
| 67 |
+
return character_crops
|
|
|
|
| 68 |
|
| 69 |
# Function to recognize characters
|
| 70 |
def recognize_characters(character_crops):
|
|
|
|
| 95 |
|
| 96 |
# Detect license plates
|
| 97 |
with st.spinner("Processing image..."):
|
| 98 |
+
cropped_plates, detected_image = detect_and_crop_license_plate(image)
|
| 99 |
|
| 100 |
if cropped_plates:
|
| 101 |
+
st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
|
| 102 |
+
st.write(f"Detected {len(cropped_plates)} license plate(s).")
|
| 103 |
+
|
| 104 |
for idx, cropped_plate in enumerate(cropped_plates, 1):
|
| 105 |
st.write(f"Processing License Plate {idx}:")
|
| 106 |
+
character_crops = detect_and_crop_characters(cropped_plate)
|
| 107 |
|
| 108 |
+
if character_crops:
|
| 109 |
+
recognized_characters = recognize_characters(character_crops)
|
| 110 |
+
st.write("Recognized Characters:", "".join(recognized_characters))
|
| 111 |
+
else:
|
| 112 |
+
st.write("No characters detected in this license plate.")
|
| 113 |
+
else:
|
| 114 |
+
st.write("No license plates detected. Running character detection on the full image.")
|
| 115 |
+
character_crops = detect_and_crop_characters(cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR))
|
| 116 |
|
| 117 |
+
if character_crops:
|
| 118 |
recognized_characters = recognize_characters(character_crops)
|
| 119 |
+
st.write("Recognized Characters:", "".join(recognized_characters))
|
| 120 |
+
else:
|
| 121 |
+
st.write("No characters detected in the full image.")
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
st.success("Processing complete!")
|