HaoFeng2019 commited on
Commit
63e518b
1 Parent(s): 3ab776d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -45
app.py CHANGED
@@ -72,59 +72,59 @@ def reload_segmodel(model, path=""):
72
  return model
73
 
74
 
75
- def rec(opt):
76
- # print(torch.__version__) # 1.5.1
77
- img_list = os.listdir(opt.distorrted_path) # distorted images list
78
-
79
- if not os.path.exists(opt.gsave_path): # create save path
80
- os.mkdir(opt.gsave_path)
81
- if not os.path.exists(opt.isave_path): # create save path
82
- os.mkdir(opt.isave_path)
83
 
84
- GeoTr_Seg_model = GeoTr_Seg()#.cuda()
85
- # reload segmentation model
86
- reload_segmodel(GeoTr_Seg_model.msk, opt.Seg_path)
87
- # reload geometric unwarping model
88
- reload_model(GeoTr_Seg_model.GeoTr, opt.GeoTr_path)
89
 
90
- IllTr_model = IllTr()#.cuda()
91
- # reload illumination rectification model
92
- reload_model(IllTr_model, opt.IllTr_path)
93
 
94
- # To eval mode
95
- GeoTr_Seg_model.eval()
96
- IllTr_model.eval()
97
 
98
- for img_path in img_list:
99
- name = img_path.split('.')[-2] # image name
100
-
101
- img_path = opt.distorrted_path + img_path # read image and to tensor
102
- im_ori = np.array(Image.open(img_path))[:, :, :3] / 255.
103
- h, w, _ = im_ori.shape
104
- im = cv2.resize(im_ori, (288, 288))
105
- im = im.transpose(2, 0, 1)
106
- im = torch.from_numpy(im).float().unsqueeze(0)
107
 
108
- with torch.no_grad():
109
- # geometric unwarping
110
- bm = GeoTr_Seg_model(im)
111
- bm = bm.cpu()
112
- bm0 = cv2.resize(bm[0, 0].numpy(), (w, h)) # x flow
113
- bm1 = cv2.resize(bm[0, 1].numpy(), (w, h)) # y flow
114
- bm0 = cv2.blur(bm0, (3, 3))
115
- bm1 = cv2.blur(bm1, (3, 3))
116
- lbl = torch.from_numpy(np.stack([bm0, bm1], axis=2)).unsqueeze(0) # h * w * 2
117
 
118
- out = F.grid_sample(torch.from_numpy(im_ori).permute(2,0,1).unsqueeze(0).float(), lbl, align_corners=True)
119
- img_geo = ((out[0]*255).permute(1, 2, 0).numpy())[:,:,::-1].astype(np.uint8)
120
- cv2.imwrite(opt.gsave_path + name + '_geo' + '.png', img_geo) # save
121
 
122
- # illumination rectification
123
- if opt.ill_rec:
124
- ill_savep = opt.isave_path + name + '_ill' + '.png'
125
- rec_ill(IllTr_model, img_geo, saveRecPath=ill_savep)
126
 
127
- print('Done: ', img_path)
128
 
129
 
130
 
 
72
  return model
73
 
74
 
75
+ # def rec(opt):
76
+ # # print(torch.__version__) # 1.5.1
77
+ # img_list = os.listdir(opt.distorrted_path) # distorted images list
78
+
79
+ # if not os.path.exists(opt.gsave_path): # create save path
80
+ # os.mkdir(opt.gsave_path)
81
+ # if not os.path.exists(opt.isave_path): # create save path
82
+ # os.mkdir(opt.isave_path)
83
 
84
+ # GeoTr_Seg_model = GeoTr_Seg()#.cuda()
85
+ # # reload segmentation model
86
+ # reload_segmodel(GeoTr_Seg_model.msk, opt.Seg_path)
87
+ # # reload geometric unwarping model
88
+ # reload_model(GeoTr_Seg_model.GeoTr, opt.GeoTr_path)
89
 
90
+ # IllTr_model = IllTr()#.cuda()
91
+ # # reload illumination rectification model
92
+ # reload_model(IllTr_model, opt.IllTr_path)
93
 
94
+ # # To eval mode
95
+ # GeoTr_Seg_model.eval()
96
+ # IllTr_model.eval()
97
 
98
+ # for img_path in img_list:
99
+ # name = img_path.split('.')[-2] # image name
100
+
101
+ # img_path = opt.distorrted_path + img_path # read image and to tensor
102
+ # im_ori = np.array(Image.open(img_path))[:, :, :3] / 255.
103
+ # h, w, _ = im_ori.shape
104
+ # im = cv2.resize(im_ori, (288, 288))
105
+ # im = im.transpose(2, 0, 1)
106
+ # im = torch.from_numpy(im).float().unsqueeze(0)
107
 
108
+ # with torch.no_grad():
109
+ # # geometric unwarping
110
+ # bm = GeoTr_Seg_model(im)
111
+ # bm = bm.cpu()
112
+ # bm0 = cv2.resize(bm[0, 0].numpy(), (w, h)) # x flow
113
+ # bm1 = cv2.resize(bm[0, 1].numpy(), (w, h)) # y flow
114
+ # bm0 = cv2.blur(bm0, (3, 3))
115
+ # bm1 = cv2.blur(bm1, (3, 3))
116
+ # lbl = torch.from_numpy(np.stack([bm0, bm1], axis=2)).unsqueeze(0) # h * w * 2
117
 
118
+ # out = F.grid_sample(torch.from_numpy(im_ori).permute(2,0,1).unsqueeze(0).float(), lbl, align_corners=True)
119
+ # img_geo = ((out[0]*255).permute(1, 2, 0).numpy())[:,:,::-1].astype(np.uint8)
120
+ # cv2.imwrite(opt.gsave_path + name + '_geo' + '.png', img_geo) # save
121
 
122
+ # # illumination rectification
123
+ # if opt.ill_rec:
124
+ # ill_savep = opt.isave_path + name + '_ill' + '.png'
125
+ # rec_ill(IllTr_model, img_geo, saveRecPath=ill_savep)
126
 
127
+ # print('Done: ', img_path)
128
 
129
 
130