使用LabelMe+python间隔提取视频帧

mac2025-03-15  12

项目中需要对数据集进行标记

首先安装环境,https://github.com/wkentaro/labelme有教程。

本机是window环境已经装好了anaconda,然后在此基础上创建一个新的labelme环境

命令:

conda create --name=labelme python=3.6 (base) C:\Users\Administrator>conda create --name=labelme python=3.6 Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 4.5.11 latest version: 4.7.12 Please update conda by running $ conda update -n base -c defaults conda ## Package Plan ## environment location: D:\Anaconda3\envs\labelme added / updated specs: - python=3.6 The following packages will be downloaded: package | build ---------------------------|----------------- pip-19.3.1 | py36_0 1.9 MB vs2015_runtime-14.16.27012 | hf0eaf9b_0 2.4 MB wincertstore-0.2 | py36h7fe50ca_0 13 KB python-3.6.9 | h5500b2f_0 20.4 MB certifi-2019.9.11 | py36_0 155 KB sqlite-3.30.1 | he774522_0 962 KB setuptools-41.6.0 | py36_0 677 KB wheel-0.33.6 | py36_0 58 KB ------------------------------------------------------------ Total: 26.5 MB The following NEW packages will be INSTALLED: certifi: 2019.9.11-py36_0 pip: 19.3.1-py36_0 python: 3.6.9-h5500b2f_0 setuptools: 41.6.0-py36_0 sqlite: 3.30.1-he774522_0 vc: 14.1-h0510ff6_4 vs2015_runtime: 14.16.27012-hf0eaf9b_0 wheel: 0.33.6-py36_0 wincertstore: 0.2-py36h7fe50ca_0 Proceed ([y]/n)? y Downloading and Extracting Packages pip-19.3.1 | 1.9 MB | #################################### | 100% vs2015_runtime-14.16 | 2.4 MB | #################################### | 100% wincertstore-0.2 | 13 KB | #################################### | 100% python-3.6.9 | 20.4 MB | #################################### | 100% certifi-2019.9.11 | 155 KB | #################################### | 100% sqlite-3.30.1 | 962 KB | #################################### | 100% setuptools-41.6.0 | 677 KB | #################################### | 100% wheel-0.33.6 | 58 KB | #################################### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate labelme # # To deactivate an active environment, use # # $ conda deactivate (base) C:\Users\Administrator>

激活环境:使用

conda activate labelme

安装

pip install labelme Successfully built labelme termcolor Installing collected packages: pyparsing, numpy, six, python-dateutil, kiwisolve r, cycler, matplotlib, Pillow, PyYAML, qtpy, termcolor, PyQt5-sip, PyQt5, labelm e Successfully installed Pillow-6.2.1 PyQt5-5.13.1 PyQt5-sip-12.7.0 PyYAML-5.1.2 c ycler-0.10.0 kiwisolver-1.1.0 labelme-3.16.7 matplotlib-3.1.1 numpy-1.17.3 pypar sing-2.4.2 python-dateutil-2.8.0 qtpy-1.9.0 six-1.12.0 termcolor-1.1.0

有一个问题: 

Successfully built labelme imgviz termcolor ERROR: imgviz 0.12.0 has requirement Pillow<7,>=5.3.0, but you'll have pillow 7.0.0 which is incompatible. Installing collected packages: kiwisolver, pyparsing, numpy, six, python-dateutil, cycler, matplotlib, Pillow, imgviz, PyYAML, qtpy, termcolor, PyQt5-sip, PyQt5, labelme Successfully installed Pillow-7.0.0 PyQt5-5.14.1 PyQt5-sip-12.7.1 PyYAML-5.3 cycler-0.10.0 imgviz-0.12.0 kiwisolver-1.1.0 labelme-4.2.7 matplotlib-3.1.3 numpy-1.18.1 pyparsing-2.4.6 python-dateutil-2.8.1 qtpy-1.9.0 six-1.14.0 termcolor-1.1.0

看一下指令

