dxvyaaa commited on
Commit
90d3a56
·
verified ·
1 Parent(s): fd2d61b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -1,38 +1,34 @@
1
  import gradio as gr
2
  from ultralytics import YOLO
3
- import torch
4
 
5
- model = YOLO("best.pt") # Make sure best.pt exists in the same folder
 
6
 
7
  def predict(image):
8
- try:
9
- results = model(image)[0]
10
 
11
- detections = []
12
- for box in results.boxes:
13
- xmin, ymin, xmax, ymax = box.xyxy[0].tolist()
14
- cls = int(box.cls)
15
- conf = float(box.conf)
16
 
 
 
17
  detections.append({
18
- "label": model.names[cls],
19
- "confidence": conf,
20
- "box": [xmin, ymin, xmax, ymax]
21
  })
22
 
23
- return image, detections
24
- except Exception as e:
25
- print("ERROR:", e)
26
- return image, []
27
 
 
28
  demo = gr.Interface(
29
  fn=predict,
30
- inputs=gr.Image(type="numpy", label="Upload Image"),
31
- outputs=[
32
- gr.Image(type="numpy", label="Processed Image"),
33
- gr.JSON(label="Detections")
34
- ],
35
- title="YOLO Helmet Detection"
36
  )
37
 
38
  demo.launch()
 
1
  import gradio as gr
2
  from ultralytics import YOLO
3
+ import numpy as np
4
 
5
+ # Load YOLOv8 model
6
+ model = YOLO("best.pt") # Make sure best.pt is in the repo root
7
 
8
  def predict(image):
9
+ # image is numpy array from Gradio
10
+ results = model(image)[0] # YOLO prediction
11
 
12
+ annotated_image = results.plot() # returns numpy array
13
+ detections = []
 
 
 
14
 
15
+ if results.boxes is not None:
16
+ for box, cls, conf in zip(results.boxes.xyxy, results.boxes.cls, results.boxes.conf):
17
  detections.append({
18
+ "label": model.names[int(cls)],
19
+ "confidence": float(conf),
20
+ "box": [float(coord) for coord in box]
21
  })
22
 
23
+ return annotated_image, detections
 
 
 
24
 
25
+ # Gradio interface
26
  demo = gr.Interface(
27
  fn=predict,
28
+ inputs=gr.Image(type="numpy"),
29
+ outputs=[gr.Image(type="numpy"), gr.JSON()],
30
+ title="Helmet Detection YOLOv8",
31
+ description="Upload an image and detect helmet / head using YOLOv8"
 
 
32
  )
33
 
34
  demo.launch()