-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion_detector.py
More file actions
54 lines (43 loc) · 1.93 KB
/
motion_detector.py
File metadata and controls
54 lines (43 loc) · 1.93 KB
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
import os
from flask import Flask, render_template, Response, request
from camera import VideoCamera
from detection import Detection
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == "POST":
return render_template('index.html', video_detect = request.form["video_value"])
else:
return render_template('index.html')
# "/" を呼び出したときには、indexが表示される。
def gen(camera):
while True:
frame = camera.get_frame()
# converted_frame = Detection(frame)
# facedetected_frame = converted_frame.face_detection()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
# returnではなくジェネレーターのyieldで逐次出力。
# Generatorとして働くためにgenとの関数名にしている
# Content-Type(送り返すファイルの種類として)multipart/x-mixed-replace を利用。
# HTTP応答によりサーバーが任意のタイミングで複数の文書を返し、紙芝居的にレンダリングを切り替えさせるもの。
@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/face_detect')
def face_detect():
return Response(generate(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
def generate(camera):
first_frame = None
while True:
first_frame, frame = camera.get_motion_detection(first_frame)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
# 0.0.0.0はすべてのアクセスを受け付けます。
# webブラウザーには、「localhost:5000」と入力