Nadun102 commited on
Commit
b6eb957
·
verified ·
1 Parent(s): 0352ab9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -23,6 +23,7 @@ processor = Owlv2Processor.from_pretrained(
23
  @spaces.GPU
24
  def query_image(img, text_queries, score_threshold):
25
 
 
26
  text_queries = text_queries.split(",")
27
 
28
  # Prepare inputs
@@ -35,14 +36,15 @@ def query_image(img, text_queries, score_threshold):
35
  with torch.no_grad():
36
  outputs = model(**inputs)
37
 
38
- # Move to CPU for processing
39
  outputs.logits = outputs.logits.cpu()
40
  outputs.pred_boxes = outputs.pred_boxes.cpu()
41
 
42
- # Correct target size
43
  target_sizes = torch.tensor([img.shape[:2]])
44
 
45
- results = processor.post_process_object_detection(
 
46
  outputs=outputs,
47
  target_sizes=target_sizes
48
  )[0]
@@ -52,6 +54,7 @@ def query_image(img, text_queries, score_threshold):
52
 
53
  output_boxes = []
54
 
 
55
  for box, score in zip(boxes, scores):
56
 
57
  if score < score_threshold:
@@ -59,10 +62,10 @@ def query_image(img, text_queries, score_threshold):
59
 
60
  x1, y1, x2, y2 = map(int, box.tolist())
61
 
62
- # store ONLY coordinates
63
  output_boxes.append([x1, y1, x2, y2])
64
 
65
- # draw rectangle (optional)
66
  cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
67
 
68
  return img, output_boxes
@@ -80,9 +83,10 @@ demo = gr.Interface(
80
  ],
81
  outputs=[
82
  gr.Image(label="Bounding Boxes"),
83
- gr.JSON(label="Coordinates")
84
  ],
85
  title="OWLv2 Bounding Box Coordinates Only"
86
  )
87
 
 
88
  demo.launch()
 
23
  @spaces.GPU
24
  def query_image(img, text_queries, score_threshold):
25
 
26
+ # Split queries (still required internally)
27
  text_queries = text_queries.split(",")
28
 
29
  # Prepare inputs
 
36
  with torch.no_grad():
37
  outputs = model(**inputs)
38
 
39
+ # Move outputs to CPU
40
  outputs.logits = outputs.logits.cpu()
41
  outputs.pred_boxes = outputs.pred_boxes.cpu()
42
 
43
+ # Correct target size (IMPORTANT)
44
  target_sizes = torch.tensor([img.shape[:2]])
45
 
46
+ # FIXED METHOD (important!)
47
+ results = processor.post_process_grounded_object_detection(
48
  outputs=outputs,
49
  target_sizes=target_sizes
50
  )[0]
 
54
 
55
  output_boxes = []
56
 
57
+ # Process detections
58
  for box, score in zip(boxes, scores):
59
 
60
  if score < score_threshold:
 
62
 
63
  x1, y1, x2, y2 = map(int, box.tolist())
64
 
65
+ # Save ONLY coordinates
66
  output_boxes.append([x1, y1, x2, y2])
67
 
68
+ # Draw rectangle ONLY (no labels)
69
  cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
70
 
71
  return img, output_boxes
 
83
  ],
84
  outputs=[
85
  gr.Image(label="Bounding Boxes"),
86
+ gr.JSON(label="Coordinates Only")
87
  ],
88
  title="OWLv2 Bounding Box Coordinates Only"
89
  )
90
 
91
+ # Launch app
92
  demo.launch()