Upload 4 files
Browse files- Dockerfile +16 -0
- app.py +81 -0
- haarcascade_frontalface_default.xml +0 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Usa una imagen base de Python
|
| 2 |
+
FROM python:3.12.7
|
| 3 |
+
# Establece el directorio de trabajo
|
| 4 |
+
WORKDIR /code
|
| 5 |
+
|
| 6 |
+
# Copia los archivos necesarios al contenedor
|
| 7 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 8 |
+
RUN pip install --no-cache-dir -r /code/requirements.txt
|
| 9 |
+
RUN pip install fastapi uvicorn
|
| 10 |
+
|
| 11 |
+
COPY . .
|
| 12 |
+
|
| 13 |
+
RUN chmod -R 777 /code
|
| 14 |
+
|
| 15 |
+
# Comando para ejecutar la aplicaci贸n
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Query
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import List
|
| 5 |
+
import cv2
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
import requests
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
def buscar_existe(image_url):
|
| 14 |
+
try:
|
| 15 |
+
# Descargar la imagen desde la URL
|
| 16 |
+
response = requests.get(image_url)
|
| 17 |
+
image = Image.open(BytesIO(response.content))
|
| 18 |
+
image = np.asarray(image)
|
| 19 |
+
|
| 20 |
+
existe = "NO"
|
| 21 |
+
print("Imagen shape: ", image.shape)
|
| 22 |
+
|
| 23 |
+
# Usar cascada de detecci贸n facial
|
| 24 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 25 |
+
|
| 26 |
+
# Convertir a escala de grises
|
| 27 |
+
if len(image.shape) == 3:
|
| 28 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 29 |
+
else:
|
| 30 |
+
gray = image
|
| 31 |
+
|
| 32 |
+
# Detectar rostros
|
| 33 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4, minSize=(30, 30))
|
| 34 |
+
|
| 35 |
+
if len(faces) > 0:
|
| 36 |
+
existe = "SI"
|
| 37 |
+
print(f"Se detectaron {len(faces)} rostro(s)")
|
| 38 |
+
else:
|
| 39 |
+
print("No se detectaron rostros")
|
| 40 |
+
|
| 41 |
+
return existe
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error procesando imagen: {str(e)}")
|
| 44 |
+
return "NO"
|
| 45 |
+
|
| 46 |
+
# Ruta para evaluar imagen por URL
|
| 47 |
+
@app.post('/predict/')
|
| 48 |
+
async def predict_from_url(image_url: str):
|
| 49 |
+
try:
|
| 50 |
+
prediction = buscar_existe(image_url)
|
| 51 |
+
return {"prediction": prediction}
|
| 52 |
+
except Exception as e:
|
| 53 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 54 |
+
|
| 55 |
+
# Ruta alternativa para subir archivo
|
| 56 |
+
@app.post('/predict_file/')
|
| 57 |
+
async def predict_from_file(file: UploadFile = File(...)):
|
| 58 |
+
try:
|
| 59 |
+
image = Image.open(BytesIO(await file.read()))
|
| 60 |
+
image = np.asarray(image)
|
| 61 |
+
|
| 62 |
+
existe = "NO"
|
| 63 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 64 |
+
|
| 65 |
+
if len(image.shape) == 3:
|
| 66 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 67 |
+
else:
|
| 68 |
+
gray = image
|
| 69 |
+
|
| 70 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4, minSize=(30, 30))
|
| 71 |
+
|
| 72 |
+
if len(faces) > 0:
|
| 73 |
+
existe = "SI"
|
| 74 |
+
|
| 75 |
+
return {"prediction": existe}
|
| 76 |
+
except Exception as e:
|
| 77 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 78 |
+
|
| 79 |
+
@app.get("/")
|
| 80 |
+
async def root():
|
| 81 |
+
return {"message": "Servicio de detecci贸n de rostros funcionando"}
|
haarcascade_frontalface_default.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
numpy
|
| 3 |
+
pydantic
|
| 4 |
+
opencv-python-headless
|
| 5 |
+
uvicorn[standard]
|
| 6 |
+
python-multipart
|
| 7 |
+
pillow
|