Exemplo n.º 1
0
 def predict_face(self, image):
     #detect face
     extracted_face = extract_face(image)
     if extracted_face.ndim == 3:
         # get face embedding
         image_embedding = self.facenet_model.get_embedding(extracted_face)
         image_embedding = np.expand_dims(image_embedding, axis=0)
         # predict
         proba = self.embedding_model.predict_proba(image_embedding)
         # print('proba', proba.shape)
         if np.max(proba) > 0.977:
             predict_names = self.out_encoder.inverse_transform(
                 [np.argmax(proba)])
             data = predict_names[0]
             print(f"Prediction is: {predict_names[0]}",
                   '\tProbability: %.6f' % (np.max(proba) * 100))
         else:
             predict_names = self.out_encoder.inverse_transform(
                 [np.argmax(proba)])
             print(f"Prediction is: {predict_names[0]}",
                   '\tProbability: %.6f' % (np.max(proba) * 100))
             data = "Unknown"
             print("Unknown faces.")
     else:
         image_embedding = 'None'
         data = "Unknown"
         print('No face in the image!')
     return data, image_embedding
Exemplo n.º 2
0
def read_image(row, augment=True):
    img_np = extract_face(CASIA_PATH + '/' + row.FILE, row)
    if img_np is None:
        return None
    if augment:
        img_np = augment_face(img_np)
    return preprocess_input(img_np)
Exemplo n.º 3
0
def set_canonical_orientation(img_before):

    #print("[INFO] Importando imagem de entrada ...\n")

    bg = cv2.cvtColor(img_before, cv2.COLOR_BGR2RGB)
    new_im = Image.fromarray(bg)
    width, height = new_im.size
    new_im = np.asarray(new_im)

    #print("[INFO] Iniciar processo para detecção de rosto no documento "
    #      "e confirmar orientação canônica ...\n")
    cont = 1
    while cont <= 4:
        #print("========")
        #print("[INFO] {}ª Verificação.".format(cont))
        #print("========\n")
        face = extract_face(new_im)
        if len(face) > 0:
            x1, y1 = face[0]
            x2, y2 = face[1]
            face_area = (x2 - x1) * (y2 - y1)
            prop_face_area = face_area / (height * width) * 100
            if height > width:
                if y1 < 0.5*height and y2 < 0.5*height and x1 < 0.5*width and x2 \
                        < 0.5*width and prop_face_area > 1:
                    #print(
                    #    "[INFO] Verificada a orientação canônica do documento.\n")
                    break
                #else:
                #print("[INFO] Coordenadas do rosto detectado não estão na "
                #      "área esperada.\n")
            else:
                if x1 < 0.5 * width and x2 < 0.5 * width and prop_face_area > 2:
                    #print(
                    #    "[INFO] Verificada a orientação canônica do documento.\n")
                    break
                #else:
                #print(
                #    "[INFO] Coordenadas do rosto detectado não estão na "
                #    "área esperada.\n")
        #print("[INFO] Rotacionando o documento e buscando novamente o rosto ...\n")
        img_before = ndimage.rotate(img_before, 90, cval=255)
        bg = cv2.cvtColor(img_before, cv2.COLOR_BGR2RGB)
        bg = Image.fromarray(bg)
        new_im = trim(bg)
        width, height = new_im.size
        new_im = np.asarray(new_im)
        cont = cont + 1
        if cont == 5:
            print("========")
            print("[INFO] Não foi possível validar a orientação correta "
                  "do documento.")
            print("========\n")

    if cont < 5:
        new_im = cv2.cvtColor(new_im, cv2.COLOR_RGB2BGR)
        return new_im
Exemplo n.º 4
0
def load_face(dataset_sub_path):
    " to extract individual faces "
    faces = list()
    files = [f for f in os.listdir(dataset_sub_path) if not f.startswith('.')]
    for filename in files:
        if not filename.startswith('.'):
            path = dir + filename
            face = extract_face(path)
            faces.append(face)
    return faces
Exemplo n.º 5
0
def verify():
    # get frame and username from tablet
    staffname = str(request.args.get("username"))
    image = request.files["image"]
    print('username-----------', staffname)
    nparr = np.fromstring(image.read(), np.uint8)
    img_np = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR)
    # extracting face image to encoding face
    extracted_face = extract_face(img_np)
    # check exist face after face detection
    if extracted_face.ndim == 3:
        face_embedding = emb_model.get_embedding(extracted_face)
        face_embedding = np.expand_dims(face_embedding, axis=0)
        emb, count = mongo_embedded.get_embedded(staffname)
        # reshape for caculate distance
        emb = emb.reshape(-1, 128)
        tolerance = 0.8
        # Normalize face embedding
        normalizer = Normalizer(norm='l2')
        face_embedding = normalizer.transform(face_embedding)
        emb = normalizer.transform(emb)
        # calculate distance
        print(emb.shape)
        result = np.linalg.norm(emb - face_embedding, axis=1) <= tolerance
        # result = face_recognition.compare_faces(emb,encoding_face,tolerance=0.45)
        print(result)
        result = np.asarray(result)
        proba = np.sum(result[result == True]) / result.shape[0]
        print(proba)
        if proba >= 0.5:
            data = {
                "message": "id has been verified",
                "code": 200,
                "error": "False",
                "data": staffname
            }

        else:
            data = {
                "message": "id has not   verified",
                "code": 404,
                "error": "True",
                "data": staffname
            }
    else:
        data = {
            "message": "image are not suitabel",
            "code": 404,
            "error": "True",
            "data": staffname
        }
    return make_response(jsonify(data), 200)
