Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,10 +15,14 @@ reader = easyocr.Reader(['en'])
|
|
| 15 |
# Directory to save images of non-helmet riders
|
| 16 |
os.makedirs("non_helmet_riders", exist_ok=True)
|
| 17 |
|
| 18 |
-
# Function to
|
| 19 |
def preprocess_image_for_ocr(image):
|
|
|
|
| 20 |
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
|
|
|
|
|
|
| 21 |
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
|
|
|
|
| 22 |
return thresh
|
| 23 |
|
| 24 |
# Function to detect non-helmet riders and their license plates
|
|
@@ -26,50 +30,81 @@ def detect_non_helmet_and_plate(image):
|
|
| 26 |
img_np = np.array(image)
|
| 27 |
results = model(image)
|
| 28 |
|
|
|
|
| 29 |
helmet_status = "Pass"
|
| 30 |
-
license_plate_text = "
|
| 31 |
license_plate_image = None
|
| 32 |
-
non_helmet_detected = False
|
| 33 |
|
|
|
|
|
|
|
| 34 |
for *xyxy, conf, cls in results.xyxy[0]:
|
| 35 |
class_id = int(cls)
|
| 36 |
-
if class_id == 0: # Class 0 is 'person'
|
| 37 |
non_helmet_detected = True
|
| 38 |
helmet_status = "Fail"
|
| 39 |
-
cv2.rectangle(img_np, (int(xyxy[0]), int(xyxy[1])),
|
| 40 |
-
(int(xyxy[2]), int(xyxy[3])), (0, 0, 255), 2)
|
| 41 |
-
cv2.putText(img_np, "No Helmet",
|
| 42 |
-
(int(xyxy[0]), int(xyxy[1]) - 10),
|
| 43 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if non_helmet_detected:
|
| 46 |
-
plate_text = reader.readtext(preprocess_image_for_ocr(image))
|
| 47 |
for detection in plate_text:
|
| 48 |
text = detection[1]
|
| 49 |
-
|
|
|
|
| 50 |
license_plate_text = text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
break
|
| 52 |
|
|
|
|
| 53 |
img_pil = Image.fromarray(img_np)
|
| 54 |
-
return img_pil, helmet_status,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# Gradio interface
|
| 57 |
-
def interface_fn(image):
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 60 |
interface = gr.Interface(
|
| 61 |
fn=interface_fn,
|
| 62 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 63 |
outputs=[
|
| 64 |
-
gr.Image(type="pil", label="Processed Image"),
|
| 65 |
-
gr.Textbox(label="Helmet Status"),
|
| 66 |
-
gr.Image(type="pil", label="License Plate Image"),
|
| 67 |
-
gr.Textbox(label="License Plate Number")
|
| 68 |
],
|
| 69 |
title="Helmet and License Plate Detection",
|
| 70 |
-
description="Detect riders without helmets. If a rider is without a helmet, capture their image and license plate."
|
| 71 |
)
|
| 72 |
|
| 73 |
# Launch Gradio app
|
| 74 |
-
|
| 75 |
-
interface.launch()
|
|
|
|
| 15 |
# Directory to save images of non-helmet riders
|
| 16 |
os.makedirs("non_helmet_riders", exist_ok=True)
|
| 17 |
|
| 18 |
+
# Function to enhance the image for better number plate recognition
|
| 19 |
def preprocess_image_for_ocr(image):
|
| 20 |
+
# Convert image to grayscale
|
| 21 |
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 22 |
+
|
| 23 |
+
# Apply thresholding to binarize the image (white text on black background)
|
| 24 |
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
|
| 25 |
+
|
| 26 |
return thresh
|
| 27 |
|
| 28 |
# Function to detect non-helmet riders and their license plates
|
|
|
|
| 30 |
img_np = np.array(image)
|
| 31 |
results = model(image)
|
| 32 |
|
| 33 |
+
# Default outputs
|
| 34 |
helmet_status = "Pass"
|
| 35 |
+
license_plate_text = "I can't detect image"
|
| 36 |
license_plate_image = None
|
|
|
|
| 37 |
|
| 38 |
+
# Parse YOLO results
|
| 39 |
+
non_helmet_detected = False
|
| 40 |
for *xyxy, conf, cls in results.xyxy[0]:
|
| 41 |
class_id = int(cls)
|
| 42 |
+
if class_id == 0: # Class 0 is 'person' in YOLOv5s
|
| 43 |
non_helmet_detected = True
|
| 44 |
helmet_status = "Fail"
|
| 45 |
+
cv2.rectangle(img_np, (int(xyxy[0]), int(xyxy[1])),
|
| 46 |
+
(int(xyxy[2]), int(xyxy[3])), (0, 0, 255), 2) # Red box
|
| 47 |
+
cv2.putText(img_np, "No Helmet",
|
| 48 |
+
(int(xyxy[0]), int(xyxy[1]) - 10),
|
| 49 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
|
| 50 |
|
| 51 |
+
# Save the image of the non-helmet rider
|
| 52 |
+
cropped_img = img_np[int(xyxy[1]):int(xyxy[3]), int(xyxy[0]):int(xyxy[2])]
|
| 53 |
+
rider_image_path = f"non_helmet_riders/rider_{np.random.randint(10000)}.jpg"
|
| 54 |
+
cv2.imwrite(rider_image_path, cropped_img)
|
| 55 |
+
|
| 56 |
+
# Detect license plate if a non-helmet rider is found
|
| 57 |
if non_helmet_detected:
|
| 58 |
+
plate_text = reader.readtext(preprocess_image_for_ocr(image)) # Preprocess image before passing to OCR
|
| 59 |
for detection in plate_text:
|
| 60 |
text = detection[1]
|
| 61 |
+
# Filter for license plate-like text
|
| 62 |
+
if len(text) > 5 and text.isalnum(): # Assuming plates have a minimum length and alphanumeric
|
| 63 |
license_plate_text = text
|
| 64 |
+
|
| 65 |
+
# Create the cropped image of the plate
|
| 66 |
+
plate_img = np.array(image)[int(detection[0][0][1]):int(detection[0][2][1]),
|
| 67 |
+
int(detection[0][0][0]):int(detection[0][2][0])]
|
| 68 |
+
license_plate_image = Image.fromarray(plate_img)
|
| 69 |
break
|
| 70 |
|
| 71 |
+
# Convert the processed image back to PIL for Gradio display
|
| 72 |
img_pil = Image.fromarray(img_np)
|
| 73 |
+
return img_pil, helmet_status, license_plate_image, license_plate_text # Returning the image, helmet status, plate image, and license plate number
|
| 74 |
+
|
| 75 |
+
# Function to capture live video frame from webcam
|
| 76 |
+
def capture_webcam_frame():
|
| 77 |
+
cap = cv2.VideoCapture(0)
|
| 78 |
+
ret, frame = cap.read()
|
| 79 |
+
cap.release()
|
| 80 |
+
if ret:
|
| 81 |
+
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 82 |
+
return detect_non_helmet_and_plate(img)
|
| 83 |
+
return None, "Error", "I can't detect image", "I can't detect image"
|
| 84 |
|
| 85 |
+
# Set up Gradio interface with both upload and webcam inputs
|
| 86 |
+
def interface_fn(image, capture_from_webcam):
|
| 87 |
+
if capture_from_webcam:
|
| 88 |
+
return capture_webcam_frame()
|
| 89 |
+
else:
|
| 90 |
+
return detect_non_helmet_and_plate(image)
|
| 91 |
|
| 92 |
+
# Set up Gradio interface
|
| 93 |
interface = gr.Interface(
|
| 94 |
fn=interface_fn,
|
| 95 |
+
inputs=[
|
| 96 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 97 |
+
gr.Checkbox(label="Capture from Webcam") # Use Checkbox to toggle between upload and webcam
|
| 98 |
+
],
|
| 99 |
outputs=[
|
| 100 |
+
gr.Image(type="pil", label="Processed Image"), # Output: Processed Image
|
| 101 |
+
gr.Textbox(label="Helmet Status"), # Output: Helmet Status
|
| 102 |
+
gr.Image(type="pil", label="License Plate Image"), # Output: License Plate Image
|
| 103 |
+
gr.Textbox(label="License Plate Number") # Output: License Plate Number
|
| 104 |
],
|
| 105 |
title="Helmet and License Plate Detection",
|
| 106 |
+
description="Detect riders without helmets. If a rider is without a helmet, capture their image and license plate.",
|
| 107 |
)
|
| 108 |
|
| 109 |
# Launch Gradio app
|
| 110 |
+
interface.launch(share=True)
|
|
|