Spaces:
Runtime error
Runtime error
Maicol2001
commited on
Commit
•
d386553
1
Parent(s):
d1472d7
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,37 @@
|
|
1 |
-
from
|
2 |
-
from
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
@app.post("/predict/")
|
15 |
-
async def predict(data: InputData):
|
16 |
-
print(f"Data: {data}")
|
17 |
-
try:
|
18 |
-
# Convertir la lista de entrada a un array de NumPy para la predicción
|
19 |
-
input_data = data.data
|
20 |
-
print(input_data)
|
21 |
-
a = input_data[0]
|
22 |
-
b = input_data[1]
|
23 |
-
c = sumar(a,b)
|
24 |
-
prediction = c
|
25 |
-
#return {"prediction": prediction.tolist()}
|
26 |
-
return {"prediction": prediction}
|
27 |
-
except Exception as e:
|
28 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
1 |
+
from transformers import DetrForObjectDetection, DetrFeatureExtractor
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
|
5 |
+
# Cargar el modelo y el extractor de características pre-entrenados
|
6 |
+
model = DetrForObjectDetection.from_pretrained('facebook/detr-resnet-50')
|
7 |
+
feature_extractor = DetrFeatureExtractor.from_pretrained('facebook/detr-resnet-50')
|
8 |
|
9 |
+
def object_exists(image_url):
|
10 |
+
# Cargar la imagen desde la URL
|
11 |
+
image = Image.open(requests.get(image_url, stream=True).raw)
|
12 |
+
|
13 |
+
# Preparar las entradas
|
14 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
15 |
+
|
16 |
+
# Pasar por el modelo
|
17 |
+
outputs = model(**inputs)
|
18 |
+
|
19 |
+
# Obtener las probabilidades de clase
|
20 |
+
logits = outputs.logits
|
21 |
+
probas = logits.softmax(-1)[0, :, :-1]
|
22 |
+
|
23 |
+
# Establecer un umbral de confianza
|
24 |
+
threshold = 0.9
|
25 |
+
|
26 |
+
# Encontrar índices de clases con probabilidad por encima del umbral
|
27 |
+
keep = probas.max(-1).values > threshold
|
28 |
+
|
29 |
+
if keep.sum() > 0:
|
30 |
+
print("Existe un objeto en la imagen.")
|
31 |
+
else:
|
32 |
+
print("No existe un objeto en la imagen.")
|
33 |
|
34 |
+
if __name__ == "__main__":
|
35 |
+
# Reemplazar con la URL de tu imagen subida a Hugging Face
|
36 |
+
image_url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beach.png'
|
37 |
+
object_exists(image_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|