| | import gradio as gr
|
| | from ultralytics import YOLO
|
| |
|
| | model = YOLO("best.pt")
|
| |
|
| | PPE_OPTIONS = ["Helmet", "Vest", "Gloves", "Boots"]
|
| |
|
| |
|
| | def detect_ppe(image, required_items):
|
| | if image is None:
|
| | return None, "Image not found. Please upload an image."
|
| |
|
| | if not required_items:
|
| | return image, "No PPE requirement selected"
|
| |
|
| | results = model(image)
|
| | detected = set()
|
| |
|
| | for r in results:
|
| | for c in r.boxes.cls:
|
| | detected.add(model.names[int(c)])
|
| | annotated = r.plot()
|
| |
|
| | required = set(required_items)
|
| | missing = required - detected
|
| |
|
| | if len(missing) == 0:
|
| | status = "ACCEPTED - Required PPE detected"
|
| | else:
|
| | status = f"REJECTED - Missing: {', '.join(missing)}, please wear the required PPE."
|
| |
|
| | return annotated, status
|
| |
|
| |
|
| | demo = gr.Interface(
|
| | fn=detect_ppe,
|
| | inputs=[
|
| | gr.Image(type="numpy", label="Input Image / Webcam"),
|
| | gr.CheckboxGroup(
|
| | choices=PPE_OPTIONS,
|
| | value=PPE_OPTIONS,
|
| | label="Select Required PPE"
|
| | )
|
| | ],
|
| | outputs=[
|
| | gr.Image(label="Detection Result"),
|
| | gr.Textbox(label="PPE Compliance Status")
|
| | ],
|
| | title="PPE Compliance Detection System",
|
| | description="Detect PPE using YOLOv8 and verify compliance based on selected requirements"
|
| | )
|
| |
|
| | demo.launch()
|
| |
|