From b811f8710ead98dcbb40d1168fe5ba97200639ce Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Sun, 4 Jul 2021 20:04:25 +1000 Subject: [PATCH] moved GenFace and GenThumb common code into shared, and hook it in both f/e and b/e where needed --- shared.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/shared.py b/shared.py index 8dbdc2c..d3499f3 100644 --- a/shared.py +++ b/shared.py @@ -1,5 +1,9 @@ import socket import os +import face_recognition +import io +import base64 +from PIL import Image, ImageOps hostname = socket.gethostname() PROD_HOST="pa_web" @@ -73,3 +77,33 @@ def SymlinkName(ptype, path, file): if symlink[-1] == '/': symlink=symlink[0:-1] return symlink + + +def GenThumb(fname): + print( f"GenThumb({fname})" ) + + try: + im_orig = Image.open(fname) + im = ImageOps.exif_transpose(im_orig) + bands = im.getbands() + if 'A' in bands: + im = im.convert('RGB') + im.thumbnail((THUMBSIZE,THUMBSIZE)) + img_bytearray = io.BytesIO() + im.save(img_bytearray, format='JPEG') + img_bytearray = img_bytearray.getvalue() + thumbnail = base64.b64encode(img_bytearray) + thumbnail = str(thumbnail)[2:-1] + return thumbnail + except Exception as e: + print( f"GenThumb failed: {e}") + return None + +def GenFace(fname): + img = face_recognition.load_image_file(fname) + location = face_recognition.face_locations(img) + encodings = face_recognition.face_encodings(img, known_face_locations=location) + if len(encodings): + return encodings[0].tobytes() + else: + return None