Keras之CNN:基于Keras利用cv2建立训练存储卷积神经网络模型(2+1)并调用摄像头进行实时人脸识别
生活随笔
收集整理的這篇文章主要介紹了
Keras之CNN:基于Keras利用cv2建立训练存储卷积神经网络模型(2+1)并调用摄像头进行实时人脸识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Keras之CNN:基于Keras利用cv2建立訓練存儲卷積神經網絡模型(2+1)并調用攝像頭進行實時人臉識別
?
?
目錄
輸出結果
設計思路
核心代碼
?
?
?
輸出結果
?
設計思路
?
核心代碼
?
# -*- coding:utf-8 -*- import cv2 from train_model import Model from read_data import read_name_list from timeit import default_timer as timer ### to calculate FPSclass Camera_reader(object):def __init__(self):self.model = Model()self.model.load()self.img_size = 128def build_camera(self):face_cascade = cv2.CascadeClassifier('F:\\Program Files\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml') # print(face_cascade) #輸出<CascadeClassifier 000002240244CC70>name_list = read_name_list('F:\\File_Python\\Python_example\\face_recognition_name\\After_cut_picture') # print(name_list)cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while success and cv2.waitKey(1) == -1: success, frame = cameraCapture.read()gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) ROI = gray[x:x + w, y:y + h]ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) label,prob = self.model.predict(ROI) if prob >0.7: show_name = name_list[label]else:show_name = 'Stranger'cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.imshow("Camera", frame) cameraCapture.release() cv2.destroyAllWindows() def detect_video(self): face_cascade = cv2.CascadeClassifier('F:\\Program Files\\Python\\Python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml')name_list = read_name_list('F:\\File_Python\\Python_example\\face_recognition_name\\After_cut_picture')video = cv2.VideoCapture(video_path) ### TODO: will video path other than 0 be used?success, frame = video.read() accum_time = 0curr_fps = 0fps = "FPS: ??" #fps = "FPS: ??"prev_time = timer()while success and cv2.waitKey(1) == -1: success, frame = video.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)faces = face_cascade.detectMultiScale(gray, 1.3, 5)curr_time = timer()exec_time = curr_time - prev_timeprev_time = curr_timeaccum_time = accum_time + exec_timecurr_fps = curr_fps + 1 #1 if accum_time > 1:accum_time = accum_time - 1 #1fps = "FPS: " + str(curr_fps)curr_fps = 0 #0 for (x, y, w, h) in faces: ROI = gray[x:x + w, y:y + h]ROI = cv2.resize(ROI, (self.img_size, self.img_size), interpolation=cv2.INTER_LINEAR) #cv2.INTER_LINEAR圖像尺寸變換的方法,默認的雙線性插值label,prob = self.model.predict(ROI) if prob >0.7: show_name = name_list[label]else:show_name = 'Stranger'cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.namedWindow("result", cv2.WINDOW_NORMAL)cv2.imshow("result",frame)if __name__ == '__main__':camera = Camera_reader()camera.build_camera() # video_path='F:/File_Python/Python_example/YOLOv3_use_TF/RunMan1.mp4' # camera.detect_video()?
?
?
?
總結
以上是生活随笔為你收集整理的Keras之CNN:基于Keras利用cv2建立训练存储卷积神经网络模型(2+1)并调用摄像头进行实时人脸识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DL框架:主流深度学习框架(Tensor
- 下一篇: CV:利用cv2(加载人脸