Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is recommended to use np.asarray instead of np.array to avoid unne… #3537

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion experiments_mask_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from extras.inpaint_mask import SAMOptions, generate_mask_from_image

original_image = Image.open('cat.webp')
image = np.array(original_image, dtype=np.uint8)
image = np.asarray(original_image, dtype=np.uint8)

sam_options = SAMOptions(
dino_prompt='eye',
Expand Down
24 changes: 12 additions & 12 deletions extras/facexlib/detection/align_trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ def get_reference_facial_points(output_size=None, inner_padding_factor=0.0, oute
inner_padding_factor))
Returns:
----------
@reference_5point: 5x2 np.array
@reference_5point: 5x2 np.asarray
each row is a pair of transformed coordinates (x, y)
"""

tmp_5pts = np.array(REFERENCE_FACIAL_POINTS)
tmp_crop_size = np.array(DEFAULT_CROP_SIZE)
tmp_5pts = np.asarray(REFERENCE_FACIAL_POINTS)
tmp_crop_size = np.asarray(DEFAULT_CROP_SIZE)

# 0) make the inner region a square
if default_square:
Expand All @@ -79,7 +79,7 @@ def get_reference_facial_points(output_size=None, inner_padding_factor=0.0, oute
if ((inner_padding_factor > 0 or outer_padding[0] > 0 or outer_padding[1] > 0) and output_size is None):
output_size = tmp_crop_size * \
(1 + inner_padding_factor * 2).astype(np.int32)
output_size += np.array(outer_padding)
output_size += np.asarray(outer_padding)
if not (outer_padding[0] < output_size[0] and outer_padding[1] < output_size[1]):
raise FaceWarpException('Not (outer_padding[0] < output_size[0] and outer_padding[1] < output_size[1])')

Expand All @@ -90,7 +90,7 @@ def get_reference_facial_points(output_size=None, inner_padding_factor=0.0, oute
tmp_crop_size += np.round(size_diff).astype(np.int32)

# 2) resize the padded inner region
size_bf_outer_pad = np.array(output_size) - np.array(outer_padding) * 2
size_bf_outer_pad = np.asarray(output_size) - np.asarray(outer_padding) * 2

if size_bf_outer_pad[0] * tmp_crop_size[1] != size_bf_outer_pad[1] * tmp_crop_size[0]:
raise FaceWarpException('Must have (output_size - outer_padding)'
Expand All @@ -103,7 +103,7 @@ def get_reference_facial_points(output_size=None, inner_padding_factor=0.0, oute
tmp_crop_size = size_bf_outer_pad

# 3) add outer_padding to make output_size
reference_5point = tmp_5pts + np.array(outer_padding)
reference_5point = tmp_5pts + np.asarray(outer_padding)
tmp_crop_size = output_size

return reference_5point
Expand All @@ -116,13 +116,13 @@ def get_affine_transform_matrix(src_pts, dst_pts):
get affine transform matrix 'tfm' from src_pts to dst_pts
Parameters:
----------
@src_pts: Kx2 np.array
@src_pts: Kx2 np.asarray
source points matrix, each row is a pair of coordinates (x, y)
@dst_pts: Kx2 np.array
@dst_pts: Kx2 np.asarray
destination points matrix, each row is a pair of coordinates (x, y)
Returns:
----------
@tfm: 2x3 np.array
@tfm: 2x3 np.asarray
transform matrix from src_pts to dst_pts
"""

Expand All @@ -149,17 +149,17 @@ def warp_and_crop_face(src_img, facial_pts, reference_pts=None, crop_size=(96, 1
apply affine transform 'trans' to uv
Parameters:
----------
@src_img: 3x3 np.array
@src_img: 3x3 np.asarray
input image
@facial_pts: could be
1)a list of K coordinates (x,y)
or
2) Kx2 or 2xK np.array
2) Kx2 or 2xK np.asarray
each row or col is a pair of coordinates (x, y)
@reference_pts: could be
1) a list of K coordinates (x,y)
or
2) Kx2 or 2xK np.array
2) Kx2 or 2xK np.asarray
each row or col is a pair of coordinates (x, y)
or
3) None
Expand Down
44 changes: 22 additions & 22 deletions extras/facexlib/detection/matlab_cp2tform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def tformfwd(trans, uv):

Parameters:
----------
@trans: 3x3 np.array
@trans: 3x3 np.asarray
transform matrix
@uv: Kx2 np.array
@uv: Kx2 np.asarray
each row is a pair of coordinates (x, y)

