Skip to content

Added max_fps feature to optionally limit fps #478

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 30 additions & 14 deletions afy/cam_fomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -278,6 +287,7 @@ def select_camera(config):
try:
while True:
tt = TicToc()
tt.tic()

timing = {
'preproc': 0,
Expand All @@ -287,7 +297,6 @@ def select_camera(config):

green_overlay = False

tt.tic()

ret, frame = cap.read()
if not ret:
Expand Down Expand Up @@ -321,7 +330,7 @@ def select_camera(config):
out = None

tt.tic()

key = cv2.waitKey(1)

if cv2.getWindowProperty('cam', cv2.WND_PROP_VISIBLE) < 1.0:
Expand Down Expand Up @@ -375,7 +384,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'):
Expand Down Expand Up @@ -429,18 +438,18 @@ 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()
overlay[:] = (0, 255, 0)
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)

Expand Down Expand Up @@ -470,10 +479,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")

Expand Down