navjotk commited on
Commit
b0c291d
·
verified ·
1 Parent(s): b63bd55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -14
app.py CHANGED
@@ -3,17 +3,17 @@ from transformers import pipeline
3
  from PIL import Image, ImageDraw, ImageFont
4
  import tempfile
5
 
6
- # Load YOLOS object detection model
7
  detector = pipeline("object-detection", model="hustvl/yolos-small")
8
 
9
- # Colors for labels
10
  COLORS = ["red", "blue", "green", "orange", "purple", "yellow", "cyan", "magenta"]
11
 
12
- # Pick color based on label
13
  def get_color_for_label(label):
14
  return COLORS[hash(label) % len(COLORS)]
15
 
16
- # Main detection + drawing function
17
  def detect_and_draw(image, threshold):
18
  results = detector(image)
19
  image = image.convert("RGB")
@@ -41,12 +41,7 @@ def detect_and_draw(image, threshold):
41
  width=3,
42
  )
43
 
44
- draw.text(
45
- (box["xmin"] + 5, box["ymin"] + 5),
46
- label,
47
- fill=color,
48
- font=font
49
- )
50
 
51
  box_coords = (box["xmin"], box["ymin"], box["xmax"], box["ymax"])
52
  annotations.append((box_coords, label))
@@ -55,9 +50,10 @@ def detect_and_draw(image, threshold):
55
  temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
56
  image.save(temp_file.name)
57
 
58
- return image, annotations, temp_file.name
 
59
 
60
- # Gradio UI
61
  demo = gr.Interface(
62
  fn=detect_and_draw,
63
  inputs=[
@@ -68,8 +64,8 @@ demo = gr.Interface(
68
  gr.AnnotatedImage(label="Detected Image"),
69
  gr.File(label="Download Processed Image"),
70
  ],
71
- title="YOLOS Object Detection (CPU Friendly)",
72
- description="Upload an image to detect objects using the YOLOS-small model. Adjust the slider to set the confidence threshold.",
73
  )
74
 
75
  demo.launch()
 
3
  from PIL import Image, ImageDraw, ImageFont
4
  import tempfile
5
 
6
+ # Load the YOLOS object detection model
7
  detector = pipeline("object-detection", model="hustvl/yolos-small")
8
 
9
+ # Define some colors to differentiate classes
10
  COLORS = ["red", "blue", "green", "orange", "purple", "yellow", "cyan", "magenta"]
11
 
12
+ # Helper function to assign color per label
13
  def get_color_for_label(label):
14
  return COLORS[hash(label) % len(COLORS)]
15
 
16
+ # Main function: detect, draw, and return outputs
17
  def detect_and_draw(image, threshold):
18
  results = detector(image)
19
  image = image.convert("RGB")
 
41
  width=3,
42
  )
43
 
44
+ draw.text((box["xmin"] + 5, box["ymin"] + 5), label, fill=color, font=font)
 
 
 
 
 
45
 
46
  box_coords = (box["xmin"], box["ymin"], box["xmax"], box["ymax"])
47
  annotations.append((box_coords, label))
 
50
  temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
51
  image.save(temp_file.name)
52
 
53
+ # ✅ Return the (image, annotations) tuple and the path to the saved image
54
+ return (image, annotations), temp_file.name
55
 
56
+ # Gradio UI setup
57
  demo = gr.Interface(
58
  fn=detect_and_draw,
59
  inputs=[
 
64
  gr.AnnotatedImage(label="Detected Image"),
65
  gr.File(label="Download Processed Image"),
66
  ],
67
+ title="YOLOS Object Detection (CPU-Friendly)",
68
+ description="Upload an image to detect objects using the YOLOS-small model. Adjust the confidence threshold using the slider.",
69
  )
70
 
71
  demo.launch()