code
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)