ghostai1 commited on
Commit
f87b5b3
·
verified ·
1 Parent(s): f77a579

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -4,43 +4,53 @@ import gradio as gr
4
  from transformers import pipeline
5
  from PIL import Image, ImageDraw
6
 
7
- # Load the DETR object-detection pipeline (CPU)
8
  detector = pipeline("object-detection", model="facebook/detr-resnet-50", device=-1)
9
 
10
  def detect_objects(image: Image.Image):
11
- # Run object detection
12
  outputs = detector(image)
13
 
14
- # Draw bounding boxes
15
  annotated = image.convert("RGB")
16
  draw = ImageDraw.Draw(annotated)
17
  table = []
 
18
  for obj in outputs:
19
- # DETR returns box as [xmin, ymin, xmax, ymax]
20
- xmin, ymin, xmax, ymax = obj["box"]
 
 
 
 
 
 
 
 
 
 
 
21
  label = obj["label"]
22
  score = round(obj["score"], 3)
23
 
24
- # draw box and label
25
  draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=2)
26
- draw.text((xmin, ymin - 10), f"{label} ({score})", fill="red")
27
 
28
  table.append([label, score])
29
 
30
- # Return the annotated image and a table of detections
31
  return annotated, table
32
 
33
  with gr.Blocks(title="📷✨ Object Detection Demo") as demo:
34
  gr.Markdown(
35
  """
36
  # 📷✨ Object Detection
37
- Upload an image and let DETR (a Transformer-based model) identify objects in real time.
38
  """
39
  )
40
 
41
  with gr.Row():
42
- img_in = gr.Image(type="pil", label="Upload Image")
43
- detect_btn = gr.Button("Detect Objects 🔍", variant="primary")
 
44
  img_out = gr.Image(label="Annotated Image")
45
  table_out = gr.Dataframe(
46
  headers=["Label", "Score"],
@@ -50,7 +60,7 @@ with gr.Blocks(title="📷✨ Object Detection Demo") as demo:
50
  label="Detections"
51
  )
52
 
53
- detect_btn.click(detect_objects, inputs=img_in, outputs=[img_out, table_out])
54
 
55
  if __name__ == "__main__":
56
- demo.launch(server_name="0.0.0.0")
 
4
  from transformers import pipeline
5
  from PIL import Image, ImageDraw
6
 
7
+ # Load DETR objectdetection pipeline (requires timm in requirements)
8
  detector = pipeline("object-detection", model="facebook/detr-resnet-50", device=-1)
9
 
10
  def detect_objects(image: Image.Image):
 
11
  outputs = detector(image)
12
 
 
13
  annotated = image.convert("RGB")
14
  draw = ImageDraw.Draw(annotated)
15
  table = []
16
+
17
  for obj in outputs:
18
+ box = obj["box"]
19
+ # DETR pipeline may return box as dict or list
20
+ if isinstance(box, dict):
21
+ xmin = int(box.get("xmin", box.get("x", 0)))
22
+ ymin = int(box.get("ymin", box.get("y", 0)))
23
+ xmax = int(box.get("xmax", xmin))
24
+ ymax = int(box.get("ymax", ymin))
25
+ else:
26
+ # assume [x, y, w, h]
27
+ x, y, w, h = box
28
+ xmin, ymin = int(x), int(y)
29
+ xmax, ymax = int(x + w), int(y + h)
30
+
31
  label = obj["label"]
32
  score = round(obj["score"], 3)
33
 
34
+ # draw box & label
35
  draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=2)
36
+ draw.text((xmin, max(ymin - 10, 0)), f"{label} ({score})", fill="red")
37
 
38
  table.append([label, score])
39
 
 
40
  return annotated, table
41
 
42
  with gr.Blocks(title="📷✨ Object Detection Demo") as demo:
43
  gr.Markdown(
44
  """
45
  # 📷✨ Object Detection
46
+ Upload an image and let DETR identify objects on CPU.
47
  """
48
  )
49
 
50
  with gr.Row():
51
+ img_in = gr.Image(type="pil", label="Upload Image")
52
+ btn = gr.Button("Detect Objects 🔍", variant="primary")
53
+
54
  img_out = gr.Image(label="Annotated Image")
55
  table_out = gr.Dataframe(
56
  headers=["Label", "Score"],
 
60
  label="Detections"
61
  )
62
 
63
+ btn.click(detect_objects, inputs=img_in, outputs=[img_out, table_out])
64
 
65
  if __name__ == "__main__":
66
+ demo.launch(server_name="0.0.0.0")