def index_id(id): if request.method == 'POST': data = request.form c = data.get('character') if c and c != config["skip_tag"]: print(c) query = Face.update(character = c).where(Face.id == id) query.execute() next_id = int(id) + 1 return redirect(url_for("index_id", id = next_id)) # get else: next_face = Face.select().where(Face.id == id).first() if not next_face: return redirect(url_for("finish")) progress = round(Face.select().where(Face.character != None).count() * 100 / Face.select().count(), 2) return render_template('index.html', face=next_face, progress=progress, config=config)
def detail(): if not "pic_path" in request.query: response.status = 400 return "'pic_path' parameter not found" pic_path = request.query["pic_path"] faces = Face.select().where(Face.pic_path == pic_path) if not faces.exists(): response.status = 404 return "pic_path '{0}' not found".format(html.escape(pic_path)) return template("detail.html", pic_path = pic_path)
def detail_api(): if not "pic_path" in request.query: response.status = 400 return { "error": "'pic_path' parameter not found" } pic_path = request.query["pic_path"] faces = Face.select().where(Face.pic_path == pic_path) if not faces.exists(): response.status = 404 return { "error": "pic_path '{0}' not found".format(html.escape(pic_path)) } return { "faces": [_face_to_dict(x) for x in faces], "thumb_url": _build_url(faces[0].thumb_path) }
def api_slack(): text = request.POST.text if text == "list": characters = config.CHARACTERS.keys() res = ', '.join(characters) return { "text" : res } if not text: return "" splited = text.split() # filter text for string in splited: if not string in config.CHARACTERS: return "" # splited -> character list characters = list(map(config.CHARACTERS.get, splited)) grouped_pics = (Face.select(Face.pic_path) .where(Face.character << splited) .group_by(Face.pic_path, Face.character)) pic = (Face.select(grouped_pics.c.pic_path) .from_(grouped_pics) .group_by(grouped_pics.c.pic_path) .having(fn.Count() > len(characters) - 1) .order_by(fn.Random()) .limit(1)) faces = list(Face.select().where(Face.pic_path << pic, Face.character << splited)) if not faces: return { "username": text, "icon_emoji": ":upside_down_face:", "text": "not found" } pic_path = faces[0].pic_path original_url = _build_url(pic_path) thumb_url = _build_url(faces[0].thumb_path) probabilities = " | ".join(map(lambda face: "{0}: {1:.2f}%, {2}: {3:.2f}%".format( face.character, face.probability * 100, face.character_1, face.probability_1 * 100 ), faces)) footer = "Detail: {0}\n{1}".format(_build_detail_url(pic_path), probabilities) return { "username": text, "icon_url": _build_static_url(characters[0]["img_path"]), "attachments": [ { "fallback": original_url, "title": original_url, "title_link": original_url, "image_url": thumb_url, "footer":footer } ] }
def _get_random(character): return Face.select().where(Face.character == character).order_by( fn.Random()).first()
def list(character): faces = Face.select().where(Face.character == character) return render_template('list.html', faces=faces, config=config)
def index(): next_face = Face.select().where(Face.character == None).first() if next_face: return redirect(url_for("index_id", id = next_face.id)) else: return redirect(url_for("finish"))
from PIL import Image import numpy as np from face import Face import characters import os faces = Face.select() def export(ran, filename): os.remove(filename) with open(filename, "a") as f: for i in ran: print (i) face = faces[i] im = Image.open(face.face_path).resize((124,124)) im = (np.array(im)) r = im[:,:,0].flatten() g = im[:,:,1].flatten() b = im[:,:,2].flatten() character_id = characters.search_id(face.character) print(character_id) label = [character_id] out = list(label) + list(r) + list(g) + list(b) np.array(out, np.uint8).tofile(f) face_count = Face.select().count() face_count_6 = int(face_count / 6) BASE_DIR = "data/"