[人臉識別]08-人臉錄入
阿新 • • 發佈:2022-04-09
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 inface: 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 iford('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.執行