-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (33 loc) · 1.23 KB
/
main.py
File metadata and controls
45 lines (33 loc) · 1.23 KB
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
import cv2
import requests
import os
from flask import Flask, request, jsonify
app = Flask(__name__)
# Ensure model directory exists
MODEL_DIR = "models"
MODEL_PATH = os.path.join(MODEL_DIR, "haarcascade_frontalface_default.xml")
MODEL_URL = "https://github.com/opencv/opencv/raw/4.x/data/haarcascades/haarcascade_frontalface_default.xml"
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
# Download the model if not available
if not os.path.exists(MODEL_PATH):
print("Downloading pre-trained model...")
response = requests.get(MODEL_URL)
with open(MODEL_PATH, "wb") as f:
f.write(response.content)
print("Model ready!")
# Load model
face_cascade = cv2.CascadeClassifier(MODEL_PATH)
@app.route("/detect_faces", methods=["POST"])
def detect_faces():
if "image" not in request.files:
return jsonify({"error": "No image uploaded"}), 400
image_file = request.files["image"]
image_path = "uploaded_image.jpg"
image_file.save(image_path)
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
return jsonify({"faces_detected": len(faces)})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001)