示例#1
0
文件: app.py 项目: orus-kade/Faces
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)
示例#2
0
def index():
    if request.method == "POST":
        b64_string = request.form['cam_photo']
        img = Image.open(BytesIO(base64.b64decode(b64_string)))
        faces = crop_rectangle(np.array(img))
        if faces is not None:
            img = np.array(faces) / 255
            embedding = do_embedding(embedding_model, [img])
            age = predict_age(age_model, embedding)
            gender = predict_gender(gender_model, embedding)
            person_embedding = predict_person(person_model, embedding)
            annoy_index, distances = read_annoy(person_embedding)
            annoy_index_name = []
            annoy_distances = []
            for index, dist in zip(annoy_index, distances):
                try:
                    person = Embeddings.query.get(index[0])
                    annoy_index_name.append(person.first_name + ' ' +
                                            person.second_name)
                    annoy_distances.append(dist)
                except:
                    print("Ошибка при поиске соответствия")
                    annoy_index_name.append(-1)
                    annoy_distances.append(-1)
            images_base64 = []
            for image in img:
                buffered = BytesIO()
                image = image * 255
                Image.fromarray(image.astype(np.uint8)).save(buffered,
                                                             format="JPEG",
                                                             mode="RGB")
                img_str = str(base64.b64encode(buffered.getvalue()))[2:-1]
                img_str = 'data:image/jpeg;base64,' + img_str
                images_base64.append(img_str)
            persons = []
            for img, a, g, ai, ad in zip(images_base64, age, gender,
                                         annoy_index_name, annoy_distances):
                a = np.round(a, 1)
                for i in range(0, len(a), 1):
                    if a[i] == 0.0:
                        a[i] = 0.1
                persons.append([img, a, g, ai, np.round(ad[0], 1)])
            return render_template("result.html", persons=persons)
        return render_template("result.html", persons=[])
    return render_template("index.html")
示例#3
0
文件: app.py 项目: orus-kade/Faces
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)