-
Notifications
You must be signed in to change notification settings - Fork 0
/
webstreaming.py
301 lines (259 loc) · 10.7 KB
/
webstreaming.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# import the necessary packages
from pyimagesearch.motion_detection.singlemotiondetector import SingleMotionDetector
from imutils.video import VideoStream
from flask import Response
from flask import Flask, jsonify, request, redirect
from flask import render_template
from tensorflow.keras.utils import load_img, img_to_array
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
import numpy as np
import joblib
import threading
import argparse
import datetime
import imutils
import time
import cv2
import os
from sqlalchemy import create_engine
import pandas as pd
# initialize the output frame and a lock used to ensure thread-safe
# exchanges of the output frames (useful when multiple browsers/tabs
# are viewing the stream)
outputFrame = None
lock = threading.Lock()
# initialize a flask object
app = Flask(__name__, static_folder="static")
# initialize the video stream and allow the camera sensor to
# warmup
vs = VideoStream(src=0).start()
time.sleep(2.0)
engine = create_engine("sqlite:///testdb.db")
@app.route("/")
def index():
# return the rendered template
return render_template("index.html")
@app.get("/registration")
def register_face():
return render_template("face_registration.html")
@app.get("/checkin")
def checkin():
return render_template("face_recognition.html")
filename = 'model_trained.sav'
model = joblib.load(filename)
@app.get("/recordcheckin")
def recordCheckIn():
# grab global references to the video stream, output frame, and
# lock variables
global vs, outputFrame, lock
db = engine.connect()
# read the next frame from the video stream, resize it,
# convert the frame to grayscale, and blur it
frame = vs.read()
frame = imutils.resize(frame, width=400)
cv2.imwrite('temp.jpg', frame)
# recognition code
# load saved model
img = load_img("temp.jpg", target_size=(224,224))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
# classes = [[0,0,0,0,0,0,0,1]]
classes_loop = [round(num, 1) for num in classes[0]]
# print(classes_loop)
if 1 in classes_loop:
class_id = classes_loop.index(1)
StudentName = db.execute(f"""SELECT STUDENT_NAME FROM master_student WHERE CLASS_ID = {class_id}""")
return jsonify({
"StudentName":list(StudentName)[0][0]
})
else:
return jsonify({
"StudentName":False
})
@app.get("/train")
def trainView():
# return the rendered template
return render_template("trainView.html")
@app.post("/trainmodel")
def trainStart():
engine = create_engine("sqlite:///testdb.db")
db = engine.connect()
classLength = db.execute("""SELECT COUNT(CLASS_ID) FROM master_student""")
classLength = list(classLength)[0][0]
data = "image_databases"
datagen = ImageDataGenerator(samplewise_center=False,
samplewise_std_normalization=False,
horizontal_flip = True,
vertical_flip = False,
height_shift_range = 0.15,
width_shift_range = 0.15,
rotation_range = 5,
shear_range = 0.01,
fill_mode = 'nearest',
zoom_range=0.10)
train_it = datagen.flow_from_directory(data,
target_size=(224,224),
batch_size=1,
color_mode='rgb',
class_mode='categorical')
base_model = keras.applications.ResNet50( weights='imagenet', input_shape=(224,224, 3), include_top=False)
inputs = keras.Input(shape=(224,224, 3))
x = base_model(inputs, training=False)
# Add pooling layer or flatten layer
x = keras.layers.Flatten()(x)
# Add final dense layer
outputs = keras.layers.Dense(classLength, activation = 'softmax')(x)
# Combine inputs and outputs to create model
model_trained = keras.Model(inputs=inputs, outputs=outputs)
base_model.trainable = False
# Compile the model with a low learning rate
model_trained.compile(optimizer=keras.optimizers.RMSprop(learning_rate = 0.0001),
loss = 'categorical_crossentropy' , metrics = ['accuracy'])
history = model_trained.fit(train_it,steps_per_epoch=100,epochs=classLength)
#save model
filename = 'model_trained.sav'
joblib.dump(model_trained, filename)
global model
model = joblib.load(filename)
# return the rendered template
return jsonify({
"data":True
})
@app.post("/appregistration")
def registerApp():
data = request.get_json()
studentName = data['NameStudent']
# grab global references to the video stream, output frame, and
# lock variables
global vs, outputFrame, lock
# initialize the motion detector and the total number of frames
# read thus far
num_sample = 160
# students = pd.read_excel("Student_Data.xlsx")
# Max_id = students['id'].max()
db = engine.connect()
maxIdStudent = db.execute(
"""SELECT MAX(CLASS_ID) AS ID FROM master_student"""
)
maxIdStudent = list(maxIdStudent)[0][0]
if maxIdStudent == None:
maxIdStudent = 0
count=0
parent_dir = "image_databases"
directory = str(maxIdStudent + 1)
path = os.path.join(parent_dir, directory)
os.makedirs(path)
# loop over frames from the video stream
while True:
# read the next frame from the video stream, resize it,
# convert the frame to grayscale, and blur it
frame = vs.read()
frame = imutils.resize(frame, width=400)
cv2.imwrite(path + '\%d.jpg' % count, frame)
count += 1
if count>num_sample:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
# grab the current timestamp and draw it on the frame
timestamp = datetime.datetime.now()
cv2.putText(frame, timestamp.strftime(
"%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
db.execute(
f"""INSERT INTO master_student (STUDENT_NAME, CLASS_ID) VALUES ('{studentName}',{maxIdStudent+1}) """
)
def detect_motion(frameCount):
# grab global references to the video stream, output frame, and
# lock variables
global vs, outputFrame, lock
# initialize the motion detector and the total number of frames
# read thus far
md = SingleMotionDetector(accumWeight=0.1)
total = 0
# loop over frames from the video stream
while True:
# read the next frame from the video stream, resize it,
# convert the frame to grayscale, and blur it
frame = vs.read()
frame = imutils.resize(frame, width=400)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
# grab the current timestamp and draw it on the frame
timestamp = datetime.datetime.now()
cv2.putText(frame, timestamp.strftime(
"%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
# if the total number of frames has reached a sufficient
# number to construct a reasonable background model, then
# continue to process the frame
if total > frameCount:
# detect motion in the image
motion = md.detect(gray)
# check to see if motion was found in the frame
if motion is not None:
# unpack the tuple and draw the box surrounding the
# "motion area" on the output frame
# (thresh, (minX, minY, maxX, maxY)) = motion
# cv2.rectangle(frame, (minX, minY), (maxX, maxY),
# (0, 0, 255), 2)
pass
# update the background model and increment the total number
# of frames read thus far
md.update(gray)
total += 1
# acquire the lock, set the output frame, and release the
# lock
with lock:
outputFrame = frame.copy()
def generate():
# grab global references to the output frame and lock variables
global outputFrame, 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 outputFrame is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
# 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("/video_feed")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate(),
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")
ap.add_argument("-d", "--debug", type=str, default="run",
help="# debug type")
args = vars(ap.parse_args())
# start a thread that will perform motion detection
t = threading.Thread(target=detect_motion, args=(
args["frame_count"],))
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()