Exemplo n.º 1
0
def extract_face(url):
    image= Image.open(url)
    image= image.convert('RGB')
    img_array= asarray(image)


    detector= MTCNN()

    results= detector.detect_faces(img_array)
    face_num=0
    faces_list= list()
    for faces in results:
        x1, y1, width, height = results[face_num]['box']
        x2, y2, = x1 + width, y1 + height

        # extract face
        face= img_array[y1:y2, x1:x2]

        # resizing for the model
        face = Image.fromarray(face)
        face = face.resize((160,160))

        #converting it back to arrray to append the list
        face= asarray(face)
        faces_list.append(face)
        face_num+= 1

    return faces_list
Exemplo n.º 2
0
def extract_faces(path, scale_size=(160, 160)):
    #scale_size is kind of required to get the best output
    #load the image/ frame
    image = Image.open(path)

    #convert to RGB for better efficiency at detecting to standardize the color scheme
    image = image.convert('RGB')

    #convert to array
    img_array = asarray(image)

    #loading detector
    detector = MTCNN()

    #detecting and storing to results object
    results = detector.detect_faces(img_array)
    # print(results)

    for face in results:
        # [{'box': [1413, 1458, 1022, 1387], 'confidence': 0.9999998807907104, 'keypoints': {'left_eye': (1731, 2008), 'right_eye': (2196, 2037), 'nose': (2001, 2362), 'mouth_left': (1754, 2542), 'mouth_right': (2143, 2558)}}]
        print("left_eye", results[0]["keypoints"]["left_eye"])
        print("right_eye", results[0]["keypoints"]["right_eye"])

        left_eye = results[0]["keypoints"]["left_eye"]
        right_eye = results[0]["keypoints"]["left_eye"]

        x1, y1, width, height = results[0]['box']
        x2, y2, = x1 + width, y1 + height

        #extract face
        face = img_array[y1:y2, x1:x2]
        # aligned_image= alignment_procedure(face, results[0]["keypoints"]["left_eye"], results[0]["keypoints"]["right_eye"])
        # mt = functions.align_face(img=face, detector_backend=backends[3])
        # dl = functions.align_face(img=face, detector_backend=backends[2])

        #resizing for the model
        image = Image.fromarray(face)
        aligned_face = align_face(image, results[0]['box'], left_eye,
                                  right_eye)
        image = image.resize(scale_size)

        #setting face path for picture input, saving it in directory
        face_folder = "extracted_face_picture/"
        face_name = "single_face_picture.jpg"
        path_of_picture_face = face_folder + face_name
        #saving
        image.save(path_of_picture_face)

        face_array = asarray(image)
    return [face_array, aligned_face]
def get_faces_locally(images: list,
                      output_frame_resolution=(112, 112),
                      threshold=0.9) -> list:

    detector = MTCNN()
    print("[INFO]: Detecting Faces in each frames")
    output = []
    for index, image in tqdm(enumerate(images)):
        if image.shape[-1] == 4:
            image = image[:, :, :-1].copy()

        # Detect faces
        faces = detector.detect_faces(image)

        if len(faces) > 0:

            for face in faces:
                if face["confidence"] < threshold:
                    continue
                else:
                    # Snip the image
                    x, y, width, height = face["box"]
                    face_img = image[y:y + height, x:x + width]

                    # Face alignment
                    key = face["keypoints"]
                    face_img = fau.face_alignment(face_img, key["left_eye"],
                                                  key["right_eye"])

                    # Resize the image
                    face_img = cv.resize(face_img, (112, 112),
                                         interpolation=cv.INTER_CUBIC)
                    face_img = np.asarray(face_img / 255., dtype="float64")

                    output.append((index + 1, {
                        "bounding_box": face["box"],
                        "face_image": face_img[None, ...]
                    }))
        else:
            continue

    if len(output) > 0:
        print(f"[INFO]: Successfully found {len(output)} faces!")
        return output
    else:
        print(
            "[WARN]: No faces were detected at all throughout the entire series of frames"
        )
        return None
Exemplo n.º 4
0
class FaceLocalizer:
    def __init__(self):
        self.model = MTCNN()

    def getFace(self, img):
        faces = self.model.detect_faces(img)
        if len(faces) <= 0:
            return []
        face = faces[0]
        b = face['box']
        x, y, w, h = b[0], b[1], b[2], b[3]
        return [x, y, w, h]
