def Predict_Dense(): # 从摄像头中读取视频流 video_capture = cv2.VideoCapture(0) check_weight(DENSE_WEIGHT_PATH) model = dense_models() # 加载权重 model.load_weights(DENSE_WEIGHT_PATH) while True: # 读取每一帧 ret, frame = video_capture.read() # 0.01s small_frame = cv2.resize(frame, (0, 0), fx=0.75, fy=0.75) rgb_frame = small_frame[:, :, ::-1] face_locations = face_recognition.face_locations(rgb_frame) # 0.17s if face_locations == []: continue face_encodings = face_recognition.face_encodings( rgb_frame, face_locations) # 找到了当前帧中所有面部的编码 #0.51s for face_location in face_locations: # 画矩形时间:不足0.001s top, right, bottom, left = face_location cv2.rectangle(small_frame, (left, top), (right, bottom), (0, 0, 255), 2) out = model.predict(np.array(face_encodings)) # 神经网络预测时间:0.001s for o in out: print("Welcome people:{}".format(o.argmax() + 1)) cv2.imshow('Video', small_frame) # 展示一帧时间:不足0.001s if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows
def test(): check_weight() model = dense_models() model.load_weights(WEIGHT_PATH) data = [] test_x, test_y = get_encodings(False) data.append(test_x) data = np.array(data).astype(np.float32).reshape(-1,128) out = model.test_on_batch(data,test_y) print("loss = {}, accuracy = {}".format(out[0],out[1]))
def train(): batch_x, batch_y = get_encodings() model =dense_models() if os.path.exists(WEIGHT_PATH): model.load_weights(WEIGHT_PATH) tb = TensorBoard(log_dir=LOG_PATH, write_images=1) call_list = [tb] model.fit(x=batch_x, y=batch_y, batch_size=batch_size, epochs = epochs, verbose=2,callbacks=call_list) # callback回调函数会在训练适当时机被调用 model.save_weights(WEIGHT_PATH, True)