Files
photoassistant/shared.py

107 lines
3.8 KiB
Python

import socket
import os
import face_recognition
import io
import base64
from PIL import Image, ImageOps
hostname = socket.gethostname()
PROD_HOST="pa_web"
ICON={}
ICON["Import"]="fa-file-upload"
ICON["Storage"]="fa-database"
ICON["Bin"]="fa-trash-alt"
if hostname == "lappy":
PA_JOB_MANAGER_HOST="localhost"
DB_URL = 'postgresql+psycopg2://pa:for_now_pa@localhost:5432/pa'
elif 'FLASK_ENV' not in os.environ or os.environ['FLASK_ENV'] == "development":
PA_JOB_MANAGER_HOST="localhost"
DB_URL = 'postgresql+psycopg2://pa:for_now_pa@mara.ddp.net:65432/pa'
elif os.environ['FLASK_ENV'] == "production":
PA_JOB_MANAGER_HOST="localhost"
DB_URL = 'postgresql+psycopg2://pa:for_now_pa@padb/pa'
else:
print( "ERROR: I do not know which environment (development, etc.) and which DB (on which host to use)" )
exit( -1 )
PA_JOB_MANAGER_PORT=55430
THUMBSIZE=256
def CreateSelect(name, selected, list, js="", add_class="" ):
str = f'<select id="{name}" name="{name}" class="{add_class} sm-txt bg-white text-info border-info border-1 p-1" onChange="{js};this.form.submit()">'
for el in list:
str += '<option '
if el == selected:
str += 'selected '
str += f'>{el}</option>'
str += '</select>'
return str
def CreateFoldersSelect(selected):
str = f'<select id="folders" name="folders" class="sm-txt bg-white text-info border-info border-1 p-1" onChange="this.form.submit()">'
# if selected is true, then folders == true, so make this the selected option
if( selected ):
str += '<option selected value="True">In Folders</option>'
str += '<option value="False">Flat View</option>'
else:
str += '<option value="True">In Folders</option>'
str += '<option selected value="False">Flat View</option>'
str += '</select>'
return str
def LocationIcon(obj):
return ICON[obj.in_dir.in_path.type.name]
# translate a path type, a full FS path and a file into the full FS path of the
# symlink on the file system. To note, this is used with file == path, when we
# want to just get the symlink to the path itself, otherwise file == <path/some_filename>
### FIXME: I think this is way over-complicated, want to revist one day, with
#what params are passed in, what we need to get out -- I think the overloaded
#file/oath use case, and why sometimes we trail with a / or not and then concat
#last_dir and bit before last_dir, etc. this feels a bit too complicated for
#what it does OR we comment this much better
def SymlinkName(ptype, path, file):
sig_bit=file.replace(path, "")
last_dir=os.path.basename(path[0:-1])
if len(sig_bit) > 0 and sig_bit[-1] == '/':
last_bit = os.path.dirname(sig_bit)[0:-1]
else:
last_bit = os.path.dirname(sig_bit)
symlink = 'static/'+ptype+'/'+last_dir+'/'+last_bit
if symlink[-1] == '/':
symlink=symlink[0:-1]
return symlink
def 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')
orig_w, orig_h = im.size
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, orig_w, orig_h
except Exception as e:
print( f"GenThumb failed: {e}")
return None, None, 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(), location
else:
return None, None