Spaces:
Running
Running
Update app/caption_model.py
Browse files- app/caption_model.py +32 -32
app/caption_model.py
CHANGED
|
@@ -1,43 +1,43 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
from PIL import Image
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Load model and processor only once at startup
|
| 10 |
-
processor = BlipProcessor.from_pretrained(MODEL_NAME)
|
| 11 |
-
model = BlipForConditionalGeneration.from_pretrained(MODEL_NAME).to(device)
|
| 12 |
-
model.eval()
|
| 13 |
|
| 14 |
def caption_image(image: Image.Image):
|
| 15 |
# Validate input
|
| 16 |
if not isinstance(image, Image.Image) or image.mode not in ('RGB', 'L'):
|
| 17 |
raise ValueError("Input must be a valid PIL Image in RGB or grayscale format")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return {
|
| 41 |
"caption": caption,
|
| 42 |
-
"
|
|
|
|
| 43 |
}
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
|
| 4 |
+
# Load object detection model
|
| 5 |
+
MODEL_NAME = "facebook/detr-resnet-50"
|
| 6 |
+
detector = pipeline("object-detection", model=MODEL_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def caption_image(image: Image.Image):
|
| 9 |
# Validate input
|
| 10 |
if not isinstance(image, Image.Image) or image.mode not in ('RGB', 'L'):
|
| 11 |
raise ValueError("Input must be a valid PIL Image in RGB or grayscale format")
|
| 12 |
|
| 13 |
+
# Run object detection
|
| 14 |
+
results = detector(image)
|
| 15 |
+
|
| 16 |
+
# Track highest score per object
|
| 17 |
+
objects_dict = {}
|
| 18 |
+
for result in results:
|
| 19 |
+
label = result['label']
|
| 20 |
+
score = result['score']
|
| 21 |
+
if label in objects_dict:
|
| 22 |
+
objects_dict[label] = max(objects_dict[label], score)
|
| 23 |
+
else:
|
| 24 |
+
objects_dict[label] = score
|
| 25 |
+
|
| 26 |
+
# Build structured list of objects
|
| 27 |
+
objects_list = [
|
| 28 |
+
{"label": label, "score": round(score, 2)}
|
| 29 |
+
for label, score in sorted(objects_dict.items(), key=lambda x: x[1], reverse=True)
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
# Create readable caption
|
| 33 |
+
detected_objects = [f"{obj['label']} ({obj['score']:.2f})" for obj in objects_list]
|
| 34 |
+
caption = "Detected objects: " + ", ".join(detected_objects) if detected_objects else "No objects detected."
|
| 35 |
+
|
| 36 |
+
# Highest confidence score
|
| 37 |
+
max_confidence = max(objects_dict.values()) if objects_dict else 0.0
|
| 38 |
+
|
| 39 |
return {
|
| 40 |
"caption": caption,
|
| 41 |
+
"objects": objects_list,
|
| 42 |
+
"confidence": round(max_confidence, 2)
|
| 43 |
}
|