opencv-读取摄像头信息-人脸识别
程序员文章站
2022-03-03 18:51:37
opencv-读取摄像头信息-人脸识别代码:import cv2import numpy as npimport os,sysface_xml = cv2.CascadeClassifier(r"C:\Users\lenovo\Desktop\python\jiqixuexi\shexiangtou_shibierenlian\haarcascade_frontalface_default.xml")eye_xml = cv2.CascadeClassifier(r"C:\Users\leno...
opencv-读取摄像头信息-人脸识别
采用的是haar特征,其中xml文件为opencv已经训练好的
笔者运行环境:
window10
vscode
代码:
import cv2 import numpy as np import os,sys
face_xml = cv2.CascadeClassifier(r"C:\Users\lenovo\Desktop\python\jiqixuexi\shexiangtou_shibierenlian\haarcascade_frontalface_default.xml") eye_xml = cv2.CascadeClassifier(r"C:\Users\lenovo\Desktop\python\jiqixuexi\shexiangtou_shibierenlian\haarcascade_eye.xml") cap = cv2.VideoCapture(0) fps = cap.get(cv2.CAP_PROP_FPS) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) print(fps,width,height) print(os.getcwd) i = 0 while True:
os.chdir(r"C:\Users\lenovo\Desktop\python\jiqixuexi\shexiangtou_shibierenlian\chuliqian") if i == 3000: break else:
i = i+1 (flag,frame) = cap.read() fileName = "image" +str(i) +".jpg" if flag == True:
cv2.imwrite(fileName,frame,[cv2.IMWRITE_JPEG_QUALITY,100]) img = cv2.imread(fileName) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) faces = face_xml.detectMultiScale(gray,1.3,5) print("face=",len(faces)) for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_face = gray[y:y+h,x:x+w] roi_color = img[y:y+h,x:x+w] eyes = eye_xml.detectMultiScale(roi_face) for (e_x,e_y,e_w,e_h) in eyes:
cv2.rectangle(roi_color,(e_x,e_y),(e_x+e_w,e_y+e_h),(0,255,0),2) os.chdir(r"C:\Users\lenovo\Desktop\python\jiqixuexi\shexiangtou_shibierenlian\chulihou") cv2.imwrite(fileName,img,[cv2.IMWRITE_JPEG_QUALITY,100]) cv2.imshow("img",img) if cv2.waitKey(1) == 27: break if cv2.waitKey(1) == ord("e"): break cap.release() cv2.destroyAllWindows()
效果展示:
代码解析:
代码主要分为俩部分组成:
1.打开摄像头,并且读取每一帧的图像,并且保存(应该也可以不用保存,直接到下一步进行人脸识别),此处我设置了终止条件,读取3000帧图片,因为发现每秒30帧,所以应该100秒停止,或者后边设置的按esc键或e键停止。
cap = cv2.VideoCapture(0) fps = cap.get(cv2.CAP_PROP_FPS) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) print(fps,width,height) print(os.getcwd) i = 0 while True:
os.chdir(r"C:\Users\lenovo\Desktop\python\jiqixuexi\shexiangtou_shibierenlian\chuliqian") if i == 3000: break else:
i = i+1 (flag,frame) = cap.read() fileName = "image" +str(i) +".jpg" if flag == True:
cv2.imwrite(fileName,frame,[cv2.IMWRITE_JPEG_QUALITY,100])
2.将摄像头保存的图片进行人脸识别处理,并且再显示出来
也设置了终止条件,按esc键或e键停止,一定要注意是在显示窗口下按esc键或者e键。
img = cv2.imread(fileName) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) faces = face_xml.detectMultiScale(gray,1.3,5) print("face=",len(faces)) for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_face = gray[y:y+h,x:x+w] roi_color = img[y:y+h,x:x+w] eyes = eye_xml.detectMultiScale(roi_face) for (e_x,e_y,e_w,e_h) in eyes:
cv2.rectangle(roi_color,(e_x,e_y),(e_x+e_w,e_y+e_h),(0,255,0),2) os.chdir(r"C:\Users\lenovo\Desktop\python\jiqixuexi\shexiangtou_shibierenlian\chulihou") cv2.imwrite(fileName,img,[cv2.IMWRITE_JPEG_QUALITY,100]) cv2.imshow("img",img) if cv2.waitKey(1) == 27: break if cv2.waitKey(1) == ord("e"): break cap.release() cv2.destroyAllWindows()
本文地址:https://blog.csdn.net/weixin_45839124/article/details/109045769