File size: 1,564 Bytes
8b36516
 
 
 
 
 
 
 
 
 
 
 
 
 
fc754ef
8b36516
 
fc754ef
8b36516
fc754ef
 
8b36516
 
fc754ef
8b36516
fc754ef
 
 
 
8b36516
 
 
 
fc754ef
 
 
 
 
 
 
 
 
 
 
 
 
 
8b36516
 
 
fc754ef
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from typing import List
import cv2
from PIL import Image
import numpy as np
from io import BytesIO
import mediapipe as mp

app = FastAPI()

# Initialize MediaPipe Face Detection
mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)

def buscar_existe(image):
    # Convert the image to RGB (MediaPipe requires RGB input)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Process the image
    results = face_detection.process(image_rgb)

    # Check if any faces were detected
    if results.detections:
        return "si"
    else:
        return "no"

# Ruta de predicción
@app.post('/predict/')
async def predict(file: UploadFile = File(...)):
    try:
        # Read the file
        contents = await file.read()
        image = Image.open(BytesIO(contents))
        
        # Convert PIL Image to numpy array
        image_np = np.array(image)
        
        # If the image is RGB, convert to BGR (OpenCV uses BGR)
        if image_np.shape[-1] == 3:
            image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
        
        # Perform face detection
        prediction = buscar_existe(image_np)
        
        return {"prediction": prediction}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/")
async def root():
    return {"message": "Face Detection API is running"}