Annotating video frames with a label based on state

帅比萌擦擦* 提交于 2021-01-24 09:10:45

问题


I have a bunch of videos and depthmaps showing human poses from the Microsoft Kinect.

I can get a skeleton of the human in the video but what I want to do is recognize a certain pose from this skeleton data.

To do that I need to annotate each frame in the videos with a 0 or 1, corresponding to "bad pose" and "good pose", i.e. the frame has a binary state variable.

I want to be able to playback the avi file in matlab and then press space to switch between these two states and simultaneously add the state variable to an array giving the state for each frame in the video.

Is there a tool in matlab that can do this? Otherwise matlab is not a restriction, python, C++ or any other language is fine.

I have been googling around, and most of the stuff I have found is to annotate individual frames with a polygon. I want to do this at maybe half the regular framerate of the video.

EDIT: I used the solution provided by miindlek and decided to share a few things if someone runs into this. I needed to see in the video what annotation I was assigning to each frame, so I made a small circle in the upper left corner of the video as I displayed it. Hopefully this will be useful for someone else later. I also capture the key pressed with waitKey and then do something based on the output. This allows for multiple keys to be pressed during the annotations.

import numpy as np
import cv2
import os
os.chdir('PathToVideo')

# Blue cicle means that the annotation haven't started
# Green circle is a good pose
# Red is a bad pose
# White circle means we are done, press d for that

# Instructions on how to use!
# Press space to swap between states, you have to press space when the person
# starts doing poses. 
# Press d when the person finishes.
# press q to quit early, then the annotations are not saved, you should only 
# use this if you made a mistake and need to start over.

cap = cv2.VideoCapture('Video.avi')

# You can INCREASE the value of speed to make the video SLOWER
speed = 33

# Start with the beginning state as 10 to indicate that the procedure has not started
current_state = 10
saveAnnotations = True
annotation_list = []
# We can check wether the video capture has been opened
cap.isOpened()
colCirc = (255,0,0)
# Iterate while the capture is open, i.e. while we still get new frames.
while(cap.isOpened()):
    # Read one frame.
    ret, frame = cap.read()
    # Break the loop if we don't get a new frame.
    if not ret:
        break
    # Add the colored circle on the image to know the state
    cv2.circle(frame,(50,50), 50, colCirc, -1)
    # Show one frame.
    cv2.imshow('frame', frame)
    # Wait for a keypress and act on it
    k = cv2.waitKey(speed)
    if k == ord(' '):
        if current_state==0:
            current_state = 1
            colCirc = (0,0,255)
        else:
            current_state = 0
            colCirc = (0,255,0)
        if current_state == 10:
            current_state = 0
            colCirc = (0,255,0)
    if k == ord('d'):
        current_state = 11
        colCirc = (255,255,255)

    # Press q to quit
    if k == ord('q'):
        print "You quit! Restart the annotations by running this script again!"
        saveAnnotations = False
        break

    annotation_list.append(current_state)

# Release the capture and close window
cap.release()
cv2.destroyAllWindows()

# Only save if you did not quit
if saveAnnotations:
    f = open('poseAnnot.txt', 'w')
    for item in annotation_list:
        print>>f, item
    f.close()

回答1:


One way to solve your task is using the opencv library with python, as described in this tutorial.

import numpy as np
import cv2

cap = cv2.VideoCapture('video.avi')

current_state = False
annotation_list = []

while(True):
    # Read one frame.
    ret, frame = cap.read()
    if not ret:
        break

    # Show one frame.
    cv2.imshow('frame', frame)

    # Check, if the space bar is pressed to switch the mode.
    if cv2.waitKey(1) & 0xFF == ord(' '):
        current_state = not current_state

    annotation_list.append(current_state)

# Convert the list of boolean values to a list of int values.    
annotation_list = map(int, annotation_list)
print annotation_list

cap.release()
cv2.destroyAllWindows()

The variable annotation_list contains all annotations for each frame. To switch between the two modes, you have to press the space bar.



来源:https://stackoverflow.com/questions/30028841/annotating-video-frames-with-a-label-based-on-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!