Spaces:
Sleeping
Sleeping
Commit
·
2a52a89
1
Parent(s):
d40a058
Upload 3 files
Browse files- Dockerfile +11 -0
- main.py +91 -0
- requirements.txt +75 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["gunicorn", ".b","0.0.0.0.7860","main:app"]
|
main.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from flask_sqlalchemy import SQLAlchemy
|
3 |
+
import cv2
|
4 |
+
import face_detection
|
5 |
+
import numpy as np
|
6 |
+
import base64
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
# Configuration for SQLite database
|
11 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///faces.db'
|
12 |
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
13 |
+
db = SQLAlchemy(app)
|
14 |
+
|
15 |
+
class Frame(db.Model):
|
16 |
+
id = db.Column(db.Integer, primary_key=True)
|
17 |
+
frame_data = db.Column(db.LargeBinary) # Use LargeBinary to store binary data
|
18 |
+
count_of_people = db.Column(db.Integer)
|
19 |
+
|
20 |
+
def create_database():
|
21 |
+
db.create_all()
|
22 |
+
|
23 |
+
|
24 |
+
def draw_faces(im, bboxes):
|
25 |
+
for bbox in bboxes:
|
26 |
+
x0, y0, x1, y1 = [int(_) for _ in bbox]
|
27 |
+
cv2.rectangle(im, (x0, y0), (x1, y1), (0, 0, 255), 2)
|
28 |
+
|
29 |
+
def detect_faces_and_save(frame):
|
30 |
+
detector = face_detection.build_detector("DSFDDetector", confidence_threshold=0.5, nms_iou_threshold=0.3)
|
31 |
+
det_raw = detector.detect(frame[:, :, ::-1])
|
32 |
+
dets = det_raw[:, :4]
|
33 |
+
draw_faces(frame, dets)
|
34 |
+
|
35 |
+
count_of_people = len(dets)
|
36 |
+
|
37 |
+
|
38 |
+
frame_data_encoded = base64.b64encode(cv2.imencode('.jpg', frame)[1].tobytes())
|
39 |
+
new_frame = Frame(frame_data=frame_data_encoded, count_of_people=count_of_people)
|
40 |
+
db.session.add(new_frame)
|
41 |
+
db.session.commit()
|
42 |
+
|
43 |
+
return frame, count_of_people
|
44 |
+
|
45 |
+
@app.route('/video_feed', methods=['POST'])
|
46 |
+
def video_feed():
|
47 |
+
if 'video' not in request.files:
|
48 |
+
return jsonify({'error': 'No video file in the request'})
|
49 |
+
|
50 |
+
video_file = request.files['video']
|
51 |
+
|
52 |
+
if video_file.filename == '':
|
53 |
+
return jsonify({'error': 'Empty video file'})
|
54 |
+
|
55 |
+
video_bytes = video_file.read()
|
56 |
+
video_np = np.frombuffer(video_bytes, dtype=np.uint8)
|
57 |
+
vidcap = cv2.imdecode(video_np, cv2.IMREAD_UNCHANGED)
|
58 |
+
|
59 |
+
if vidcap is None:
|
60 |
+
return jsonify({'error': 'Error decoding video file'})
|
61 |
+
|
62 |
+
create_database()
|
63 |
+
|
64 |
+
success, image = vidcap.read()
|
65 |
+
|
66 |
+
while success:
|
67 |
+
|
68 |
+
detect_faces_and_save(image)
|
69 |
+
|
70 |
+
success, image = vidcap.read()
|
71 |
+
|
72 |
+
return jsonify({'result': 'Video parsed'})
|
73 |
+
|
74 |
+
@app.route('/get_frames', methods=['GET'])
|
75 |
+
def get_frames():
|
76 |
+
frames = Frame.query.all()
|
77 |
+
frames_data = []
|
78 |
+
|
79 |
+
for frame in frames:
|
80 |
+
frames_data.append({
|
81 |
+
'frame_data': frame.frame_data.decode('latin1'),
|
82 |
+
'count_of_people': frame.count_of_people
|
83 |
+
})
|
84 |
+
|
85 |
+
db.session.query(Frame).delete()
|
86 |
+
db.session.commit()
|
87 |
+
|
88 |
+
return jsonify(frames_data)
|
89 |
+
|
90 |
+
if __name__ == '__main__':
|
91 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
asttokens==2.2.1
|
2 |
+
backcall==0.2.0
|
3 |
+
blinker==1.7.0
|
4 |
+
certifi==2023.11.17
|
5 |
+
charset-normalizer==3.3.2
|
6 |
+
click==8.1.7
|
7 |
+
colorama==0.4.6
|
8 |
+
comm==0.1.4
|
9 |
+
contourpy
|
10 |
+
cvzone==1.6.1
|
11 |
+
cycler==0.12.1
|
12 |
+
debugpy
|
13 |
+
decorator==5.1.1
|
14 |
+
executing==1.2.0
|
15 |
+
face-detection==0.2.2
|
16 |
+
filelock==3.13.1
|
17 |
+
Flask==3.0.0
|
18 |
+
Flask-SQLAlchemy==3.1.1
|
19 |
+
fonttools==4.46.0
|
20 |
+
fsspec==2023.12.1
|
21 |
+
greenlet==3.0.2
|
22 |
+
gunicorn
|
23 |
+
idna==3.6
|
24 |
+
imutils==0.5.4
|
25 |
+
ipykernel==6.25.0
|
26 |
+
ipython==8.14.0
|
27 |
+
itsdangerous==2.1.2
|
28 |
+
jedi==0.19.0
|
29 |
+
Jinja2==3.1.2
|
30 |
+
jupyter_client==8.3.0
|
31 |
+
jupyter_core==5.3.1
|
32 |
+
kiwisolver==1.4.5
|
33 |
+
MarkupSafe==2.1.3
|
34 |
+
matplotlib==3.8.2
|
35 |
+
matplotlib-inline==0.1.6
|
36 |
+
mpmath==1.3.0
|
37 |
+
nest-asyncio==1.5.7
|
38 |
+
networkx==3.2.1
|
39 |
+
numpy==1.26.2
|
40 |
+
opencv-python==4.8.1.78
|
41 |
+
packaging==23.1
|
42 |
+
pandas==2.1.3
|
43 |
+
parso==0.8.3
|
44 |
+
pickleshare==0.7.5
|
45 |
+
Pillow==10.1.0
|
46 |
+
platformdirs==3.10.0
|
47 |
+
prompt-toolkit==3.0.39
|
48 |
+
psutil==5.9.5
|
49 |
+
pure-eval==0.2.2
|
50 |
+
py-cpuinfo==9.0.0
|
51 |
+
Pygments==2.15.1
|
52 |
+
pyparsing==3.1.1
|
53 |
+
python-dateutil==2.8.2
|
54 |
+
pytz==2023.3.post1
|
55 |
+
PyYAML==6.0.1
|
56 |
+
pyzmq==25.1.0
|
57 |
+
requests==2.31.0
|
58 |
+
scipy==1.11.4
|
59 |
+
seaborn==0.13.0
|
60 |
+
six==1.16.0
|
61 |
+
SQLAlchemy==2.0.23
|
62 |
+
stack-data==0.6.2
|
63 |
+
sympy==1.12
|
64 |
+
thop==0.1.1.post2209072238
|
65 |
+
torch==2.1.1
|
66 |
+
torchvision==0.16.1
|
67 |
+
tornado==6.3.2
|
68 |
+
tqdm==4.66.1
|
69 |
+
traitlets==5.9.0
|
70 |
+
typing_extensions==4.8.0
|
71 |
+
tzdata==2023.3
|
72 |
+
ultralytics==8.0.222
|
73 |
+
urllib3==2.1.0
|
74 |
+
wcwidth==0.2.6
|
75 |
+
Werkzeug==3.0.1
|