From cf88f3a8ee5d92e4af9a9cc2e0b0ed46ecac4ef9 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Mon, 12 Oct 2020 15:27:43 +0200 Subject: [PATCH] Windows pyinstaller compatibilities --- face_recognition/comparison.py | 24 ++++++++++++++---------- face_recognition/tools.py | 32 ++++++++++++++++++++++++++++++++ mirror.py | 4 +++- 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 face_recognition/tools.py diff --git a/face_recognition/comparison.py b/face_recognition/comparison.py index d326dd1..e0c38f1 100644 --- a/face_recognition/comparison.py +++ b/face_recognition/comparison.py @@ -229,7 +229,7 @@ def process1_hog(in_q, out_q): face_detector = dlib.get_frontal_face_detector() visualisation_factor = 1 - detection_factor = .4 + detection_factor = .3 process_this_frame = True @@ -239,7 +239,7 @@ def process1_hog(in_q, out_q): frame = in_q.get() frame = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY) - viz_frame = cv2.resize(frame, (0, 0), fx=visualisation_factor, fy=visualisation_factor) + # viz_frame = cv2.resize(frame, (0, 0), fx=visualisation_factor, fy=visualisation_factor) det_frame = cv2.resize(frame, (0, 0), fx=detection_factor, fy=detection_factor) start = time.time() @@ -371,16 +371,20 @@ def process3_haar(in_q, out_q, cascade_file, library_filename = None): dir_path = os.path.dirname(os.path.realpath(__file__)) lib_path = os.path.join(dir_path, "..", "visualhaar", "target", "release") - so_path = os.path.join(lib_path, "libvisual_haarcascades_lib.so") - dll_path = os.path.join(lib_path, "visual_haarcascades_lib.dll") + possible_paths = [ + os.path.join(lib_path, "libvisual_haarcascades_lib.so"), + os.path.join(lib_path, "visual_haarcascades_lib.dll"), + os.path.join(dir_path, "..", "visual_haarcascades_lib.dll"), + ] - if os.path.exists(so_path): - C = ffi.dlopen(so_path) - elif os.path.exists(dll_path): - C = ffi.dlopen(dll_path) - else: + existing_paths = [p for p in possible_paths if os.path.exists(p)] + + if not len(existing_paths): raise RuntimeError("Visual haarcascades library is not found") - + + logger.debug(f"Using library: {existing_paths[0]}") + C = ffi.dlopen(existing_paths[0]) + # print(C.test(9)) # i = Image.open("Marjo.jpg") # width = i.size[0] diff --git a/face_recognition/tools.py b/face_recognition/tools.py new file mode 100644 index 0000000..61acc53 --- /dev/null +++ b/face_recognition/tools.py @@ -0,0 +1,32 @@ + from PIL import ImageFont, ImageDraw, Image + import cv2 + import numpy as np + + text_to_show = "The quick brown fox jumps over the lazy dog" + + # Load image in OpenCV + image = cv2.imread("Me.jpg") + + # Convert the image to RGB (OpenCV uses BGR) + cv2_im_rgb = cv2.cvtColor(image,cv2.COLOR_BGR2RGB) + + # Pass the image to PIL + pil_im = Image.fromarray(cv2_im_rgb) + + draw = ImageDraw.Draw(pil_im) + + # Draw the text + draw.text((10, 700), text_to_show, font=font) + + # Get back the image to OpenCV + cv2_im_processed = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR) + + cv2.imshow('Fonts', cv2_im_processed) + cv2.waitKey(0) + + cv2.destroyAllWindows() + +def get_font(filename, size): + return ImageFont.truetype(filename, size) + + def draw_text(img, ): \ No newline at end of file diff --git a/mirror.py b/mirror.py index 0e4a412..52a0860 100644 --- a/mirror.py +++ b/mirror.py @@ -1,9 +1,11 @@ import argparse import face_recognition.comparison import cv2 - +from multiprocessing import freeze_support if __name__ == '__main__': + freeze_support() # support pyinstaller on Windows + parser = argparse.ArgumentParser(description='Visualise face recognition algorithms.') parser.add_argument('--camera', '-c', type=int, default=0, help='Numeric id of the camera')