path = r"\\oppo-camera-image-prod.oppoit.com\nas07\202410BokehAIDET检测头饰、首饰、手持物第5批\分割…
- User
path = r"\oppo-camera-image-prod.oppoit.com\nas07\202410_Bokeh_AIDET检测_头饰、首饰、手持物_第5批\分割 20241109_梅拉尼亚小镇\1\spdebug\bokehdump" save_path = r"E:\Emonodepth_collect_2k" files = [file for file in os.listdir(path) if "_afterbeauty_doBokehEffect_inputmaster.yuv" in file]
用 tqdm 包装文件列表以显示进度条
for file in tqdm(files, desc="Processing files"): yuv = os.path.join(path, file) frames = int(os.path.getsize(yuv) / IMG_SIZE) with open(yuv, "rb") as yuv_f: yuv_bytes = yuv_f.read() yuv_data = np.frombuffer(yuv_bytes, np.uint8) Y, U, V = from_NV21(yuv_data, frames) rgb_data = np.zeros((IMG_HEIGHT, IMG_WIDTH, 3), dtype=np.uint8) bgr_data = np_yuv2rgb(Y[0, :, :], U[0, :, :], V[0, :, :]) # numpy h, w, c = bgr_data.shape bgr_data = cv2.resize(bgr_data, (w // 2, h // 2),interpolation=cv2.INTER_AREA) print(yuv) if bgr_data is not None: print('1') cv2.imwrite(os.path.join(save_path, file.replace("_master_depth_input.yuv", ".jpg")), bgr_data) 对这个代码,在不改变基本功能和结构的基础上,写一个txt读取路径的函数,txt的每一行都是一个文件夹路径,在文件夹路径下找到spdebug\bokehdump子文件夹,并进行文件按处理
- Assistant
你可以通过编写一个函数来读取文本文件并提取每一行中包含的文件夹路径,然后在这些路径中查找
spdebug\bokehdump子文件夹。以下是一个可以满足你需求的例子。我们将读取路径的函数命名为process_directories_from_txt,并将在for循环外部调用这个函数。import os import cv2 import numpy as np from tqdm import tqdm def from_NV21(yuv_data, frames): # 这里是你根据需要实现的从NV21转换的函数 pass def np_yuv2rgb(Y, U, V): # 这里是你根据需要实现的YUV到RGB的函数 pass def process_directories_from_txt(txt_file_path): with open(txt_file_path, 'r') as file: directories = file.readlines() for directory in directories: directory = directory.strip() bokehdump_path = os.path.join(directory, "spdebug", "bokehdump") if os.path.exists(bokehdump_path): process_bokehdump_files(bokehdump_path) def process_bokehdump_files(path): files = [file for file in os.listdir(path) if "_afterbeauty_doBokehEffect_inputmaster.yuv" in file] # 用 tqdm 包装文件列表以显示进度条 for file in tqdm(files, desc="Processing files in " + path): yuv = os.path.join(path, file) frames = int(os.path.getsize(yuv) / IMG_SIZE) with open(yuv, "rb") as yuv_f: yuv_bytes = yuv_f.read() yuv_data = np.frombuffer(yuv_bytes, np.uint8) Y, U, V = from_NV21(yuv_data, frames) rgb_data = np.zeros((IMG_HEIGHT, IMG_WIDTH, 3), dtype=np.uint8) bgr_data = np_yuv2rgb(Y[0, :, :], U[0, :, :], V[0, :, :]) # numpy h, w, c = bgr_data.shape bgr_data = cv2.resize(bgr_data, (w // 2, h // 2), interpolation=cv2.INTER_AREA) if bgr_data is not None: cv2.imwrite(os.path.join(save_path, file.replace("_master_depth_input.yuv", ".jpg")), bgr_data) # 示例:调用读取路径和处理函数 txt_file_path = 'path/to/your.txt' # 替换为你的txt文件路径 process_directories_from_txt(txt_file_path) - User
Traceback (most recent call last): File "F:\lbyuv2rgbtxt.py", line 166, in process_directories_from_txt(txt_file_path) File "F:\lbyuv2rgbtxt.py", line 136, in process_directories_from_txt directories = file.readlines() UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 62: illegal multibyte sequence
- Assistant
这个错误通常是由于 Python 尝试使用错误的编码读取文本文件导致的。默认情况下,Python 在 Windows 系统上使用
"gbk"编码打开文件,这在处理包含非 ASCII 字符(比如 Unicode 字符集)的文本文件时容易出错。为了解决这个问题,你可以明确指定文本文件的编码格式为
"utf-8"或其他正确的编码格式(例如"utf-16"或"latin1"),具体取决于你的文本文件的实际编码。以下是如何修改process_directories_from_txt函数以指定编码格式的示例:def process_directories_from_txt(txt_file_path): # 尝试使用 utf-8 编码读取文件 with open(txt_file_path, 'r', encoding='utf-8') as file: directories = file.readlines() for directory in directories: directory = directory.strip() bokehdump_path = os.path.join(directory, "spdebug", "bokehdump") if os.path.exists(bokehdump_path): process_bokehdump_files(bokehdump_path)说明:
- 在
open函数中添加了encoding='utf-8'参数用来指定文件的编码格式。你也可以根据需要将其改为其他格式,如"utf-16"、 等等。
- 在