Spaces:
Runtime error
Runtime error
update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,10 @@ import gradio as gr
|
|
| 4 |
from typing import *
|
| 5 |
from pillow_heif import register_heif_opener
|
| 6 |
register_heif_opener()
|
|
|
|
|
|
|
| 7 |
import vision_agent as va
|
| 8 |
-
from vision_agent.tools import
|
| 9 |
|
| 10 |
from vision_agent.tools import load_image, owl_v2, overlay_bounding_boxes, save_image
|
| 11 |
|
|
@@ -21,7 +23,7 @@ def detect_brain_tumor(image, debug: bool = False) -> str:
|
|
| 21 |
Detects a brain tumor in the given image and saves the image with bounding boxes.
|
| 22 |
|
| 23 |
Parameters:
|
| 24 |
-
image: The input image (
|
| 25 |
debug (bool): Flag to enable logging for debugging purposes.
|
| 26 |
|
| 27 |
Returns:
|
|
@@ -30,14 +32,28 @@ def detect_brain_tumor(image, debug: bool = False) -> str:
|
|
| 30 |
# Generate a unique output filename
|
| 31 |
output_path = f"./output/tumor_detection_{int(time.time())}.jpg"
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if debug:
|
| 36 |
-
print(f"Image loaded")
|
| 37 |
|
| 38 |
# Step 2: Detect brain tumor using owl_v2
|
| 39 |
prompt = "detect brain tumor"
|
| 40 |
-
detections = owl_v2(prompt,
|
| 41 |
if debug:
|
| 42 |
print(f"Detections: {detections}")
|
| 43 |
|
|
@@ -53,7 +69,6 @@ def detect_brain_tumor(image, debug: bool = False) -> str:
|
|
| 53 |
|
| 54 |
return output_path
|
| 55 |
|
| 56 |
-
|
| 57 |
# Example usage (uncomment to run):
|
| 58 |
# detect_brain_tumor("/content/drive/MyDrive/kaggle/datasets/brain-tumor-image-dataset-semantic-segmentation_old/train_categories/1385_jpg.rf.3c67cb92e2922dba0e6dba86f69df40b.jpg", "/content/drive/MyDrive/kaggle/datasets/brain-tumor-image-dataset-semantic-segmentation_old/output/1385_jpg.rf.3c67cb92e2922dba0e6dba86f69df40b.jpg", debug=True)
|
| 59 |
|
|
|
|
| 4 |
from typing import *
|
| 5 |
from pillow_heif import register_heif_opener
|
| 6 |
register_heif_opener()
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import numpy as np
|
| 9 |
import vision_agent as va
|
| 10 |
+
from vision_agent.tools import owl_v2, overlay_bounding_boxes, save_image
|
| 11 |
|
| 12 |
from vision_agent.tools import load_image, owl_v2, overlay_bounding_boxes, save_image
|
| 13 |
|
|
|
|
| 23 |
Detects a brain tumor in the given image and saves the image with bounding boxes.
|
| 24 |
|
| 25 |
Parameters:
|
| 26 |
+
image: The input image (can be PIL Image, numpy array, or file path).
|
| 27 |
debug (bool): Flag to enable logging for debugging purposes.
|
| 28 |
|
| 29 |
Returns:
|
|
|
|
| 32 |
# Generate a unique output filename
|
| 33 |
output_path = f"./output/tumor_detection_{int(time.time())}.jpg"
|
| 34 |
|
| 35 |
+
# Ensure image is in the correct format
|
| 36 |
+
if isinstance(image, str):
|
| 37 |
+
# If image is a file path
|
| 38 |
+
image = Image.open(image)
|
| 39 |
+
elif isinstance(image, np.ndarray):
|
| 40 |
+
# If image is already a numpy array
|
| 41 |
+
image = Image.fromarray(image)
|
| 42 |
+
elif not isinstance(image, Image.Image):
|
| 43 |
+
raise ValueError("Unsupported image type. Please provide a PIL Image, numpy array, or file path.")
|
| 44 |
+
|
| 45 |
+
# Convert to RGB if it's not
|
| 46 |
+
image = image.convert('RGB')
|
| 47 |
+
|
| 48 |
+
# Convert PIL Image to numpy array for owl_v2
|
| 49 |
+
image_array = np.array(image)
|
| 50 |
+
|
| 51 |
if debug:
|
| 52 |
+
print(f"Image loaded and converted to numpy array of shape {image_array.shape}")
|
| 53 |
|
| 54 |
# Step 2: Detect brain tumor using owl_v2
|
| 55 |
prompt = "detect brain tumor"
|
| 56 |
+
detections = owl_v2(prompt, image_array)
|
| 57 |
if debug:
|
| 58 |
print(f"Detections: {detections}")
|
| 59 |
|
|
|
|
| 69 |
|
| 70 |
return output_path
|
| 71 |
|
|
|
|
| 72 |
# Example usage (uncomment to run):
|
| 73 |
# detect_brain_tumor("/content/drive/MyDrive/kaggle/datasets/brain-tumor-image-dataset-semantic-segmentation_old/train_categories/1385_jpg.rf.3c67cb92e2922dba0e6dba86f69df40b.jpg", "/content/drive/MyDrive/kaggle/datasets/brain-tumor-image-dataset-semantic-segmentation_old/output/1385_jpg.rf.3c67cb92e2922dba0e6dba86f69df40b.jpg", debug=True)
|
| 74 |
|