This repository was archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
executable file
·155 lines (114 loc) · 4.16 KB
/
server.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import datetime
import cv2
from utils import opts, handling_file
from yolo import Yolo
from threading import Thread
from flask import Flask, render_template, Response, request, session
# import pyrebase
# import json
# with open("auth.json") as f:
# config = json.load(f)
# firebase = pyrebase.initialize_app(config)
# db = firebase.database()
# password는 암호화해서 넣어야 함.
# 일단 여기서는 했다고 가정.
# signin = {"password": 1234, "username":"heejin"}
# db.child("users").child("kook").set(signin)
app = Flask(__name__)
app.secret_key = "인천대학교 컴퓨터공학과 캡스톤디자인 Be Bomb"
# video_output: output of video recording
# rec: recording status
video_output = None
rec = False
def record(video_output):
iter = 0
while rec:
if iter % 500 == 0:
print("녹화 중 ...")
video_output.write(yolo.frame)
iter += 1
def reserve_record(video_output, start_time, end_time):
now = datetime.datetime.now()
iter = 0
while start_time <= now and now <= end_time:
if iter % 500 == 0:
print("예약녹화 중 ...")
video_output.write(yolo.frame)
now = datetime.datetime.now()
iter += 1
video_output.release()
print("예약녹화 완료")
@app.route("/video_feed")
def video_feed():
# Video streaming route. Put this in the src attribute of an img tag
return Response(
yolo.gen_frames(), mimetype="multipart/x-mixed-replace; boundary=frame"
)
# Video streaming home page.
@app.route("/")
def index():
global rec
return render_template("index.html", rec=rec)
# Video streaming home page.
@app.route("/result", methods=["GET", "POST"])
def result():
global rec, video_output
now = datetime.datetime.now()
if request.method == "POST":
form_button = request.form.get("button", None)
form_scheduler = request.form.get("scheduler", None)
if form_button[:2] == "녹화":
rec = not rec
if rec:
fourcc = cv2.VideoWriter_fourcc(*"XVID")
video_output = cv2.VideoWriter(
f"static/videos/{str(now).replace(':','')}.avi",
fourcc,
120,
(yolo.frame.shape[1], yolo.frame.shape[0]),
)
thread = Thread(
target=record,
args=[video_output],
)
thread.start()
else:
video_output.release()
print("녹화 완료")
elif form_button == "캡쳐":
cv2.imwrite(f"static/images/{str(now).replace(':','')}.jpeg", yolo.frame)
print("캡쳐 완료")
elif form_button == "예약녹화":
start_time = form_scheduler[:19]
start_time = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
end_time = form_scheduler[22:]
end_time = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
fourcc = cv2.VideoWriter_fourcc(*"XVID")
video_output = cv2.VideoWriter(
f"static/videos/{str(now).replace(':','')}.avi",
fourcc,
120,
(yolo.frame.shape[1], yolo.frame.shape[0]),
)
thread = Thread(
target=reserve_record,
args=[video_output, start_time, end_time],
)
thread.start()
return render_template("index.html", rec=rec)
@app.route("/data_chart")
def data_chart():
return yolo.json if hasattr(yolo, "json") else "Data Chart"
@app.route("/get_images")
def get_images():
session.clear()
handling_file.remove_outdated_files()
image_list = handling_file.get_detected_images()
if image_list:
session["imageName"] = image_list
return "get images"
if __name__ == "__main__":
handling_file.remove_outdated_files()
opt = opts.parse_opt()
yolo = Yolo(opt)
app.run(host="localhost", debug=True, port=3000)