-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebstreaming.py
More file actions
153 lines (121 loc) · 4.7 KB
/
Copy pathwebstreaming.py
File metadata and controls
153 lines (121 loc) · 4.7 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
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
# USAGE
# python webstreaming.py --ip 0.0.0.0 --port 8000
# import the necessary packages
from imutils.video import VideoStream
from flask import Response
from flask import Flask
from flask import render_template
from tensorflow.keras.models import load_model
import resizer as re
import threading
import argparse
import imutils
import time
import cv2
WIDTH = HEIGHT = 100
# initialize the output frame and a lock used to ensure thread-safe
# exchanges of the output frames (useful for multiple browsers/tabs
# are viewing tthe stream)
outputFrame2 = None
lock = threading.Lock()
# initialize a flask object
app = Flask(__name__)
# loading model
model = load_model("model98keypoints.h5")
# initialize the video stream and allow the camera sensor to
# warmup
vs = VideoStream(src=0).start()
time.sleep(2.0)
@app.route("/")
def index():
# return the rendered template
return render_template("index.html")
obj = re.Resizer(WIDTH, HEIGHT, 1.1)
def get_keypoints():
global vs, lock, frame_original, outputFrame2
while True:
frame_original = vs.read()
frame = imutils.resize(frame_original, width=400)
img, faces = obj.get_resized_withoutdata(frame)
try:
faces = faces[0]
temp = img[0].copy()
temp = cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY)
temp = temp.reshape(1, WIDTH, HEIGHT, 1)
data = model.predict(temp)
for i in range(0, len(data[0]), 2):
cv2.circle(img[0], center=(data[0][i], data[0][i + 1]), radius=1,
color=(255, 255, 255))
frame[faces[1]:faces[1] + faces[3], faces[0]:faces[0] + faces[2], :] = cv2.resize(img[0],
(faces[2], faces[3]))
except:
pass
with lock:
outputFrame2 = frame.copy()
def generate1():
# grab global references to the output frame and lock variables
global outputFrame2, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame2 is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", frame_original)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
def generate2():
# grab global references to the output frame and lock variables
global outputFrame2, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame2 is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame2)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
@app.route("/original_feed")
def origianl_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate1(),
mimetype="multipart/x-mixed-replace; boundary=frame")
@app.route("/keypoints_feed")
def keypoints_feed():
return Response(generate2(),
mimetype="multipart/x-mixed-replace; boundary=frame")
# check to see if this is the main thread of execution
if __name__ == '__main__':
# construct the argument parser and parse command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--ip", type=str, required=True,
help="ip address of the device")
ap.add_argument("-o", "--port", type=int, required=True,
help="ephemeral port number of the server (1024 to 65535)")
ap.add_argument("-f", "--frame-count", type=int, default=32,
help="# of frames used to construct the background model")
args = vars(ap.parse_args())
t = threading.Thread(target=get_keypoints)
t.daemon = True
t.start()
# start the flask app
app.run(host=args["ip"], port=args["port"], debug=True,
threaded=True, use_reloader=False)
# release the video stream pointer
vs.stop()