赞
踩
处理HMC-QU数据集记录,每一个avi视频,通过xlsx中记录的初始帧和结束帧进行帧转png,保存在以avi视频名为名的文件夹下。
import os import cv2 import pandas as pd # 输入文件夹路径 folder_path = 'avidata' # 输出文件夹路径 output_folder = 'avipng' if not os.path.exists(output_folder): os.makedirs(output_folder) # 读取Excel文件 excel_path = 'du.xlsx' df = pd.read_excel(excel_path) def format_numbers(start, end, width=3): return [f"{num:0{width}}" for num in range(start, end + 1)] # 生成png的名字id save_id = format_numbers(1,1000,4) # 遍历文件夹下所有avi文件 total = 0 for filename in os.listdir(folder_path): if filename.endswith(".avi"): video_path = os.path.join(folder_path, filename) cap = cv2.VideoCapture(video_path) # 检查视频是否成功打开 if not cap.isOpened(): print(f"Error: Could not open video {filename}") continue # 获取视频帧数 frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) video_info = df[df['name'] == filename.split(".")[0]] if video_info.empty: print(f"No frame indices found for video {filename} in the Excel file.") continue # folder = os.path.join(output_folder, filename.split(".")[0]) # os.makedirs(folder) start_frame = int(video_info['start']) end_frame = int(video_info['end']) # 检查索引是否在视频帧数范围内 if start_frame < 1 or end_frame > frame_count or start_frame >= end_frame: print(f"Invalid frame indices for video {filename}. Skipping...") continue # 设置视频读取位置到起始帧 cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame - 1) # 创建输出文件夹 # 读取视频帧并保存为图片 frame_num = start_frame while frame_num <= end_frame: ret, frame = cap.read() print(type(frame), frame.shape) # 检查视频是否读取完毕 # if not ret: # break # # 将视频帧保存为图片 # img_name = f"D{frame_num}.png" # img_name = f"D{save_id[frame_num-start_frame]}.png" # img_path = os.path.join(folder, img_name) # cv2.imwrite(img_path, frame) # # 打印进度 # print(f"Frame {frame_num} of {filename} saved") # # 更新帧计数器 # frame_num += 1 # total += 1 # # 释放资源 # cap.release() print("All videos processed successfully!") print(total)
''' Descripttion: Result: Author: Philo Date: 2024-04-25 08:34:57 LastEditors: Philo LastEditTime: 2024-04-25 08:35:10 ''' from PIL import Image import os def crop_and_replace(image_path, target_size=(224, 224)): try: # 打开图片文件 image = Image.open(image_path) # 裁剪图片 image = image.resize(target_size, Image.ANTIALIAS) # 替换原始图片 image.save(image_path) print(f"{image_path} 裁剪并替换成功") except Exception as e: print(f"处理 {image_path} 时出错: {e}") def crop_and_replace_in_folder(folder_path): # 遍历文件夹中的每个文件 for filename in os.listdir(folder_path): file_path = os.path.join(folder_path, filename) # 如果是文件夹,则递归处理子文件夹 if os.path.isdir(file_path): crop_and_replace_in_folder(file_path) # 如果是图片文件,则裁剪并替换 elif filename.endswith(('.jpg', '.jpeg', '.png', '.bmp')): crop_and_replace(file_path) # 指定包含图片文件的文件夹路径 folder_path = "2224" # 开始裁剪并替换图片 crop_and_replace_in_folder(folder_path)
''' Descripttion: Result: Author: Philo Date: 2024-04-24 10:18:47 LastEditors: Philo LastEditTime: 2024-04-24 10:41:32 ''' # 生成的数据放在各自的文件夹中 import scipy.io as scio from PIL import Image import numpy as np import matplotlib.pyplot as plt import os # 根据自己任务指定文件夹的前缀和范围 folder_prefix = "LV Ground-truth Segmentation Masks" folders_name = os.listdir(folder_prefix) # 循环创建文件夹 for name in folders_name: # 使用zfill方法来保证文件夹名称的固定长度 rename = name.split(".")[0] folder_name = os.path.join(folder_prefix, rename) print(folder_name) # # 使用os.makedirs来创建文件夹,exist_ok=True表示如果文件夹已存在则不会引发错误 os.makedirs(folder_name, exist_ok=True) # 生成图片名称,因为mat数据格式都有顺序 # 从1-100和从001到100的命名方式对图片的影响不同 def format_numbers(start, end, width=3): return [f"{num:0{width}}" for num in range(start, end + 1)] # 生成png的名字id save_id = format_numbers(1,1000,4) # 二值数据转到0-255范围(可见) def MatrixToImage(data): data = data*255 new_im = Image.fromarray(data.astype(np.uint8)) return new_im sum = 0 for tt in folders_name: rename = tt.split(".")[0] name = os.path.join(folder_prefix, rename) path = name mat_data = scio.loadmat(path) data = mat_data["predicted"] # 这里是根据自己mat文件中提取数值 for j in range(data.shape[0]): sum += 1 new_im = MatrixToImage(data[j]) save_name = save_id[j] new_im.save(f"{path}/D{save_name}.png") print(sum)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。