目标检测学习笔记(二)——图片取样及数据集制作

2024-02-07 04:08

本文主要是介绍目标检测学习笔记(二)——图片取样及数据集制作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 拍摄视频并提取图片
    • 拍摄视频
    • 提取图片
      • 方法1:视频帧差法提取图片
      • 方法2:定时从视频截图
    • 整理文件并重命名
  • 数据集制作
      • labelimg的安装及使用
      • 批量修改标签名称

前言

参考:

从视频中提取图片:如何将视频逐帧提取成为一张张图片?

图片重命名:python文件批量改名

制作数据集:睿智的目标检测12——使用labeling进行目标检测数据集标注

批量修改标签名称:如何批量修改Pascal VOC数据集中xml标签中的标签名称

拍摄视频并提取图片

拍摄视频

getvedio.py

'''
python 录制mp4视频 by 郑瑞国
'''
import numpy as np
import cv2cap = cv2.VideoCapture(0)## some videowriter props
sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))fps = 20
# fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
# fourcc = cv2.VideoWriter_fourcc('m', 'p', 'e', 'g')
fourcc = cv2.VideoWriter_fourcc(*'mpeg')## open and set props
out = cv2.VideoWriter()
out.open('getvedio/16.mp4', fourcc, fps, sz, True)while (True):ret, frame = cap.read()out.write(frame)cv2.imshow('frame', frame)if cv2.waitKey(1) & 0xff == ord('q'):breakout.release()
cap.release()
cv2.destroyAllWindows()

提取图片

方法1:视频帧差法提取图片

frame_extract.py

# -*- coding: utf-8 -*-import cv2
import numpy as np
import os
import urllibdef frame_difference(frame1, frame2):   #求取侦差# f1, f2 f1 = cv2.resize(frame1, (416, 416))f2 = cv2.resize(frame2, (416, 416))f1 = cv2.cvtColor(f1, cv2.COLOR_BGR2GRAY)f2 = cv2.cvtColor(f2, cv2.COLOR_BGR2GRAY)                 # 灰度化f1 = cv2.threshold(f1, 128, 255, cv2.THRESH_BINARY)[1]    # 二值化f2 = cv2.threshold(f2, 128, 255, cv2.THRESH_BINARY)[1]diff = cv2.absdiff(f1, f2)          #求取差值p = cv2.countNonZero(diff) / float(416 * 416)return pif __name__ == "__main__":root = r'F:\0-0\Myyolo\vedio_img\getvedio'threshold = 0.09      #阙值frame_interval = 15    #帧间隔save_root = r'F:\0-0\Myyolo\vedio_img\getvedio\img'for c, path in enumerate(os.listdir(root)):path = os.path.join(root, path)video = cv2.VideoCapture(path)count = 0st, frame1 = video.read() #st,frame是获cv2.VideoCapture.read()方法的两个返回值。st, frame2 = video.read() #其中st是布尔值,如果读取帧是正确的则返回True,如果文件读取到结尾,它的返回值就为False。frame就是每一帧的图像,是个三维矩阵。while st:if count % frame_interval == 0:st, frame2 = video.read()p = frame_difference(frame1, frame2)  #求取侦差print(p)if p > threshold:save_dir = os.path.join(save_root, os.path.splitext(os.path.basename(path))[0])if not os.path.exists(save_dir):                                        #如果目录不存在os.makedirs(save_dir)   #创建目录print(save_dir)#cv2.imencode(os.path.join(save_dir, str(count)+'.jpg'), frame1)#cv2.imencode(str(count)+'.jpg', frame1)[1].tofile(save_dir)cv2.imwrite(os.path.join(save_dir, str(count) + '.jpg'), frame1)print('保存图片')frame1 = frame2st, frame2 = video.read()count += 1if st:cv2.imshow("video%d"%c, frame2)  #函数可以在窗口中显示图像。该窗口和图像的原始大小自适应(自动调整到原始尺寸)。cv2.waitKey(100)   #是一个和键盘绑定的函数,它的作用是等待一个键盘的输入(因为我们创建的图片窗口如果没有这个函数的话会闪一下就消失了,所以如果需要让它持久输出,我们可以使用该函数)。它的参数是毫秒级。该函数等待任何键盘事件的指定毫秒。如果您在此期间按下任何键,程序将继续进行。我们也可以将其设置为一个特定的键。video.release()           #发布软件资源  /  释放硬件资源cv2.destroyAllWindows()   #销毁我们创建的所有窗口。如果要销毁任何特定窗口,请使用函数cv2.destroyWindow(),其中传递确切的窗口名称作为参数。(应该是使用创建窗口时所使用的窗口名称,字符串类型。)print("video%d"%c, "done!")

