def edit_person(id): print(id) person = Embeddings.query.get_or_404(id) if request.method == "POST": person.first_name = request.form['first_name'] person.second_name = request.form['second_name'] if len(request.form['photo_hidden']) == 0: try: db.session.commit() return render_template("edit_person.html", person=person, edited=True, photo_err=False, photo_edit=False) except: return "При редактировании данных произошла ошибка" else: b64_string = request.form['photo_hidden'] img = Image.open(BytesIO(base64.b64decode(b64_string))) faces = crop_rectangle(np.array(img)) if faces is not None and len(faces) == 1: img = np.array(faces) / 255 embedding = do_embedding(embedding_model, [img]) person_embedding = predict_person(person_model, embedding)[0] person.person_embedding = json.dumps(person_embedding.tolist()) try: db.session.commit() persons = Embeddings.query.order_by( Embeddings.second_name).all() emb_arr = [] index_arr = [] for p in persons: emb_arr.append(json.loads(p.person_embedding)) index_arr.append(p.id) write_annoy(emb_arr, index_arr) return render_template("edit_person.html", person=person, edited=True, photo_err=False, photo_edit=True) except: return "При добавлении человека произошла ошибка" else: return render_template("edit_person.html", person=person, edited=False, photo_err=True, photo_edit=False) else: return render_template("edit_person.html", person=person, edited=False, photo_err=False, photo_edit=False)
def delete_person(id): person = Embeddings.query.get_or_404(id) try: db.session.delete(person) db.session.commit() persons = Embeddings.query.order_by(Embeddings.second_name).all() emb_arr = [] index_arr = [] for person in persons: emb_arr.append(json.loads(person.person_embedding)) index_arr.append(person.id) write_annoy(emb_arr, index_arr) return redirect("/person/saved?deleted=True") except: return "При удалении произошла ошибка!"
def add_person(): form = forms.AddPersonForm(request.form, meta={'csrf': False}) if request.method == "POST" and form.validate(): b64_string = request.form['photo_hidden'] img = Image.open(BytesIO(base64.b64decode(b64_string))) faces = crop_rectangle(np.array(img)) if faces is not None and len(faces) == 1: img = np.array(faces) / 255 embedding = do_embedding(embedding_model, [img]) person_embedding = predict_person(person_model, embedding)[0] first_name = request.form['first_name'] second_name = request.form['second_name'] person_embedding = json.dumps(person_embedding.tolist()) person = Embeddings(first_name=first_name, second_name=second_name, person_embedding=person_embedding) try: db.session.add(person) db.session.commit() persons = Embeddings.query.order_by( Embeddings.second_name).all() emb_arr = [] index_arr = [] for person in persons: emb_arr.append(json.loads(person.person_embedding)) index_arr.append(person.id) write_annoy(emb_arr, index_arr) return redirect("/person/saved?added=True") except: return "При добавлении человека произошла ошибка" else: return render_template("add_person.html", form=form, photo_error=True) else: return render_template("add_person.html", form=form, photo_error=False)