From 1a9c07167a7ef6cdfd2f0c522fd887783165e2db Mon Sep 17 00:00:00 2001 From: Migushthe2nd Date: Sun, 12 Apr 2020 21:34:27 +0200 Subject: [PATCH 1/6] Automatically adjust window size and borders --- faceit_live.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/faceit_live.py b/faceit_live.py index db82410..7b99fe6 100644 --- a/faceit_live.py +++ b/faceit_live.py @@ -37,6 +37,7 @@ gpu_id = args.gpu_id webcam_height = 480 webcam_width = 640 screen_width, screen_height = pyautogui.size() +img_shape = [256, 256, 0] system = args.system if system=="linux": @@ -117,7 +118,7 @@ def main(): cv2.namedWindow('Stream', cv2.WINDOW_GUI_NORMAL) # rendered to fake webcam cv2.moveWindow('Stream', int(screen_width/2)-int(webcam_width/2), 400) - cv2.resizeWindow('Stream', webcam_width,webcam_width) + cv2.resizeWindow('Stream', webcam_width,webcam_height) print("Press C to center Webcam, Press N for next image in media directory, T to alter between relative and absolute transformation, Q to quit") @@ -146,9 +147,11 @@ def main(): cv2.imshow('DeepFake', deep_fake) - rgb = cv2.resize(deep_fake,(480,480)) + rgb = cv2.resize(deep_fake,(int(img_shape[1] / img_shape[0] * 480),480)) # pad image - stream_v = cv2.copyMakeBorder( rgb, 0, 0, 80, 80, cv2.BORDER_CONSTANT) + x_border = int((640-(img_shape[1] / img_shape[0] * 480))/2) + y_border = int((480-(img_shape[0] / img_shape[1] * 640))/2) + stream_v = cv2.copyMakeBorder(rgb, y_border if y_border >=0 else 0, y_border if y_border >=0 else 0, x_border if x_border >=0 else 0, x_border if x_border >=0 else 0, cv2.BORDER_CONSTANT) cv2.imshow('Stream',stream_v) #time.sleep(1/30.0) @@ -248,7 +251,7 @@ def find_face_cut(net,face,previous=False): return cut_x1,cut_y1,cut_x2,cut_y2 def readnextimage(position=-1): - global img_list,pos + global img_list,pos,img_shape if (position != -1): pos = position else: @@ -257,6 +260,8 @@ def readnextimage(position=-1): else: pos=0 img = imageio.imread(img_list[pos]) + img_shape = img.shape + cv2.resizeWindow('DeepFake', int(img_shape[1] / img_shape[0] * 256), 256) img = resize(img, (256, 256))[..., :3] return img From d5092a35fe3e11bcebdd3caaaa54f42bf4c68cdd Mon Sep 17 00:00:00 2001 From: Migushthe2nd Date: Sun, 12 Apr 2020 21:57:30 +0200 Subject: [PATCH 2/6] Also resize first image --- faceit_live.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faceit_live.py b/faceit_live.py index 7b99fe6..7032376 100644 --- a/faceit_live.py +++ b/faceit_live.py @@ -113,7 +113,7 @@ def main(): cv2.namedWindow('DeepFake', cv2.WINDOW_GUI_NORMAL) # face transformation cv2.moveWindow('DeepFake', int(screen_width/2)+150, 100) - cv2.resizeWindow('DeepFake', 256,256) + cv2.resizeWindow('DeepFake', int(img_shape[1] / img_shape[0] * 256), 256) cv2.namedWindow('Stream', cv2.WINDOW_GUI_NORMAL) # rendered to fake webcam From e14ba3008e4f99a37806f431f3bf1e93500fa8cd Mon Sep 17 00:00:00 2001 From: Migushthe2nd Date: Sun, 12 Apr 2020 22:08:20 +0200 Subject: [PATCH 3/6] Key B for previous image --- faceit_live.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/faceit_live.py b/faceit_live.py index 7032376..8a87181 100644 --- a/faceit_live.py +++ b/faceit_live.py @@ -174,9 +174,14 @@ def main(): print("Centering the image") # center reset = True + elif k==ord('b'): + # rotate images + print("Loading previous image") + source_image = readpreviousimage() + reset = True elif k==ord('n'): # rotate images - print("Loading new image") + print("Loading next image") source_image = readnextimage() reset = True elif k==ord('t'): @@ -250,8 +255,24 @@ def find_face_cut(net,face,previous=False): return cut_x1,cut_y1,cut_x2,cut_y2 +def readimage(): + global img_list,img_shape + img = imageio.imread(img_list[pos]) + img_shape = img.shape + cv2.resizeWindow('DeepFake', int(img_shape[1] / img_shape[0] * 256), 256) + img = resize(img, (256, 256))[..., :3] + return img + +def readpreviousimage(): + global pos + if pos Date: Sun, 12 Apr 2020 22:15:25 +0200 Subject: [PATCH 4/6] Also resize first image --- faceit_live.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faceit_live.py b/faceit_live.py index 7b99fe6..7032376 100644 --- a/faceit_live.py +++ b/faceit_live.py @@ -113,7 +113,7 @@ def main(): cv2.namedWindow('DeepFake', cv2.WINDOW_GUI_NORMAL) # face transformation cv2.moveWindow('DeepFake', int(screen_width/2)+150, 100) - cv2.resizeWindow('DeepFake', 256,256) + cv2.resizeWindow('DeepFake', int(img_shape[1] / img_shape[0] * 256), 256) cv2.namedWindow('Stream', cv2.WINDOW_GUI_NORMAL) # rendered to fake webcam From 6e0a1253621d302900bb717f4b279094a4133f2b Mon Sep 17 00:00:00 2001 From: Migushthe2nd Date: Sun, 12 Apr 2020 22:29:51 +0200 Subject: [PATCH 5/6] Updated console message --- faceit_live.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faceit_live.py b/faceit_live.py index 8a87181..a0f57f5 100644 --- a/faceit_live.py +++ b/faceit_live.py @@ -121,7 +121,7 @@ def main(): cv2.resizeWindow('Stream', webcam_width,webcam_height) - print("Press C to center Webcam, Press N for next image in media directory, T to alter between relative and absolute transformation, Q to quit") + print("Press C to center Webcam, Press B/N for previous/next image in media directory, T to alter between relative and absolute transformation, Q to quit") x1,y1,x2,y2 = [0,0,0,0] relative = True while True: From af03144b5e108bbb44926b2aa472c09827c09b0b Mon Sep 17 00:00:00 2001 From: Migushthe2nd Date: Sun, 12 Apr 2020 22:31:52 +0200 Subject: [PATCH 6/6] Updated console message --- README.md | 1 + faceit_live.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b018a2d..84479d3 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ $ python faceit_live.py --webcam_id 0 --stream_id 1 --gpu_id 0 --system linux ## Key Shortcuts when running ``` +B - cycle previous image in media folder N - cycle next image in media folder C - recenter webcam and create a new base image T - option to alter between 'Relative' and 'Absolute' transformations mode diff --git a/faceit_live.py b/faceit_live.py index 8a87181..02f2f84 100644 --- a/faceit_live.py +++ b/faceit_live.py @@ -121,7 +121,7 @@ def main(): cv2.resizeWindow('Stream', webcam_width,webcam_height) - print("Press C to center Webcam, Press N for next image in media directory, T to alter between relative and absolute transformation, Q to quit") + print("Press C to center Webcam, Press B/N for previous/next image in media directory, T to alter between relative and absolute transformation, Q to quit") x1,y1,x2,y2 = [0,0,0,0] relative = True while True: @@ -171,16 +171,16 @@ def main(): video_capture.release() break elif k==ord('c'): - print("Centering the image") # center + print("Centering the image") reset = True elif k==ord('b'): - # rotate images + # previous image print("Loading previous image") source_image = readpreviousimage() reset = True elif k==ord('n'): - # rotate images + # next image print("Loading next image") source_image = readnextimage() reset = True