Upload 3 files
Browse files- Dockerfile (1) +20 -0
- app (1).py +42 -0
- requirements (1).txt +6 -0
Dockerfile (1)
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Usa una imagen base de Python
|
2 |
+
FROM python:3.11.0
|
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 apt-get update && apt-get install -y libgl1-mesa-glx
|
9 |
+
RUN pip install --no-cache-dir -r /code/requirements.txt
|
10 |
+
RUN pip install fastapi uvicorn pillow opencv-python
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
COPY . .
|
15 |
+
|
16 |
+
RUN chmod -R 777 /code
|
17 |
+
EXPOSE 7860
|
18 |
+
|
19 |
+
# Comando para ejecutar la aplicaci贸n
|
20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app (1).py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
@app.post("/detect/")
|
11 |
+
async def detect_face(file: UploadFile = File(...)):
|
12 |
+
try:
|
13 |
+
image_bytes = await file.read()
|
14 |
+
image = Image.open(io.BytesIO(image_bytes))
|
15 |
+
img_np = np.array(image)
|
16 |
+
|
17 |
+
if img_np.shape[2] == 4:
|
18 |
+
img_np = cv2.cvtColor(img_np, cv2.COLOR_BGRA2BGR)
|
19 |
+
|
20 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
21 |
+
gray = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
|
22 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
23 |
+
|
24 |
+
if len(faces) == 0:
|
25 |
+
raise HTTPException(status_code=404, detail="No se detectaron rostros en la imagen.")
|
26 |
+
|
27 |
+
for (x, y, w, h) in faces:
|
28 |
+
cv2.rectangle(img_np, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
29 |
+
|
30 |
+
result_image = Image.fromarray(cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB))
|
31 |
+
img_byte_arr = io.BytesIO()
|
32 |
+
result_image.save(img_byte_arr, format='JPEG')
|
33 |
+
img_byte_arr = img_byte_arr.getvalue()
|
34 |
+
|
35 |
+
return {
|
36 |
+
"message": "Rostros detectados",
|
37 |
+
"rostros": len(faces),
|
38 |
+
"imagen_base64": base64.b64encode(img_byte_arr).decode('utf-8')
|
39 |
+
}
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements (1).txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
opencv-python
|
4 |
+
numpy
|
5 |
+
Pillow
|
6 |
+
python-multipart
|