Spaces:
Sleeping
Sleeping
axvg commited on
Commit 路
b942ddb
1
Parent(s): a044aeb
up
Browse files- .gitignore +1 -0
- Dockerfile +1 -4
- app.py +53 -20
- haarcascade_eye.xml +0 -0
- haarcascade_frontalface_default.xml +0 -0
- requirements.txt +1 -1
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
.venv/
|
|
|
|
|
|
| 1 |
.venv/
|
| 2 |
+
__pycache__/
|
Dockerfile
CHANGED
|
@@ -1,15 +1,12 @@
|
|
| 1 |
-
# Usa una imagen base de Python
|
| 2 |
FROM python:3.12.7
|
| 3 |
-
|
| 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
|
|
|
|
|
|
|
| 1 |
FROM python:3.12.7
|
| 2 |
+
|
| 3 |
WORKDIR /code
|
| 4 |
|
|
|
|
| 5 |
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
RUN pip install --no-cache-dir -r /code/requirements.txt
|
| 7 |
RUN pip install fastapi uvicorn
|
| 8 |
|
| 9 |
COPY . .
|
|
|
|
| 10 |
RUN chmod -R 777 /code
|
| 11 |
|
| 12 |
# Comando para ejecutar la aplicaci贸n
|
app.py
CHANGED
|
@@ -1,33 +1,66 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
-
from fastapi.responses import
|
| 3 |
-
from
|
| 4 |
-
from typing import List
|
| 5 |
import cv2
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
from io import BytesIO
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
eyeglasses_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
| 16 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
return existe
|
| 23 |
|
| 24 |
-
# Ruta de predicci贸n
|
| 25 |
@app.post('/predict/')
|
| 26 |
-
async def predict(file: UploadFile = File(...)
|
| 27 |
try:
|
| 28 |
-
image = Image.open(BytesIO(await file.read()))
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
raise HTTPException(
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from typing import List, Dict
|
|
|
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from io import BytesIO
|
| 8 |
+
import base64
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
|
| 13 |
+
def detect_eyes(image: np.ndarray) -> List[Dict[str, str]]:
|
| 14 |
+
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
|
|
|
|
| 15 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 16 |
+
eyes = eye_cascade.detectMultiScale(gray,
|
| 17 |
+
scaleFactor=1.1,
|
| 18 |
+
minNeighbors=5,
|
| 19 |
+
minSize=(30, 30)
|
| 20 |
+
)
|
| 21 |
+
print("Detected eyes:", eyes, type(eyes))
|
| 22 |
+
if len(eyes) == 0:
|
| 23 |
+
return []
|
| 24 |
+
detections = []
|
| 25 |
+
for (x, y, w, h) in eyes:
|
| 26 |
+
# para dibujar rectangulos en ojos detectados
|
| 27 |
+
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 28 |
+
|
| 29 |
+
detections.append({
|
| 30 |
+
"label": "eye",
|
| 31 |
+
"bounding_box": {
|
| 32 |
+
"x": int(x),
|
| 33 |
+
"y": int(y),
|
| 34 |
+
"width": int(w),
|
| 35 |
+
"height": int(h)
|
| 36 |
+
}
|
| 37 |
+
})
|
| 38 |
+
return detections
|
| 39 |
|
|
|
|
| 40 |
|
|
|
|
| 41 |
@app.post('/predict/')
|
| 42 |
+
async def predict(file: UploadFile = File(...)) -> JSONResponse:
|
| 43 |
try:
|
| 44 |
+
image = Image.open(BytesIO(await file.read())).convert("RGB")
|
| 45 |
+
image_np = np.array(image)
|
| 46 |
+
|
| 47 |
+
detections = detect_eyes(image_np)
|
| 48 |
+
if len(detections) == 0:
|
| 49 |
+
return JSONResponse(content={
|
| 50 |
+
"detections": detections,
|
| 51 |
+
"count": 0,
|
| 52 |
+
"image_with_detections": None
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
_, buffer = cv2.imencode('.jpg', image_np)
|
| 56 |
+
image_base64 = base64.b64encode(buffer).decode('utf-8')
|
| 57 |
+
|
| 58 |
+
return JSONResponse(content={
|
| 59 |
+
"detections": detections,
|
| 60 |
+
"count": len(detections),
|
| 61 |
+
"image_with_detections": image_base64
|
| 62 |
+
})
|
| 63 |
except Exception as e:
|
| 64 |
+
raise HTTPException(
|
| 65 |
+
status_code=500,
|
| 66 |
+
detail=f"Error al procesar la imagen: {str(e)}")
|
haarcascade_eye.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
haarcascade_frontalface_default.xml
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
fastapi[
|
| 2 |
numpy
|
| 3 |
pydantic
|
| 4 |
opencv-python-headless
|
|
|
|
| 1 |
+
fastapi[stardard]
|
| 2 |
numpy
|
| 3 |
pydantic
|
| 4 |
opencv-python-headless
|