117 lines
4.8 KiB
Python
117 lines
4.8 KiB
Python
"""
|
|
This file implements an avatar studio
|
|
"""
|
|
import py_avataaars as pa
|
|
from py_avataaars import PyAvataaar as Avatar
|
|
import pandas as pd
|
|
import numpy as np
|
|
import transport
|
|
from transport import providers
|
|
from enum import Enum
|
|
import io
|
|
import base64
|
|
import copy
|
|
|
|
|
|
# _map = {'eye type':pa.EyesType,'frame style':pa.AvatarStyle,'race':pa.SkinColor,'hair color':pa.HairColor,'facial hair':pa.FacialHairType, 'facial hair color':pa.HairColor,'hair dress':pa.TopType,'mouth':pa.MouthType,'nose':pa.NoseType,'eyebrows':pa.EyebrowType }
|
|
# _vmap= {'eye type':'eye_type','frame style':'style','race':'skin_color','hair color':'hair_color','facial hair color':'facial_hair_color','facial hair':'facial_hair_type','hair dress':'top_type','eyebrows':'eye_brow','nose':'nose_type','mouth':'mouth_type'}
|
|
# _omap = {'eye_type':pa.EyesType,'style':pa.AvatarStyle}
|
|
_df = [['eye type','eye_type',pa.EyesType],['frame style','style',pa.AvatarStyle],['hair color','hair_color',pa.HairColor],['race','skin_color',pa.SkinColor],['facial hair','facial_hair_type',pa.FacialHairType],
|
|
['facial hair color','facial_hair_color',pa.Color],['mouth','mouth_type',pa.MouthType],['hat color','hat_color',pa.Color],['accessory','accessories_type',pa.AccessoriesType],['nose','nose_type',pa.NoseType],['eyebrows','eyebrow_type',pa.EyebrowType],
|
|
['hair dress','top_type',pa.TopType],['clothes', 'clothe_type', pa.ClotheType],['clothe color','clothe_color', pa.Color],['clothe graphics','clothe_graphic_type',pa.ClotheGraphicType]
|
|
]
|
|
|
|
_df = pd.DataFrame(_df,columns=['label','variable','values'])
|
|
# _df.to_csv('/home/steve/me.avatar.csv',index=False)
|
|
def _parameters():
|
|
_out = {'basic':{},'face':{},'clothes':{}}
|
|
for _index in np.arange(_df.shape[0]) :
|
|
row = _df.iloc[_index]
|
|
key = row['label']
|
|
if key in ['race','nose','mouth','eyebrows','eye type'] :
|
|
_id = 'basic'
|
|
elif 'clothe' in key or key=='accessory':
|
|
_id = 'clothes'
|
|
else:
|
|
_id = 'face'
|
|
if len( list(row['values'])) < 2 :
|
|
continue
|
|
_out[_id][key] = {'values': [{'name':_item.name.replace('_', ' '),'value':_item.value} for _item in row['values']],'variable':row['variable']}
|
|
|
|
|
|
return _out
|
|
def cast(_args) :
|
|
_params = {}
|
|
for key in _args :
|
|
value = int(_args[key])
|
|
|
|
_info = _df[_df.variable == key].copy()
|
|
if _info.shape[0] > 0 :
|
|
_params[key] = list(_info['values'].tolist()[0])[value]
|
|
# _params[key] = _info['values'].tolist()[value]
|
|
return _params
|
|
# def _xparameters() :
|
|
# """
|
|
# This function returns parameters to be used within an HTML context
|
|
# """
|
|
# _orgout = {'basic':{},'extended':{}}
|
|
# _out = {}
|
|
# for _key in _map :
|
|
|
|
# # _out[_key] = {'class':_map[_key].__name__,'values':[],"variable":_vmap[_key]}
|
|
# # _out[_key]['values'] = [{'name':_item.name.replace('_', ' '),'value':_item.value} for _item in _map[_key]]
|
|
# if _key == 'nose' :
|
|
# continue
|
|
# if _key in ['race','nose','mouth','eyebrows','eye type'] :
|
|
# _out = _orgout['basic']
|
|
# else:
|
|
# _out = _orgout['extended']
|
|
# _out[_key] = {'class':_map[_key].__name__,'values':[],"variable":_vmap[_key]}
|
|
# _out[_key]['values'] = [{'name':_item.name.replace('_', ' '),'value':_item.value} for _item in _map[_key]]
|
|
|
|
# # return _out
|
|
# return _orgout
|
|
|
|
def _build (_args):
|
|
"""
|
|
This function builds the avatar with a set of arguments
|
|
"""
|
|
|
|
_args = cast(_args)
|
|
if _args :
|
|
|
|
_avatar = Avatar(**_args)
|
|
_stream = _avatar.render_png()
|
|
_stream =io.BytesIO(_stream)
|
|
|
|
_stream = base64.encodebytes(_stream.getvalue()).decode('ascii')
|
|
else:
|
|
_stream = None
|
|
return _stream
|
|
def _save(_args):
|
|
"""
|
|
This function will save the data/candidate to the database
|
|
:_args {alias,email,stream}
|
|
"""
|
|
|
|
writer = transport.factory.instance(provider=providers.MONGO,context='write',db='genomix',doc='participants')
|
|
reader = transport.factory.instance(provider=providers.MONGO,context='read',db='genomix',doc='participants')
|
|
|
|
#
|
|
# Let us make sure the candidate doesn't exist
|
|
_df = reader.read(mongo={"find":"participants","filter":{"alias":_args['alias']},"projection":{"_id":0}})
|
|
_info = None
|
|
try:
|
|
if _df.shape[0] == 0 :
|
|
writer.write(_args)
|
|
else:
|
|
writer.set(_args)
|
|
_info = "1"
|
|
except Exception as e:
|
|
pass
|
|
return _info
|
|
|
|
def _participants ():
|
|
reader = transport.factory.instance(provider=providers.MONGO,context='read',db='genomix',doc='participants')
|
|
_df = reader.read(mongo={"find":"participants","limit":10,"projection":{"_id":0}})
|
|
return _df.to_dict(orient='records') |