方法2:定时从视频截图

使用img_extract.py

# 每隔 多少 秒 提取视频中的图片到文件夹# This is a sample Python script.# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.def print_hi(name):# Use a breakpoint in the code line below to debug your script.print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.# Press the green button in the gutter to run the script.
if __name__ == '__main__':print_hi('PyCharm')# See PyCharm help at https://www.jetbrains.com/help/pycharm/
# -*- coding: utf-8 -*-
import os
import cv2    ##加载OpenCV模块def video2frames(pathIn: object = '',pathOut: object = '',only_output_video_info: object = False,extract_time_points: object = None,initial_extract_time: object = 0,end_extract_time: object = None,extract_time_interval: object = -1,output_prefix: object = 'frame',jpg_quality: object = 100,isColor: object = True) -> object:'''pathIn:视频的路径,比如:F:\python_tutorials\test.mp4pathOut:设定提取的图片保存在哪个文件夹下,比如:F:\python_tutorials\frames1\。如果该文件夹不存在,函数将自动创建它only_output_video_info:如果为True,只输出视频信息(长度、帧数和帧率),不提取图片extract_time_points:提取的时间点,单位为秒,为元组数据,比如,(2, 3, 5)表示只提取视频第2秒, 第3秒,第5秒图片initial_extract_time:提取的起始时刻,单位为秒,默认为0(即从视频最开始提取)end_extract_time:提取的终止时刻,单位为秒,默认为None(即视频终点)extract_time_interval:提取的时间间隔,单位为秒,默认为-1(即输出时间范围内的所有帧)output_prefix:图片的前缀名,默认为frame,图片的名称将为frame_000001.jpg、frame_000002.jpg、frame_000003.jpg......jpg_quality:设置图片质量,范围为0到100,默认为100(质量最佳)isColor:如果为False,输出的将是黑白图片'''cap = cv2.VideoCapture(pathIn)  ##打开视频文件n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))  ##视频的帧数fps = cap.get(cv2.CAP_PROP_FPS)  ##视频的帧率dur = n_frames/fps  ##视频的时间##如果only_output_video_info=True, 只输出视频信息,不提取图片if only_output_video_info:print('only output the video information (without extract frames)::::::')print("Duration of the video: {} seconds".format(dur))print("Number of frames: {}".format(n_frames))print("Frames per second (FPS): {}".format(fps))##提取特定时间点图片elif extract_time_points is not None:if max(extract_time_points) > dur:   ##判断时间点是否符合要求raise NameError('the max time point is larger than the video duration....')try:os.mkdir(pathOut)except OSError:passsuccess = Truecount = 0while success and count < len(extract_time_points):cap.set(cv2.CAP_PROP_POS_MSEC, (1000*extract_time_points[count]))success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  ##转化为黑白图片print('Write a new frame: {}, {}th'.format(success, count+1))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality])     # save frame as JPEG filecount = count + 1else:##判断起始时间、终止时间参数是否符合要求if initial_extract_time > dur:raise NameError('initial extract time is larger than the video duration....')if end_extract_time is not None:if end_extract_time > dur:raise NameError('end extract time is larger than the video duration....')if initial_extract_time > end_extract_time:raise NameError('end extract time is less than the initial extract time....')##时间范围内的每帧图片都输出if extract_time_interval == -1:if initial_extract_time > 0:cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time))try:os.mkdir(pathOut)except OSError:passprint('Converting a video into frames......')if end_extract_time is not None:N = (end_extract_time - initial_extract_time)*fps + 1success = Truecount = 0while success and count < N:success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}/{}'.format(success, count+1, n_frames))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality])     # save frame as JPEG filecount =  count + 1else:success = Truecount = 0while success:success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}/{}'.format(success, count+1, n_frames))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality])     # save frame as JPEG filecount =  count + 1##判断提取时间间隔设置是否符合要求elif extract_time_interval > 0 and extract_time_interval < 1/fps:raise NameError('extract_time_interval is less than the frame time interval....')elif extract_time_interval > (n_frames/fps):raise NameError('extract_time_interval is larger than the duration of the video....')##时间范围内每隔一段时间输出一张图片else:try:os.mkdir(pathOut)except OSError:passprint('Converting a video into frames......')if end_extract_time is not None:N = (end_extract_time - initial_extract_time)/extract_time_interval + 1success = Truecount = 0while success and count < N:cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time+count*1000*extract_time_interval))success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}th'.format(success, count+1))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality])     # save frame as JPEG filecount = count + 1else:success = Truecount = 0while success:cap.set(cv2.CAP_PROP_POS_MSEC, (1000*initial_extract_time+count*1000*extract_time_interval))success,image = cap.read()if success:if not isColor:image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)print('Write a new frame: {}, {}th'.format(success, count+1))cv2.imwrite(os.path.join(pathOut, "{}_{:06d}.jpg".format(output_prefix, count+1)), image, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality])     # save frame as JPEG filecount = count + 1##### 测试
# pathIn = 'pathIn/1.1.avi'# video2frames(pathIn, only_output_video_info = True) #只输出视频信息(长度、帧数和帧率),不提取图片
#
# pathOut = './frames1/'
# video2frames(pathIn, pathOut)
#
# pathOut = './frames2'
# video2frames(pathIn, pathOut, extract_time_points=(1, 2, 5)) #提取的时间点,单位为秒,为元组数据,比如,(2, 3, 5)表示只提取视频第2秒, 第3秒,第5秒图片
#
# pathOut = './frames3'
# video2frames(pathIn, pathOut,
#              initial_extract_time=1,          #提取的起始时刻,单位为秒,默认为0(即从视频最开始提取)
#              end_extract_time=3,              #提取的终止时刻,单位为秒,默认为None(即视频终点)
#              extract_time_interval = 0.5)     #提取的时间间隔,单位为秒,默认为-1(即输出时间范围内的所有帧)
#
# pathOut = './frames4/'
# video2frames(pathIn, pathOut, extract_time_points=(0.3, 2), isColor = False)  #isColor:如果为False,输出的将是黑白图片
#
#
# pathOut = './frames5/'
# video2frames(pathIn, pathOut, extract_time_points=(0.3, 2), jpg_quality=50)   #jpg_quality:设置图片质量,范围为0到100,默认为100(质量最佳)#输出
# pathIn = 'pathIn/1.1.avi'
pathIn = '0'
pathOut = 'pathOut/1/'
video2frames(pathIn, pathOut, extract_time_interval = 5, jpg_quality=100)

