这里写自定义目录标题

  • 介绍
  • 1. yolov5负样本构造:标注区域内随机裁剪等大子区域
  • 2. 使用python批量修改图像尺寸、文件名称、xml文件内容、生成无标记xml文件!
    • 一、批量修改图像尺寸
    • 二、批量修改文件名称(以修改xml文件名为对应的jpg文件名为例,不包含后缀)
    • 三、批量修改xml文件各节点内容
    • 四、批量生成空的xml文件
  • 3. 数据集相关
    • xml
      • 划分训练集xml
    • txt格式
      • YOLO格式,一. xml 转为txt
      • 可视化txt标注
      • 生成train、val、test的txt划分
      • 随机划分数据集复制文件版
      • txt转化为VOC(xml格式)
      • kmeans 算法
  • 3. 视频处理
    • 提取视频每帧照片
    • 输出视频帧数等信息
    • jpg to video

介绍

在深度学习中会遇到需要脚本程序,在此记录一下用过的各种脚本,慢慢积累,防止丢失。

1. yolov5负样本构造:标注区域内随机裁剪等大子区域

在做目标检测的时候,模型总是会学到一些局部信息,比如总把红色的东西识别成灭火器。为了构造对抗样本,考虑在标注范围内随机裁剪子区域作为负样本。

 name = 'test'label_path = path_label + name + '.txt'img_path = path_img + name + '.jpg'# print(img_path)img_none=cv2.imread(img_path)# print(img_none)ww, hh = img_none.shape[0], img_none.shape[1]# print(ww, hh)label_ = []with open(label_path, mode='r', encoding = "utf-8") as fin:finl = fin.readlines()for i in finl: #这部分是把yolov5的标注进行复原il = i.replace('\n','')il = i.split(' ')# print(int(il[0]))x,y,w,h = float(il[1]), float(il[2]), float(il[3]), float(il[4])x *= www *= wwy *= hhh *= hhx_max = int(((x+1)*2+w)/2)x_min = int(((x+1)*2-w)/2)y_max = int(((y+1)*2+h)/2)y_min = int(((y+1)*2-h)/2)label_.append([x_min, x_max, y_min, y_max])# print(x_min, x_max, y_min, y_max)j = -1for ll in label_:j += 1x_min, x_max, y_min, y_max = ll[0], ll[1], ll[2], ll[3]# print(x_min, x_max, y_min, y_max)sw = int((x_max-x_min)*0.2)sh = int((y_max-y_min)*0.2)for i in range(3): # 取3个子区域 x=random.randint(x_min, x_max-sw)y=random.randint(y_min, y_max-sh)# print('({},{})'.format(x,y))res=img_none[x:x+sw,y:y+sh] #子区域cv2.imwrite(out_path+name+'_'+str(j)+'_'+str(i)+'.jpg',res)

https://blog.csdn.net/weixin_43499457/article/details/126308215

2. 使用python批量修改图像尺寸、文件名称、xml文件内容、生成无标记xml文件!

一、批量修改图像尺寸

import os
import cv2#obtain the filename
path_ori = '/home/dulingwen/Music/jpg/'
filename = os.listdir(path_ori)#resize the image
for fn in filename:img = cv2.imread(path_ori+fn)dim = (1920,1080)img_res = cv2.resize(img,dim,interpolation=cv2.INTER_AREA)cv2.imwrite(path_ori+fn,img_res)

二、批量修改文件名称(以修改xml文件名为对应的jpg文件名为例,不包含后缀)

import ospath_ori = '/home/dulingwen/Music/jpg/'
path_mod = '/home/dulingwen/Music/xml/'
file_ori = os.listdir(path_ori)
file_mod = os.listdir(path_mod)
#print('file_mod number is ',len(file_mod))
#print('file_ori number is ',len(file_ori))for fn in file_mod:oldname = path_mod + fnnewname = path_mod + file_ori[n][:-4] + '.xml'os.rename(oldname,newname)print(oldname,newname)

三、批量修改xml文件各节点内容

import os
import xml.dom.minidom
import xml.etree.ElementTreexmldir = '/home/dulingwen/Music/xml/'
for xmlfile in os.listdir(xmldir):xmlname = os.path.splitext(xmlfile)[0]#read the xml filedom = xml.dom.minidom.parse(os.path.join(xmldir,xmlfile))root = dom.documentElement#obtain the filename label pair and give it a new valueroot.getElementsByTagName('filename')[0].firstChild.data = xmlname + '.jpg'root.getElementsByTagName('path')[0].firstChild.data = '/home/dulingwen/Music/jpg/' + xmlname + '.jpg'root.getElementsByTagName('width')[0].firstChild.data = '1920'root.getElementsByTagName('height')[0].firstChild.data = '1080'xml_specific = xmldir + xmlfilewith open(xml_specific,'w') as fh:dom.writexml(fh)

四、批量生成空的xml文件

import os
import xml.dom.minidomimg_path = '/home/dulingwen/Pictures/img/'
xml_path = '/home/dulingwen/Pictures/xml/'
for img_file in os.listdir(img_path):img_name = os.path.splitext(img_file)[0]#create an empty dom document objectdoc = xml.dom.minidom.Document()#creat a root node which name is annotationannotation = doc.createElement('annotation')#add the root node to the dom document objectdoc.appendChild(annotation)#add the folder subnodefolder = doc.createElement('folder')folder_text = doc.createTextNode('VOC2012')folder.appendChild(folder_text)annotation.appendChild(folder)#add the filename subnodefilename = doc.createElement('filename')filename_text = doc.createTextNode(img_file)filename.appendChild(filename_text)annotation.appendChild(filename)# add the path subnodepath = doc.createElement('path')path_text = doc.createTextNode(img_path + img_file)path.appendChild(path_text)annotation.appendChild(path)#add the source subnodesource = doc.createElement('source')database = doc.createElement('database')database_text = doc.createTextNode('Unknown')source.appendChild(database)database.appendChild(database_text)annotation.appendChild(source)#add the size subnodesize = doc.createElement('size')width = doc.createElement('width')width_text = doc.createTextNode('1920')height = doc.createElement('height')height_text = doc.createTextNode('1080')depth = doc.createElement('depth')depth_text = doc.createTextNode('3')size.appendChild(width)width.appendChild(width_text)size.appendChild(height)height.appendChild(height_text)size.appendChild(depth)depth.appendChild(depth_text)annotation.appendChild(size)#add the segmented subnodesegmented = doc.createElement('segmented')segmented_text = doc.createTextNode('0')segmented.appendChild(segmented_text)annotation.appendChild(segmented)#write into the xml text fileos.mknod(xml_path+'%s.xml'%img_name)fp = open(xml_path+'%s.xml'%img_name, 'w+')doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding='utf-8')fp.close()

3. 数据集相关

xml

划分训练集xml

"""这个脚本从Annotations中随机划分训练集和测试集,最终生成ImagesSet/train.txt和val.txt"""
import os
import random
from os.path import *# --------------------------全局地址变量--------------------------------#
dir_path = dirname(dirname(abspath(__file__)))  # 当前项目位置
xml_path = os.path.join(dir_path, "Pest", "Annotations")  # xml文件位置
assert os.path.exists(xml_path), "xml_path not exist!"# 建立ImageSets文件
ImageSets_path = os.path.join(dir_path, "Pest", "ImageSets")
if not os.path.exists(ImageSets_path):os.makedirs(ImageSets_path)# train.txt 和 val.txt文件位置
traintxt_path = os.path.join(dir_path, "Pest", "ImageSets", "train.txt")
valtxt_path = os.path.join(dir_path, "Pest", "ImageSets", "val.txt")
testtxt_path = os.path.join(dir_path, "Pest", "ImageSets", "test.txt")# 如果已经存在txt文件 则删除
if os.path.exists(traintxt_path):os.remove(traintxt_path)
if os.path.exists(valtxt_path):os.remove(valtxt_path)
if os.path.exists(testtxt_path):os.remove(testtxt_path)
# --------------------------全局地址变量--------------------------------#
def create_imagesets_train_val(xml_full_path, traintxt_full_path, valtxt_full_path):# 训练集比例train_percent = 0.8# 验证集比例val_percent = 0.2# xml文件目录位置xml_path = xml_full_pathtotal_xml = os.listdir(xml_path)  # 获得目录下所有xml文件num = len(total_xml)lists = list(range(num))num_train = int(num * train_percent)# 随机选num_train个train文件train_list = random.sample(lists, num_train)for i in train_list:lists.remove(i)val_list = lists  # val等于train剩下的 这里没有划分testftrain = open(traintxt_full_path, 'w')fval = open(valtxt_full_path, 'w')for i in range(num):name = total_xml[i][:-4] + '\n'if i in train_list:ftrain.write(name)  # train.txt文件写入else:fval.write(name)  # val.txt文件写入ftrain.close()  # 关闭train.txtfval.close()    # 关闭val.txtdef create_imagesets_train_val_test(xml_full_path, traintxt_full_path, valtxt_full_path, testtxt_full_path):# 训练集比例train_percent = 0.6# 验证集比例val_percent = 0.2# 测试集比例test_percent = 0.2# xml文件目录位置xml_path = xml_full_pathtotal_xml = os.listdir(xml_path)  # 获得目录下所有xml文件num = len(total_xml)lists = list(range(num))num_train = int(num * train_percent)  # 训练集个数num_val = int(num * val_percent)  # 验证集个数# 随机选num_train个train文件train_list = random.sample(lists, num_train)for i in train_list:lists.remove(i)val_list = random.sample(lists, num_val)for j in val_list:lists.remove(j)test_list = listsftrain = open(traintxt_full_path, 'w')fval = open(valtxt_full_path, 'w')ftest = open(testtxt_full_path, 'w')for i in range(num):name = total_xml[i][:-4] + '\n'if i in train_list:ftrain.write(name)  # train.txt文件写入elif i in val_list:fval.write(name)  # val.txt文件写入else:ftest.write(name)  # test.txt文件写入ftrain.close()  # 关闭train.txtfval.close()  # 关闭val.txtftest.close()  # 关闭test.txtif __name__ == '__main__':create_imagesets_train_val_test(xml_path, traintxt_path, valtxt_path, testtxt_path)

txt格式

YOLO格式,一. xml 转为txt

import os
import xml.etree.ElementTree as ETdirpath = r'D:\yanyi\shixi\sangang\xml/'  # 原来存放xml文件的目录
newdir = r'D:\yanyi\shixi\sangang\abels/'  # 修改label后形成的txt目录if not os.path.exists(newdir):os.makedirs(newdir)for fp in os.listdir(dirpath):root = ET.parse(os.path.join(dirpath, fp)).getroot()xmin, ymin, xmax, ymax = 0, 0, 0, 0sz = root.find('size')width = float(sz[0].text)height = float(sz[1].text)filename = root.find('filename').textprint(fp)for child in root.findall('object'):  # 找到图片中的所有框print('***********************')sub = child.find('bndbox')  # 找到框的标注值并进行读取xmin = float(sub[0].text)ymin = float(sub[1].text)xmax = float(sub[2].text)ymax = float(sub[3].text)try:  # 转换成yolov3的标签格式,需要归一化到(0-1)的范围内x_center = (xmin + xmax) / (2 * width)y_center = (ymin + ymax) / (2 * height)w = (xmax - xmin) / widthh = (ymax - ymin) / heightexcept ZeroDivisionError:print(filename, '的 width有问题')with open(os.path.join(newdir, fp.split('.')[0] + '.txt'), 'a+') as f:print(child.find('name').text)if child.find('name').text =='gang':f.write(' '.join([str(0), str(x_center), str(y_center), str(abs(w)), str(abs(h)) + '\n']))if child.find('name').text =='b':f.write(' '.join([str(1), str(x_center), str(y_center), str(abs(w)), str(abs(h)) + '\n']))

可视化txt标注


import os
import cv2
img_path = r'D:\yanyi\shixi\sangang\train-images/'
label_path = r'D:\yanyi\shixi\sangang\train-labels/'
f = os.listdir(img_path)
def paint(label_file, img_file):#读取照片print(img_file)img = cv2.imread(img_file)img_h, img_w, _ = img.shapewith open(label_file, 'r') as f:obj_lines = [l.strip() for l in f.readlines()]for obj_line in obj_lines:cls, cx, cy, nw, nh = [float(item) for item in obj_line.split(' ')]color = (0, 0, 255) if cls == 0.0 else (0, 255, 0)x_min = int((cx - (nw / 2.0)) * img_w)y_min = int((cy - (nh / 2.0)) * img_h)x_max = int((cx + (nw / 2.0)) * img_w)y_max = int((cy + (nh / 2.0)) * img_h)cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, 2)cv2.imshow('Ima', img)cv2.waitKey(0)
for i in f:if i.split('.')[-1] =='jpg':label_path_name = label_path + i.replace('jpg','txt')elif i.split('.')[-1] == 'png':label_path_name = label_path + i.replace('png', 'txt')img_path_name = img_path + iprint(label_path_name)print(img_path_name)paint(label_path_name,img_path_name)

生成train、val、test的txt划分

import os
import random
import sysif len(sys.argv) < 2:print("no directory specified, please input target directory")exit()root_path = 'sys.argv[1]'xmlfilepath = root_path + '/images'txtsavepath = root_path + '/labels'if not os.path.exists(root_path):print("cannot find such directory: " + root_path)exit()if not os.path.exists(txtsavepath):os.makedirs(txtsavepath)trainval_percent = 0.9  #train和val总共占比
train_percent = 0.8  #trainval里的train占比
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)print("train and val size:", tv)
print("train size:", tr)ftrainval = open(root_path + '/trainval.txt', 'w')
ftest = open(root_path + '/test.txt', 'w')
ftrain = open(root_path + '/train.txt', 'w')
fval = open(root_path + '/val.txt', 'w')for i in list:name = total_xml[i][:-4] + '\n'if i in trainval:ftrainval.write(name)if i in train:ftrain.write(name)else:fval.write(name)else:ftest.write(name)ftrainval.close()
ftrain.close()
# fval.close()
# ftest.close()

随机划分数据集复制文件版

# 将图片和标注数据按比例切分为 训练集和测试集
import shutil
import random
import os# 原始路径
image_original_path = r'D:\yanyi\shixi\puyang\puyang8.6\images/'
label_original_path = r'D:\yanyi\shixi\puyang\puyang8.6\labels/'
# 复制文件总路径
src_root = r'D:\yanyi\shixi\puyang\puyang8.6\nowpuyang'
# 训练集路径
train_image_path = src_root + '/images/train/'
train_label_path = src_root + '/labels/train/'
# 验证集路径
val_image_path = src_root + '/images/val/'
val_label_path = src_root + '/labels/val/'
# 测试集路径
test_image_path = src_root + '/images/test/'
test_label_path = src_root + '/labels/test/'# 数据集划分比例,训练集75%,验证集15%,测试集15%
train_percent = 0.8
val_percent = 0.1
test_percent = 0.1
# 数据集划分数量,训练集800,验证集800,测试集800
# num_train = 850
# num_val = 800
# num_test = 5000# 检查文件夹是否存在
def mkdir():if not os.path.exists(train_image_path):os.makedirs(train_image_path)if not os.path.exists(train_label_path):os.makedirs(train_label_path)if not os.path.exists(val_image_path):os.makedirs(val_image_path)if not os.path.exists(val_label_path):os.makedirs(val_label_path)if not os.path.exists(test_image_path):os.makedirs(test_image_path)if not os.path.exists(test_label_path):os.makedirs(test_label_path)def main():mkdir()total_txt = os.listdir(label_original_path)num_txt = len(total_txt)list_all_txt = range(num_txt)  # 范围 range(0, num)if train_percent:num_train = int(num_txt * train_percent)num_val = int(num_txt * val_percent)num_test = num_txt - num_train - num_valtrain = random.sample(list_all_txt, num_train) #打乱顺序# train从list_all_txt取出num_train个元素# 所以list_all_txt列表只剩下了这些元素:val_testval_test = [i for i in list_all_txt if not i in train]# 再从val_test取出num_val个元素,val_test剩下的元素就是testval = random.sample(val_test, num_val)# 检查两个列表元素是否有重合的元素set_c = set(val_test) & set(val)list_c = list(set_c)print(list_c)print(len(list_c))# print("训练集数目:{}, 验证集数目:{},测试集数目:{}".format(len(train), len(val), len(val_test) - len(val)))for i in list_all_txt:name = total_txt[i][:-4]srcImage = image_original_path + name + '.jpg'srcLabel = label_original_path + name + '.txt'if i in train:if os.path.exists(srcImage):dst_train_Image = train_image_path + name + '.jpg'dst_train_Label = train_label_path + name + '.txt'shutil.copyfile(srcImage, dst_train_Image)shutil.copyfile(srcLabel, dst_train_Label)else:print(srcImage)elif i in val:if os.path.exists(srcImage):dst_val_Image = val_image_path + name + '.jpg'dst_val_Label = val_label_path + name + '.txt'shutil.copyfile(srcImage, dst_val_Image)shutil.copyfile(srcLabel, dst_val_Label)else:print(srcImage)else:dst_test_Image = test_image_path + name + '.jpg'dst_test_Label = test_label_path + name + '.txt'shutil.copyfile(srcImage, dst_test_Image)shutil.copyfile(srcLabel, dst_test_Label)if __name__ == '__main__':main()

txt转化为VOC(xml格式)

需要修改文件地址和类名,

import os
import glob
from PIL import Imagevoc_annotations = 'D:/yolo_test_review/annotations/'##
yolo_txt = 'D:/yolo_test_review/yolov3/labels/'##
img_path = 'D:/yolo_test_review/yolov3/images/'##
labels = ['A', 'B', 'C']  # label for datasets
# 图像存储位置
src_img_dir = img_path  # 添加你的路径
# 图像的txt文件存放位置src_txt_dir = yolo_txt
src_xml_dir = voc_annotationsimg_Lists = glob.glob(src_img_dir + '/*.jpg')img_basenames = []
for item in img_Lists:img_basenames.append(os.path.basename(item))img_names = []
for item in img_basenames:temp1, temp2 = os.path.splitext(item)img_names.append(temp1)for img in img_names:im = Image.open((src_img_dir + '/' + img + '.jpg'))width, height = im.size# 打开txt文件gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines()print(gt)if gt:# 将主干部分写入xml文件中xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')xml_file.write('<annotation>\n')xml_file.write('    <folder>VOC2007</folder>\n')xml_file.write('    <filename>' + str(img) + '.jpg' + '</filename>\n')xml_file.write('    <size>\n')xml_file.write('        <width>' + str(width) + '</width>\n')xml_file.write('        <height>' + str(height) + '</height>\n')xml_file.write('        <depth>3</depth>\n')xml_file.write('    </size>\n')# write the region of image on xml filefor img_each_label in gt:spt = img_each_label.split(' ')  # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。print(f'spt:{spt}')xml_file.write('    <object>\n')xml_file.write('        <name>' + str(labels[int(spt[0])]) + '</name>\n')xml_file.write('        <pose>Unspecified</pose>\n')xml_file.write('        <truncated>0</truncated>\n')xml_file.write('        <difficult>0</difficult>\n')xml_file.write('        <bndbox>\n')center_x = round(float(spt[1].strip()) * width)center_y = round(float(spt[2].strip()) * height)bbox_width = round(float(spt[3].strip()) * width)bbox_height = round(float(spt[4].strip()) * height)xmin = str(int(center_x - bbox_width / 2))ymin = str(int(center_y - bbox_height / 2))xmax = str(int(center_x + bbox_width / 2))ymax = str(int(center_y + bbox_height / 2))xml_file.write('            <xmin>' + xmin + '</xmin>\n')xml_file.write('            <ymin>' + ymin + '</ymin>\n')xml_file.write('            <xmax>' + xmax + '</xmax>\n')xml_file.write('            <ymax>' + ymax + '</ymax>\n')xml_file.write('        </bndbox>\n')xml_file.write('    </object>\n')xml_file.write('</annotation>')

kmeans 算法

import numpy as np
import json
import os
from PIL import Imagedef iou(box, clusters):"""计算 IOUparam:box: tuple or array, shifted to the origin (i. e. width and height)clusters: numpy array of shape (k, 2) where k is the number of clustersreturn:numpy array of shape (k, 0) where k is the number of clusters"""x = np.minimum(clusters[:, 0], box[0])y = np.minimum(clusters[:, 1], box[1])if np.count_nonzero(x == 0) > 0 or np.count_nonzero(y == 0) > 0:raise ValueError("Box has no area")intersection = x * ybox_area = box[0] * box[1]cluster_area = clusters[:, 0] * clusters[:, 1]iou_ = intersection / (box_area + cluster_area - intersection + 1e-10)return iou_#  计算框的 numpy 数组和 k 个簇之间的平均并集交集(IoU)。
def avg_iou(boxes, clusters):"""param:boxes: numpy array of shape (r, 2), where r is the number of rowsclusters: numpy array of shape (k, 2) where k is the number of clustersreturn:average IoU as a single float"""return np.mean([np.max(iou(boxes[i], clusters)) for i in range(boxes.shape[0])])# 将所有框转换为原点。
def translate_boxes(boxes):"""param:boxes: numpy array of shape (r, 4)return:numpy array of shape (r, 2)"""new_boxes = boxes.copy()for row in range(new_boxes.shape[0]):new_boxes[row][2] = np.abs(new_boxes[row][2] - new_boxes[row][0])new_boxes[row][3] = np.abs(new_boxes[row][3] - new_boxes[row][1])return np.delete(new_boxes, [0, 1], axis=1)# 使用联合上的交集(IoU)度量计算k均值聚类。
def kmeans(boxes, k, dist=np.median):"""param:boxes: numpy array of shape (r, 2), where r is the number of rowsk: number of clustersdist: distance functionreturn:numpy array of shape (k, 2)"""rows = boxes.shape[0]distances = np.empty((rows, k))last_clusters = np.zeros((rows,))np.random.seed()# the Forgy method will fail if the whole array contains the same rowsclusters = boxes[np.random.choice(rows, k, replace=False)]  # 初始化k个聚类中心(方法是从原始数据集中随机选k个)while True:for row in range(rows):# 定义的距离度量公式:d(box,centroid)=1-IOU(box,centroid)。到聚类中心的距离越小越好,但IOU值是越大越好,所以使用 1 - IOU,这样就保证距离越小,IOU值越大。distances[row] = 1 - iou(boxes[row], clusters)# 将标注框分配给“距离”最近的聚类中心(也就是这里代码就是选出(对于每一个box)距离最小的那个聚类中心)。nearest_clusters = np.argmin(distances, axis=1)# 直到聚类中心改变量为0(也就是聚类中心不变了)。if (last_clusters == nearest_clusters).all():break# 更新聚类中心(这里把每一个类的中位数作为新的聚类中心)for cluster in range(k):clusters[cluster] = dist(boxes[nearest_clusters == cluster], axis=0)last_clusters = nearest_clustersreturn clusters# 获取图片宽高
def get_image_width_high(full_image_name):image = Image.open(full_image_name)image_width, image_high = image.size[0], image.size[1]return image_width, image_high# 读取 json 文件中的标注数据
def parse_label_json(label_path):with open(label_path, 'r') as f:label = json.load(f)result = []for line in label:bbox = line['bbox']x_label_min, y_label_min, x_label_max, y_label_max = bbox[0], bbox[1], bbox[2], bbox[3]# 计算边框的大小width = x_label_max - x_label_minheight = y_label_max - y_label_minassert width > 0assert height > 0result.append([width, height])result = np.asarray(result)return result# 读取 txt 标注数据文件
def parse_label_txt(label_path):all_label = os.listdir(label_path)result = []for i in range(len(all_label)):full_label_name = os.path.join(label_path, all_label[i])print(full_label_name)# 分离文件名和文件后缀label_name, label_extension = os.path.splitext(all_label[i])full_image_name = os.path.join(label_path.replace('labels', 'images'), label_name + '.jpg')image_width, image_high = get_image_width_high(full_image_name)fp = open(full_label_name, mode="r")lines = fp.readlines()for line in lines:array = line.split()x_label_min = (float(array[1]) - float(array[3]) / 2) * image_widthx_label_max = (float(array[1]) + float(array[3]) / 2) * image_widthy_label_min = (float(array[2]) - float(array[4]) / 2) * image_highy_label_max = (float(array[2]) + float(array[4]) / 2) * image_high# 计算边框的大小width = x_label_max - x_label_minheight = y_label_max - y_label_minassert width > 0assert height > 0result.append([round(width, 2), round(height, 2)])result = np.asarray(result)return resultdef get_kmeans(label, cluster_num=9):anchors = kmeans(label, cluster_num)ave_iou = avg_iou(label, anchors)anchors = anchors.astype('int').tolist()anchors = sorted(anchors, key=lambda x: x[0] * x[1])return anchors, ave_iouif __name__ == '__main__':# # 读取 json 格式的标注数据# label_path = "tile_round1_train_20201231/train_annos.json"# label_result = parse_label_json(label_path)# 读取 txt 格式的标注数据label_path = r"D:\BaiduNetdiskDownload\brainwash\kmerans\labels"    # seed/images/ 内是对应图片文件label_result = parse_label_txt(label_path)anchors, ave_iou = get_kmeans(label_result, 9)anchor_string = ''for anchor in anchors:anchor_string += '{},{}, '.format(anchor[0], anchor[1])anchor_string = anchor_string[:-2]print(f'anchors are: {anchor_string}')print(f'the average iou is: {ave_iou}')

修改txt地址和输出的anchor个数

3. 视频处理

提取视频每帧照片

import cv2
import os
#要提取视频的文件名,隐藏后缀
root_path="D:\yanyi\shixi\sangang/"#地址
sourceFileName='01' #文件名
#在这里把后缀接上
video_path = os.path.join(root_path, sourceFileName+'.mp4')
times=0
#提取视频的频率,每1帧提取一个
frameFrequency=3
# 起始ID文件名
start_ID=1 #
#输出图片到当前目录vedio文件夹下
outPutDirName=root_path+sourceFileName+'/'
if not os.path.exists(outPutDirName):#如果文件目录不存在则创建目录os.makedirs(outPutDirName)
camera = cv2.VideoCapture(video_path)
while True:times+=1print('总数:',times/frameFrequency)res, image = camera.read()if not res:print('not res , not image')print(video_path)breakif times%frameFrequency==0:cv2.imwrite(outPutDirName + str(start_ID)+'.jpg', image)# print(outPutDirName + str(times)+'.jpg')print(outPutDirName + str(start_ID)+'.jpg')start_ID+=1
print('图片提取结束')
camera.release()

输出视频帧数等信息

import cv2
# videoPath=input("请输入视屏文件的绝对路径:")
# 将视频文件路径转化为标准的路径
videoPath=r'D:\yanyi\shixi\sangang/01.mp4'
# 视屏获取
videoCapture=cv2.VideoCapture(videoPath)
# 帧率(frames per second)
fps = videoCapture.get(cv2.CAP_PROP_FPS)
# 总帧数(frames)
frames = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
print("帧数:"+str(fps))
print("总帧数:"+str(frames))
print("视屏总时长:"+"{0:.2f}".format(frames/fps)+"秒")

jpg to video

# 要转换的图片的保存地址,按顺序排好,后面会一张一张按顺序读取。
import glob
import cv2
# import numpy as np# cv2.__version__
convert_image_path = 'images-out'
frame_rate = 30# 帧率(fps),尺寸(size),size为图片的大小,本文转换的图片大小为1920×1080,
# 即宽为1920,高为1080,要根据自己的情况修改图片大小。
size = (960, 544)
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # mp4# cv2.VideoWriter_fourcc('I', '4', '2', '0')
videoWriter = cv2.VideoWriter('output.mp4', fourcc,frame_rate, size)
for img in glob.glob(convert_image_path + "/*.jpg"):read_img = cv2.imread(img)videoWriter.write(read_img)
videoWriter.release()

学习中用到的python脚本记录相关推荐

  1. CTF Web学习(三)----python脚本的编写及应用

    CTF Web学习(三) python脚本的编写及应用 CTF Web学习目录链接 CTF Web学习(一):基础篇及头文件修改.隐藏 CTF Web学习(二):代码审计.burp suite应用 C ...

  2. 【python教程入门学习】通过运行python脚本来更改Windows背景

    通过运行python脚本来更改Windows背景 在我们开始之前,一定要注意这篇文章只针对Windows用户!对于那些使用Windows的人来说,这是一个有趣的想法. 如果您想使用python更改桌面 ...

  3. python脚本-记录Python脚本的运行日志的方法

    Python中有一个模块logging,可以直接记录日志# 日志级别 # CRITICAL 50 # ERROR 40 # WARNING 30 # INFO 20 # DEBUG 10 loggin ...

  4. php 如果则,如果python脚本在phpfi中运行,则导入python模块时会出错

    我使用这个script在localhost中用php运行python脚本,但它不读取numpy和cv2之类的模块.在 通过PHP,当它不读取模块时,我遇到了一个常见的错误:import numpy a ...

  5. 【Linux学习】如何编写Shell脚本调用企业微信api来发消息给企业微信成员?

    一.前言 最近通过python实现了发送消息给企业微信的功能,参考链接: [Jenkins学习 ]如何编写Python脚本来调用企业微信的api通知企业微信成员关于Jenkins的编译结果? http ...

  6. 如何使用Python脚本来处理电子邮件?

    本文我们将学习如何使用Python脚本来处理电子邮件.首先,我们将学习电子邮件消息格式,并使用smtplib模块发送和接收电子邮件.然后将学习使用Python的Email程序包发送带附件和HTML内容 ...

  7. Blender中的Python脚本介绍学习教程

    Blender中的Python脚本介绍学习教程 MP4 |视频:h264,1280×720 |音频:AAC,48000 Hz 语言:英语+中英文字幕(根据原英文字幕机译更准确)|大小解压后:1.63 ...

  8. 【Python】深度学习中将数据按比例随机分成随机 训练集 和 测试集的python脚本

    深度学习中经常将数据分成 训练集 和 测试集,参考博客,修改python脚本 randPickAITrainTestData.py . 功能:从 输入目录 中随机检出一定比例的文件或目录,移动到保存 ...

  9. spark-submit提交python脚本过程记录

    最近刚学习spark,用spark-submit命令提交一个python脚本,一开始老报错,所以打算好好整理一下用spark-submit命令提交python脚本的过程.先看一下spark-submi ...

最新文章

  1. 【怎样写代码】参数化类型 -- 泛型(五):泛型类
  2. 嵌入式系统启动流程和启动代码的作用 !
  3. 【采用】如何搭建反欺诈策略与模型
  4. 白话Elasticsearch67-不随意调节jvm和thread pool的原因jvm和服务器内存分配的最佳实践
  5. 获取控制器 nextResponder的简单应用
  6. 第9课_2_dbsoft安装
  7. LeetCode 1838. 最高频元素的频数(二分查找)
  8. Html TextArea 长度限制
  9. java模块间调用信息_java与c++模块之间的交互方法?
  10. anaconda怎么运行python程序_怎么用cmd运行python
  11. MySQL中NOT IN语句对NULL值的处理
  12. 题解 AT25 【プログラミングコンテスト】
  13. HDU 5950 Recursive sequence
  14. matlab实现图像直方图
  15. CSAPP第八章家庭作业参考答案
  16. 层次分析法(AHP)
  17. 单面打印机双面打印———python pdf拆分重排
  18. sumif单列求和_sumif函数使用方法:单条件求和
  19. 2021年茶艺师(初级)新版试题及茶艺师(初级)考试总结
  20. 开发QQ互联ios版Ane扩展 辛酸史

热门文章

  1. 缓存架构设计之——Redis集群搭建
  2. 杰理AC692X---U盘播放无损音乐卡顿问题
  3. 赛扬处理器_海尔逸15笔记本仅2299元,其酷睿10代赛扬处理器是什么情况?
  4. 【山外笔记-Linux 命令】cp 命令详解
  5. 程序员都秃顶?Python 创始人笑了,养生还得学这门语言
  6. 发光二极管的keil代码c语言,用Keil点亮一个发光二极管
  7. linux的操作系统相关
  8. 云原生数据库是未来数据库的天下
  9. 淘宝/天猫店铺接口:查询淘宝卖家用户信息接口接入说明
  10. 图文并茂使用CocosBuilder制作Cocos2D游戏 分享0