使用基于状态的标签注释视频帧

时间:2015-05-04 11:20:22

标签: python matlab video annotations computer-vision

我有一堆视频和深度图显示来自Microsoft Kinect的人体姿势。

我可以在视频中获得人类的骨架,但我想要做的是识别这个骨架数据中的某个姿势。

要做到这一点,我需要用0或1注释视频中的每个帧,对应于“坏姿势”和“好姿势”,即帧具有二进制状态变量。

我希望能够在matlab中播放avi文件,然后按空格键在这两种状态之间切换,同时将状态变量添加到一个数组中,为视频中的每一帧提供状态。

matlab中是否有可以执行此操作的工具?否则matlab不是限制,python,C ++或任何其他语言都可以。

我一直在谷歌上搜索,我发现的大部分内容都是用多边形注释单个框架。我希望以视频的常规帧率的一半来做到这一点。

编辑:我使用了miindlek提供的解决方案,并决定分享一些事情,如果有人碰到这个。我需要在视频中看到我分配给每个帧的注释,所以当我显示时,我在视频的左上角做了一个小圆圈。希望这对以后的其他人有用。我还捕获了使用waitKey按下的键,然后根据输出执行某些操作。这允许在注释期间按下多个键。

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 个答案:

答案 0 :(得分:3)

解决您的任务的一种方法是使用带有python的opencv库,如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()

变量annotation_list包含每个帧的所有注释。要在两种模式之间切换,您必须按空格键。