Skip to content

Commit

Permalink
feat: performance printing (#156)
Browse files Browse the repository at this point in the history
* update performance print

* upgrade printing
  • Loading branch information
Zeyi-Lin authored Sep 20, 2024
1 parent b3c0a7a commit ecf028c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions hivision/creator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from hivision.plugin.beauty.handler import beauty_face
from .photo_adjuster import adjust_photo
import cv2
import time


class IDCreator:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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
3 changes: 0 additions & 3 deletions hivision/creator/face_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -182,15 +181,13 @@ 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(
ctx.origin_image,
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 = []
Expand Down
1 change: 0 additions & 1 deletion hivision/creator/photo_adjuster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion hivision/creator/rotation_adjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion rotation_adjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ecf028c

Please sign in to comment.