gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
def main(): args = parser.parseargs() device = torch.device("cuda" if…
- User
def main():
args = parser.parse_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print('=> get test data pairs') args.data_path = "/home/notebook/data/group/Ruby/langan_datasets/bigface_mono/" args.flip_LR = 0 args.useMinMax = 0 lists = [args.data_path+file for file in os.listdir(args.data_path) if ("left" in file or "master" in file) and "disp" not in file] total = len(lists) print(total) print('=> get models from configuration file') content = getYamlCfg(fn='configs/configModelTest.yaml') model = LightStereo(parser) model = model.to(device).eval() model = loadPretrain(model, "./checkpoint/gru/best.pth.tar", strict=False) print('=> infer') count = 0 for file in os.listdir(args.data_path): try: # Extract the number from the file name file_number = int(''.join(filter(str.isdigit, file))) except ValueError: # If there is no number in the filename, skip the file continue # Only process files within the specified range # if 2074 <= file_number <= : if file_number == 0: if "left" in file or "master" in file: if "disp" in file: continue try: srcLFn = args.data_path + file srcRFn = srcLFn.replace('left', 'right').replace('master', 'slave') print(srcLFn) print(srcRFn) lImg = cv2.imread(srcLFn, 1) rImg = cv2.imread(srcRFn, 1) h, w, c = lImg.shape hh, ww, cc = rImg.shape lMask = ((lImg[:, :, 0] == 0) & (lImg[:, :, 1] == 0) & (lImg[:, :, 2] == 0)).astype(np.uint8) except: continue if args.flip_LR > 0: lImg = lImg[:, ::-1, :] rImg = rImg[:, ::-1, :] lMask = lMask[:, ::-1] rszH, rszW = lImg.shape[0], lImg.shape[1] lImg, rImg = resizer(lImg, rImg, args) imgPair = normerImageNet(lImg, rImg) imgPairTs = torch.from_numpy(imgPair).to(device).float().unsqueeze(0) with torch.no_grad(): output= model(imgPairTs) if isinstance(output, list): output = output[-1].squeeze().cpu().numpy() else: output = output.squeeze().cpu().numpy() if args.flip_LR > 0: output = output[:, ::-1] lMask = lMask[:, ::-1] outH, outW = output.shape[0], output.shape[1] scaleDisp = rszW * 1. / outW output = cv2.resize(output, (rszW, rszH), interpolation=cv2.INTER_LINEAR) * scaleDisp output_path = '/home/notebook/data/group/Ruby/langan_datasets/bigface_mono/' cv2.imwrite(output_path + '/' + os.path.basename(srcLFn).replace('left', 'pred').replace('master', 'pred').replace('.jpg', '.png'), output * 10)if name == 'main': main() 对于这个代码,请在不改变代码的基本结构和功能的前提下,使得代码能够完成以下功能:1.给定的输入文件夹是母文件夹,遍历母文件夹,读取所有文件夹下后缀为_left.png、_right.png和_depth.png的文件;2.left和right文件完成原文件的功能,生成pred文件,并将pred文件保存在指定文件夹,pred文件的命名为从0开始的数字加一个_pred.png的后缀;3.创建一个txt,将left、depth、pred文件路径按照顺序写进txt。
- Assistant