from future import absolute_import, division, print_function
import os
import random
import numpy as np
from PIL import Image
from skimage.filters import gaussian, sobel
from scipy.interpolate import griddata
import cv2
import torch
from torchvision import transforms
import multiprocessing as mp
from utils import transfer_color
import matplotlib.pyplot as plt
import sys
from tqdm import tqdm
def pil_loader(path):
# print(path)
with open(path, 'rb') as f:
from PIL import Image
with Image.open(f) as img:
return img.convert('RGB')
def read_pfm(file):
with open(file, 'rb') as fh:
fh.readline()
width, height = str(fh.readline().rstrip())[2:-1].split()
fh.readline()
disp = np.fromfile(fh, '<f')
return np.flipud(disp.reshape(int(height), int(width)))
def preprocess(inputs, feed_width, feed_height, disable_normalisation, has_gt, to_tensor, is_train, brightness, contrast, saturation, hue):
if is_train and random.random() > 0.5:
color_aug = transforms.ColorJitter.get_params(
brightness, contrast, saturation, hue)
for key in ['image', 'stereo_image']:
inputs[key] = color_aug(inputs[key])
for key in ['image', 'stereo_image']:
if disable_normalisation:
inputs[key] = to_tensor(inputs[key])
else:
inputs[key] = (to_tensor(inputs[key]) - 0.45) / 0.225
if has_gt:
inputs['disparity'] = torch.from_numpy(inputs['disparity']).float()
if inputs.get('mono_disparity') is not None:
inputs['mono_disparity'] = torch.from_numpy(inputs['mono_disparity']).float()
if name == "main":
# data_path = "/home/notebook/data/group/Ruby/langan_datasets/nightlangan3_deblur/"
np.random.seed(42)
itxt_path = 'humanwmsk_image.txt'
dtxt_path = 'humanwmsk_depth.txt'
all_img = []
all_depth = []
with open(itxt_path, 'r') as fi:
for li in fi.readlines():
li = li.strip('\n')
all_img.append(li)
with open(dtxt_path, 'r') as fd:
for ld in fd.readlines():
ld = ld.strip('\n')
all_depth.append(ld)
img_path = all_img
depth_path = all_depth
save_path = r"/home/notebook/data/group/Ruby/langan_datasets/humanwithmask/stereo4mono/"
count_start, count_end = 3979, len(all_img)
# feed_height, feed_width, process_width = 1024, 768, 768 + 20
feed_height, feed_width, process_width = 1024, 768, 768
disable_background, is_train = False, True
disable_normalisation, has_gt, disable_sharpening = True, True, True
max_disparity = 20
disable_synthetic_augmentation = True
brightness, contrast, saturation, hue = (0.8, 1.2), (0.8, 1.2), (0.8, 1.2), (-0.1, 0.1)
np_xs, np_ys = np.meshgrid(np.arange(process_width), np.arange(feed_height))
to_tensor = transforms.ToTensor()
keep_aspect_ratio = True
data_paths = (img_path, depth_path)
num_workers = 16
with mp.Pool(processes=num_workers) as pool:
results = [
pool.apply_async(worker,
args=(save_path, data_paths, max_disparity,
disable_background, disable_sharpening,
disable_synthetic_augmentation, process_width,
feed_height, feed_width, disable_normalisation,
has_gt, to_tensor, is_train, brightness, contrast,
saturation, hue, np_xs, np_ys, keep_aspect_ratio, count))
for count in tqdm(range(count_start, count_end), desc='Processing Images')
]
processed_data = [p.get() for p in results]
print("Processing complete.")
在不改变代码的基本功能和结构的同时,把这个代码改成从count的最大值开始,依次倒着对图像进行处理
To modify the code to process images in reverse order (from count_start to count_end in descending order), you'll need to adjust the range you pass to the tqdm function, as well as the range used in the loop that creates the apply_async calls.
Here’s the modified section of your code in the if __name__ == "__main__": block, focusing on reversing the order of processing images:
if __name__ == "__main__":
np.random.seed(42)
itxt_path = 'humanwmsk_image.txt'
dtxt_path = 'humanwmsk_depth.txt'
all_img = []
all_depth = []
with open(itxt_path, 'r') as fi:
for li in fi.readlines():
li = li.strip('\n')
all_img.append(li)
with open(dtxt_path, 'r') as fd:
for ld in fd.readlines():
ld = ld.strip('\n')
all_depth.append(ld)
img_path = all_img
depth_path = all_depth
save_path = r"/home/notebook/data/group/Ruby/langan_datasets/humanwithmask/stereo4mono/"
count_start, count_end = 3979, len(all_img)
feed_height, feed_width, process_width = 1024, 768, 768
disable_background, is_train = False, True
disable_normalisation, has_gt, disable_sharpening = True, True, True
max_disparity = 20
disable_synthetic_augmentation = True
brightness, contrast, saturation, hue = (0.8, 1.2), (0.8, 1.2), (0.8, 1.2), (-0.1, 0.1)
np_xs, np_ys = np.meshgrid(np.arange(process_width), np.arange(feed_height))
to_tensor = transforms.ToTensor()
keep_aspect_ratio = True
data_paths = (img_path, depth_path)
num_workers = 16
# Change here to process in reverse order
with mp.Pool(processes=num_workers) as pool:
results = [
pool.apply_async(worker,
args=(save_path, data_paths, max_disparity,
disable_background, disable_sharpening,
disable_synthetic_augmentation, process_width,
feed_height, feed_width, disable_normalisation,
has_gt, to_tensor, is_train, brightness, contrast,
saturation, hue, np_xs, np_ys, keep_aspect_ratio, count))
for count in tqdm(range(count_end - 1, count_start - 1, -1), desc='Processing Images') # Descending order
]
processed_data = [p.get() for p in results]
print("Processing complete.")
Key Changes Made:
Changed the range in the tqdm function to iterate from count_end - 1 to count_start - 1 in reverse order.
The use of step -1 allows the counting to decrement as it progresses through the loop.
Important Notes:
Make sure that count_end is the number of images, and count_start is the index from where you want to begin processing (in your case, it starts processing from 3979 to the end).
This approach ensures that images will be processed starting from the last one down to the first one, effectively reversing the order of processing without altering the overall structure and functionality of the code.
在你的 if __name__ == "__main__": 块中,找到处理图像的地方,做以下修改:
if __name__ == "__main__":
np.random.seed(42)
itxt_path = 'humanwmsk_image.txt'
dtxt_path = 'humanwmsk_depth.txt'
all_img = []
all_depth = []
with open(itxt_path, 'r') as fi:
for li in fi.readlines():
li = li.strip('\n')
all_img.append(li)
with open(dtxt_path, 'r') as fd:
for ld in fd.readlines():
ld = ld.strip('\n')
all_depth.append(ld)
img_path = all_img
depth_path = all_depth
save_path = r"/home/notebook/data/group/Ruby/langan_datasets/humanwithmask/stereo4mono/"
count_start, count_end = 3979, len(all_img)
feed_height, feed_width, process_width = 1024, 768, 768
disable_background, is_train = False, True
disable_normalisation, has_gt, disable_sharpening = True, True, True
max_disparity = 20
disable_synthetic_augmentation = True
brightness, contrast, saturation, hue = (0.8, 1.2), (0.8, 1.2), (0.8, 1.2), (-0.1, 0.1)
np_xs, np_ys = np.meshgrid(np.arange(process_width), np.arange(feed_height))
to_tensor = transforms.ToTensor()
keep_aspect_ratio = True
data_paths = (img_path, depth_path)
num_workers = 16
# 改为逆序处理
with mp.Pool(processes=num_workers) as pool:
results = [
pool.apply_async(worker,
args=(save_path, data_paths, max_disparity,
disable_background, disable_sharpening,
disable_synthetic_augmentation, process_width,
feed_height, feed_width, disable_normalisation,
has_gt, to_tensor, is_train, brightness, contrast,
saturation, hue, np_xs, np_ys, keep_aspect_ratio, count))
for count in tqdm(range(count_end - 1, count_start - 1, -1), desc='Processing Images') # 倒序处理
]
processed_data = [p.get() for p in results]
print("处理完成。")