SamiKhokhar commited on
Commit
65f7d8b
·
verified ·
1 Parent(s): b51a894

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -15,58 +15,61 @@ 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 detect non-helmet riders and their license plates
19
  def detect_non_helmet_and_plate(image):
20
  img_np = np.array(image)
21
  results = model(image)
22
 
23
- # Default outputs
24
  helmet_status = "Pass"
25
- license_plate_text = "Pass"
 
26
 
27
- # Parse YOLO results
28
  non_helmet_detected = False
29
  for *xyxy, conf, cls in results.xyxy[0]:
30
  class_id = int(cls)
31
  if class_id == 0: # Class 0 is 'person' in YOLOv5s
32
  non_helmet_detected = True
33
  helmet_status = "Fail"
34
- cv2.rectangle(img_np, (int(xyxy[0]), int(xyxy[1])),
35
- (int(xyxy[2]), int(xyxy[3])), (0, 0, 255), 2) # Red box
36
- cv2.putText(img_np, "No Helmet",
37
- (int(xyxy[0]), int(xyxy[1]) - 10),
38
  cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
39
 
40
- # Save the image of the non-helmet rider
41
- cropped_img = img_np[int(xyxy[1]):int(xyxy[3]), int(xyxy[0]):int(xyxy[2])]
42
- rider_image_path = f"non_helmet_riders/rider_{np.random.randint(10000)}.jpg"
43
- cv2.imwrite(rider_image_path, cropped_img)
44
-
45
- # Detect license plate if a non-helmet rider is found
46
  if non_helmet_detected:
47
- plate_text = reader.readtext(img_np)
48
  for detection in plate_text:
49
  text = detection[1]
50
- if len(text) > 5: # Assuming plates have a minimum length
51
  license_plate_text = text
52
  break
53
 
54
- # Convert the processed image back to PIL for Gradio display
55
  img_pil = Image.fromarray(img_np)
56
- return img_pil, helmet_status, license_plate_text
 
 
 
 
57
 
58
- # Set up Gradio interface
59
  interface = gr.Interface(
60
- fn=detect_non_helmet_and_plate,
61
  inputs=gr.Image(type="pil", label="Upload Image"),
62
  outputs=[
63
  gr.Image(type="pil", label="Processed Image"),
64
  gr.Textbox(label="Helmet Status"),
65
- gr.Textbox(label="License Plate"),
 
66
  ],
67
  title="Helmet and License Plate Detection",
68
- description="Detect riders without helmets. If a rider is without a helmet, capture their image and license plate.",
69
  )
70
 
71
  # Launch Gradio app
72
- interface.launch(share=True)
 
 
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
+ 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
25
  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 = "I can't detect image"
31
+ license_plate_image = None
32
 
 
33
  non_helmet_detected = False
34
  for *xyxy, conf, cls in results.xyxy[0]:
35
  class_id = int(cls)
36
  if class_id == 0: # Class 0 is 'person' in YOLOv5s
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
+ if len(text) > 5 and text.isalnum():
50
  license_plate_text = text
51
  break
52
 
 
53
  img_pil = Image.fromarray(img_np)
54
+ return img_pil, helmet_status, None, license_plate_text
55
+
56
+ # Gradio interface
57
+ def interface_fn(image):
58
+ return detect_non_helmet_and_plate(image)
59
 
 
60
  interface = gr.Interface(
61
+ fn=interface_fn,
62
  inputs=gr.Image(type="pil", label="Upload Image"),
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
+ if __name__ == "__main__":
75
+ interface.launch()