TalHach61 commited on
Commit
c5482a8
·
verified ·
1 Parent(s): f950302

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -32
app.py CHANGED
@@ -38,26 +38,19 @@ model_configs = {
38
  'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
39
  }
40
 
41
- ratios_map = {
42
- 0.5:{"width":704,"height":1408},
43
- 0.57:{"width":768,"height":1344},
44
- 0.68:{"width":832,"height":1216},
45
- 0.72:{"width":832,"height":1152},
46
- 0.78:{"width":896,"height":1152},
47
- 0.82:{"width":896,"height":1088},
48
- 0.88:{"width":960,"height":1088},
49
- 0.94:{"width":960,"height":1024},
50
- 1.00:{"width":1024,"height":1024},
51
- 1.13:{"width":1088,"height":960},
52
- 1.21:{"width":1088,"height":896},
53
- 1.29:{"width":1152,"height":896},
54
- 1.38:{"width":1152,"height":832},
55
- 1.46:{"width":1216,"height":832},
56
- 1.67:{"width":1280,"height":768},
57
- 1.75:{"width":1344,"height":768},
58
- 2.00:{"width":1408,"height":704}
59
  }
60
- ratios = np.array(list(ratios_map.keys()))
61
 
62
  encoder = 'vitl'
63
  model = DepthAnythingV2(**model_configs[encoder])
@@ -154,19 +147,12 @@ def tile(downscale_factor, input_image):
154
  control_image = input_image.resize((input_image.size[0] // downscale_factor, input_image.size[1] // downscale_factor)).resize(input_image.size, Image.NEAREST)
155
  return control_image
156
 
157
- def get_size(init_image):
158
- w,h=init_image.size
159
- curr_ratio = w/h
160
- ind = np.argmin(np.abs(curr_ratio-ratios))
161
- ratio = ratios[ind]
162
- chosen_ratio = ratios_map[ratio]
163
- w,h = chosen_ratio['width'], chosen_ratio['height']
164
- return w,h
165
-
166
- def resize_img(image):
167
- image = image.convert('RGB')
168
- w,h = get_size(image)
169
- resized_image = image.resize((w, h))
170
  return resized_image
171
 
172
  @spaces.GPU(duration=180)
 
38
  'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
39
  }
40
 
41
+ RATIO_CONFIGS_1024 = {
42
+ 0.6666666666666666: {"width": 832, "height": 1248},
43
+ 0.7432432432432432: {"width": 880, "height": 1184},
44
+ 0.8028169014084507: {"width": 912, "height": 1136},
45
+ 1.0: {"width": 1024, "height": 1024},
46
+ 1.2456140350877194: {"width": 1136, "height": 912},
47
+ 1.3454545454545455: {"width": 1184, "height": 880},
48
+ 1.4339622641509433: {"width": 1216, "height": 848},
49
+ 1.5: {"width": 1248, "height": 832},
50
+ 1.5490196078431373: {"width": 1264, "height": 816},
51
+ 1.62: {"width": 1296, "height": 800},
52
+ 1.7708333333333333: {"width": 1360, "height": 768},
 
 
 
 
 
 
53
  }
 
54
 
55
  encoder = 'vitl'
56
  model = DepthAnythingV2(**model_configs[encoder])
 
147
  control_image = input_image.resize((input_image.size[0] // downscale_factor, input_image.size[1] // downscale_factor)).resize(input_image.size, Image.NEAREST)
148
  return control_image
149
 
150
+ def resize_img(control_image):
151
+ image_ratio = control_image.width / control_image.height
152
+ ratio = min(RATIO_CONFIGS_1024.keys(), key=lambda k: abs(k - image_ratio))
153
+ to_height = RATIO_CONFIGS_1024[ratio]["height"]
154
+ to_width = RATIO_CONFIGS_1024[ratio]["width"]
155
+ resized_image = control_image.resize((to_width, to_height), resample=Image.Resampling.LANCZOS)
 
 
 
 
 
 
 
156
  return resized_image
157
 
158
  @spaces.GPU(duration=180)