moved GenFace and GenThumb common code into shared, and hook it in both f/e and b/e where needed

This commit is contained in:
2021-07-04 20:04:25 +10:00
parent 518df7ee10
commit b811f8710e

View File

@@ -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