Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,220 +1,199 @@
|
|
1 |
-
from flask import Flask, render_template, request, Response, jsonify, redirect, url_for
|
2 |
-
import cv2
|
3 |
-
import os
|
4 |
-
import numpy as np
|
5 |
-
import pickle
|
6 |
-
import
|
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 |
-
def
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
def
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
if not
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
(
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
person_dir = os.path.join(FACE_DATA_DIR, name)
|
166 |
-
if
|
167 |
-
os.
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
os.remove(os.path.join(person_dir, filename))
|
201 |
-
os.rmdir(person_dir)
|
202 |
-
|
203 |
-
# Update daftar nama
|
204 |
-
names_file = os.path.join(FACE_DATA_DIR, 'names.pkl')
|
205 |
-
if os.path.exists(names_file):
|
206 |
-
with open(names_file, 'rb') as f:
|
207 |
-
names = pickle.load(f)
|
208 |
-
|
209 |
-
if name in names:
|
210 |
-
names.remove(name)
|
211 |
-
with open(names_file, 'wb') as f:
|
212 |
-
pickle.dump(names, f)
|
213 |
-
|
214 |
-
# Retrain model
|
215 |
-
load_face_data()
|
216 |
-
|
217 |
-
return redirect(url_for('index'))
|
218 |
-
|
219 |
-
if __name__ == '__main__':
|
220 |
-
app.run(debug=True, host='0.0.0.0', port=5000)
|
|
|
1 |
+
from flask import Flask, render_template, request, Response, jsonify, redirect, url_for
|
2 |
+
import cv2
|
3 |
+
import os
|
4 |
+
import numpy as np
|
5 |
+
import pickle
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
# ==== Bagian Flask (kode kamu tetap) ====
|
9 |
+
flask_app = Flask(__name__)
|
10 |
+
|
11 |
+
FACE_DATA_DIR = 'face_data'
|
12 |
+
FACE_CASCADE_PATH = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
|
13 |
+
|
14 |
+
if not os.path.exists(FACE_DATA_DIR):
|
15 |
+
os.makedirs(FACE_DATA_DIR)
|
16 |
+
|
17 |
+
face_cascade = cv2.CascadeClassifier(FACE_CASCADE_PATH)
|
18 |
+
camera = None
|
19 |
+
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
|
20 |
+
is_trained = False
|
21 |
+
|
22 |
+
def load_face_data():
|
23 |
+
global is_trained
|
24 |
+
faces = []
|
25 |
+
labels = []
|
26 |
+
names = []
|
27 |
+
|
28 |
+
if os.path.exists(os.path.join(FACE_DATA_DIR, 'names.pkl')):
|
29 |
+
with open(os.path.join(FACE_DATA_DIR, 'names.pkl'), 'rb') as f:
|
30 |
+
names = pickle.load(f)
|
31 |
+
|
32 |
+
for idx, name in enumerate(names):
|
33 |
+
face_dir = os.path.join(FACE_DATA_DIR, name)
|
34 |
+
if os.path.exists(face_dir):
|
35 |
+
for filename in os.listdir(face_dir):
|
36 |
+
if filename.endswith('.jpg'):
|
37 |
+
img_path = os.path.join(face_dir, filename)
|
38 |
+
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
|
39 |
+
faces.append(img)
|
40 |
+
labels.append(idx)
|
41 |
+
|
42 |
+
if faces:
|
43 |
+
face_recognizer.train(faces, np.array(labels))
|
44 |
+
is_trained = True
|
45 |
+
return names
|
46 |
+
return []
|
47 |
+
|
48 |
+
def get_camera():
|
49 |
+
global camera
|
50 |
+
if camera is None:
|
51 |
+
camera = cv2.VideoCapture(0)
|
52 |
+
return camera
|
53 |
+
|
54 |
+
@flask_app.route('/')
|
55 |
+
def index():
|
56 |
+
names = load_face_data()
|
57 |
+
return render_template('index.html', registered_faces=names)
|
58 |
+
|
59 |
+
@flask_app.route('/register')
|
60 |
+
def register():
|
61 |
+
return render_template('register.html')
|
62 |
+
|
63 |
+
@flask_app.route('/recognize')
|
64 |
+
def recognize():
|
65 |
+
return render_template('recognize.html')
|
66 |
+
|
67 |
+
@flask_app.route('/video_feed')
|
68 |
+
def video_feed():
|
69 |
+
def generate():
|
70 |
+
camera = get_camera()
|
71 |
+
while True:
|
72 |
+
success, frame = camera.read()
|
73 |
+
if not success:
|
74 |
+
break
|
75 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
76 |
+
frame = buffer.tobytes()
|
77 |
+
yield (b'--frame\r\n'
|
78 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
79 |
+
return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
80 |
+
|
81 |
+
@flask_app.route('/recognition_feed')
|
82 |
+
def recognition_feed():
|
83 |
+
def generate():
|
84 |
+
camera = get_camera()
|
85 |
+
names = load_face_data()
|
86 |
+
|
87 |
+
while True:
|
88 |
+
success, frame = camera.read()
|
89 |
+
if not success:
|
90 |
+
break
|
91 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
92 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
93 |
+
|
94 |
+
for (x, y, w, h) in faces:
|
95 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
96 |
+
|
97 |
+
if is_trained and names:
|
98 |
+
roi_gray = gray[y:y+h, x:x+w]
|
99 |
+
roi_gray = cv2.resize(roi_gray, (100, 100))
|
100 |
+
id_, confidence = face_recognizer.predict(roi_gray)
|
101 |
+
|
102 |
+
if confidence < 100:
|
103 |
+
name = names[id_]
|
104 |
+
confidence_text = f"{name} ({round(100-confidence)}%)"
|
105 |
+
else:
|
106 |
+
confidence_text = "Unknown"
|
107 |
+
|
108 |
+
cv2.putText(frame, confidence_text, (x, y-10),
|
109 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
|
110 |
+
|
111 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
112 |
+
frame = buffer.tobytes()
|
113 |
+
yield (b'--frame\r\n'
|
114 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
115 |
+
return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
116 |
+
|
117 |
+
@flask_app.route('/capture_face', methods=['POST'])
|
118 |
+
def capture_face():
|
119 |
+
name = request.json.get('name', '').strip()
|
120 |
+
if not name:
|
121 |
+
return jsonify({'error': 'Nama tidak boleh kosong'})
|
122 |
+
|
123 |
+
camera = get_camera()
|
124 |
+
success, frame = camera.read()
|
125 |
+
if not success:
|
126 |
+
return jsonify({'error': 'Gagal mengambil gambar dari kamera'})
|
127 |
+
|
128 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
129 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
130 |
+
|
131 |
+
if len(faces) == 0:
|
132 |
+
return jsonify({'error': 'Tidak ada wajah yang terdeteksi'})
|
133 |
+
if len(faces) > 1:
|
134 |
+
return jsonify({'error': 'Terdeteksi lebih dari satu wajah'})
|
135 |
+
|
136 |
+
(x, y, w, h) = faces[0]
|
137 |
+
face_roi = gray[y:y+h, x:x+w]
|
138 |
+
face_roi = cv2.resize(face_roi, (100, 100))
|
139 |
+
|
140 |
+
person_dir = os.path.join(FACE_DATA_DIR, name)
|
141 |
+
if not os.path.exists(person_dir):
|
142 |
+
os.makedirs(person_dir)
|
143 |
+
|
144 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
145 |
+
filename = f"{timestamp}.jpg"
|
146 |
+
cv2.imwrite(os.path.join(person_dir, filename), face_roi)
|
147 |
+
|
148 |
+
names_file = os.path.join(FACE_DATA_DIR, 'names.pkl')
|
149 |
+
if os.path.exists(names_file):
|
150 |
+
with open(names_file, 'rb') as f:
|
151 |
+
names = pickle.load(f)
|
152 |
+
else:
|
153 |
+
names = []
|
154 |
+
|
155 |
+
if name not in names:
|
156 |
+
names.append(name)
|
157 |
+
with open(names_file, 'wb') as f:
|
158 |
+
pickle.dump(names, f)
|
159 |
+
|
160 |
+
load_face_data()
|
161 |
+
return jsonify({'success': f'Wajah {name} berhasil didaftarkan'})
|
162 |
+
|
163 |
+
@flask_app.route('/delete_face/<name>')
|
164 |
+
def delete_face(name):
|
165 |
+
person_dir = os.path.join(FACE_DATA_DIR, name)
|
166 |
+
if os.path.exists(person_dir):
|
167 |
+
for filename in os.listdir(person_dir):
|
168 |
+
os.remove(os.path.join(person_dir, filename))
|
169 |
+
os.rmdir(person_dir)
|
170 |
+
|
171 |
+
names_file = os.path.join(FACE_DATA_DIR, 'names.pkl')
|
172 |
+
if os.path.exists(names_file):
|
173 |
+
with open(names_file, 'rb') as f:
|
174 |
+
names = pickle.load(f)
|
175 |
+
if name in names:
|
176 |
+
names.remove(name)
|
177 |
+
with open(names_file, 'wb') as f:
|
178 |
+
pickle.dump(names, f)
|
179 |
+
load_face_data()
|
180 |
+
return redirect(url_for('index'))
|
181 |
+
|
182 |
+
# ==== Bungkus Flask ke FastAPI untuk Hugging Face ====
|
183 |
+
from fastapi import FastAPI
|
184 |
+
from fastapi.middleware.wsgi import WSGIMiddleware
|
185 |
+
|
186 |
+
app = FastAPI()
|
187 |
+
|
188 |
+
# Endpoint bawaan FastAPI
|
189 |
+
@app.get("/hello")
|
190 |
+
def greet_json():
|
191 |
+
return {"Hello": "World!"}
|
192 |
+
|
193 |
+
# Mount Flask app ke FastAPI
|
194 |
+
app.mount("/", WSGIMiddleware(flask_app))
|
195 |
+
|
196 |
+
# Jalankan dengan uvicorn kalau lokal
|
197 |
+
if __name__ == "__main__":
|
198 |
+
import uvicorn
|
199 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|