[人脸识别]08-人脸录入

1-程序

#导入CV模块

import cv2 as cv
#import os

def face_detect_fun(img):
    gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    print(cv.data.haarcascades)
    face_detect=cv.CascadeClassifier(cv.data.haarcascades+"haarcascade_frontalface_default.xml")
    face=face_detect.detectMultiScale(gray)

    for x,y,w,h in face:
        cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
    cv.imshow("result",img)

#读取摄像头
#root = os.getcwd()
cap=cv.VideoCapture(0)

flag=1
num=1

#等待
while (cap.isOpened()):
    ret,frame=cap.read()
    if not ret:
        break

    cv.imshow("capture",frame)

    key=cv.waitKey(1) & 0xff

    if ord('q')==key:
        break
    if ord('s')==key:
        #cv.imwrite(root+"/"+str(num)+".jpg",frame)
        #print("save ok:"+root+"/"+str(num)+".jpg")
        cv.imwrite("./jm/" + str(num) + ".zhangsan.jpg", frame)
        print("save ok:" + "./jm/" + str(num) + ".zhangsan.jpg")
        num=num+1


#释放内存
cv.destroyAllWindows()

#释放摄像头
cap.release()

 

 

 

2.运行

 

 

 

posted @ 2022-04-09 16:17  cqmcu_yth  阅读(271)  评论(0编辑  收藏  举报