视频标注(vott)+自动标注(vott)+ 视频标注框显示(python)
下载vott:www.github.com/Microsoft/VoTT/releases
第一步:创建文件夹
source文件夹:用来放需要标注的视频
target文件夹:用来放vott导出的json文件
result文件夹:用来存放python转换后的视频和图片
第二步:创建项目
1.打开vott,点击New Project
2.给项目取名为text,
2.添加源文件夹,导入需要标注的视频
3.添加目标文件夹,即设置标注视频导出的json文件存放的位置,步骤同上
4.保存项目
第三步:打视频标注
1.手动标注
2.自动标注(自动标注没手动的精细,一般需对自动的进行调整)
第四步:保存导出
第五步:视频标注框显示
pip install bounding_box --user
pip install opencv-python --user
pip install opencv-contrib-python --user
import json
import cv2
import os
from bounding_box import bounding_box as bbox_plot
# 填写标签名称
Draw_Tags = ['turtle','banana']
# 填写target文件夹中vott-json-export文件夹的路径
Img_Dir = r'D:\English_path\text\target\vott-json-export'
# 填写result文件夹,即保存显示标注框视频的路径
Save_Dir = r'D:\English_path\text\result'
def make_video(img_dir, img_size=(1920, 1080), fps=5):
file_path = os.path.join(Save_Dir, "demo.mp4")
filelist = os.listdir(img_dir)
fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X')
video = cv2.VideoWriter(file_path, fourcc, fps, img_size)
for item in filelist:
if item.endswith('.jpg'):
item = img_dir + '/' + item
img = cv2.imread(item)
video.write(img)
video.release()
def draw_one_img(img_name, img_timestamp, labels_list):
img_path = os.path.join(Img_Dir, img_name)
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
for label in labels_list:
tag = label['tags'][0]
if not tag in Draw_Tags:
continue
bbox = label['boundingBox']
left = bbox['left']
top = bbox['top']
right = left + bbox['width']
bottom = top + bbox['height']
bbox_plot.add(img, left, top, right, bottom, tag, 'red')
save_name = 'time%s.jpg' % format(img_timestamp, '.2f').replace('.', '')
save_path = os.path.join(Save_Dir, save_name)
cv2.imwrite(save_path, img)
print(save_name, 'ok~')
def main():
lb_json = os.path.join(Img_Dir, 'text-export.json') # 填写\target\vott-json-export文件夹中.json后缀的文件名
with open(lb_json, 'r',encoding='utf-8') as f:
labels_dict = json.load(f)
assets = labels_dict['assets']
size = None
for k, v in assets.items():
img_name = v['asset']['name']
img_timestamp = v['asset']['timestamp']
if size is None:
w = v['asset']['size']['width']
h = v['asset']['size']['height']
size = (w, h)
labels_list = v['regions']
draw_one_img(img_name, img_timestamp, labels_list)
make_video(Save_Dir, img_size=size)
if __name__ == '__main__':
main()
作者:雨前老鼠干