python对excel表格进行操作
参考链接:https://www.jianshu.com/p/4e39444d5ebc
https://www.cnblogs.com/zhangwuxuan/p/12433350.html
只能重新创建新的excel文件进行元素内容的操作,无法同时使用 xlwt 库和 xlrd 库
# 读取xls文件,一定要把xlsx后缀改成xls
import xlwt
# 创建新execl文件的路径以及工作薄
excelpath = r'C:\Users\sjh\Desktop\PATAC\video_process\test.xlsx'
workbook = xlwt.Workbook()
# 创建一个worksheet
worksheet = workbook.add_sheet('OD')
# Create Alignment 对齐方式
alignment = xlwt.Alignment()
# May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
# 格式居中合并
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style = xlwt.XFStyle() # Create Style
style.alignment = alignment # Add Alignment to Style
# 合并单元格,居中,并写入元素(起始行,终止行,起始列,终止列),列宽 = 6000
worksheet.col(1).width = 6000
worksheet.col(7).width = 6000
worksheet.col(8).width = 6000
for count in range(4):
# 合并单元格填写原始采集视频名称
worksheet.write_merge(0+count*4, 3+count*4, 7, 7, '2021_7_24_11_49_24.h264', style)
# 填写裁剪后的视频名称
worksheet.write(0+count*4, 8, '20210724_'+str(count+1).zfill(2)+'_F.mp4')
worksheet.write(1+count*4, 8, '20210724_'+str(count+1).zfill(2)+'_B.mp4')
worksheet.write(2+count*4, 8, '20210724_'+str(count+1).zfill(2)+'_L.mp4')
worksheet.write(3+count*4, 8, '20210724_'+str(count+1).zfill(2)+'_R.mp4')
# 填写task name
worksheet.write(0+count*4, 1, 'od_20210724-'+str(count+1).zfill(2)+'_F')
worksheet.write(1+count*4, 1, 'od_20210724-'+str(count+1).zfill(2)+'_B')
worksheet.write(2+count*4, 1, 'od_20210724-'+str(count+1).zfill(2)+'_L')
worksheet.write(3+count*4, 1, 'od_20210724-'+str(count+1).zfill(2)+'_R')
workbook.save(excelpath)
更改文件名字&裁剪图片(对文件夹中数据的处理)
# 重新命名,不管是图片还是其他格式
import os
path = r"C:\Users\sjh\Desktop\labels\annotations_txt_1" # 存放图片(文本)的地址,根据需求自行修改
filelist = os.listdir(path) # 该文件夹下所有的文件(包括文件夹)
count = 0
# for file in filelist:
# print(file)
for file in filelist: # 遍历所有文件
Olddir=os.path.join(path, file) # 原来的文件路径
if os.path.isdir(Olddir): # 如果是文件夹则跳过
continue
filename = os.path.splitext(file)[0] # 文件名,分离文件名与扩展名
filetype = '.txt' # 文件扩展名
Newdir = os.path.join(path, str(count).zfill(4)+filetype) # 用字符串函数zfill 以0补全所需位数
# Newdir = os.path.join(path, str(count) + filetype)
os.rename(Olddir, Newdir) # 重命名
count += 1
# 将文件夹中的图片集进行统一处理,该程序为裁剪成目标尺寸大小
import os
import cv2 as cv
path = r"C:\Users\sjh\Desktop\PATAC\xml_tfrecord\images" # 待处理的图片位置,存放图片(文本)的地址,根据需求自行修改
new_path = r"C:\Users\sjh\Desktop\PATAC\xml_tfrecord\new_images" # 处理后的图片位置
if not os.path.exists(new_path):
os.makedirs(new_path)
filelist = os.listdir(path) # 该文件夹下所有的文件(包括文件夹)
for file in filelist: # 遍历所有文件
Olddir = os.path.join(path, file) # 原来的文件路径
if os.path.isdir(Olddir): # 如果是文件夹则跳过
continue
img = cv.imread(Olddir)
new_img = img[2:964, :] # 需要保留的区域--裁剪
# 参数1 是高度的范围,参数2是宽度的范围
cv.imwrite(os.path.join(new_path, file), new_img) # 保存裁剪后的图片
用于裁剪视频部分区域并将各区域保存为.mp4格式的新视频
'''
用于裁剪视频部分区域并将各区域保存为.mp4格式的新视频
'''
import cv2 as cv
import sys
# 读取视频文件,注意根据自己视频文件位置进行更换
video = cv.VideoCapture('C:/Users/sjh/Desktop/PATAC/video_process/2021_6_16_19_30_39.h264')
# 获取视频帧率,25fps
fps = video.get(cv.CAP_PROP_FPS)
print(fps)
fourcc = cv.VideoWriter_fourcc('m', 'p', '4', 'v')
# 获取视频帧的宽高尺寸
sz = (int(video.get(cv.CAP_PROP_FRAME_WIDTH)/2), int(video.get(cv.CAP_PROP_FRAME_HEIGHT)/2))
print(sz)
vout_1 = cv.VideoWriter()
vout_1.open('C:/Users/sjh/Desktop/left_top.mp4',fourcc,fps,sz,True)
vout_2 = cv.VideoWriter()
vout_2.open('C:/Users/sjh/Desktop/right_top.mp4',fourcc,fps,sz,True)
vout_3 = cv.VideoWriter()
vout_3.open('C:/Users/sjh/Desktop/left_bottom.mp4',fourcc,fps,sz,True)
vout_4 = cv.VideoWriter()
vout_4.open('C:/Users/sjh/Desktop/right_bottom.mp4',fourcc,fps,sz,True)
count = 0
# 循环读取视频每一帧
while True:
ret, frame = video.read()
if ret == 1:
img_h, img_w, img_ch = frame.shape
# 注意图像帧的尺寸顺序,先高度,后宽度
print(count)
print(frame.shape)
left_top = frame[0:962, 0:1280]
right_top = frame[0:962, 1280:2560]
left_bottom = frame[962:1924, 0:1280]
right_bottom = frame[962:1924, 1280:2560]
# left_top = cv.resize(left_top, (962, 1280))
# 显示4个截取后的图像区域
# cv.imshow('left_top', left_top)
# cv.imshow('right_top', right_top)
# cv.imshow('left_bottom', left_bottom)
# cv.imshow('right_bottom', right_bottom)
# 图像帧保存为.mp4视频
vout_1.write(left_top)
vout_2.write(right_top)
vout_3.write(left_bottom)
vout_4.write(right_bottom)
count += 1
# 按照帧率进行延时
# cv.waitKey(int(1000 / int(fps)))
else:
break
vout_1.release()
vout_2.release()
vout_3.release()
vout_4.release()
sys.exit()