From 36ccae0baa185515509cd68cf03ee8ffee9a5ba7 Mon Sep 17 00:00:00 2001 From: Source61 Date: Sat, 3 Dec 2022 17:47:38 -0500 Subject: [PATCH 1/2] Added max_fps feature to optionally limit fps --- afy/cam_fomm.py | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/afy/cam_fomm.py b/afy/cam_fomm.py index 13ac1d4a3..6ac4b7be8 100644 --- a/afy/cam_fomm.py +++ b/afy/cam_fomm.py @@ -29,26 +29,26 @@ def is_new_frame_better(source, driving, predictor): global avatar_kp global display_string - + if avatar_kp is None: display_string = "No face detected in avatar." return False - + if predictor.get_start_frame() is None: display_string = "No frame to compare to." return True - + driving_smaller = resize(driving, (128, 128))[..., :3] new_kp = predictor.get_frame_kp(driving) - + if new_kp is not None: new_norm = (np.abs(avatar_kp - new_kp) ** 2).sum() old_norm = (np.abs(avatar_kp - predictor.get_start_frame_kp()) ** 2).sum() - + out_string = "{0} : {1}".format(int(new_norm * 100), int(old_norm * 100)) display_string = out_string log(out_string) - + return new_norm < old_norm else: display_string = "No face found!" @@ -183,6 +183,15 @@ def select_camera(config): if __name__ == "__main__": with open('config.yaml', 'r') as f: config = yaml.load(f, Loader=yaml.FullLoader) + if not 'max_fps' in config: + f.seek(0) + data = f.read(10 ** 4).rstrip("\n") + config['max_fps'] = None + max_fps = config['max_fps'] + if config['max_fps'] == None: + with open('config.yaml', 'w') as f: + f.write(f"{data}\n\n# limit cam fps by setting this value to a positive integer\nmax_fps: 0") + max_fps = config['max_fps'] = 0 global display_string display_string = "" @@ -245,11 +254,11 @@ def select_camera(config): else: enable_vcam = False # log("Virtual camera is supported only on Linux.") - + # if not enable_vcam: # log("Virtual camera streaming will be disabled.") - cur_ava = 0 + cur_ava = 0 avatar = None change_avatar(predictor, avatars[cur_ava]) passthrough = False @@ -276,8 +285,10 @@ def select_camera(config): print_help() try: + start = time.time() while True: tt = TicToc() + tt.tic() timing = { 'preproc': 0, @@ -287,7 +298,6 @@ def select_camera(config): green_overlay = False - tt.tic() ret, frame = cap.read() if not ret: @@ -321,7 +331,7 @@ def select_camera(config): out = None tt.tic() - + key = cv2.waitKey(1) if cv2.getWindowProperty('cam', cv2.WND_PROP_VISIBLE) < 1.0: @@ -375,7 +385,7 @@ def select_camera(config): if not is_calibrated: cv2.namedWindow('avatarify', cv2.WINDOW_GUI_NORMAL) cv2.moveWindow('avatarify', 600, 250) - + is_calibrated = True show_landmarks = False elif key == ord('z'): @@ -429,10 +439,10 @@ def select_camera(config): draw_face_landmarks(preview_frame, avatar_kp, (200, 20, 10)) frame_kp = predictor.get_frame_kp(frame) draw_face_landmarks(preview_frame, frame_kp) - + if preview_flip: preview_frame = cv2.flip(preview_frame, 1) - + if green_overlay: green_alpha = 0.8 overlay = preview_frame.copy() @@ -440,7 +450,7 @@ def select_camera(config): preview_frame = cv2.addWeighted( preview_frame, green_alpha, overlay, 1.0 - green_alpha, 0.0) timing['postproc'] = tt.toc() - + if find_keyframe: preview_frame = cv2.putText(preview_frame, display_string, (10, 220), 0, 0.5 * IMG_SIZE / 256, (255, 255, 255), 1) @@ -470,10 +480,17 @@ def select_camera(config): cv2.imshow('avatarify', out[..., ::-1]) + if max_fps >= 1: + lapse = tt.toc(total=True) / 1000 + sleep = (1 / max_fps) - lapse + if sleep > 0: + time.sleep(sleep) + fps_hist.append(tt.toc(total=True)) if len(fps_hist) == 10: fps = 10 / (sum(fps_hist) / 1000) fps_hist = [] + except KeyboardInterrupt: log("main: user interrupt") From cd8ee26ada6a496c61b47d6fc0db3df5170ad2d7 Mon Sep 17 00:00:00 2001 From: Source61 Date: Sat, 3 Dec 2022 18:14:43 -0500 Subject: [PATCH 2/2] Removed unnecessary line --- afy/cam_fomm.py | 1 - 1 file changed, 1 deletion(-) diff --git a/afy/cam_fomm.py b/afy/cam_fomm.py index 6ac4b7be8..d97c89758 100644 --- a/afy/cam_fomm.py +++ b/afy/cam_fomm.py @@ -285,7 +285,6 @@ def select_camera(config): print_help() try: - start = time.time() while True: tt = TicToc() tt.tic()