skytnt commited on
Commit
865d2cc
1 Parent(s): 95c6d27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -189,34 +189,31 @@ class Model:
189
  if detail:
190
  print(msg + "ratio out of limit")
191
  continue
192
- # 扩展边界
193
  expand_pixel = (bounds[3] - bounds[1]) // 20
194
- bounds = (max(bounds[0] - expand_pixel // 2, 0),
195
  max(bounds[1] - expand_pixel, 0),
196
  min(bounds[2] + expand_pixel // 2, im0.shape[1]),
197
  min(bounds[3] + expand_pixel, im0.shape[0]),
198
- )
199
- if bounds[3] - bounds[1] >= (bounds[2] - bounds[0]) * 2: # 等高度剪裁
200
- cx = (bounds[2] + bounds[0]) // 2
201
- h = bounds[3] - bounds[1]
202
- w = h // 2
203
- w2 = w // 2
204
- l1 = max(cx - w2, 0)
205
- r1 = min(cx + w2, im0.shape[1])
206
- bounds = (l1, bounds[1], r1, bounds[3])
207
- temp_bound = (w2 - (cx - l1), 0, w2 + (r1 - cx), h)
208
- else: # 等宽度剪裁
209
- cy = (bounds[3] + bounds[1]) // 2
210
- w = bounds[2] - bounds[0]
211
- h = w * 2
212
- h2 = h // 2
213
- tp1 = max(cy - h2, 0)
214
- b1 = min(cy + h2, im0.shape[0])
215
- bounds = (bounds[0], tp1, bounds[2], b1)
216
- temp_bound = (0, h2 - (cy - tp1), w, h2 + (b1 - cy))
217
  temp_img = np.full((h, w, 3), 255, dtype=np.uint8)
218
- temp_img[temp_bound[1]:temp_bound[3], temp_bound[0]:temp_bound[2]] = im0[bounds[1]:bounds[3],
219
- bounds[0]:bounds[2]]
220
  temp_img = transform.resize(temp_img, (1024, 512), preserve_range=True).astype(np.uint8)
221
  imgs.append(temp_img)
222
  return imgs
 
189
  if detail:
190
  print(msg + "ratio out of limit")
191
  continue
 
192
  expand_pixel = (bounds[3] - bounds[1]) // 20
193
+ bounds = [max(bounds[0] - expand_pixel // 2, 0),
194
  max(bounds[1] - expand_pixel, 0),
195
  min(bounds[2] + expand_pixel // 2, im0.shape[1]),
196
  min(bounds[3] + expand_pixel, im0.shape[0]),
197
+ ]
198
+ # corp and resize
199
+ w = bounds[2] - bounds[0]
200
+ h = bounds[3] - bounds[1]
201
+ bounds[3] += h % 2
202
+ h += h % 2
203
+ r = min(512 / w, 1024 / h)
204
+ pw, ph = int(512 / r - w), int(1024 / r - h)
205
+ bounds_tmp = (bounds[0] - pw // 2, bounds[1] - ph // 2,
206
+ bounds[2] + pw // 2 + pw % 2, bounds[3] + ph // 2 + ph % 2)
207
+ bounds = (max(0, bounds_tmp[0]), max(0, bounds_tmp[1]),
208
+ min(im0.shape[1], bounds_tmp[2]), min(im0.shape[0], bounds_tmp[3]))
209
+ dl = bounds[0] - bounds_tmp[0]
210
+ dr = bounds[2] - bounds_tmp[2]
211
+ dt = bounds[1] - bounds_tmp[1]
212
+ db = bounds[3] - bounds_tmp[3]
213
+ w = bounds_tmp[2] - bounds_tmp[0]
214
+ h = bounds_tmp[3] - bounds_tmp[1]
 
215
  temp_img = np.full((h, w, 3), 255, dtype=np.uint8)
216
+ temp_img[dt:h + db, dl:w + dr] = im0[bounds[1]:bounds[3], bounds[0]:bounds[2]]
 
217
  temp_img = transform.resize(temp_img, (1024, 512), preserve_range=True).astype(np.uint8)
218
  imgs.append(temp_img)
219
  return imgs