Exemplo n.º 6
0
def training():
    name = request.args.get('staffname', type=str)
    # image =request.files["image"]
    images = request.files.getlist('image')
    embedding_face = list()
    for i, image in enumerate(images):
        nparr = np.fromstring(image.read(), np.uint8)
        img_np = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR)
        extracted_face = extract_face(img_np)
        print('face shape', face.shape)
        if extracted_face.ndim == 0:
            data = {
                "message": "image are not suitabel",
                "code": 404,
                "error": "True",
                "data": name
            }
        else:
            data = {
                "message": "image has been extract to embedding and push DB",
                "code": 200,
                "error": "False",
                "data": name
            }
            emb = emb_model.get_embedding(face)
            face_embedding.append(emb)
        return face_embedding
    # put new staff embedding into mongodb
    mongo_embedded.add_staff(face_embedding, staffname)
    # get all embedding from mongodb
    username, all_embeded = mongo_embedded.get_full_embedded()
    all_embeded = all_embeded.reshape(-1, 128)
    # split data for svm training
    trainX, testX, trainy, testy = train_test_split(all_embeded,
                                                    username,
                                                    test_size=0.2,
                                                    random_state=42)
    train_svm_model.fit(trainX, trainy, testX, testy)
    data = {
        "message": "train has been done",
        "code": 200,
        "error": "False",
        "data": 'first train'
    }
    return make_response(jsonify(data), 200)
Exemplo n.º 7
0
def show_webcam(mirror=False):
    import multiprocessing as mp
    mp.set_start_method('spawn') # NOTE: this is important to make opencv compatible with multi-process

    q = Queue(maxsize=1)
    webcam_proc = Process(target=collect_frames, args=(q, ))
    webcam_proc.start()

    def exit_handler(sigNum, frame):
        webcam_proc.terminate()
        exit(0)

    import signal
    signal.signal(signal.SIGINT, exit_handler)
    signal.signal(signal.SIGQUIT, exit_handler)

    # cam = cv2.VideoCapture(0)
    # fps = cam.get(cv2.CAP_PROP_FPS)
    while True:
        img = q.get()
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # ret_val, img = None, None
        # for _ in range(0, 10):
        #     cam.grab()
        # ret_val, img = cam.read()
        if mirror:
            img = cv2.flip(img, 1)

        start = datetime.datetime.now()

        face_pixels, box = extract_face(detector, img)
        if face_pixels is None:
            print("no face found in the screen")
            boxes, names, distances = [], [], []
        else:
            embeddings = get_embedding(facenet, face_pixels)
            name, distance = embeddings_recognition(embeddings)
            boxes, names, distances = [box], [name], [distance]

        end = datetime.datetime.now()
        delta = end - start
        print("%d ms elapsed" % (int(delta.total_seconds() * 1000)))

        draw_boxes(img, boxes, names, distances)
Exemplo n.º 8
0
def distance():
    """idont nkow what purpose of this api created by me

    Returns:
        NONE: NONE
    """

    # get encoding from DB
    print("error")

    username, all_embeded = mongo_embedded.get_full_embedded()
    all_embeded = all_embeded.reshape(-1, 128)
    # print('username-----------',username)
    # distance = np.sqrt((all_embeded -));
    image = request.files['image']
    nparr = np.fromstring(image.read(), np.uint8)
    img_np = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR)
    # extracting face image to encoding face
    face = extract_face(img_np)
    if len(face) == 0:
        data = {
            "message": "image are not suitabel",
            "code": 404,
            "error": "True",
            "data": staffname
        }

    else:
        starttime = time.time()
        encoding_face = svm_model.extract_embedding(face)
        distance = np.sort(np.sqrt(
            np.sum((all_embeded - encoding_face)**2, axis=1)),
                           axis=0)
        print('-------------------', len(distance))
        print('time of processing', time.time() - starttime)

    res = {"message": "", "code": 404, "error": "True", "data": 'sthing'}
    return make_response(jsonify(res), 200)
def read_image(row):
    img_np = extract_face(LFW_PATH + '/' + row.FILE, row)
    return preprocess_input(img_np)
Exemplo n.º 10
0
import sys
from extract_face import extract_face

image_path = str(input("Specify the path of the image :- "))
if not image_path:
    print("Exception:- Enter path of image")
    sys.exit()
try:
    scale_factor = float(
        input(
            "Specifying how much the image size is reduced at each image scale (default - 1.1) :- "
        ) or "1.1")
except ValueError as e:
    print("Exception:- Enter correct float value")
    sys.exit()

output_directory = input(
    "Directory where to save output (default - same as input image) :- "
) or None

output_prefix = input(
    "Prefix of output (default - the name of input image) :- ") or None

try:
    extract_face(image_path=image_path,
                 scale_factor=scale_factor,
                 output_directory=output_directory,
                 output_prefix=output_prefix)
except Exception as e:
    print("Exception:- " + str(e))
    sys.exit()
Exemplo n.º 11
0
def get_embedding_from_image(image):
    face_pixels, box = extract_face(detector, image)
    if face_pixels is None:
        return None, None
    return get_embedding(facenet, face_pixels), box