@@ -3736,98 +3736,66 @@ def resize_image(image, annos, mask, target_width, target_height):
37363736
37373737 _target_height = size [0 ]
37383738 _target_width = size [1 ]
3739+
37393740 if len (np .shape (image )) == 2 :
37403741 image = cv2 .cvtColor (image , cv2 .COLOR_GRAY2RGB )
3741- height , width , _ = np .shape (image )
3742- # print("the size of original img is:", height, width)
3743- if height <= width :
3744- ratio = _target_height / height
3745- new_width = int (ratio * width )
3746- if height == width :
3747- new_width = _target_height
3742+ input_height , input_width , _ = np .shape (image )
37483743
3749- image , annos , mask = resize_image (image , annos , mask , new_width , _target_height )
3744+ vertical_ratio = _target_height / input_height
3745+ horizontal_ratio = _target_width / input_width
37503746
3751- # for i in annos:
3752- # if len(i) is not 19:
3753- # print('Joints of person is not 19 ERROR FROM RESIZE')
3747+ rescale_ratio = max (vertical_ratio , horizontal_ratio )
37543748
3755- if new_width > _target_width :
3756- crop_range_x = np .random .randint (0 , new_width - _target_width )
3757- else :
3758- crop_range_x = 0
3759- image = image [:, crop_range_x :crop_range_x + _target_width , :]
3749+ image , annos , mask = resize_image (
3750+ image , annos , mask , round (input_width * rescale_ratio ), round (input_height * rescale_ratio )
3751+ )
3752+
3753+ # At this point we should have input image which matches at least target
3754+ # height or target width, while the other dimensions larger than target.
3755+ new_height , new_width , _ = np .shape (image )
3756+
3757+ if new_height > _target_height :
3758+ crop_range_y = np .random .randint (0 , new_height - _target_height )
3759+ image = image [crop_range_y :crop_range_y + _target_height , :, :]
37603760 if mask is not None :
3761- mask = mask [:, crop_range_x :crop_range_x + _target_width ]
3762- # joint_list= []
3761+ mask = mask [crop_range_y :crop_range_y + _target_height , :]
37633762 new_joints = []
3764- #annos-pepople-joints (must be 19 or [])
3765- for people in annos :
3766- # print("number of keypoints is", np.shape(people))
3763+
3764+ for people in annos : # TODO : speed up with affine transform
37673765 new_keypoints = []
37683766 for keypoints in people :
3769- if keypoints [0 ] < - 10 or keypoints [1 ] < - 10 :
3770- new_keypoints .append ((- 1000 , - 1000 ))
3771- continue
3772- top = crop_range_x + _target_width - 1
3773- if keypoints [0 ] >= crop_range_x and keypoints [0 ] <= top :
3774- # pts = (keypoints[0]-crop_range_x, keypoints[1])
3775- pts = (int (keypoints [0 ] - crop_range_x ), int (keypoints [1 ]))
3767+
3768+ # case orginal points are not usable
3769+ if keypoints [1 ] >= crop_range_y and keypoints [1 ] <= crop_range_y + _target_height - 1 :
3770+ pts = (int (keypoints [0 ]), int (keypoints [1 ] - crop_range_y ))
37763771 else :
37773772 pts = (- 1000 , - 1000 )
37783773 new_keypoints .append (pts )
37793774
37803775 new_joints .append (new_keypoints )
3781- # if len(new_keypoints) != 19:
3782- # print('1:The Length of joints list should be 0 or 19 but actually:', len(new_keypoints))
37833776 annos = new_joints
37843777
3785- if height > width :
3786- ratio = _target_width / width
3787- new_height = int (ratio * height )
3788- image , annos , mask = resize_image (image , annos , mask , _target_width , new_height )
3789-
3790- # for i in annos:
3791- # if len(i) is not 19:
3792- # print('Joints of person is not 19 ERROR')
3793-
3794- if new_height > _target_height :
3795- crop_range_y = np .random .randint (0 , new_height - _target_height )
3796-
3797- else :
3798- crop_range_y = 0
3799- image = image [crop_range_y :crop_range_y + _target_width , :, :]
3778+ elif new_width > _target_width :
3779+ crop_range_x = np .random .randint (0 , new_width - _target_width )
3780+ image = image [:, crop_range_x :crop_range_x + _target_width , :]
38003781 if mask is not None :
3801- mask = mask [crop_range_y : crop_range_y + _target_width , : ]
3782+ mask = mask [:, crop_range_x : crop_range_x + _target_width ]
38023783 new_joints = []
38033784
3804- for people in annos : # TODO : speed up with affine transform
3785+ for people in annos :
38053786 new_keypoints = []
38063787 for keypoints in people :
38073788
38083789 # case orginal points are not usable
3809- if keypoints [0 ] < 0 or keypoints [1 ] < 0 :
3810- new_keypoints .append ((- 1000 , - 1000 ))
3811- continue
3812- # y axis coordinate change
3813- bot = crop_range_y + _target_height - 1
3814- if keypoints [1 ] >= crop_range_y and keypoints [1 ] <= bot :
3815- # pts = (keypoints[0], keypoints[1]-crop_range_y)
3816- pts = (int (keypoints [0 ]), int (keypoints [1 ] - crop_range_y ))
3817- # if pts[0]>367 or pts[1]>367:
3818- # print('Error2')
3790+ if keypoints [0 ] >= crop_range_x and keypoints [0 ] <= crop_range_x + _target_width - 1 :
3791+ pts = (int (keypoints [0 ] - crop_range_x ), int (keypoints [1 ]))
38193792 else :
38203793 pts = (- 1000 , - 1000 )
3821-
38223794 new_keypoints .append (pts )
38233795
38243796 new_joints .append (new_keypoints )
3825- # if len(new_keypoints) != 19:
3826- # print('2:The Length of joints list should be 0 or 19 but actually:', len(new_keypoints))
3827-
38283797 annos = new_joints
38293798
3830- # mask = cv2.resize(mask, (46, 46), interpolation=cv2.INTER_AREA)
38313799 if mask is not None :
38323800 return image , annos , mask
38333801 else :
0 commit comments