当前位置:网站首页>Face detection and tracking based on Dlib database

Face detection and tracking based on Dlib database

2022-07-08 02:21:00 So come on

1、 be based on dlib library —— Face detection 、 Track face , Add two auxiliary functions

1、 Add function 1 : Save the video

2、 Add function 2 : message cv.putText() Function application

The algorithm is still relatively old , Easy to identify errors .

Identify errors

#  The algorithm is still relatively old , Easy to identify errors .

# 1  Add to Library 
import cv2
import dlib

#  Add function 2 : message   cv.putText() Function application 
def show_info(frame, tracking_state):
    #  Two anchor points , Write the corresponding content at the point .
    pos1 = (20, 40)
    pos2 = (20, 80)
    cv2.putText(frame, "'1' : reset ", pos1, cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255))  #  The last few parameters are about fonts .
    #  According to the State , Display different information 
    if tracking_state is True:
        cv2.putText(frame, "tracking now ...", pos2, cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0))
    else:
        cv2.putText(frame, "no tracking ...", pos2, cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0))

# 2  The main function 
def main():
    # 3  Turn on the camera 
    capture = cv2.VideoCapture(0)

    # 4  be based on dlib Library get face detector 
    detector = dlib.get_frontal_face_detector()

    # 5  be based on dlib Library real-time tracking 
    tractor = dlib.correlation_tracker()

    # 6  Tracking status 
    tracking_state = False

    #  Add function 1 : Save the video 
    # frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
    w = int(capture.get(3))  #  Get video of width
    # frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
    h = int(capture.get(4))  #  Get video of height
    # frame_fps = capture.get(cv2.CAP_PROP_FPS) #  There are mistakes , I still don't understand 

    # #  Format the video 
    fourcc = cv2.VideoWriter_fourcc(*"XVID")
    # output = cv2.VideoWriter('record.avi', fourcc, 30.0, (int(frame_width), int(frame_height)), True)  #  It means color , Sure 
    # output = cv2.VideoWriter('record.avi', fourcc, int(frame_fps), (int(frame_width), int(frame_height)), True)  #  Output error 
    # output= cv2.VideoWriter(args.video_output, fourcc, int(frame_fps), (int(frame_width), int(frame_height)), False) #  Output error 
    output = cv2.VideoWriter(r"C:\Users\xiaohao\Desktop\record.mp4", fourcc, 15.0, (w, h))

    # 7  Cycle through every frame 
    while True:
        ret, frame = capture.read()

        #  Display prompt message , In circulation .
        show_info(frame, tracking_state)

        # 8  If there is no tracking ,  Start the tracker 
        if tracking_state is False:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            dets = detector(gray, 1) #  Return the detected face 
            if len(dets) > 0:
                tractor.start_track(frame, dets[0])
                tracking_state = True

        # 9  Tracking , Get the position of the face in real time , Show 
        if tracking_state is True:
            tractor.update(frame) #  Follow the new picture 
            position = tractor.get_position() #  Get face coordinates 
            cv2.rectangle(frame,(int(position.left()), int(position.top())), (int(position.right()), int(position.bottom())), (0,255,0), 3)

        key = cv2.waitKey(1) & 0xFF

        if key == ord('q'):
            break

        #  Resume reset 
        if key == ord('1'):
            tracking_state = False

        cv2.imshow("face tracking", frame)
        #  Save the video 
        output.write(frame)

    capture.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()

2、 Running error

output = cv2.VideoWriter("record.mp4", fourcc, int(frame_fps), (int(frame_width), int(frame_height)), True)

SystemError: <class 'cv2.VideoWriter'> returned a result with an error set

The length and width of the picture can be obtained normally (int(frame_width), int(frame_height)), The frame value is not determined int(frame_fps), You can input data by yourself . The specific reason , I'm eager to leave a message for help .

原网站

版权声明
本文为[So come on]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130539412181.html