Beispiel #1
0
def predict_img():
    global mtcnn_detector, facenet, face_db
    if request.method == 'POST':
        files = request.files.getlist('file')
        # print(request.files['file'].filename)
        # thresh = request.form['thresh']
        if request.files['file'].filename == '':
            return jsonify(message='No file selected for predict'), 400
        else:
            result = {}
            for file in files:
                if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    path_to_file = os.path.join(predict_path, filename)
                    print(path_to_file)
                    if os.path.isfile(path_to_file):
                        os.remove(path_to_file)
                    file.save(path_to_file)
                    img = cv2.imread(path_to_file)
                    faces, _ = mtcnn_detector.detect(img)
                    names, sims = [], []
                    for face in faces:
                        x1, y1, x2, y2 = int(face[0]), int(face[1]), int(
                            face[2]), int(face[3])
                        face_image = img[y1:y2, x1:x2]
                        names, sims = predict(face_image,
                                              facenet,
                                              face_db,
                                              VERIFICATION_THRESHOLD=0.5)
                    result[filename] = len(names), names, sims
            return jsonify(str(result)), 200
def test_with_cam(facenet, facedb):
    cam = cv2.VideoCapture(0)
    mtcnn_detector = load_mtcnn(scale_factor=0.709)
    while True:
        ret, frame = cam.read()
        if ret:
            faces, _ = mtcnn_detector.detect(frame)
            for face in faces:
                x1, y1, x2, y2 = int(face[0]), int(face[1]), int(face[2]), int(
                    face[3])
                face_image = frame[y1:y2, x1:x2]
                names, sims = predict(face_image,
                                      facenet,
                                      facedb,
                                      VERIFICATION_THRESHOLD=0.5)
                print(names)
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255),
                              thickness=2)
            cv2.imshow("frame", frame)
            key = cv2.waitKey(1)
            if key == ord('q'):
                break
            if key == 32:
                cv2.waitKey(0)
        else:
            break
Beispiel #3
0
def predict_image(image):
    names, sims = predict(image, facenet, face_db, VERIFICATION_THRESHOLD=0.5)
    print(names)