yukta1 commited on
Commit
683f67c
·
verified ·
1 Parent(s): 4e8c594

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -8
app.py CHANGED
@@ -3,17 +3,13 @@ from transformers import pipeline
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")
@@ -46,14 +42,11 @@ def detect_and_draw(image, threshold):
46
  box_coords = (box["xmin"], box["ymin"], box["xmax"], box["ymax"])
47
  annotations.append((box_coords, label))
48
 
49
- # Save image for download
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=[
 
3
  from PIL import Image, ImageDraw, ImageFont
4
  import tempfile
5
 
 
6
  detector = pipeline("object-detection", model="hustvl/yolos-small")
7
 
 
8
  COLORS = ["red", "blue", "green", "orange", "purple", "yellow", "cyan", "magenta"]
9
 
 
10
  def get_color_for_label(label):
11
  return COLORS[hash(label) % len(COLORS)]
12
 
 
13
  def detect_and_draw(image, threshold):
14
  results = detector(image)
15
  image = image.convert("RGB")
 
42
  box_coords = (box["xmin"], box["ymin"], box["xmax"], box["ymax"])
43
  annotations.append((box_coords, label))
44
 
 
45
  temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
46
  image.save(temp_file.name)
47
 
48
+
49
  return (image, annotations), temp_file.name
 
 
50
  demo = gr.Interface(
51
  fn=detect_and_draw,
52
  inputs=[