Spaces:
Sleeping
Sleeping
Kesheratmex
commited on
Commit
·
98eefdf
1
Parent(s):
2cf4b9b
**Add Grounding DINO zero‑shot detection fallback and logging**
Browse filesImplemented a Grounding DINO fallback for zero‑shot object detection in `GPTOSSWrapper`, added detailed debug prints, updated comments, and introduced necessary imports. Updated `app.py` to use the new detection logic and added a README for vision‑model usage.
- README_VISION_MODELS.md +151 -0
- app.py +9 -4
- gptoss_wrapper.py +270 -9
- requirements.txt +9 -3
README_VISION_MODELS.md
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 🎯 KESHERAT AI - Detección Zero-Shot con OWL-V2 + Grounding DINO
|
2 |
+
|
3 |
+
## 🚀 **Nuevo Sistema de Detección**
|
4 |
+
|
5 |
+
Hemos migrado de YOLO a un sistema de **detección zero-shot** que puede encontrar cualquier defecto que describas en texto, sin necesidad de entrenamiento previo.
|
6 |
+
|
7 |
+
### **🔧 Modelos Utilizados:**
|
8 |
+
|
9 |
+
#### **1. Grounding DINO (Primario)**
|
10 |
+
- **Modelo**: `IDEA-Research/grounding-dino-base`
|
11 |
+
- **Ventajas**: Excelente para detección zero-shot
|
12 |
+
- **Uso**: Busca defectos usando descripciones en texto natural
|
13 |
+
|
14 |
+
#### **2. OWL-V2 (Respaldo)**
|
15 |
+
- **Modelo**: `google/owlv2-large-patch14-ensemble`
|
16 |
+
- **Ventajas**: Robusto y confiable
|
17 |
+
- **Uso**: Se activa si Grounding DINO falla
|
18 |
+
|
19 |
+
#### **3. GPT Vision (Análisis)**
|
20 |
+
- **Modelos**: GPT-4 Vision o BLIP/LLaVA
|
21 |
+
- **Uso**: Análisis visual detallado en español
|
22 |
+
|
23 |
+
## 🎯 **Consultas de Detección**
|
24 |
+
|
25 |
+
El sistema busca estos defectos automáticamente:
|
26 |
+
|
27 |
+
```python
|
28 |
+
DEFECT_QUERIES = [
|
29 |
+
"crack", "grieta", "fisura", # Grietas
|
30 |
+
"erosion", "erosión", "desgaste", # Erosión
|
31 |
+
"dirt", "suciedad", "mancha", # Suciedad
|
32 |
+
"damage", "daño", "impacto", # Daños
|
33 |
+
"corrosion", "corrosión", "oxidación", # Corrosión
|
34 |
+
"hole", "agujero", "perforación", # Agujeros
|
35 |
+
"stain", "mancha", "decoloración", # Manchas
|
36 |
+
"wear", "desgaste", "deterioro", # Desgaste
|
37 |
+
"lightning damage", "daño por rayo", # Rayos
|
38 |
+
"bird strike", "impacto de ave" # Impactos
|
39 |
+
]
|
40 |
+
```
|
41 |
+
|
42 |
+
## 🛠️ **Configuración en HF Space**
|
43 |
+
|
44 |
+
### **Variables de Entorno (Opcionales):**
|
45 |
+
|
46 |
+
```bash
|
47 |
+
# Para GPT Vision (opcional)
|
48 |
+
HUGGINGFACE_API_TOKEN = tu_token_hf
|
49 |
+
VISION_MODEL_ID = Salesforce/blip-image-captioning-base
|
50 |
+
|
51 |
+
# Para OpenAI GPT-4 Vision (opcional)
|
52 |
+
OPENAI_API_KEY = tu_openai_key
|
53 |
+
```
|
54 |
+
|
55 |
+
### **Dependencias Requeridas:**
|
56 |
+
|
57 |
+
```
|
58 |
+
transformers>=4.35.0
|
59 |
+
torch==2.2.0
|
60 |
+
torchvision
|
61 |
+
accelerate
|
62 |
+
sentencepiece
|
63 |
+
Pillow
|
64 |
+
```
|
65 |
+
|
66 |
+
## 🔍 **Flujo de Trabajo**
|
67 |
+
|
68 |
+
1. **Usuario sube imagen/video**
|
69 |
+
2. **Grounding DINO** busca defectos usando texto
|
70 |
+
3. **OWL-V2** (respaldo) si Grounding DINO falla
|
71 |
+
4. **GPT Vision** analiza la imagen completa
|
72 |
+
5. **Sistema** combina detecciones + análisis
|
73 |
+
6. **Usuario** recibe resultado en español
|
74 |
+
|
75 |
+
## 💡 **Ventajas del Nuevo Sistema**
|
76 |
+
|
77 |
+
### **vs YOLO:**
|
78 |
+
- ✅ **Zero-shot**: No necesita entrenamiento
|
79 |
+
- ✅ **Flexible**: Busca cualquier defecto que describas
|
80 |
+
- ✅ **Multilingüe**: Funciona en español e inglés
|
81 |
+
- ✅ **Actualizable**: Agregar nuevos defectos es fácil
|
82 |
+
|
83 |
+
### **Capacidades:**
|
84 |
+
- 🔍 **Detección precisa** de defectos específicos
|
85 |
+
- 🎯 **Búsqueda por texto** ("grieta en el borde")
|
86 |
+
- 🌍 **Multilingüe** (español/inglés)
|
87 |
+
- 🧠 **Análisis inteligente** con GPT
|
88 |
+
- 📊 **Reportes detallados** en PDF/MD/JSON
|
89 |
+
|
90 |
+
## 🚀 **Uso en HF Space**
|
91 |
+
|
92 |
+
### **1. Subir Imagen/Video**
|
93 |
+
- Formatos: JPG, PNG, MP4, AVI, MOV
|
94 |
+
|
95 |
+
### **2. Detectar Defectos**
|
96 |
+
- Click en "Detectar defectos con OWL-V2 + GPT"
|
97 |
+
- El sistema automáticamente:
|
98 |
+
- Busca todos los defectos de la lista
|
99 |
+
- Analiza visualmente con GPT
|
100 |
+
- Genera reporte completo
|
101 |
+
|
102 |
+
### **3. Ver Resultados**
|
103 |
+
- **Imagen anotada** con detecciones marcadas
|
104 |
+
- **Análisis de GPT** en español
|
105 |
+
- **Reportes** descargables (PDF/MD/JSON)
|
106 |
+
|
107 |
+
## 🔧 **Personalización**
|
108 |
+
|
109 |
+
### **Agregar Nuevos Defectos:**
|
110 |
+
Edita `DEFECT_QUERIES` en `app.py`:
|
111 |
+
|
112 |
+
```python
|
113 |
+
DEFECT_QUERIES = [
|
114 |
+
# Defectos existentes...
|
115 |
+
"nuevo_defecto", "new defect",
|
116 |
+
"otro_problema", "another issue"
|
117 |
+
]
|
118 |
+
```
|
119 |
+
|
120 |
+
### **Ajustar Sensibilidad:**
|
121 |
+
Modifica el threshold en la detección:
|
122 |
+
|
123 |
+
```python
|
124 |
+
# Más sensible (más detecciones)
|
125 |
+
threshold = 0.05
|
126 |
+
|
127 |
+
# Menos sensible (menos detecciones)
|
128 |
+
threshold = 0.2
|
129 |
+
```
|
130 |
+
|
131 |
+
## 🎯 **Resultado Esperado**
|
132 |
+
|
133 |
+
```markdown
|
134 |
+
## 🔍 Análisis Visual Directo de la Pala
|
135 |
+
|
136 |
+
**Estado General:** Bueno con mantenimiento menor requerido
|
137 |
+
|
138 |
+
**Detecciones Automáticas:**
|
139 |
+
- Dirt (suciedad): 2 áreas detectadas
|
140 |
+
- Erosion (erosión): 1 área en borde de ataque
|
141 |
+
|
142 |
+
**Análisis de GPT:**
|
143 |
+
La superficie muestra condición general buena con dos áreas
|
144 |
+
de acumulación de suciedad claramente visibles...
|
145 |
+
|
146 |
+
**Recomendaciones:**
|
147 |
+
- Limpieza programada en 2 semanas
|
148 |
+
- Inspección de erosión en 3 meses
|
149 |
+
```
|
150 |
+
|
151 |
+
¡El sistema ahora es mucho más potente y flexible! 🎉
|
app.py
CHANGED
@@ -146,15 +146,17 @@ def infer_media(media_path, conf=0.1, out_res="720p"):
|
|
146 |
writer = None
|
147 |
counts = {}
|
148 |
|
149 |
-
# Configurar OWL-V2
|
150 |
try:
|
151 |
GPTClass = _load_gptoss_wrapper()
|
152 |
if GPTClass:
|
153 |
wrapper = GPTClass()
|
|
|
154 |
else:
|
155 |
wrapper = None
|
|
|
156 |
except Exception as e:
|
157 |
-
print(f"Error configurando
|
158 |
wrapper = None
|
159 |
|
160 |
# Procesar frames con OWL-V2 (cada 30 frames para eficiencia)
|
@@ -226,17 +228,20 @@ def infer_media(media_path, conf=0.1, out_res="720p"):
|
|
226 |
elif ext in [".jpg", ".jpeg", ".png", ".bmp"]:
|
227 |
img = cv2.imread(media_path)
|
228 |
|
229 |
-
# Usar
|
230 |
try:
|
231 |
GPTClass = _load_gptoss_wrapper()
|
232 |
if GPTClass:
|
233 |
wrapper = GPTClass()
|
|
|
234 |
detection_result = wrapper.detect_objects_owlv2(media_path, DEFECT_QUERIES, threshold=conf)
|
235 |
detections = detection_result.get("detections", [])
|
|
|
236 |
else:
|
|
|
237 |
detections = []
|
238 |
except Exception as e:
|
239 |
-
print(f"Error en detección
|
240 |
detections = []
|
241 |
|
242 |
counts = {}
|
|
|
146 |
writer = None
|
147 |
counts = {}
|
148 |
|
149 |
+
# Configurar modelos de detección (OWL-V2 + Grounding DINO)
|
150 |
try:
|
151 |
GPTClass = _load_gptoss_wrapper()
|
152 |
if GPTClass:
|
153 |
wrapper = GPTClass()
|
154 |
+
print("Wrapper de detección configurado correctamente")
|
155 |
else:
|
156 |
wrapper = None
|
157 |
+
print("No se pudo cargar el wrapper de detección")
|
158 |
except Exception as e:
|
159 |
+
print(f"Error configurando modelos de detección: {e}")
|
160 |
wrapper = None
|
161 |
|
162 |
# Procesar frames con OWL-V2 (cada 30 frames para eficiencia)
|
|
|
228 |
elif ext in [".jpg", ".jpeg", ".png", ".bmp"]:
|
229 |
img = cv2.imread(media_path)
|
230 |
|
231 |
+
# Usar modelos de detección zero-shot (Grounding DINO + OWL-V2)
|
232 |
try:
|
233 |
GPTClass = _load_gptoss_wrapper()
|
234 |
if GPTClass:
|
235 |
wrapper = GPTClass()
|
236 |
+
print(f"Iniciando detección zero-shot en imagen: {media_path}")
|
237 |
detection_result = wrapper.detect_objects_owlv2(media_path, DEFECT_QUERIES, threshold=conf)
|
238 |
detections = detection_result.get("detections", [])
|
239 |
+
print(f"Detecciones encontradas: {len(detections)}")
|
240 |
else:
|
241 |
+
print("Wrapper no disponible, sin detecciones")
|
242 |
detections = []
|
243 |
except Exception as e:
|
244 |
+
print(f"Error en detección zero-shot: {e}")
|
245 |
detections = []
|
246 |
|
247 |
counts = {}
|
gptoss_wrapper.py
CHANGED
@@ -23,6 +23,8 @@ import os
|
|
23 |
import time
|
24 |
import requests
|
25 |
import base64
|
|
|
|
|
26 |
from typing import Optional
|
27 |
|
28 |
|
@@ -115,7 +117,8 @@ class GPTOSSWrapper:
|
|
115 |
|
116 |
def detect_objects_owlv2(self, image_path: str, text_queries: list, threshold: float = 0.1) -> dict:
|
117 |
"""
|
118 |
-
Detect objects in image using OWL-V2 zero-shot detection with text queries.
|
|
|
119 |
|
120 |
Args:
|
121 |
image_path: Path to the image file
|
@@ -126,12 +129,24 @@ class GPTOSSWrapper:
|
|
126 |
Dictionary with detections: {"detections": [{"label": str, "confidence": float, "bbox": [x1,y1,x2,y2]}, ...]}
|
127 |
|
128 |
Raises:
|
129 |
-
RuntimeError if
|
130 |
"""
|
131 |
-
|
132 |
-
raise RuntimeError("OWL-V2 detection requires Hugging Face token. Set HUGGINGFACE_API_TOKEN.")
|
133 |
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
def _generate_openai(self, prompt: str, max_tokens: int, temperature: float) -> str:
|
137 |
if not self.openai_key:
|
@@ -435,6 +450,252 @@ class GPTOSSWrapper:
|
|
435 |
except Exception as e:
|
436 |
raise RuntimeError(f"Hugging Face Vision API call failed: {e}")
|
437 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
438 |
def _detect_owlv2_hf(self, image_path: str, text_queries: list, threshold: float) -> dict:
|
439 |
"""
|
440 |
Detect objects using OWL-V2 via Hugging Face Inference API.
|
@@ -445,12 +706,12 @@ class GPTOSSWrapper:
|
|
445 |
except Exception as e:
|
446 |
raise RuntimeError(f"Failed to read image file {image_path}: {e}")
|
447 |
|
448 |
-
#
|
449 |
-
|
450 |
-
url = f"https://api-inference.huggingface.co/models/{
|
451 |
headers = {"Authorization": f"Bearer {self.hf_token}"}
|
452 |
|
453 |
-
# Prepare payload for
|
454 |
# OWL-V2 expects image as binary data and text queries as parameters
|
455 |
payload = {
|
456 |
"parameters": {
|
|
|
23 |
import time
|
24 |
import requests
|
25 |
import base64
|
26 |
+
import torch
|
27 |
+
from PIL import Image
|
28 |
from typing import Optional
|
29 |
|
30 |
|
|
|
117 |
|
118 |
def detect_objects_owlv2(self, image_path: str, text_queries: list, threshold: float = 0.1) -> dict:
|
119 |
"""
|
120 |
+
Detect objects in image using OWL-V2 or Grounding DINO zero-shot detection with text queries.
|
121 |
+
Runs on HF GPU when available.
|
122 |
|
123 |
Args:
|
124 |
image_path: Path to the image file
|
|
|
129 |
Dictionary with detections: {"detections": [{"label": str, "confidence": float, "bbox": [x1,y1,x2,y2]}, ...]}
|
130 |
|
131 |
Raises:
|
132 |
+
RuntimeError if models not available or detection fails
|
133 |
"""
|
134 |
+
print(f"Starting zero-shot detection with {len(text_queries)} queries")
|
|
|
135 |
|
136 |
+
# Try Grounding DINO first (usually better for zero-shot), then OWL-V2 as fallback
|
137 |
+
try:
|
138 |
+
print("Attempting Grounding DINO detection...")
|
139 |
+
return self._detect_grounding_dino(image_path, text_queries, threshold)
|
140 |
+
except Exception as e:
|
141 |
+
print(f"Grounding DINO failed: {e}")
|
142 |
+
print("Falling back to OWL-V2...")
|
143 |
+
try:
|
144 |
+
return self._detect_owlv2_local(image_path, text_queries, threshold)
|
145 |
+
except Exception as e2:
|
146 |
+
print(f"OWL-V2 also failed: {e2}")
|
147 |
+
# Return empty detections instead of failing completely
|
148 |
+
print("Both models failed, returning empty detections")
|
149 |
+
return {"detections": []}
|
150 |
|
151 |
def _generate_openai(self, prompt: str, max_tokens: int, temperature: float) -> str:
|
152 |
if not self.openai_key:
|
|
|
450 |
except Exception as e:
|
451 |
raise RuntimeError(f"Hugging Face Vision API call failed: {e}")
|
452 |
|
453 |
+
def _detect_grounding_dino(self, image_path: str, text_queries: list, threshold: float) -> dict:
|
454 |
+
"""
|
455 |
+
Detect objects using Grounding DINO running on HF GPU.
|
456 |
+
"""
|
457 |
+
try:
|
458 |
+
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
|
459 |
+
|
460 |
+
# Load Grounding DINO model (will use HF GPU)
|
461 |
+
model_id = "IDEA-Research/grounding-dino-base"
|
462 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
463 |
+
|
464 |
+
print(f"Loading Grounding DINO on device: {device}")
|
465 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
466 |
+
model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device)
|
467 |
+
|
468 |
+
# Load image
|
469 |
+
image = Image.open(image_path)
|
470 |
+
|
471 |
+
# Prepare text queries (VERY important: lowercase + end with dot)
|
472 |
+
text = ". ".join([query.lower() for query in text_queries]) + "."
|
473 |
+
print(f"Grounding DINO text query: {text}")
|
474 |
+
|
475 |
+
# Process inputs
|
476 |
+
inputs = processor(images=image, text=text, return_tensors="pt").to(device)
|
477 |
+
|
478 |
+
# Run inference
|
479 |
+
with torch.no_grad():
|
480 |
+
outputs = model(**inputs)
|
481 |
+
|
482 |
+
# Post-process results
|
483 |
+
results = processor.post_process_grounded_object_detection(
|
484 |
+
outputs,
|
485 |
+
inputs.input_ids,
|
486 |
+
box_threshold=threshold,
|
487 |
+
text_threshold=0.3,
|
488 |
+
target_sizes=[image.size[::-1]]
|
489 |
+
)
|
490 |
+
|
491 |
+
# Convert to our format
|
492 |
+
detections = []
|
493 |
+
if results and len(results) > 0:
|
494 |
+
result = results[0]
|
495 |
+
boxes = result.get("boxes", [])
|
496 |
+
scores = result.get("scores", [])
|
497 |
+
labels = result.get("labels", [])
|
498 |
+
|
499 |
+
print(f"Grounding DINO found {len(boxes)} detections")
|
500 |
+
|
501 |
+
for box, score, label_idx in zip(boxes, scores, labels):
|
502 |
+
if score >= threshold:
|
503 |
+
x1, y1, x2, y2 = box.tolist()
|
504 |
+
label = text_queries[label_idx] if label_idx < len(text_queries) else "unknown"
|
505 |
+
|
506 |
+
detections.append({
|
507 |
+
"label": label,
|
508 |
+
"confidence": float(score),
|
509 |
+
"bbox": [int(x1), int(y1), int(x2), int(y2)]
|
510 |
+
})
|
511 |
+
|
512 |
+
return {"detections": detections}
|
513 |
+
|
514 |
+
except Exception as e:
|
515 |
+
raise RuntimeError(f"Grounding DINO detection failed: {e}")
|
516 |
+
|
517 |
+
def _detect_owlv2_local(self, image_path: str, text_queries: list, threshold: float) -> dict:
|
518 |
+
"""
|
519 |
+
Detect objects using OWL-V2 running on HF GPU.
|
520 |
+
"""
|
521 |
+
try:
|
522 |
+
from transformers import Owlv2Processor, Owlv2ForObjectDetection
|
523 |
+
|
524 |
+
# Load OWL-V2 model (will use HF GPU)
|
525 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
526 |
+
print(f"Loading OWL-V2 on device: {device}")
|
527 |
+
|
528 |
+
processor = Owlv2Processor.from_pretrained("google/owlv2-large-patch14-ensemble")
|
529 |
+
model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14-ensemble").to(device)
|
530 |
+
|
531 |
+
# Load image
|
532 |
+
image = Image.open(image_path)
|
533 |
+
|
534 |
+
# Prepare text queries (format: [["query1", "query2", ...]])
|
535 |
+
texts = [text_queries]
|
536 |
+
print(f"OWL-V2 text queries: {texts}")
|
537 |
+
|
538 |
+
# Process inputs
|
539 |
+
inputs = processor(text=texts, images=image, return_tensors="pt").to(device)
|
540 |
+
|
541 |
+
# Run inference
|
542 |
+
with torch.no_grad():
|
543 |
+
outputs = model(**inputs)
|
544 |
+
|
545 |
+
# Target image sizes for rescaling
|
546 |
+
target_sizes = torch.Tensor([image.size[::-1]])
|
547 |
+
|
548 |
+
# Post-process results
|
549 |
+
results = processor.post_process_object_detection(
|
550 |
+
outputs=outputs,
|
551 |
+
target_sizes=target_sizes,
|
552 |
+
threshold=threshold
|
553 |
+
)
|
554 |
+
|
555 |
+
# Convert to our format
|
556 |
+
detections = []
|
557 |
+
if results and len(results) > 0:
|
558 |
+
result = results[0]
|
559 |
+
boxes = result.get("boxes", [])
|
560 |
+
scores = result.get("scores", [])
|
561 |
+
labels = result.get("labels", [])
|
562 |
+
|
563 |
+
print(f"OWL-V2 found {len(boxes)} detections")
|
564 |
+
|
565 |
+
for box, score, label_idx in zip(boxes, scores, labels):
|
566 |
+
if score >= threshold:
|
567 |
+
x1, y1, x2, y2 = box.tolist()
|
568 |
+
label = text_queries[label_idx] if label_idx < len(text_queries) else "unknown"
|
569 |
+
|
570 |
+
detections.append({
|
571 |
+
"label": label,
|
572 |
+
"confidence": float(score),
|
573 |
+
"bbox": [int(x1), int(y1), int(x2), int(y2)]
|
574 |
+
})
|
575 |
+
|
576 |
+
return {"detections": detections}
|
577 |
+
|
578 |
+
except Exception as e:
|
579 |
+
raise RuntimeError(f"OWL-V2 detection failed: {e}")
|
580 |
+
|
581 |
+
def _detect_grounding_dino(self, image_path: str, text_queries: list, threshold: float) -> dict:
|
582 |
+
"""
|
583 |
+
Detect objects using Grounding DINO locally.
|
584 |
+
"""
|
585 |
+
try:
|
586 |
+
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
|
587 |
+
|
588 |
+
# Load Grounding DINO model
|
589 |
+
model_id = "IDEA-Research/grounding-dino-base"
|
590 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
591 |
+
|
592 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
593 |
+
model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device)
|
594 |
+
|
595 |
+
# Load image
|
596 |
+
image = Image.open(image_path)
|
597 |
+
|
598 |
+
# Prepare text queries (VERY important: lowercase + end with dot)
|
599 |
+
text = ". ".join([query.lower() for query in text_queries]) + "."
|
600 |
+
|
601 |
+
# Process inputs
|
602 |
+
inputs = processor(images=image, text=text, return_tensors="pt").to(device)
|
603 |
+
|
604 |
+
# Run inference
|
605 |
+
with torch.no_grad():
|
606 |
+
outputs = model(**inputs)
|
607 |
+
|
608 |
+
# Post-process results
|
609 |
+
results = processor.post_process_grounded_object_detection(
|
610 |
+
outputs,
|
611 |
+
inputs.input_ids,
|
612 |
+
box_threshold=threshold,
|
613 |
+
text_threshold=0.3,
|
614 |
+
target_sizes=[image.size[::-1]]
|
615 |
+
)
|
616 |
+
|
617 |
+
# Convert to our format
|
618 |
+
detections = []
|
619 |
+
if results and len(results) > 0:
|
620 |
+
result = results[0]
|
621 |
+
boxes = result.get("boxes", [])
|
622 |
+
scores = result.get("scores", [])
|
623 |
+
labels = result.get("labels", [])
|
624 |
+
|
625 |
+
for box, score, label_idx in zip(boxes, scores, labels):
|
626 |
+
if score >= threshold:
|
627 |
+
x1, y1, x2, y2 = box.tolist()
|
628 |
+
label = text_queries[label_idx] if label_idx < len(text_queries) else "unknown"
|
629 |
+
|
630 |
+
detections.append({
|
631 |
+
"label": label,
|
632 |
+
"confidence": float(score),
|
633 |
+
"bbox": [int(x1), int(y1), int(x2), int(y2)]
|
634 |
+
})
|
635 |
+
|
636 |
+
return {"detections": detections}
|
637 |
+
|
638 |
+
except Exception as e:
|
639 |
+
raise RuntimeError(f"Grounding DINO detection failed: {e}")
|
640 |
+
|
641 |
+
def _detect_owlv2_local(self, image_path: str, text_queries: list, threshold: float) -> dict:
|
642 |
+
"""
|
643 |
+
Detect objects using OWL-V2 locally.
|
644 |
+
"""
|
645 |
+
try:
|
646 |
+
from transformers import Owlv2Processor, Owlv2ForObjectDetection
|
647 |
+
|
648 |
+
# Load OWL-V2 model
|
649 |
+
processor = Owlv2Processor.from_pretrained("google/owlv2-large-patch14-ensemble")
|
650 |
+
model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-large-patch14-ensemble")
|
651 |
+
|
652 |
+
# Load image
|
653 |
+
image = Image.open(image_path)
|
654 |
+
|
655 |
+
# Prepare text queries (format: [["query1", "query2", ...]])
|
656 |
+
texts = [text_queries]
|
657 |
+
|
658 |
+
# Process inputs
|
659 |
+
inputs = processor(text=texts, images=image, return_tensors="pt")
|
660 |
+
|
661 |
+
# Run inference
|
662 |
+
with torch.no_grad():
|
663 |
+
outputs = model(**inputs)
|
664 |
+
|
665 |
+
# Target image sizes for rescaling
|
666 |
+
target_sizes = torch.Tensor([image.size[::-1]])
|
667 |
+
|
668 |
+
# Post-process results
|
669 |
+
results = processor.post_process_object_detection(
|
670 |
+
outputs=outputs,
|
671 |
+
target_sizes=target_sizes,
|
672 |
+
threshold=threshold
|
673 |
+
)
|
674 |
+
|
675 |
+
# Convert to our format
|
676 |
+
detections = []
|
677 |
+
if results and len(results) > 0:
|
678 |
+
result = results[0]
|
679 |
+
boxes = result.get("boxes", [])
|
680 |
+
scores = result.get("scores", [])
|
681 |
+
labels = result.get("labels", [])
|
682 |
+
|
683 |
+
for box, score, label_idx in zip(boxes, scores, labels):
|
684 |
+
if score >= threshold:
|
685 |
+
x1, y1, x2, y2 = box.tolist()
|
686 |
+
label = text_queries[label_idx] if label_idx < len(text_queries) else "unknown"
|
687 |
+
|
688 |
+
detections.append({
|
689 |
+
"label": label,
|
690 |
+
"confidence": float(score),
|
691 |
+
"bbox": [int(x1), int(y1), int(x2), int(y2)]
|
692 |
+
})
|
693 |
+
|
694 |
+
return {"detections": detections}
|
695 |
+
|
696 |
+
except Exception as e:
|
697 |
+
raise RuntimeError(f"OWL-V2 detection failed: {e}")
|
698 |
+
|
699 |
def _detect_owlv2_hf(self, image_path: str, text_queries: list, threshold: float) -> dict:
|
700 |
"""
|
701 |
Detect objects using OWL-V2 via Hugging Face Inference API.
|
|
|
706 |
except Exception as e:
|
707 |
raise RuntimeError(f"Failed to read image file {image_path}: {e}")
|
708 |
|
709 |
+
# DETR model endpoint (object detection)
|
710 |
+
detr_model = os.getenv("DETR_MODEL_ID", "facebook/detr-resnet-101")
|
711 |
+
url = f"https://api-inference.huggingface.co/models/{detr_model}"
|
712 |
headers = {"Authorization": f"Bearer {self.hf_token}"}
|
713 |
|
714 |
+
# Prepare payload for DETR
|
715 |
# OWL-V2 expects image as binary data and text queries as parameters
|
716 |
payload = {
|
717 |
"parameters": {
|
requirements.txt
CHANGED
@@ -1,9 +1,15 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
2 |
gradio==4.36.1 # 4.x permite auth=
|
3 |
opencv-python-headless
|
4 |
reportlab==3.6.13
|
5 |
requests # For GPT-OSS API calls
|
|
|
6 |
# Fijar NumPy 1.x para compatibilidad con PyTorch 2.2 en ZeroGPU
|
7 |
numpy==1.26.4
|
8 |
-
#
|
9 |
-
|
|
|
|
1 |
+
# Vision models for zero-shot detection
|
2 |
+
transformers>=4.35.0
|
3 |
+
torch==2.2.0
|
4 |
+
torchvision
|
5 |
+
# UI and processing
|
6 |
gradio==4.36.1 # 4.x permite auth=
|
7 |
opencv-python-headless
|
8 |
reportlab==3.6.13
|
9 |
requests # For GPT-OSS API calls
|
10 |
+
Pillow
|
11 |
# Fijar NumPy 1.x para compatibilidad con PyTorch 2.2 en ZeroGPU
|
12 |
numpy==1.26.4
|
13 |
+
# Additional dependencies for vision models
|
14 |
+
accelerate
|
15 |
+
sentencepiece
|