CoolFace
Apppublic

AngelGS23/Cara

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py53 linesDownload Raw Back to root
1from flask import Flask, request, jsonify2import requests3import io4from PIL import Image5import base646 7app = Flask(__name__)8 9# Reemplaza con la URL de tu modelo de Hugging Face10HF_API_URL = "https://api-inference.huggingface.co/models/face-detection"11 12def detect_face(image_path):13    """Envía la imagen a Hugging Face para detectar rostros"""14    with open(image_path, "rb") as f:15        img_bytes = f.read()16 17    # Envía la solicitud POST al modelo de Hugging Face18    response = requests.post(HF_API_URL, headers=headers, files={"file": img_bytes})19    result = response.json()20 21    # Si el resultado contiene 'error', lo maneja22    if 'error' in result:23        return False24 25    # Verifica si se detectó algún rostro en la imagen26    if result and len(result) > 0:27        return True28    return False29 30@app.route('/detect-face', methods=['POST'])31def detect_face_route():32    """Ruta para subir la imagen y detectar si hay un rostro"""33    if 'file' not in request.files:34        return jsonify({"error": "No file part"}), 40035 36    file = request.files['file']37    if file.filename == '':38        return jsonify({"error": "No selected file"}), 40039 40    image_path = f"./uploads/{file.filename}"41    file.save(image_path)42 43    # Detecta si hay un rostro en la imagen44    face_detected = detect_face(image_path)45 46    if face_detected:47        return jsonify({"message": "Face detected in the image!"}), 20048    else:49        return jsonify({"message": "No face detected in the image."}), 20050 51if __name__ == "__main__":52    app.run(debug=True, host="0.0.0.0", port=5000)53