截取视频为单帧图片

ffmpeg -i "D:\BaiduNetdiskDownload\Anti_UAV_test_dev\02_6319_0000-1499\IR.mp4" -vf fps=fps=5 -s 256x256 %3d.bmp

修改

import os
import re
import csv


###############################################
# 一个标签
def Annotations1_write(xmax, xmin, ymax, ymin):
    # print (rename)#打印文件名
    index_ = str(index+897)
    with open(os.path.join(foldername, index_ + '.xml'), 'w') as f:
        f.write(f"""<annotation>
    <folder>{foldername}</folder>
    <filename>{index_}.png</filename>
   <source>
      <database>Unknown</database>
   </source>
    <size>
        <width>{256}</width>
        <height>{256}</height>
        <depth>{3}</depth>
    </size>
    <segmented>0</segmented>\n""")
        f.write(f"""    <object>
            <name>Target</name>
            <pose>Unspecified</pose>
            <truncated>0</truncated>
            <difficult>0</difficult>
            <bndbox>
                  <xmin>{int(xmin)}</xmin>
                  <ymin>{int(ymin)}</ymin>
                  <xmax>{int(xmax)}</xmax>
                  <ymax>{int(ymax)}</ymax>
            </bndbox>
        </object>\n""")
        f.write("</annotation>")


def class1(index):
    with open(os.path.join(root, foldername, 'IR_label.json'), 'r') as f:
        reader = csv.reader(f)
        result = list(reader)
        # print(result[5*i-1]) #取出想要的行,第5*i+4行
        result = str(result[5 * index - 1])
        # print(result)
        # 取出x,y的值
        number = re.findall("\d+", result)  # 正则化去掉多余字符(输出结果为列表)
        # print(number)
        number = str(number)  # 转化为字符串
        # print(number)

        xmax_ = (number.split(',')[0])  # 以,为分隔符取出各个元素
        # print(xmax_)
        xmax = "".join(list(filter(str.isdigit, xmax_)))  # 取出数字,最后是字符串
        # print(xmax)
        # 同理
        xmin_ = (number.split(',')[1])
        xmin = "".join(list(filter(str.isdigit, xmin_)))  # 取出数字,最后是字符串

        ymax_ = (number.split(',')[2])
        ymax = "".join(list(filter(str.isdigit, ymax_)))  # 取出数字,最后是字符串

        ymin_ = (number.split(',')[3])
        ymin = "".join(list(filter(str.isdigit, ymin_)))  # 取出数字,最后是字符串
        return (xmax, xmin, ymax, ymin)


def class2(index):
    with open(os.path.join(root, foldername, 'IR_label.json'), 'r') as f:
        reader = csv.reader(f)
        result = list(reader)
        # print(result[4*i]) #取出想要的行,第4*i行
        result = str(result[4 * index])
        # print(result)
        # 取出x,y的值
        number = re.findall("\d+", result)  # 正则化去掉多余字符(输出结果为列表)
        # print(number)
        number = str(number)  # 转化为字符串

        xmax_ = (number.split(',')[0])  # 以,为分隔符取出各个元素
        # print(xmax_)
        xmax = "".join(list(filter(str.isdigit, xmax_)))  # 取出数字,最后是字符串
        # print(xmax)
        # 同理
        xmin_ = (number.split(',')[1])
        xmin = "".join(list(filter(str.isdigit, xmin_)))  # 取出数字,最后是字符串

        ymax_ = (number.split(',')[2])
        ymax = "".join(list(filter(str.isdigit, ymax_)))  # 取出数字,最后是字符串

        ymin_ = (number.split(',')[3])
        ymin = "".join(list(filter(str.isdigit, ymin_)))  # 取出数字,最后是字符串
        return (xmax, xmin, ymax, ymin)



root = r'D:\BaiduNetdiskDownload\Anti_UAV_test_dev\02_6319_0000-1499'
# 标签文件路径
foldername = 'images'  ####换文件夹改这里
if not os.path.exists(foldername):
    os.mkdir(foldername)

# 打开标签文件
with open(os.path.join(root, foldername, 'IR_label.json')) as label_json:
    # 分成两类,判断属于哪一类
    lines = []
    for i in label_json.readlines()[1004:1005:]:
        if len(i) == 19:
            num = 300
            index = 1
            while index != num:
                (xmax, xmin, ymax, ymin) = class1(index)
                Annotations1_write(xmax, xmin, ymax, ymin)
                index = int(index)
                index += 1

        elif len(i) == 18:
            num = 300
            index = 1
            while index != num:
                (xmax, xmin, ymax, ymin) = class1(index)
                Annotations1_write(xmax, xmin, ymax, ymin)
                index = int(index)
                index += 1
        elif len(i) == 2:
            num = 250
            index = 1
            while index != num:
                (xmax, xmin, ymax, ymin) = class2(index)
                Annotations1_write(xmax, xmin, ymax, ymin)
                index = int(index)
                index += 1
        else:
            print('出错了')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值