Returns:
----------
@xy: Kx2 np.array
@xy: Kx2 np.asarray
each row is a pair of transformed coordinates (x, y)
"""
uv = np.hstack((uv, np.ones((uv.shape[0], 1))))
Expand All @@ -42,14 +42,14 @@ def tforminv(trans, uv):

Parameters:
----------
@trans: 3x3 np.array
@trans: 3x3 np.asarray
transform matrix
@uv: Kx2 np.array
@uv: Kx2 np.asarray
each row is a pair of coordinates (x, y)

Returns:
----------
@xy: Kx2 np.array
@xy: Kx2 np.asarray
each row is a pair of inverse-transformed coordinates (x, y)
"""
Tinv = inv(trans)
Expand Down Expand Up @@ -84,18 +84,18 @@ def findNonreflectiveSimilarity(uv, xy, options=None):
tx = r[2]
ty = r[3]

Tinv = np.array([[sc, -ss, 0], [ss, sc, 0], [tx, ty, 1]])
Tinv = np.asarray([[sc, -ss, 0], [ss, sc, 0], [tx, ty, 1]])
T = inv(Tinv)
T[:, 2] = np.array([0, 0, 1])
T[:, 2] = np.asarray([0, 0, 1])

return T, Tinv


def findSimilarity(uv, xy, options=None):
options = {'K': 2}

# uv = np.array(uv)
# xy = np.array(xy)
# uv = np.asarray(uv)
# xy = np.asarray(xy)

# Solve for trans1
trans1, trans1_inv = findNonreflectiveSimilarity(uv, xy, options)
Expand All @@ -109,7 +109,7 @@ def findSimilarity(uv, xy, options=None):
trans2r, trans2r_inv = findNonreflectiveSimilarity(uv, xyR, options)

