Binayak Panigrahi commited on
Commit
ddddd08
·
1 Parent(s): a76167d

Updated some files

Browse files
Files changed (3) hide show
  1. app.py +80 -5
  2. packages.txt +2 -0
  3. requirements.txt +6 -1
app.py CHANGED
@@ -1,7 +1,82 @@
1
- import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import cv2
3
+ import numpy as np
4
+ import os
5
 
6
+ # Optional: FCM (uncomment and configure if you want Python to send push directly)
7
+ # from pyfcm import FCMNotification
8
+ # push_service = FCMNotification(api_key="YOUR_FCM_SERVER_KEY_HERE")
9
 
10
+ app = Flask(__name__)
11
+
12
+ # Load YuNet face detector
13
+ model_path = "face_detection_yunet_2023mar.onnx"
14
+ if not os.path.exists(model_path):
15
+ raise FileNotFoundError(f"YuNet model not found: {model_path}")
16
+
17
+ face_detector = cv2.FaceDetectorYN_create(
18
+ model_path,
19
+ "",
20
+ (320, 320), # Input size (can adjust)
21
+ 0.87, # Score threshold
22
+ 0.3, # NMS threshold
23
+ 5000 # Top K
24
+ )
25
+
26
+ @app.route('/detect', methods=['POST'])
27
+ def detect_faces():
28
+ if 'image' not in request.files:
29
+ return jsonify({"error": "No image file provided"}), 400
30
+
31
+ file = request.files['image']
32
+ filestr = file.read()
33
+ npimg = np.frombuffer(filestr, np.uint8)
34
+ img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
35
+
36
+ if img is None:
37
+ return jsonify({"error": "Invalid image"}), 400
38
+
39
+ height, width, _ = img.shape
40
+ face_detector.setInputSize((width, height))
41
+
42
+ _, faces = face_detector.detect(img)
43
+
44
+ result = {
45
+ "faces_detected": 0,
46
+ "faces": []
47
+ }
48
+
49
+ if faces is not None:
50
+ result["faces_detected"] = len(faces)
51
+ for face in faces:
52
+ # face: [x, y, w, h, confidence, ... landmarks]
53
+ x, y, w, h = list(map(int, face[:4]))
54
+ conf = float(face[4])
55
+ result["faces"].append({
56
+ "box": [x, y, w, h],
57
+ "confidence": round(conf, 3)
58
+ # "landmarks": [...] # if you want eyes/nose etc.
59
+ })
60
+
61
+ # Optional: Draw rectangle (for debug, save image locally if needed)
62
+ # cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
63
+
64
+ # Optional: Send FCM if face detected
65
+ if result["faces_detected"] > 0:
66
+ try:
67
+ # Example with pyfcm - replace with your logic
68
+ # data_message = {"title": "Face Detected!", "body": f"{result['faces_detected']} face(s) seen"}
69
+ # push_service.notify_topic_subscribers(topic_name="uncle_alerts", data_message=data_message)
70
+ print("FCM would be sent here - face detected")
71
+ except Exception as e:
72
+ print(f"FCM error: {e}")
73
+
74
+ return jsonify(result)
75
+
76
+ if __name__ == '__main__':
77
+ # For local testing
78
+ app.run(host='0.0.0.0', port=7860, debug=True)
79
+ else:
80
+ # For gunicorn in production (Hugging Face)
81
+ # The port 7860 is what HF expects
82
+ pass
packages.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ libgl1-mesa-glx
2
+ libglib2.0-0
requirements.txt CHANGED
@@ -1 +1,6 @@
1
- gradio
 
 
 
 
 
 
1
+ flask==3.0.3
2
+ gunicorn==23.0.0
3
+ opencv-python-headless==4.10.0.84 # or latest 4.x version
4
+ numpy==1.26.4
5
+ pyfcm==1.5.4 # optional - for FCM from Python
6
+ # firebase-admin==6.5.0 # alternative to pyfcm, more modern