python opencv face detection extraction and preservation methods

  • 2020-12-09 00:55:58
  • OfStack

Note that the address of the extracted face image should be changed to the address you want to save

The path of opencv's face detection model should also be changed to the path of opencv's face detection model installed by yourself


import cv2
save_path = 'F:\\face_photo_save\\chenym\\'
cascade = cv2.CascadeClassifier("D:\\opencv249\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml")
cap = cv2.VideoCapture(0)
i = 0
while True:
 ret,frame = cap.read()
 gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
 rect = cascade.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=9,minSize=(50,50),flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
 print "rect",rect
 if not rect is (): 
  for x,y,z,w in rect:
   roiImg = frame[y:y+w,x:x+z]
   cv2.imwrite(save_path+str(i)+'.jpg',roiImg)
   cv2.rectangle(frame,(x,y),(x+z,y+w),(0,0,255),2)
   i +=1
 cv2.imshow('frame',frame)  
 if cv2.waitKey(1) &0xFF == ord('q'):
  break
cap.release()
cv2.destroyAllWindows()

Related articles: