简介:
在写好测试案例后,在数据准备时,往往我们需要为准备数据而花费大量的时间,尤其是大数据量的测试时,机器学习的测试时,往往会耗费大量的测试数据,所以写一个适合自己测试的造数工具非常有必要。
DownImageForUrl.py模块的downFilefang,用来把数据库上的图片或视频下载下来作为测试的数据,有了它的好处是可以批量下载,不用手动一个个的下载。
import csv
import time
import urllib.request
encoding='utf-8'
import os
class DownImageForUrl():
def getCsVData(self,file_path):
value_rows = []
with open(file_path, encoding='UTF-8') as file:
f_csv = csv.reader(file)
for r in f_csv:
value_rows.append(r)
return value_rows
'''通过url如:http://www.xxx.mp4下载视频'''
def downFile(self,file_path):
print('图片/视频正在下载中,请去E:\\MP4\\根据url下载的视频\\文件夹中去查看')
count = 1
for file_path in self.getCsVData(file_path):
cunmulu = 'E:\\MP4\\标准视频\\根据url下载的视频\\'
if not os.path.exists(cunmulu):
os.makedirs(cunmulu)
file_path = file_path[0]
if not 'test.vivo.com.cn' in file_path:
file_path = 'https://test.vivo.com.cn/'+file_path
video_name = file_path.split('/')[-1]
urllib.request.urlretrieve(file_path,cunmulu+video_name)
print(f"正在下载第{count}个")
count = count+1
print("下载结束~")
if __name__ == '__main__':
before_time = time.time()
downImageForUrl = DownImageForUrl()
downImageForUrl.downFile('image_or_video_url.csv')
after_time = time.time()
print('您一共花费了{0}秒'.format(after_time-before_time))
ImageFormatResolution.py模块,其中有
①resolution修改分辨率的方法,如把横屏1280*720的图片改为竖屏720*1280的图片,或者某些特殊分辨率的图片,如1281*720、500*500、721*1280等等在互联网上无法下载的图片。
②format1修改图片格式的方法,如JPG、JPEG、PNG、webp、GIF、TIFF、PSD、BMP、PDF等等,我们测试图片上传时或者关于图片的测试时,都需要把这些图片格式覆盖到,那么你如何获取充足的各种各样的图片格式呢,这个方法就能很好的解决这个问题。
# pip install pillow #pil库已更名为pillow库了
import os
from PIL import Image
class ImageFormatResolution():
'''修改分辨率'''
def resolution(file_in, size):
for root, dirs, files in os.walk(file_in):
for file in files:
file_path = os.path.join(root, file)
image = Image.open(file_path)
resized_image = image.resize(size, Image.ANTIALIAS)
resized_image.save(file_path)
'''修改图片格式'''
def format1(file_in, suffix):
for root, dirs, files in os.walk(file_in):
for file in files:
file_path = os.path.join(root, file)
size = Image.open(file_path).size
image = Image.open(file_path)
resized_image = image.resize(size, Image.ANTIALIAS)
file_out = file_path.split('.')[0] + '.' + suffix
if not suffix in file_path:
os.remove(file_path)
resized_image.save(file_out)
'''即修改分辨率又修改格式'''
def resolution_format(file_in, size, suffix):
for root, dirs, files in os.walk(file_in):
for file in files:
file_path = os.path.join(root, file)
image = Image.open(file_path)
resized_image = image.resize(size, Image.ANTIALIAS)
file_out = file_path.split('.')[0] + '.' + suffix
if not suffix in file_path:
os.remove(file_path)
resized_image.save(file_out)
'''修改图片大小'''
if __name__ == '__main__':
file_in = 'E:\\data\\test\\image\\3\\'
suffix = 'png'
size = (500, 500)
imageFormatResolution = ImageFormatResolution()
# imageFormatResolution.resolution(file_in,(500,500))
# imageFormatResolution.format1(file_in, suffix)
imageFormatResolution.resolution_format(file_in, size, suffix)
VideoProcessing.py模块,
①specifications方法可以修改视频非分辨率,当我们项目需要上传的视频是很标准的横屏1280*720或竖屏720*1280的视频时,而一把从网上(哔哩哔哩,抖音,快手)上下载的视频都是很杂乱且不符合需求时,就可以用这个方法把不规则的视频分辨率改变成规则的视频分辨率了,在测视频分辨率的等价类边界值时会起到很好的作用。
②slicing方法可以对视频进行切割,当手上有很大的视频,你又需要比较小的视频时,可以对很大的视频进行切割。或者是需要的测试视频很多时,可以把有限的视频变成很多很多的视频。
# pip install os
# pip install -i https://pypi.douban.com/simple/ pip install opencv-python==4.3.0.38
import os
from pathlib import Path
import cv2
class VideoProcessing():
# E:\\MP4\\标准视频\\竖屏\\1\
def v_write(self,video_dir, fps, size,videoCapture):
'''合成视频
video_dir:要切的文件
fps:要合成的帧数
size:要合成的分辨率
'''
print(video_dir,fps,size,videoCapture)
videoWriter = cv2.VideoWriter(video_dir, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), fps, size)
i = 0
while True:
success, frame = videoCapture.read()
if success:
i += 1
if i > 300:
a = 1
break
# if (i >= 1000 and i <= 2000):
else:
frame = cv2.resize(frame, size)
videoWriter.write(frame)
else:
a = 0
break
return a
def getFPS(self,file_path):
# 获取视频总帧数
video_cap = cv2.VideoCapture(file_path)
frame_count = 0
all_frames = []
while (True):
ret, frame = video_cap.read()
if ret is False:
break
# all_frames.append(frame)
frame_count = frame_count + 1
return frame_count
# print(frame_count) # 6491
# print(len(all_frames)) # 6491
'''按帧切割视频+改变分辨率'''
def slicing_specifications(self,num,size,file_in):
for root,dirs,files in os.walk(file_in):
for file in files:
video_path = os.path.join(root,file)
videoCapture = cv2.VideoCapture(video_path)
c = 1 # 已经剪了多少个
aa = 1
while aa != 0:
target_path = os.path.join(root,''.join(file.split('.')[:-1]))
print(target_path)
if not os.path.exists(target_path):
os.makedirs(target_path)
video_dir = os.path.join(target_path,str(c) + '.mp4')
print(video_dir)
aa = self.v_write(video_dir, num, size,videoCapture)
c += 1
# if c==10:#只要十个
# break
'''切割视频'''
def slicing(self, num, file_in):
for root, dirs, files in os.walk(file_in):
for file in files:
video_path = os.path.join(root, file)
print('video_path',video_path)
videoCapture = cv2.VideoCapture(video_path)
width = int(str(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)).split('.')[0])
height = int(str(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)).split('.')[0])
size = (width,height)
print('size',size)
c = 1 # 已经剪了多少个
aa = 1
while aa != 0:
target_path = os.path.join(root, ''.join(file.split('.')[:-1]))
print(target_path)
if not os.path.exists(target_path):
os.makedirs(target_path)
video_dir = os.path.join(target_path, str(c) + '.mp4')
print(video_dir)
aa = self.v_write(video_dir, num, size, videoCapture)
c += 1
# if c==10:#只要十个
# break
def specifications(self,source_path,size):
# 目标
# 1. 拷贝视频文件并修改后缀
# 2. 修改图片的分辨率
# 3. 批量完成
sink_path = source_path+"\\改变分辨率的视频\\"
if not os.path.exists(sink_path):
os.makedirs(sink_path)
if not os.path.exists(source_path) or not os.path.exists(sink_path):
print('Path not exit!')
exit()
videos_list = os.listdir(source_path)
for video in videos_list:
video_path = os.path.join(source_path, video)
if Path(video_path).suffix in ['.MOV', '.mov', '.mp4']:
# 修改后缀名
dis_video_name = video
dis_video_name = ''.join(dis_video_name.split('.')[:-1]) + '_改变分辨率.MP4'
dis_path = os.path.join(sink_path, dis_video_name)
# 进行转换
cap = cv2.VideoCapture(video_path)
success, _ = cap.read()
# 重新合成的视频在原文件夹,如果需要分开,可以修改file_n
video_writer = cv2.VideoWriter(dis_path, cv2.VideoWriter_fourcc(*'XVID'), 25, (1280, 720))
while success:
success, vid1 = cap.read()
try:
vid = cv2.resize(vid1, size, interpolation=cv2.INTER_LINEAR) # 希望的分辨率大小可以在这里改
video_writer.write(vid)
except:
break
if __name__ == '__main__':
videoProcessing = VideoProcessing()
# videoProcessing.specifications(fps = 20,size = (320, 240),file_in="E:\\MP4\\标准视频\\根据url下载的视频\\1\\" )
source_path = "E:\\MP4\\标准视频\\根据url下载的视频\\1" #自己视频的路径
size = (1280, 720) #分辨率
# videoProcessing.specifications(source_path,size) #修改分辨率
# videoProcessing.slicing_specifications(5,size,source_path) #切割视频+修改分辨率
videoProcessing.slicing(5,source_path) #切割视频