Spaces:
Sleeping
Sleeping
Commit
·
9bad066
1
Parent(s):
3934710
Update main.py
Browse files
main.py
CHANGED
@@ -5,6 +5,9 @@ import face_detection
|
|
5 |
import numpy as np
|
6 |
import base64
|
7 |
import os
|
|
|
|
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
@@ -27,51 +30,83 @@ def draw_faces(im, bboxes):
|
|
27 |
x0, y0, x1, y1 = [int(_) for _ in bbox]
|
28 |
cv2.rectangle(im, (x0, y0), (x1, y1), (0, 0, 255), 2)
|
29 |
|
30 |
-
def detect_faces_and_save(
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
return frame, count_of_people
|
45 |
|
46 |
@app.route('/video_feed', methods=['POST'])
|
47 |
def video_feed():
|
|
|
|
|
|
|
|
|
|
|
48 |
if 'video' not in request.files:
|
49 |
return jsonify({'error': 'No video file in the request'})
|
50 |
|
51 |
video_file = request.files['video']
|
52 |
-
video_path = "uploaded_video.mp4"
|
53 |
video_file.save(video_path)
|
54 |
|
55 |
vidObj = cv2.VideoCapture(video_path)
|
56 |
|
57 |
success, image = vidObj.read()
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
success, image = vidObj.read()
|
67 |
-
|
68 |
-
# Close the video capture object
|
69 |
-
vidObj.release()
|
70 |
-
|
71 |
-
# Delete the video file after processing
|
72 |
-
os.remove(video_path)
|
73 |
|
74 |
-
return jsonify({'result': 'Video parsed'})
|
75 |
|
76 |
|
77 |
@app.route('/')
|
@@ -89,9 +124,6 @@ def get_frames():
|
|
89 |
'count_of_people': frame.count_of_people
|
90 |
})
|
91 |
|
92 |
-
db.session.query(Frame).delete()
|
93 |
-
db.session.commit()
|
94 |
-
|
95 |
return jsonify(frames_data)
|
96 |
|
97 |
if __name__ == '__main__':
|
|
|
5 |
import numpy as np
|
6 |
import base64
|
7 |
import os
|
8 |
+
import shutil
|
9 |
+
import asyncio
|
10 |
+
import threading
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
|
|
30 |
x0, y0, x1, y1 = [int(_) for _ in bbox]
|
31 |
cv2.rectangle(im, (x0, y0), (x1, y1), (0, 0, 255), 2)
|
32 |
|
33 |
+
async def detect_faces_and_save(vidObj, media_folder):
|
34 |
+
with app.app_context():
|
35 |
+
# Get video properties
|
36 |
+
fps = vidObj.get(cv2.CAP_PROP_FPS)
|
37 |
+
total_frames = int(vidObj.get(cv2.CAP_PROP_FRAME_COUNT))
|
38 |
+
video_duration = total_frames / fps
|
39 |
|
40 |
+
# Calculate frame sampling interval
|
41 |
+
target_fps = 1 # One frame per second
|
42 |
+
sampling_interval = int(fps / target_fps)
|
43 |
+
|
44 |
+
success, image = vidObj.read()
|
45 |
+
frame_counter = 0
|
46 |
+
|
47 |
+
while success:
|
48 |
+
if frame_counter % sampling_interval == 0:
|
49 |
+
detector = face_detection.build_detector("DSFDDetector", confidence_threshold=0.5, nms_iou_threshold=0.3)
|
50 |
+
det_raw = detector.detect(image[:, :, ::-1])
|
51 |
+
dets = det_raw[:, :4]
|
52 |
+
draw_faces(image, dets)
|
53 |
|
54 |
+
count_of_people = len(dets)
|
55 |
|
56 |
+
frame_data_encoded = base64.b64encode(cv2.imencode('.jpg', image)[1].tobytes())
|
57 |
+
new_frame = Frame(frame_data=frame_data_encoded, count_of_people=count_of_people)
|
58 |
+
db.session.add(new_frame)
|
59 |
+
db.session.commit()
|
60 |
+
|
61 |
+
success, image = vidObj.read()
|
62 |
+
frame_counter += 1
|
63 |
+
|
64 |
+
if not success:
|
65 |
+
vidObj.release() # Release the video capture object before breaking out of the loop
|
66 |
+
break
|
67 |
+
|
68 |
+
# After the loop, release the video capture object and delete the file
|
69 |
+
vidObj.release()
|
70 |
+
shutil.rmtree(media_folder)
|
71 |
+
|
72 |
+
def process_upload_thread(vidObj, media_folder):
|
73 |
+
loop = asyncio.new_event_loop()
|
74 |
+
asyncio.set_event_loop(loop)
|
75 |
+
loop.run_until_complete(detect_faces_and_save(vidObj, media_folder))
|
76 |
+
loop.close()
|
77 |
|
|
|
78 |
|
79 |
@app.route('/video_feed', methods=['POST'])
|
80 |
def video_feed():
|
81 |
+
|
82 |
+
media_folder = "media"
|
83 |
+
if not os.path.exists(media_folder):
|
84 |
+
os.makedirs(media_folder)
|
85 |
+
|
86 |
if 'video' not in request.files:
|
87 |
return jsonify({'error': 'No video file in the request'})
|
88 |
|
89 |
video_file = request.files['video']
|
90 |
+
video_path = os.path.join(media_folder, "uploaded_video.mp4")
|
91 |
video_file.save(video_path)
|
92 |
|
93 |
vidObj = cv2.VideoCapture(video_path)
|
94 |
|
95 |
success, image = vidObj.read()
|
96 |
+
|
97 |
+
detector = face_detection.build_detector("DSFDDetector", confidence_threshold=0.5, nms_iou_threshold=0.3)
|
98 |
+
det_raw = detector.detect(image[:, :, ::-1])
|
99 |
+
dets = det_raw[:, :4]
|
100 |
+
draw_faces(image, dets)
|
101 |
|
102 |
+
count_of_people = len(dets)
|
103 |
+
frame_data_encoded = base64.b64encode(cv2.imencode('.jpg', image)[1].tobytes())
|
104 |
+
frame_data_encoded_str = frame_data_encoded.decode('latin1')
|
105 |
+
|
106 |
+
threading.Thread(target=process_upload_thread, args=(vidObj, media_folder)).start()
|
107 |
+
|
108 |
+
return jsonify({'frame': frame_data_encoded_str, 'count_of_people': count_of_people})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
|
|
110 |
|
111 |
|
112 |
@app.route('/')
|
|
|
124 |
'count_of_people': frame.count_of_people
|
125 |
})
|
126 |
|
|
|
|
|
|
|
127 |
return jsonify(frames_data)
|
128 |
|
129 |
if __name__ == '__main__':
|