(labelme) C:\Users\Administrator>labelme -h usage: labelme [-h] [--version] [--reset-config] [--logger-level {debug,info,warning,fatal,error}] [--output OUTPUT] [--config CONFIG_FILE] [--nodata] [--autosave] [--nosortlabels] [--flags FLAGS] [--labelflags LABEL_FLAGS] [--labels LABELS] [--validatelabel {exact,instance}] [--keep-prev] [--epsilon EPSILON] [filename] positional arguments: filename image or label filename optional arguments: -h, --help show this help message and exit --version, -V show version --reset-config reset qt config --logger-level {debug,info,warning,fatal,error} logger level --output OUTPUT, -O OUTPUT, -o OUTPUT output file or directory (if it ends with .json it is recognized as file, else as directory) --config CONFIG_FILE config file (default: C:\Users\Administrator\.labelmerc) --nodata stop storing image data to JSON file --autosave auto save --nosortlabels stop sorting labels --flags FLAGS comma separated list of flags OR file containing flags --labelflags LABEL_FLAGS yaml string of label specific flags OR file containing json string of label specific flags (ex. {person-\d+: [male, tall], dog-\d+: [black, brown, white], .*: [occluded]}) --labels LABELS comma separated list of labels OR file containing labels --validatelabel {exact,instance} label validation types --keep-prev keep annotation of previous frame --epsilon EPSILON epsilon to find nearest vertex on canvas

直接输入labelme运行gui(logo是lena!)


到这一步想起来还需要用matlab提取视频的关键帧。。

obj = VideoReader('F://ds//8//ncf5f8001.avi');%输入视频位置 numFrames = obj.NumberOfFrames;% 帧的总数  for k = 1 : numFrames% 读取前15帧      frame=read(obj,k);%读取第几帧     % imshow(frame);%显示 k=k+30;      imwrite(frame,strcat('F:\ds\8\',num2str(k),'.jpg'),'jpg');% 保存帧  end

出现了错误:

搜索一下发现可能是本机不支持avi的播放所导致的,因此下载一个media player codec pack http://www.mediaplayercodecpack.com/,安装到电脑上试试。(基本上一路next就可以了)仍然不行,改用python好了


参考:https://blog.csdn.net/qq_38214193/article/details/80997924

先安装一下opencv,一句话:pip install opencv-python

Requirement already satisfied: numpy>=1.11.3 in d:\anaconda3\envs\labelme\lib\s ite-packages (from opencv-python) (1.17.3) Installing collected packages: opencv-python Successfully installed opencv-python-4.1.1.26

说明已经安装好了

import cv2 # 使用opencv按一定间隔截取视频帧,并保存为图片 vc = cv2.VideoCapture('F:/ds/8/ncf5f8001.avi') # 读取视频文件 c = 1 if vc.isOpened():# 判断是否正常打开 rval,frame = vc.read() else: rval = False timeF = 15 #视频帧计数间隔频率 while rval: #循环读取视频帧 rval,frame = vc.read() if (c%timeF == 0): #每隔timeF帧进行存储操作 cv2.imwrite('F:/ds/8'+str(c)+'.jpg',frame) # 存储为图像 c = c + 1 cv2.waitKey(1) vc.release()

进入当前目录 cd /f f:\ds\8\ 运行python frame.py提取关键帧如下:

把它变成一个工具

第一个参数为视频所在目录,第二个为视频文件名,第三个为提取的帧数。

python frame.py F:\ds\10\ ncf5f10001.avi 10

import sys import cv2 dir = sys.argv[1] file = sys.argv[2] time = sys.argv[3] vc = cv2.VideoCapture(dir+file) c = 1 if vc.isOpened(): rval,frame = vc.read() else: rval = False timeF = int(time) while rval: rval,frame = vc.read() if (c%timeF == 0): cv2.imwrite(dir+str(c)+'.jpg',frame) c = c + 1 cv2.waitKey(1) vc.release()

继续打开labelme进行提取。

open打开一张图片,点击Create Polygons开始标定。鼠标定点,框住生成区域打标签就可以。

就这样搞定一张图

然后保存为json,注意json名应当与图片名一致。

最新回复(0)