tidalove commited on
Commit
3ae25b7
·
verified ·
1 Parent(s): bcbdf9f

Update square_crop.py

Browse files
Files changed (1) hide show
  1. square_crop.py +5 -5
square_crop.py CHANGED
@@ -20,15 +20,15 @@ def load_coco(json_path):
20
  return id2fname, coco["annotations"]
21
 
22
 
23
- def square_from_bbox(x, y, w, h, img_w, img_h):
24
  """
25
  Compute (left, top, side) of the smallest square fully containing the bbox.
26
  The square is centred on the bbox; if it overflows the image, it is shifted
27
  (but not resized) so it lies inside the image. Returns the final crop box
28
  (left, top, side).
29
  """
30
- side = max(w, h)
31
- cx, cy = x + w / 2.0, y + h / 2.0
32
  left = int(round(cx - side / 2.0))
33
  top = int(round(cy - side / 2.0))
34
 
@@ -41,8 +41,8 @@ def square_from_bbox(x, y, w, h, img_w, img_h):
41
  def crop_annotation(img_path, ann, out_dir, pad_color=0):
42
  with Image.open(img_path) as img:
43
  img_w, img_h = img.size
44
- x, y, w, h = ann["bbox"] # COCO bbox = [x, y, width, height]
45
- left, top, side = square_from_bbox(x, y, w, h, img_w, img_h)
46
 
47
  # Perform crop (may be smaller than 'side' at edges)
48
  crop = img.crop((left, top, left + side, top + side))
 
20
  return id2fname, coco["annotations"]
21
 
22
 
23
+ def square_from_bbox(x1, y1, x2, y2, img_w, img_h):
24
  """
25
  Compute (left, top, side) of the smallest square fully containing the bbox.
26
  The square is centred on the bbox; if it overflows the image, it is shifted
27
  (but not resized) so it lies inside the image. Returns the final crop box
28
  (left, top, side).
29
  """
30
+ side = max(x2-x1, y2-y1)
31
+ cx, cy = (x1+x2) / 2.0, (y1+y2) / 2.0
32
  left = int(round(cx - side / 2.0))
33
  top = int(round(cy - side / 2.0))
34
 
 
41
  def crop_annotation(img_path, ann, out_dir, pad_color=0):
42
  with Image.open(img_path) as img:
43
  img_w, img_h = img.size
44
+ x1, y1, x2, y2 = ann["bbox"]
45
+ left, top, side = square_from_bbox(x1, y1, x2, y2, img_w, img_h)
46
 
47
  # Perform crop (may be smaller than 'side' at edges)
48
  crop = img.crop((left, top, left + side, top + side))