整理文件并重命名

先设置文件夹名称,再用img_rename.py

import os#将多个文件夹文件调到一个文件里面,并重命名
m = 0   # 文件夹名称
#for i in range(1,15):   #对14个文件夹中的 图片 调用到一个文件夹中
for i in range(1,2):m = 15m += 1# 输入路径# path = input('请输入文件路径(结尾加上/):')path = 'getvedio/img/' + str(m) + '/'# 输出路径path2 = 'getvedio/img_rename/'# 获取该目录下所有文件,存入列表中f = os.listdir(path)#重命名n = 1for i in f:# 设置旧文件名(就是路径+文件名)oldname = path + f[n - 1]# 设置新文件名newname = path2 +str(m) + '_' + str(n) + '.JPG'# 用os模块中的rename方法对文件改名os.renames(oldname, newname)print(oldname, '======>', newname)n += 1# #
# m = 1
# n = 1
# #输入路径
# # path = input('请输入文件路径(结尾加上/):')
# path = 'img_pre/' + str(m) + '/'
# #输出路径
# path2 = 'img_ed/'
# # 获取该目录下所有文件,存入列表中
# f = os.listdir(path)
#
# for i in f:
#     # 设置旧文件名(就是路径+文件名)
#     oldname = path + f[n - 1]
#
#     # 设置新文件名
#     newname = path2  + str(n) + '.JPG'
#
#     # 用os模块中的rename方法对文件改名
#     os.renames(oldname, newname)
#     print(oldname, '======>', newname)
#     n += 1

