From ecf028c1a3764980db0c8a57fe7fa54e2293af96 Mon Sep 17 00:00:00 2001 From: Ze-Yi LIN <58305964+Zeyi-Lin@users.noreply.github.com> Date: Fri, 20 Sep 2024 21:29:32 +0800 Subject: [PATCH] feat: performance printing (#156) * update performance print * upgrade printing --- .gitignore | 1 + hivision/creator/__init__.py | 34 +++++++++++++++++++++++++++-- hivision/creator/face_detector.py | 3 --- hivision/creator/photo_adjuster.py | 1 - hivision/creator/rotation_adjust.py | 1 - rotation_adjust.py | 1 - 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index f475332d..a73d9102 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ demo/kb_output/*.png scripts/sync_swanhub.py scripts/sync_huggingface.py scripts/sync_modelscope.py +scripts/sync_all.py **/flagged/ # build outputs dist diff --git a/hivision/creator/__init__.py b/hivision/creator/__init__.py index fe8a2c26..d6f94366 100644 --- a/hivision/creator/__init__.py +++ b/hivision/creator/__init__.py @@ -16,6 +16,7 @@ from hivision.plugin.beauty.handler import beauty_face from .photo_adjuster import adjust_photo import cv2 +import time class IDCreator: @@ -100,6 +101,10 @@ def __call__( face_alignment=face_alignment, ) + + # 总的开始时间 + total_start_time = time.time() + self.ctx = Context(params) ctx = self.ctx ctx.processing_image = image @@ -110,15 +115,26 @@ def __call__( self.before_all and self.before_all(ctx) # 1. ------------------人像抠图------------------ + # 如果仅裁剪,则不进行抠图 if not ctx.params.crop_only: # 调用抠图工作流 + print("[1] Start Human Matting...") + start_matting_time = time.time() self.matting_handler(ctx) + end_matting_time = time.time() + print(f"[1] Human Matting Time: {end_matting_time - start_matting_time:.3f}s") self.after_matting and self.after_matting(ctx) + # 如果进行抠图 else: ctx.matting_image = ctx.processing_image + # 2. ------------------美颜------------------ + print("[2] Start Beauty...") + start_beauty_time = time.time() self.beauty_handler(ctx) + end_beauty_time = time.time() + print(f"[2] Beauty Time: {end_beauty_time - start_beauty_time:.3f}s") # 如果仅换底,则直接返回抠图结果 if ctx.params.change_bg_only: @@ -134,15 +150,19 @@ def __call__( return ctx.result # 3. ------------------人脸检测------------------ + print("[3] Start Face Detection...") + start_detection_time = time.time() self.detection_handler(ctx) + end_detection_time = time.time() + print(f"[3] Face Detection Time: {end_detection_time - start_detection_time:.3f}s") self.after_detect and self.after_detect(ctx) # 3.1 ------------------人脸对齐------------------ if ctx.params.face_alignment and abs(ctx.face["roll_angle"]) > 2: + print("[3.1] Start Face Alignment...") + start_alignment_time = time.time() from hivision.creator.rotation_adjust import rotate_bound_4channels - print("执行人脸对齐") - print("旋转角度:", ctx.face["roll_angle"]) # 根据角度旋转原图和抠图 b, g, r, a = cv2.split(ctx.matting_image) ctx.origin_image, ctx.matting_image, _, _, _, _ = rotate_bound_4channels( @@ -154,11 +174,17 @@ def __call__( # 旋转后再执行一遍人脸检测 self.detection_handler(ctx) self.after_detect and self.after_detect(ctx) + end_alignment_time = time.time() + print(f"[3.1] Face Alignment Time: {end_alignment_time - start_alignment_time:.3f}s") # 4. ------------------图像调整------------------ + print("[4] Start Image Post-Adjustment...") + start_adjust_time = time.time() result_image_hd, result_image_standard, clothing_params, typography_params = ( adjust_photo(ctx) ) + end_adjust_time = time.time() + print(f"[4] Image Post-Adjustment Time: {end_adjust_time - start_adjust_time:.3f}s") # 5. ------------------返回结果------------------ ctx.result = Result( @@ -171,4 +197,8 @@ def __call__( ) self.after_all and self.after_all(ctx) + # 总的结束时间 + total_end_time = time.time() + print(f"[Total] Total Time: {total_end_time - total_start_time:.3f}s") + return ctx.result diff --git a/hivision/creator/face_detector.py b/hivision/creator/face_detector.py index b6851b56..03088125 100644 --- a/hivision/creator/face_detector.py +++ b/hivision/creator/face_detector.py @@ -173,7 +173,6 @@ def detect_face_retinaface(ctx: Context): global RETINAFCE_SESS if RETINAFCE_SESS is None: - print("首次加载RetinaFace模型...") # 计算用时 tic = time() faces_dets, sess = retinaface_detect_faces( @@ -182,7 +181,6 @@ def detect_face_retinaface(ctx: Context): sess=None, ) RETINAFCE_SESS = sess - print("首次RetinaFace模型推理用时: {:.4f}s".format(time() - tic)) else: tic = time() faces_dets, _ = retinaface_detect_faces( @@ -190,7 +188,6 @@ def detect_face_retinaface(ctx: Context): os.path.join(base_dir, "retinaface/weights/retinaface-resnet50.onnx"), sess=RETINAFCE_SESS, ) - print("二次RetinaFace模型推理用时: {:.4f}s".format(time() - tic)) faces_num = len(faces_dets) faces_landmarks = [] diff --git a/hivision/creator/photo_adjuster.py b/hivision/creator/photo_adjuster.py index bd25e193..325da333 100644 --- a/hivision/creator/photo_adjuster.py +++ b/hivision/creator/photo_adjuster.py @@ -173,7 +173,6 @@ def IDphotos_cut(x1, y1, x2, y2, img): temp_x_2 = temp_x_2 - x2 # 生成一张全透明背景 - print("crop_size:", crop_size) background_bgr = np.full((crop_size[0], crop_size[1]), 255, dtype=np.uint8) background_a = np.full((crop_size[0], crop_size[1]), 0, dtype=np.uint8) background = cv2.merge( diff --git a/hivision/creator/rotation_adjust.py b/hivision/creator/rotation_adjust.py index 6d8e72e8..e3f1f041 100644 --- a/hivision/creator/rotation_adjust.py +++ b/hivision/creator/rotation_adjust.py @@ -26,7 +26,6 @@ def rotate_bound(image: np.ndarray, angle: float, center=None): - dW (int): 宽度变化量 - dH (int): 高度变化量 """ - print("rotate_bound", image.shape) (h, w) = image.shape[:2] if center is None: (cX, cY) = (w / 2, h / 2) diff --git a/rotation_adjust.py b/rotation_adjust.py index 6d8e72e8..e3f1f041 100644 --- a/rotation_adjust.py +++ b/rotation_adjust.py @@ -26,7 +26,6 @@ def rotate_bound(image: np.ndarray, angle: float, center=None): - dW (int): 宽度变化量 - dH (int): 高度变化量 """ - print("rotate_bound", image.shape) (h, w) = image.shape[:2] if center is None: (cX, cY) = (w / 2, h / 2)