# Dlib_face_recognition_from_camera **Repository Path**: Eric_995/Dlib_face_recognition_from_camera ## Basic Information - **Project Name**: Dlib_face_recognition_from_camera - **Description**: Detect and recognize the faces from camera / 调用摄像头进行人脸识别,支持多张人脸同时识别 - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 6 - **Created**: 2019-05-17 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Face recognition from camera ############################ Introduction ************ Detect and recognize single/multi-faces from camera; 调用摄像头进行人脸识别,支持多张人脸同时识别; #. 摄像头人脸录入 / Face register .. image:: introduction/get_face_from_camera.png :align: center #. 提取特征建立人脸数据库 / Generate database from images captured #. 利用摄像头进行人脸识别 / Face recognizer 当单张人脸 / When single-face: .. image:: introduction/face_reco_single_person.png :align: center 当多张人脸 / When multi-faces: .. image:: introduction/face_reco_two_people.png :align: center ** 关于精度 / About accuracy: * When using a distance threshold of ``0.6``, the dlib model obtains an accuracy of ``99.38%`` on the standard LFW face recognition benchmark. Steps ***** #. 进行 face register / 人脸信息采集录入 .. code-block:: python python3 get_face_from_camera.py #. 提取所有录入人脸数据存入 features_all.csv .. code-block:: python python3 get_features_into_CSV.py #. 调用摄像头进行实时人脸识别 .. code-block:: python python3 face_reco_from_camera.py ***************** #. 新增功能:摄像头可以录制视频 ***************** # OpenCv 调用摄像头 # 默认调用笔记本摄像头 import cv2 cap = cv2.VideoCapture(0) # cap.set(propId, value) # 设置视频参数: propId - 设置的视频参数, value - 设置的参数值 cap.set(3, 480) # cap.isOpened() 返回 true/false, 检查摄像头初始化是否成功 print(cap.isOpened()) # cap.read() """ 返回两个值 先返回一个布尔值, 如果视频读取正确, 则为 True, 如果错误, 则为 False; 也可用来判断是否到视频末尾; 再返回一个值, 为每一帧的图像, 该值是一个三维矩阵; 通用接收方法为: ret,frame = cap.read(); ret: 布尔值; frame: 图像的三维矩阵; 这样 ret 存储布尔值, frame 存储图像; 若使用一个变量来接收两个值, 如: frame = cap.read() 则 frame 为一个元组, 原来使用 frame 处需更改为 frame[1] """ while cap.isOpened(): ret_flag, img_camera = cap.read() cv2.imshow("camera", img_camera) # 每帧数据延时 1ms, 延时为0, 读取的是静态帧 k = cv2.waitKey(1) # 按下 's' 保存截图 if k == ord('s'): cv2.imwrite("test.jpg", img_camera) # 按下 'q' 退出 if k == ord('q'): break # 释放所有摄像头 cap.release() # 删除建立的所有窗口 cv2.destroyAllWindows() ***************** # 使用摄像头录制视频 # coding:utf-8 +import sys +import cv2 +import warnings +warnings.filterwarnings('ignore') +face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml') +#face_cascade.load('D:\\opencv\\haarcascades\\haarcascade_frontalface_default.xml') +camear = cv2.VideoCapture(0)#用摄像头 +fps = 20 +## some videowriter props +size = (int(camear.get(cv2.CAP_PROP_FRAME_WIDTH)), + int(camear.get(cv2.CAP_PROP_FRAME_HEIGHT))) +videowriter = cv2.VideoWriter("test.mp4",-1,fps,size) +while(1): + ret,frame = camear.read() + gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) + faces = face_cascade.detectMultiScale(gray,1.3,5) + for (x,y,w,h) in faces: + img = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) + roi_gray = gray[y:y+h,x:x+w] + cv2.imshow('camera',frame) + videowriter.write(frame) + if cv2.waitKey(5)&0xff ==27: + break +camear.release() +cv2.destroyAllWindows()