# manually reflect the tform to undo the reflection done on xyR
TreflectY = np.array([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
TreflectY = np.asarray([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])

trans2 = np.dot(trans2r, TreflectY)

Expand Down Expand Up @@ -140,9 +140,9 @@ def get_similarity_transform(src_pts, dst_pts, reflective=True):

Parameters:
----------
@src_pts: Kx2 np.array
@src_pts: Kx2 np.asarray
source points, each row is a pair of coordinates (x, y)
@dst_pts: Kx2 np.array
@dst_pts: Kx2 np.asarray
destination points, each row is a pair of transformed
coordinates (x, y)
@reflective: True or False
Expand All @@ -153,9 +153,9 @@ def get_similarity_transform(src_pts, dst_pts, reflective=True):

Returns:
----------
@trans: 3x3 np.array
@trans: 3x3 np.asarray
transform matrix from uv to xy
trans_inv: 3x3 np.array
trans_inv: 3x3 np.asarray
inverse of trans, transform matrix from xy to uv
"""

Expand All @@ -181,12 +181,12 @@ def cvt_tform_mat_for_cv2(trans):

Parameters:
----------
@trans: 3x3 np.array
@trans: 3x3 np.asarray
transform matrix from uv to xy

Returns:
----------
@cv2_trans: 2x3 np.array
@cv2_trans: 2x3 np.asarray
transform matrix from src_pts to dst_pts, could be directly used
for cv2.warpAffine()
"""
Expand All @@ -209,9 +209,9 @@ def get_similarity_transform_for_cv2(src_pts, dst_pts, reflective=True):

Parameters:
----------
@src_pts: Kx2 np.array
@src_pts: Kx2 np.asarray
source points, each row is a pair of coordinates (x, y)
@dst_pts: Kx2 np.array
@dst_pts: Kx2 np.asarray
destination points, each row is a pair of transformed
coordinates (x, y)
reflective: True or False
Expand All @@ -222,7 +222,7 @@ def get_similarity_transform_for_cv2(src_pts, dst_pts, reflective=True):

Returns:
----------
@cv2_trans: 2x3 np.array
@cv2_trans: 2x3 np.asarray
transform matrix from src_pts to dst_pts, could be directly used
for cv2.warpAffine()
"""
Expand Down Expand Up @@ -276,8 +276,8 @@ def get_similarity_transform_for_cv2(src_pts, dst_pts, reflective=True):
x = [-1, 0, 4]
y = [-1, -10, 4]

uv = np.array((u, v)).T
xy = np.array((x, y)).T
uv = np.asarray((u, v)).T
xy = np.asarray((x, y)).T

print('\n--->uv:')
print(uv)
Expand Down
12 changes: 6 additions & 6 deletions extras/facexlib/detection/retinaface.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def __align_multi(self, image, boxes, landmarks, limit=None):
for landmark in landmarks:
facial5points = [[landmark[2 * j], landmark[2 * j + 1]] for j in range(5)]

warped_face = warp_and_crop_face(np.array(image), facial5points, self.reference, crop_size=(112, 112))
warped_face = warp_and_crop_face(np.asarray(image), facial5points, self.reference, crop_size=(112, 112))
faces.append(warped_face)

return np.concatenate((boxes, landmarks), axis=1), faces
Expand Down Expand Up @@ -304,15 +304,15 @@ def batched_transform(self, frames, use_origin_size):
def batched_detect_faces(self, frames, conf_threshold=0.8, nms_threshold=0.4, use_origin_size=True):
"""
Arguments:
frames: a list of PIL.Image, or np.array(shape=[n, h, w, c],
frames: a list of PIL.Image, or np.asarray(shape=[n, h, w, c],
type=np.uint8, BGR format).
conf_threshold: confidence threshold.
nms_threshold: nms threshold.
use_origin_size: whether to use origin size.
Returns:
final_bounding_boxes: list of np.array ([n_boxes, 5],
final_bounding_boxes: list of np.asarray ([n_boxes, 5],
type=np.float32).
final_landmarks: list of np.array ([n_boxes, 10], type=np.float32).
final_landmarks: list of np.asarray ([n_boxes, 10], type=np.float32).
"""
# self.t['forward_pass'].tic()
frames, self.resize = self.batched_transform(frames, use_origin_size)
Expand Down Expand Up @@ -340,8 +340,8 @@ def batched_detect_faces(self, frames, conf_threshold=0.8, nms_threshold=0.4, us
# ignore low scores
pred, landm = pred[inds, :], landm[inds, :]
if pred.shape[0] == 0:
final_bounding_boxes.append(np.array([], dtype=np.float32))
final_landmarks.append(np.array([], dtype=np.float32))
final_bounding_boxes.append(np.asarray([], dtype=np.float32))
final_landmarks.append(np.asarray([], dtype=np.float32))
continue

# sort
Expand Down
14 changes: 7 additions & 7 deletions extras/facexlib/utils/face_restoration_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def get_location(val, length):

def get_center_face(det_faces, h=0, w=0, center=None):
if center is not None:
center = np.array(center)
center = np.asarray(center)
else:
center = np.array([w / 2, h / 2])
center = np.asarray([w / 2, h / 2])
center_dist = []
for det_face in det_faces:
face_center = np.array([(det_face[0] + det_face[2]) / 2, (det_face[1] + det_face[3]) / 2])
face_center = np.asarray([(det_face[0] + det_face[2]) / 2, (det_face[1] + det_face[3]) / 2])
dist = np.linalg.norm(face_center - center)
center_dist.append(dist)
center_idx = center_dist.index(min(center_dist))
Expand Down Expand Up @@ -67,10 +67,10 @@ def __init__(self,
self.face_size = (int(face_size * self.crop_ratio[1]), int(face_size * self.crop_ratio[0]))

if self.template_3points:
self.face_template = np.array([[192, 240], [319, 240], [257, 371]])
self.face_template = np.asarray([[192, 240], [319, 240], [257, 371]])
else:
# standard 5 landmarks for FFHQ faces with 512 x 512
self.face_template = np.array([[192.98138, 239.94708], [318.90277, 240.1936], [256.63416, 314.01935],
self.face_template = np.asarray([[192.98138, 239.94708], [318.90277, 240.1936], [256.63416, 314.01935],
[201.26117, 371.41043], [313.08905, 371.15118]])
self.face_template = self.face_template * (face_size / 512.0)
if self.crop_ratio[0] > 1:
Expand Down Expand Up @@ -144,9 +144,9 @@ def get_face_landmarks_5(self,
continue

if self.template_3points:
landmark = np.array([[bbox[i], bbox[i + 1]] for i in range(5, 11, 2)])
landmark = np.asarray([[bbox[i], bbox[i + 1]] for i in range(5, 11, 2)])
else:
landmark = np.array([[bbox[i], bbox[i + 1]] for i in range(5, 15, 2)])
landmark = np.asarray([[bbox[i], bbox[i + 1]] for i in range(5, 15, 2)])
self.all_landmarks_5.append(landmark)
self.det_faces.append(bbox[0:5])
if len(self.det_faces) == 0:
Expand Down
10 changes: 5 additions & 5 deletions extras/facexlib/utils/face_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def align_crop_face_landmarks(img,
transform_size = output_size * 4

# Parse landmarks
lm = np.array(landmarks)
lm = np.asarray(landmarks)
if lm.shape[0] == 5 and lm_type == 'retinaface_5':
eye_left = lm[0]
eye_right = lm[1]
Expand Down Expand Up @@ -163,7 +163,7 @@ def align_crop_face_landmarks(img,
# Transform use cv2
h_ratio = shrink_ratio[0] / shrink_ratio[1]
dst_h, dst_w = int(transform_size * h_ratio), transform_size
template = np.array([[0, 0], [0, dst_h], [dst_w, dst_h], [dst_w, 0]])
template = np.asarray([[0, 0], [0, dst_h], [dst_w, dst_h], [dst_w, 0]])
# use cv2.LMEDS method for the equivalence to skimage transform
# ref: https://blog.csdn.net/yichxi/article/details/115827338
affine_matrix = cv2.estimateAffinePartial2D(quad, template, method=cv2.LMEDS)[0]
Expand All @@ -176,11 +176,11 @@ def align_crop_face_landmarks(img,

if return_inverse_affine:
dst_h, dst_w = int(output_size * h_ratio), output_size
template = np.array([[0, 0], [0, dst_h], [dst_w, dst_h], [dst_w, 0]])
template = np.asarray([[0, 0], [0, dst_h], [dst_w, dst_h], [dst_w, 0]])
# use cv2.LMEDS method for the equivalence to skimage transform
# ref: https://blog.csdn.net/yichxi/article/details/115827338
affine_matrix = cv2.estimateAffinePartial2D(
quad_ori, np.array([[0, 0], [0, output_size], [dst_w, dst_h], [dst_w, 0]]), method=cv2.LMEDS)[0]
quad_ori, np.asarray([[0, 0], [0, output_size], [dst_w, dst_h], [dst_w, 0]]), method=cv2.LMEDS)[0]
inverse_affine = cv2.invertAffineTransform(affine_matrix)
else:
inverse_affine = None
Expand Down Expand Up @@ -234,7 +234,7 @@ def paste_face_back(img, face, inverse_affine):
bboxes = get_largest_face(bboxes, h, w)[0]
visualize_detection(img_ori, [bboxes], f'tmp/{img_name}_det.png')

landmarks = np.array([[bboxes[i], bboxes[i + 1]] for i in range(5, 15, 2)])
landmarks = np.asarray([[bboxes[i], bboxes[i + 1]] for i in range(5, 15, 2)])

cropped_face, inverse_affine = align_crop_face_landmarks(
img_ori,
Expand Down
4 changes: 2 additions & 2 deletions extras/inpaint_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def generate_mask_from_image(image: np.ndarray, mask_model: str = 'sam', extras=
draw = ImageDraw.Draw(debug_dino_image)
for box in boxes.numpy():
draw.rectangle(box.tolist(), fill="white")
return np.array(debug_dino_image), dino_detection_count, sam_detection_count, sam_detection_on_mask_count
return np.asarray(debug_dino_image), dino_detection_count, sam_detection_count, sam_detection_on_mask_count

transformed_boxes = sam_predictor.transform.apply_boxes_torch(boxes, image.shape[:2])
masks, _, _ = sam_predictor.predict_torch(
Expand All @@ -126,5 +126,5 @@ def generate_mask_from_image(image: np.ndarray, mask_model: str = 'sam', extras=

final_mask_tensor = (final_mask_tensor > 0).to('cpu').numpy()
mask_image = np.dstack((final_mask_tensor, final_mask_tensor, final_mask_tensor)) * 255
mask_image = np.array(mask_image, dtype=np.uint8)
mask_image = np.asarray(mask_image, dtype=np.uint8)
return mask_image, dino_detection_count, sam_detection_count, sam_detection_on_mask_count
2 changes: 1 addition & 1 deletion extras/wd14tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def default_interrogator(image_rgb, threshold=0.35, character_threshold=0.85, ex
square = Image.new("RGB", (height, height), (255, 255, 255))
square.paste(image, ((height-new_size[0])//2, (height-new_size[1])//2))

image = np.array(square).astype(np.float32)
image = np.asarray(square).astype(np.float32)
image = image[:, :, ::-1] # RGB -> BGR
image = np.expand_dims(image, 0)

Expand Down
6 changes: 3 additions & 3 deletions ldm_patched/contrib/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,10 +1476,10 @@ def load_image(self, image):
if i.mode == 'I':
i = i.point(lambda i: i * (1 / 255))
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = np.asarray(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = np.asarray(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
Expand Down Expand Up @@ -1536,7 +1536,7 @@ def load_image(self, image, channel):
mask = None
c = channel[0].upper()
if c in i.getbands():
mask = np.array(i.getchannel(c)).astype(np.float32) / 255.0
mask = np.asarray(i.getchannel(c)).astype(np.float32) / 255.0
mask = torch.from_numpy(mask)
if c == 'A':
mask = 1. - mask
Expand Down
Loading