-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
129 lines (100 loc) Β· 3.52 KB
/
app.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
ο»Ώfrom flask_cors import CORS
import configparser
import json
import sys
from logging import getLogger, basicConfig, INFO
import cv2
from flask import Flask, Response, render_template, request, jsonify
from flask_cors import CORS, cross_origin
from libs.argparser import build_argparser
from libs.camera import VideoCamera
from libs.interactive_detection import Detections
app = Flask(__name__, static_folder="./static", template_folder="./templates")
CORS(app) #Cross Origin Resource Sharing
logger = getLogger(__name__)
basicConfig(
level=INFO, format="%(asctime)s %(levelname)s %(name)s %(funcName)s(): %(message)s"
)
config = configparser.ConfigParser()
config.read("config.ini")
# detection control flag
is_async = eval(config.get("DEFAULT", "is_async"))
is_det = eval(config.get("DEFAULT", "is_det"))
is_reid = eval(config.get("DEFAULT", "is_reid"))
# 0:x-axis 1:y-axis -1:both axis
flip_code = eval(config.get("DEFAULT", "flip_code"))
resize_width = int(config.get("CAMERA", "resize_width"))
def gen(camera):
while True:
frame = camera.get_frame(flip_code)
frame = detections.person_detection(frame, is_async, is_det, is_reid)
ret, jpeg = cv2.imencode(".jpg", frame)
frame = jpeg.tostring()
yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n\r\n")
@app.route("/")
@cross_origin()
def index():
return render_template(
"index.html", is_async=is_async, flip_code=flip_code, enumerate=enumerate,
)
@app.route("/video_feed")
def video_feed():
return Response(gen(camera), mimetype="multipart/x-mixed-replace; boundary=frame")
@app.route("/detection", methods=["POST"])
def detection():
global is_async
global is_det
global is_reid
command = request.json["command"]
if command == "async":
is_async = True
elif command == "sync":
is_async = False
if command == "person-det":
is_det = not is_det
is_reid = False
if command == "person-reid":
is_det = False
is_reid = not is_reid
result = {
"command": command,
"is_async": is_async,
"flip_code": flip_code,
"is_det": is_det,
"is_reid": is_reid,
}
logger.info(
"command:{} is_async:{} flip_code:{} is_det:{} is_reid:{}".format(
command, is_async, flip_code, is_det, is_reid
)
)
return jsonify(ResultSet=json.dumps(result))
@app.route("/flip", methods=["POST"])
def flip_frame():
global flip_code
command = request.json["command"]
if command == "flip" and flip_code is None:
flip_code = 0
elif command == "flip" and flip_code == 0:
flip_code = 1
elif command == "flip" and flip_code == 1:
flip_code = -1
elif command == "flip" and flip_code == -1:
flip_code = None
result = {"command": command, "is_async": is_async, "flip_code": flip_code}
return jsonify(ResultSet=json.dumps(result))
if __name__ == "__main__":
# arg parse
args = build_argparser().parse_args()
devices = [args.device, args.device_reidentification]
if 0 < args.grid < 3:
print("\nargument grid must be grater than 3")
sys.exit(1)
camera = VideoCamera(args.input, resize_width, args.v4l)
logger.info(
f"input:{args.input} v4l:{args.v4l} frame shape: {camera.frame.shape} axis:{args.axis} grid:{args.grid}"
)
detections = Detections(camera.frame, devices, args.axis, args.grid)
app.run(host="0.0.0.0", threaded=True)
# app -i cam
# app -i people-waling.mp4 --grid 10