Python+OpenCV: cv2.imwrite

后端 未结 3 1175
离开以前
离开以前 2021-01-30 09:23

I\'m trying to detect a face and write down area with the face in a separate file. How can I do it? I think that i must use \"faces\" (you can see this var in code). But how?

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 10:09

    This following code should extract face in images and save faces on disk

    def detect(image):
        image_faces = []
        bitmap = cv.fromarray(image)
        faces = cv.HaarDetectObjects(bitmap, cascade, cv.CreateMemStorage(0))
        if faces:
            for (x,y,w,h),n in faces:
                image_faces.append(image[y:(y+h), x:(x+w)])
                #cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,255),3)
        return image_faces
    
    if __name__ == "__main__":
        cam = cv2.VideoCapture(0)
        while 1:
            _,frame =cam.read()
            image_faces = []
            image_faces = detect(frame)
            for i, face in enumerate(image_faces):
                cv2.imwrite("face-" + str(i) + ".jpg", face)
    
            #cv2.imshow("features", frame)
            if cv2.waitKey(1) == 0x1b: # ESC
                print 'ESC pressed. Exiting ...'
                break
    

提交回复
热议问题