donb-hf commited on
Commit
0dc767e
1 Parent(s): 59ad67f

update app.py

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +13 -7
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .venv/
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import time
3
  import gradio as gr
@@ -8,8 +10,6 @@ import vision_agent as va
8
  from vision_agent.tools import register_tool
9
  from vision_agent.tools import load_image, owl_v2, overlay_bounding_boxes, save_image
10
 
11
- from huggingface_hub import login
12
- import spaces
13
 
14
  # Perform login using the token
15
  hf_token = os.getenv("HF_TOKEN")
@@ -18,7 +18,7 @@ login(token=hf_token, add_to_git_credential=True)
18
  import numpy as np
19
  from PIL import Image
20
 
21
- def detect_brain_tumor(image, seg_input, debug: bool = False):
22
  """
23
  Detects a brain tumor in the given image and returns the annotated image.
24
 
@@ -31,27 +31,33 @@ def detect_brain_tumor(image, seg_input, debug: bool = False):
31
  tuple: (numpy array of image, list of (label, (x1, y1, x2, y2)) tuples)
32
  """
33
  if debug:
34
- print(f"Image received")
35
 
36
  # Step 2: Detect brain tumor using owl_v2
37
  prompt = "detect brain tumor"
38
  detections = owl_v2(prompt, image)
39
  if debug:
40
- print(f"Detections: {detections}")
41
 
42
  # Step 3: Overlay bounding boxes on the image
43
  image_with_bboxes = overlay_bounding_boxes(image, detections)
44
  if debug:
45
  print("Bounding boxes overlaid on the image")
46
 
47
- # Prepare annotations for AnnotatedImage output
48
  annotations = []
49
  for detection in detections:
50
  label = detection['label']
51
  score = detection['score']
52
  bbox = detection['bbox']
53
  x1, y1, x2, y2 = bbox
54
- annotations.append((f"{label} {score:.2f}", (x1, y1, x2, y2)))
 
 
 
 
 
 
55
 
56
  # Convert image to numpy array if it's not already
57
  if isinstance(image_with_bboxes, Image.Image):
 
1
+ from huggingface_hub import login
2
+ # import spaces
3
  import os
4
  import time
5
  import gradio as gr
 
10
  from vision_agent.tools import register_tool
11
  from vision_agent.tools import load_image, owl_v2, overlay_bounding_boxes, save_image
12
 
 
 
13
 
14
  # Perform login using the token
15
  hf_token = os.getenv("HF_TOKEN")
 
18
  import numpy as np
19
  from PIL import Image
20
 
21
+ def detect_brain_tumor(image, seg_input, debug: bool = True):
22
  """
23
  Detects a brain tumor in the given image and returns the annotated image.
24
 
 
31
  tuple: (numpy array of image, list of (label, (x1, y1, x2, y2)) tuples)
32
  """
33
  if debug:
34
+ print(f"Image received, shape: {image.shape}")
35
 
36
  # Step 2: Detect brain tumor using owl_v2
37
  prompt = "detect brain tumor"
38
  detections = owl_v2(prompt, image)
39
  if debug:
40
+ print(f"Raw detections: {detections}")
41
 
42
  # Step 3: Overlay bounding boxes on the image
43
  image_with_bboxes = overlay_bounding_boxes(image, detections)
44
  if debug:
45
  print("Bounding boxes overlaid on the image")
46
 
47
+ # Prepare annotations for AnnotatedImage output
48
  annotations = []
49
  for detection in detections:
50
  label = detection['label']
51
  score = detection['score']
52
  bbox = detection['bbox']
53
  x1, y1, x2, y2 = bbox
54
+ # Convert normalized coordinates to pixel coordinates
55
+ height, width = image.shape[:2]
56
+ x1, y1, x2, y2 = int(x1*width), int(y1*height), int(x2*width), int(y2*height)
57
+ annotations.append(((x1, y1, x2, y2), f"{label} {score:.2f}"))
58
+
59
+ if debug:
60
+ print(f"Annotations: {annotations}")
61
 
62
  # Convert image to numpy array if it's not already
63
  if isinstance(image_with_bboxes, Image.Image):