newtechdevng commited on
Commit
c80404c
·
verified ·
1 Parent(s): 6ab057c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -1,30 +1,34 @@
1
  import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
  from ultralytics import YOLO
4
- from PIL import Image
5
 
6
- # Load YOLO model correctly
7
  model_path = hf_hub_download(repo_id="newtechdevng/detect", filename="best.pt")
8
  model = YOLO(model_path)
9
 
10
  def predict(image):
11
  results = model(image)
12
  result = results[0]
13
-
14
- output = []
 
 
 
15
  for box in result.boxes:
16
- label = result.names[int(box.cls)]
17
  confidence = float(box.conf)
18
- output.append(f"{label}: {confidence:.2f}")
19
-
20
- if not output:
21
- return "No objects detected"
22
-
23
- return "\n".join(output)
24
 
25
  gr.Interface(
26
  fn=predict,
27
  inputs=gr.Image(type="pil"),
28
- outputs=gr.Text(label="Detections"),
 
 
 
29
  title="Car / Bike / Mountain / Road Detector"
30
  ).launch()
 
1
  import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
  from ultralytics import YOLO
4
+ import numpy as np
5
 
 
6
  model_path = hf_hub_download(repo_id="newtechdevng/detect", filename="best.pt")
7
  model = YOLO(model_path)
8
 
9
  def predict(image):
10
  results = model(image)
11
  result = results[0]
12
+
13
+ # Get annotated image with boxes drawn
14
+ annotated = result.plot()
15
+
16
+ labels = []
17
  for box in result.boxes:
 
18
  confidence = float(box.conf)
19
+ if confidence < 0.5:
20
+ continue
21
+ label = result.names[int(box.cls)]
22
+ labels.append(f"{label}: {confidence:.2f}")
23
+
24
+ return annotated, "\n".join(labels) if labels else "No objects detected"
25
 
26
  gr.Interface(
27
  fn=predict,
28
  inputs=gr.Image(type="pil"),
29
+ outputs=[
30
+ gr.Image(label="Detected Objects"), # image with boxes
31
+ gr.Text(label="Labels") # text results
32
+ ],
33
  title="Car / Bike / Mountain / Road Detector"
34
  ).launch()