class PresenceDetector:
    def __init__(self, kwargs: dict):
        self._kwargs = kwargs
        self._face_detector = MTCNN()

    def detect(self) -> PresenceEvent:
        camera = self._kwargs.get('camera')
        video_capture = cv2.VideoCapture(camera, cv2.CAP_DSHOW)
        if not video_capture.isOpened():
            log.info('Unable to open video device {}'.format(camera))
            return PresenceEvent.NOT_AVAILABLE

        frames = self._capture_frames(video_capture)
        video_capture.release()

        start_detect = time.time()
        for image in frames:
            results = self._detect_faces(image, confidence_threshold=0.9)
            if len(results) > 0:
                log.debug('Face detected in {} seconds: {}'.format(
                    time.time() - start_detect, results))
                return PresenceEvent.PRESENT
        log.debug('No face detected in {} seconds'.format(time.time() -
                                                          start_detect))
        return PresenceEvent.NOT_PRESENT

    def _capture_frames(self, video_capture):
        start_at = time.time()
        num_of_snapshots = self._kwargs.get('num_of_snapshots')
        time_between_snapshots = self._kwargs.get(
            'time_between_snapshots_millis')
        frames = []
        for i in range(num_of_snapshots):
            retval, frame = video_capture.read()
            if retval:
                frames.append(frame)
            if i < num_of_snapshots - 1:
                time.sleep(time_between_snapshots / 1000)
        duration = time.time() - start_at
        log.debug('Captured {} snapshots in {} seconds'.format(
            len(frames), duration))
        return frames

    def _detect_faces(self, image, confidence_threshold=0.9):
        image = self._preprocess_image(image)
        results = self._face_detector.detect_faces(image)
        return [
            result['confidence'] > confidence_threshold for result in results
        ]

    def _preprocess_image(self, image):
        return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
def input_preprocessing(input_image_path: str, model: tf.keras.Model) -> tuple:

    # Open the image
    input_image = cv.imread(input_image_path)

    # Pre-process it out
    input_image = cv.cvtColor(input_image, cv.COLOR_BGR2RGB)

    # Run MTCNN
    detector = MTCNN()
    faces = detector.detect_faces(input_image)

    if (len(faces) > 0):
        # Get the first face
        for face in faces:
            if face["confidence"] > 0.9:
                # Crop it out
                x, y, width, height = face["box"]
                face_image = np.asarray(input_image[y:y + height, x:x + width],
                                        dtype=np.uint8)

                key = face["keypoints"]
                face_image = fau.face_alignment(face_image, key["left_eye"],
                                                key["right_eye"])

                # Interpolate
                face_image = cv.resize(face_image, (112, 112),
                                       interpolation=cv.INTER_CUBIC)

                # Normalize
                face_image = np.asarray(face_image / 255.0, dtype="float64")

                return face_image, model(face_image[None, ...]).numpy()
            else:
                continue
        raise Exception("[ERROR]: Proper Face not found!")
    else:
        raise Exception("[ERROR]: NO FACE FOUND IN IMAGE")
Exemplo n.º 7
0
class LandmarkDetection:
    def __init__(self):
        self.model = MTCNN()

    def getLandmarks(self, img):
        faces = self.model.detect_faces(img)
        if (len(faces) == 0):
            return []
        face = faces[0]
        keypoints = face['keypoints']
        landmark = []
        for key in keypoints:
            x, y = keypoints[key]
            landmark.append(x)
            landmark.append(y)
        #landmark:leye,reye,nose,lmouth,rmouth
        return landmark
Exemplo n.º 8
0
def create_npz_faces(url=""):
    #below condition to stop it run automatically when import
    # if url== "":
    #     return
    cap = cv2.VideoCapture(url)
    countframes = 0
    faces_list = list()
    while True:
        # Grab a single frame of video
        try:
            ret, frame = cap.read()
            countframes += 1

        except:
            print("URL/selection for the video file is not valid:", url)
            break

        # below if conditional logic is to boost up the speed
        # reducing frames rate 6 fps actual was 30fps
        if countframes % 5 != 0:
            continue

        # Convert the image from BGR color(which OpenCV uses) to RGB
        # RGB is preferred color while detection faces
        try:
            rgb_frame = frame[:, :, ::-1]
        except:
            print("video file has no more frames, total frames= ", countframes)
            break

        # loading detector from MTCNN
        detector = MTCNN()

        # saving all faces in result variable
        result = detector.detect_faces(rgb_frame)
        print(result)

        #if result get some faces
        if len(result) > 0:
            frame_array = np.array(rgb_frame)
            # frame_array = asarray(rgb_frame)

            # taking variable face_num; to iterate in loop; for detecting multiple faces in single frame
            face_num = 0
            for face in result:
                # read the documentation of cv2.rectangle()
                # starting point coordinates of face are being stored in x1, y1
                x1, y1, width, height = result[face_num]['box']
                # storing ending points in x2, y2
                x2, y2, = x1 + width, y1 + height

                # extracting this face from frame_array
                frame_face = frame_array[y1:y2, x1:x2]

                #to resiize, converting it PIL Image
                frame_face = Image.fromarray(frame_face)
                frame_face = frame_face.resize((160, 160))
                #back to array
                frame_face = asarray(frame_face)

                #append in face list
                faces_list.append(frame_face)

        print(countframes)
        #just for test purpose limiting frames
        if countframes >= 130:
            break

    #saving faces list into npz
    # savez("video_faces.npz", faces_list)

    # releasing video and destroying windows
    cap.release()
    return faces_list