数据集制作

labelimg的安装及使用

  • 安装及启动

安装:

pip install labelimg

启动命令:

labelimg
  • 标记图片

image-20201103191218092

JPEGimages储存原图片

数据集(.xml文件 )保存至 Annotations

然后 复制到 VOCdevkit/VOC2007 文件夹中。

批量修改标签名称

参考:如何批量修改Pascal VOC数据集中xml标签中的标签名称

运行xml_rename.py

import os
import xml.etree.ElementTree as ET#程序功能:批量修改VOC数据集中xml标签文件的标签名称
def changelabelname(inputpath):listdir = os.listdir(inputpath)for file in listdir:if file.endswith('xml'):file = os.path.join(inputpath,file)tree = ET.parse(file)root = tree.getroot()for object1 in root.findall('object'):for sku in object1.findall('name'):           #查找需要修改的名称if (sku.text == 'preName'):               #‘preName’为修改前的名称sku.text = 'TESTNAME'                 #‘TESTNAME’为修改后的名称tree.write(file,encoding='utf-8')     #写进原始的xml文件并避免原始xml中文字符乱码else:passelse:passif __name__ == '__main__':inputpath = 'E:/Research/Dataset/Test/Annotations'  #此处替换为自己的路径changelabelname(inputpath)

这篇关于目标检测学习笔记(二)——图片取样及数据集制作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/686473

相关文章

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

C#自动化实现检测并删除PDF文件中的空白页面

《C#自动化实现检测并删除PDF文件中的空白页面》PDF文档在日常工作和生活中扮演着重要的角色,本文将深入探讨如何使用C#编程语言,结合强大的PDF处理库,自动化地检测并删除PDF文件中的空白页面,感... 目录理解PDF空白页的定义与挑战引入Spire.PDF for .NET库核心实现:检测并删除空白页

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

python库pydantic数据验证和设置管理库的用途

《python库pydantic数据验证和设置管理库的用途》pydantic是一个用于数据验证和设置管理的Python库,它主要利用Python类型注解来定义数据模型的结构和验证规则,本文给大家介绍p... 目录主要特点和用途:Field数值验证参数总结pydantic 是一个让你能够 confidentl

JAVA实现亿级千万级数据顺序导出的示例代码

《JAVA实现亿级千万级数据顺序导出的示例代码》本文主要介绍了JAVA实现亿级千万级数据顺序导出的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 前提:主要考虑控制内存占用空间,避免出现同时导出,导致主程序OOM问题。实现思路:A.启用线程池

使用python制作一款文件粉碎工具

《使用python制作一款文件粉碎工具》这篇文章主要为大家详细介绍了如何使用python制作一款文件粉碎工具,能够有效粉碎密码文件和机密Excel表格等,感兴趣的小伙伴可以了解一下... 文件粉碎工具:适用于粉碎密码文件和机密的escel表格等等,主要作用就是防止 别人用数据恢复大师把你刚删除的机密的文件恢

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码