如何提取从视频流中检测到的68个面部地标并以骨架视图显示

时间:2019-05-21 14:32:07

标签: python numpy opencv cv2 dlib

我只想看到68个面部地标的骨架视图

我试图用numpy创建黑色背景 黑色= np.zeros(512,512,3),np.uint8) 并将检测到的地标放置在其上而不是框架本身上


# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), fps, (frame_width,frame_height))

time.sleep(2.0)



# loop over the frames from the video file
#2D image points. If you change the image, you need to change vector
image_points = np.array([
                            (355, 391),     # Nose tip 34
                            (389, 541),     # Chin 9
                            (327, 227),     # Left eye left corner 37
                            (533, 301),     # Right eye right corne 46
                            (345, 465),     # Left Mouth corner 49
                            (455, 415)      # Right mouth corner 55
                        ], dtype="double")

# 3D model points.
model_points = np.array([
                            (0.0, 0.0, 0.0),             # Nose tip 34
                            (0.0, -330.0, -65.0),        # Chin 9
                            (-225.0, 170.0, -135.0),     # Left eye left corner 37
                            (225.0, 170.0, -135.0),      # Right eye right corne 46
                            (-150.0, -150.0, -125.0),    # Left Mouth corner 49
                            (150.0, -150.0, -125.0)      # Right mouth corner 55

                        ])

black = np.zeros((512,512,3), np.uint8)
# Iterating count
i=0
# Read until video is completed
while(cap.isOpened()):
    # Capture frame-by-frame
    ret, image = cap.read()
    if ret == True:
        frame = imutils.resize(image, width=frame_width, height=frame_height)
        # Convert image to grayscale
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # Process The Image
        size = gray.shape
        # Detect faces
        rects = detector(gray,0)
        # check to see if a face was detected, and if so, draw the total
        # number of faces on the frame
        if len(rects) > 0:
            for rect in rects:
                # compute the bounding box of the face and draw it on the
                # frame
                (bX, bY, bW, bH) = face_utils.rect_to_bb(rect)
                cv2.rectangle(black, (bX, bY), (bX + bW, bY + bH),(0, 255, 0), 1)
            # determine the facial landmarks for the face region, then
            # convert the facial landmark (x, y)-coordinates to a NumPy
            # array
                shape = predictor(gray, rect)
                shape = face_utils.shape_to_np(shape)
                # loop over the (x, y)-coordinates for the facial landmarks
                # and draw each of them
                for (i, (x, y)) in enumerate(shape):
                    if i == 33:
                    #something to our key landmarks
                    # save to our new key point list
                    # i.e. keypoints = [(i,(x,y))]
                    image_points[0] = np.array([x,y],dtype='double')
                    # write on frame in Green
                    cv2.circle(black, (x, y), 1, (0, 255, 0), -1)
                    elif i == 8:
                        #something to our key landmarks
                        # save to our new key point list
                        # i.e. keypoints = [(i,(x,y))]
                        image_points[1] = np.array([x,y],dtype='double')
                        # write on frame in Green
                        cv2.circle(black, (x, y), 1, (0, 255, 0), -1)
                    elif i == 36:
                        #something to our key landmarks
                        # save to our new key point list
                        # i.e. keypoints = [(i,(x,y))]
                        image_points[2] = np.array([x,y],dtype='double')
                        # write on frame in Green
                        cv2.circle(black, (x, y), 1, (0, 255, 0), -1)
                    elif i == 45:
                        #something to our key landmarks
                        # save to our new key point list
                        # i.e. keypoints = [(i,(x,y))]
                        image_points[3] = np.array([x,y],dtype='double')
                        # write on frame in Green
                        cv2.circle(black, (x, y), 1, (0, 255, 0), -1)
                    elif i == 48:
                        #something to our key landmarks
                        # save to our new key point list
                        # i.e. keypoints = [(i,(x,y))]
                        image_points[4] = np.array([x,y],dtype='double')
                        # write on frame in Green
                        cv2.circle(black, (x, y), 1, (0, 255, 0), -1)
                    elif i == 54:
                        #something to our key landmarks
                        # save to our new key point list
                        # i.e. keypoints = [(i,(x,y))]
                        image_points[5] = np.array([x,y],dtype='double')
                        # write on frame in Green
                        cv2.circle(black, (x, y), 1, (0, 255, 0), -1)
                    else:
                        #everything to all other landmarks
                        # write on frame in Red
                        cv2.circle(black, (x, y), 1, (0, 0, 255), -1)
                # Add pose estimation
                # camera internals
                focal_length = size[1]
                center = (size[1]/2, size[0]/2)
                camera_matrix = np.array([[focal_length,0,center[0]],[0, focal_length, center[1]],[0,0,1]], dtype="double")

                print "Camera Matrix :\n {0}".format(camera_matrix)

                dist_coeffs = np.zeros((4,1)) # Assuming no lens distortion
                (success, rotation_vector, translation_vector) = cv2.solvePnP(model_points, image_points, camera_matrix, dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE)#flags=cv2.CV_ITERATIVE)

                print "Rotation Vector:\n {0}".format(rotation_vector)
                print "Translation Vector:\n {0}".format(translation_vector)
                # Project a 3D point (0, 0 , 1000.0) onto the image plane
                # We use this to draw a line sticking out of the nose_end_point2D
                (nose_end_point2D, jacobian) = cv2.projectPoints(np.array([(0.0, 0.0, 1000.0)]),rotation_vector, translation_vector, camera_matrix, dist_coeffs)
                for p in image_points:
                    cv2.circle(black, (int(p[0]), int(p[1])), 3, (0,0,255), -1)

                p1 = ( int(image_points[0][0]), int(image_points[0][1]))
                p2 = ( int(nose_end_point2D[0][0][0]), int(nose_end_point2D[0][0][1]))
                cv2.line(black, p1, p2, (255,0,0), 2)


        # Get image dimensions
        #height, width = image.shape[:2]

        # Output landmarks to black screen
        out.write(black)
        # Display image to screen
        cv2.imshow('Output Image',black)
        # Wait for key press 0
        cv2.waitKey(1)
        i+=1

        # Press Q on keyboard to  exit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    # Break the loop
    else:
        break

# Remove all windows when finished
out.release()
cv2.destroyAllWindows()

我希望输出在检测后具有透明/任何颜色的背景,而不是帧本身


expectedOutput,currentOutput

enter image description here

0 个答案:

没有答案