# def plot_from_npz():
#     faces_np_name= "video_faces.npz"
#     faces= np.load(faces_np_name)['arr_0']
#     for face_arr in faces:
#         face= Image.fromarray(face_arr)
#         plt.imshow(face)
#     print("outer")
#     plt.show()

# create_npz_faces()
# plot_from_npz()
 def __init__(self, kwargs: dict):
     self._kwargs = kwargs
     self._face_detector = MTCNN()
Exemplo n.º 10
0
        'camera': 0,
        'num_of_snapshots': 3,
        'time_between_snapshots_millis': 200
    })
    while True:
        event, results = presence_detector.detect()
        for result in results:
            src = result['src'].copy()
            detected = result['detected']
            for key, boxes in detected.items():
                for (x, y, w, h) in boxes:
                    cv2.rectangle(src, (x, y), (x + w, y + h), (255, 255, 0), 2)
        cv2.imshow('PresenceDetector[Debug]', src)
        cv2.waitKey(delay=100)
    """
    detector = MTCNN()
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("Cannot open camera")
        exit()
    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
        # if frame is read correctly ret is True
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        result = detector.detect_faces(image)
        if len(result) > 0:
            print(result)
            keypoints = result[0]['keypoints']
            bounding_box = result[0]['box']
Exemplo n.º 11
0
 def __init__(self):
     self.model = MTCNN()
def face_detect(img_path):
    detector = MTCNN()
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    faces = detector.detect_faces(img)
    return faces
Exemplo n.º 13
0
from cv2 import cv2
from mtcnn_cv2 import MTCNN

detector = MTCNN()
cap = cv2.VideoCapture(0)

while (True):

    ret, frame = cap.read()
    frame = cv2.resize(frame, (800, 600))
    boxes = detector.detect_faces(frame)

    for box in boxes:

        x, y, w, h = box['box']
        conf = box['confidence']

        if conf > 0.8:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(frame,
                        str(conf)[:4], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
                        1, (0, 255, 0), 2, cv2.LINE_AA)

    cv2.imshow('Face Detection', frame)

    key = cv2.waitKey(1) & 0xFF
    # Press `q` to exit
    if key == ord("q"):
        break

cap.release()
Exemplo n.º 14
0
# Biến check: 1 là lấy ảnh, 2 nhận dạng ảnh, default là 0
check = 0
# số lần đọc file train
numberReadTrain = 1
recognizer = cv2.face.LBPHFaceRecognizer_create()
# ID trong lấy ảnh
userID = None
# error trong insertPeople
error = ""
# biến count chạy lần đầu khi lấy ảnh
count = 1
# số lượng ảnh hiện có
numberImage = None
# bổ sung ảnh
addPhoto = None
detector = MTCNN()


########### Sự Kiên $$$$$$$$$$
def update_frame():
    global canvas, photo, bw, check, sampleNum, numberReadTrain, recognizer, userID, count, numberImage
    ret, frame = video.read()
    # lấy ảnh
    if check == 1:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = detector.detect_faces(frame)
        if len(faces) > 0:
            box = faces[0]['box']
            cv2.rectangle(frame, (box[0], box[1]),
                          (box[0] + box[2], box[1] + box[3]), (0, 255, 0), 2)
            # tạo thư mục dataSet
Exemplo n.º 15
0
        countframes += 1
    except:
        print("End of frames")
        break

    #below if conditional logic is to boost up the speed
    #reducing frames rate 6 fps actual was 30fps
    if countframes % 5 != 0:
        continue

    # Convert the image from BGR color(which OpenCV uses) to RGB
    # RGB is preferred color while detection faces
    rgb_frame = frame[:, :, ::-1]

    #loading detector from MTCNN
    detector = MTCNN()

    #saving all faces in result variable
    result = detector.detect_faces(rgb_frame)

    #if only one face in the frames
    print(result)

    if len(result) > 0:
        #to work with deep face
        frame_array = asarray(rgb_frame)

        #taking variable face_num; to iterate in loop; for detecting multiple faces in single frame
        face_num = 0
        for face in result:
            #if confused read the documentation of cv2.rectangle() it would help a lot