Exemple #1
0
def ff(img):
    """
    :param img:
    :return: faceAligned[0] the aligned face version of the original input image
    """
    # initialize dlib's face detector (HOG-based) and then create
    # the facial landmark predictor and the face aligner
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(str(_path) + "/shape_predictor_68_face_landmarks.dat") ########################
    fa = FaceAligner(predictor, desiredFaceWidth=256)

    # load the input image, resize it, and convert it to grayscale
    image = img ##########################################################
    image = imutils.resize(image, width=800)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # show the original input image and detect faces in the grayscale
    # image
    #cv2.imshow("Input", image)
    rects = detector(gray, 2)

    lst = []
    # loop over the face detections
    for rect in rects:
        # extract the ROI of the *original* face, then align the face
        # using facial landmarks
        (x, y, w, h) = rect_to_bb(rect)
        faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256)
        faceAligned = fa.align(image, gray, rect)

        import uuid
        f = str(uuid.uuid4())
        cv2.imwrite("foo/" + f + ".png", faceAligned)

        # display the output images
        #cv2.imshow("Original", faceOrig)
        #cv2.imshow("Aligned", faceAligned)
        #cv2.waitKey(0)
        lst.append(faceAligned)

    return lst[0]
Exemple #2
0
os.chdir('..')

for n in range(len(time_array)):
    #

    frame = cv2.imread('info/' + str(time_array[n]) + '.jpg')

    frame = imutils.resize(frame, width=600)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    height, width = frame.shape[:2]
    rects = detector(gray, 0)
    i = 0
    if (len(rects) > 0):
        array_data_not_col = []
        for rect in rects:
            (x, y, w, h) = face_utils.rect_to_bb(rect)

            if (x > 0 and y > 0 and x + w < width and y + h < height):
                i += 1
                start = time_array[n]

                shape_cam = predictor(gray, rect)
                shape = face_utils.shape_to_np(shape_cam)

                gor = np.sqrt((shape[0][0] - shape[16][0])**2 +
                              (shape[0][1] - shape[16][1])**2)
                vert = np.sqrt((shape[8][0] - shape[27][0])**2 +
                               (shape[8][1] - shape[27][1])**2)
                otn = gor / vert

                face_descriptor = facerec.compute_face_descriptor(
Exemple #3
0
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor and the face aligner
# detector = dlib.cnn_face_detection_model_v1(args["weights"])
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
fa = FaceAligner(predictor, desiredFaceWidth=256)

# load the input image, resize it, and convert it to grayscale
image = cv2.imread(args["image"])
image = imutils.resize(image, width=800)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# show the original input image and detect faces in the grayscale
# image
cv2.imshow("Input", image)
rects = detector(gray, 2)

# loop over the face detections
for rect in rects:
	# extract the ROI of the *original* face, then align the face
	# using facial landmarks
	(x, y, w, h) = rect_to_bb(rect)
	faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256)
	faceAligned = fa.align(image, gray, rect)
    
	# display the output images
	cv2.imshow("Original", faceOrig)
	cv2.imshow("Aligned", faceAligned)
	cv2.waitKey(0)

Exemple #4
0
#ap.add_argument("-i", "--image", required=True, help="path to input image")
#ap.add_argument('--draw', nargs='?', const=True, type=bool, default=False, help="Fill landmarks")
#args = vars(ap.parse_args())

detector = dlib.get_frontal_face_detector()
#standard Histogram of Oriented Gradients + Linear SVM method for object detection.
predictor = dlib.shape_predictor("./shape_predictor_68_face_landmarks.dat")

image = cv2.imread("bts.jpg")
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

rect = detector(gray, 1)

for (i, rect) in enumerate(rect):
    shape = predictor(gray, rect)
    shape = face_utils.shape_to_np(
        shape)  # coverts object to Numpy array with(68,2)
    (x, y, w, h) = face_utils.rect_to_bb(
        rect)  #draw bounding box surrounding the detected face
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    #show the face number
    cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    #draw facial landmarks one image
for (x, y) in shape:
    cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

cv2.imshow("Output", image)
cv2.waitKey(0)
Exemple #5
0
def main():
    width = 800
    height = 600

    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-p",
                    "--shape-predictor",
                    required=True,
                    help="path to facial landmark predictor")
    ap.add_argument("-v", "--video", required=True, help="path to input video")
    #ap.add_argument("-n", "--num-frames", type=int, default=2000,
    #    help="# of frames to loop over for FPS test")
    args = vars(ap.parse_args())

    #initialize dlib's face detector (HOG-based) and then create
    # the facial landmark predictor
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(args["shape_predictor"])
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))

    # load the input image, resize it, and convert it to grayscale
    #cap = cv2.VideoCapture(args["video"])
    print("[INFO] starting video file thread...")
    cap = cv2.VideoCapture(args["video"])
    #time.sleep(1)

    # start the FPS timer
    fps = FPS().start()

    window_name = "cctv"
    cv2.namedWindow(window_name, WINDOW_NORMAL)
    cv2.resizeWindow(window_name, width, height)

    if cap.isOpened():
        ret, image = cap.read()
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # Assign new height and width for ROI
        (xr, yr, wr, hr) = cv2.boundingRect(gray)
        startYROI = 3 * hr / 5
        endYROI = hr - 1 * hr / 20
    else:
        print "Camera is not open"

    while ret:
        cv2.rectangle(image, (0, startYROI), (wr, endYROI), (0, 255, 0), 2)
        roi_image = image[startYROI:endYROI, 0:wr]
        #image = cv2.imread(args["image"])
        #image = cv2.resize(image, (width, height), interpolation = cv2.INTER_CUBIC)
        #image_scaled = cv2.resize(image, (600, 500), interpolation=cv2.INTER_AREA)
        gray = cv2.cvtColor(roi_image, cv2.COLOR_BGR2GRAY)
        #pil_gray = Image.fromarray(gray)
        #pil_gray = pil_gray.resize((width, height), Image.BICUBIC)
        #cv_gray = np.array(pil_gray)

        #roi = image[]

        # detect faces in the grayscale image
        rects = detector(gray, 0)
        # loop over the face detections
        for (i, rect) in enumerate(rects):
            # determine the facial landmarks for the face region, then
            # convert the facial landmark (x, y)-coordinates to a NumPy array
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)

            # convert dlib's rectangle to a OpenCV-style bounding box
            # [i.e., (x, y, w, h)], then draw the face bounding box
            (x, y, w, h) = face_utils.rect_to_bb(rect)
            cv2.rectangle(image, (x, (y + startYROI)),
                          (x + w, (y + startYROI) + h), (0, 255, 0), 2)

            # show the face number
            cv2.putText(image, "Face #{}".format(i + 1),
                        (x - 10, y + startYROI - 10), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (0, 255, 0), 2)

            # loop over the (x, y)-coordinates for the facial landmarks
            # and draw them on the image
            for (x, y) in shape:
                cv2.circle(image, (x, (y + startYROI)), 1, (0, 0, 255), -1)
        # show the output image with the face detections + facial landmarks
        cv2.imshow(window_name, image)
        ret, image = cap.read()

        if cv2.waitKey(1) & 0xff == ord('q'):
            break
        fps.update()

    fps.stop()
    print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
    print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
    # do a bit of cleanup
    cap.release()
    cv2.destroyAllWindows()
Exemple #6
0
def begin_classifier():

    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    out = cv2.VideoWriter('DT.avi', fourcc, 4.0, (200, 112))

    option = input("Use pre-trained model(1) or train again(2) ? ")
    if option == 1:
        # Loading the saved decision tree model pickle
        decision_tree_pkl = 'resources/decision_tree_classifier.pkl'
        decision_tree_model_pkl = open(decision_tree_pkl_filename, 'rb')
        clf = pickle.load(decision_tree_model_pkl)
    else:
        clf = prepare_dataset()  # Decision tree model

    # initialize dlib's face detector (HOG-based) and then create
    # the facial landmark predictor
    print("[INFO] loading facial landmark predictor...")
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(
        "resources/shape_predictor_68_face_landmarks.dat")
    headCascade = cv2.CascadeClassifier("resources/haarcascade_head.xml")
    faceCascade = cv2.CascadeClassifier(
        "resources/haarcascade_frontalface_alt.xml")

    # initialize the video stream and allow the camera sensor to warmup
    print("[INFO] camera sensor warming up...")
    vs = cv2.VideoCapture(0)
    vs.set(cv2.CAP_PROP_FPS, 8.0)
    time.sleep(2.0)
    inAttention = False
    lastAttentionFrame = 0
    lastAttentionFrameThresh = 48
    frameNumber = 0

    (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
    (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
    xlast = 0
    ylast = 0
    hlast = 0
    wlast = 0
    # loop over the frames from the video stream
    while True:
        # grab the frame from the threaded video stream, resize it to
        # have a maximum width of 400 pixels, and convert it to
        # grayscale
        ret, frame = vs.read()
        #framecrop = frame
        frame = imutils.resize(frame, width=200)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frameNumber = frameNumber + 1
        # detect faces in the grayscale frame
        # rects = detector(gray, 0)
        faces = faceCascade.detectMultiScale(gray,
                                             scaleFactor=1.1,
                                             minNeighbors=5,
                                             minSize=(50, 50),
                                             maxSize=(200, 200),
                                             flags=cv2.CASCADE_SCALE_IMAGE)
        if (len(faces) > 0):
            for (x, y, w, h) in faces:
                xlast = x
                ylast = y
                hlast = h
                wlast = w
                rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                shape = predictor(gray, rect)
                shape = face_utils.shape_to_np(shape)
                ns = []
                for (xs, ys) in shape:
                    # cv2.circle(frame, (xs, ys), 1, (0, 255, 0), -1)
                    ns.append(xs - x)
                    ns.append(ys - y)
                leftEye = shape[lStart:lEnd]
                rightEye = shape[rStart:rEnd]
                leftEAR = eye_aspect_ratio(leftEye)
                rightEAR = eye_aspect_ratio(rightEye)
                ns.append(leftEAR)
                ns.append(rightEAR)
                ns = np.asarray(ns)
                ys = clf.predict([ns])
                if (leftEAR + rightEAR < 0.5):
                    cv2.putText(frame, "EAR EYES CLOSED", (200, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                    print("EAR EYES CLOSED")
                if (ys == 1.0):
                    cv2.putText(frame, "NORMAL MODE", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                elif (ys == 2.0):
                    cv2.putText(frame, "Looking DOWN", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                    print("LOOKING DOWN")
                elif (ys == 3.0):
                    cv2.putText(frame, "Looking LEFT", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                    print("LOOKING LEFT")
                elif (ys == 4.0):
                    cv2.putText(frame, "Looking RIGHT", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                    print("LOOKING RIGHT")
                elif (ys == 5.0):
                    cv2.putText(frame, "Looking UP", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                    print("LOOKING UP")
                elif (ys == 6.0):
                    cv2.putText(frame, "DT EYES CLOSED", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                    print("DT EYES CLOSED")
                elif (ys == 7.0):
                    cv2.putText(frame, "YAWNING", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
                    print("YAWNING")
        else:
            graycrop = gray[ylast - int(0.5 * hlast):ylast + int(1.5 * hlast),
                            xlast - int(0.5 * wlast):xlast +
                            int(1.5 * wlast)].copy()
            framecrop = frame[ylast - int(0.5 * hlast):ylast +
                              int(1.5 * hlast), xlast -
                              int(0.5 * wlast):int(xlast +
                                                   1.5 * wlast)].copy()
            rects = detector(graycrop, 0)
            if (len(rects) > 0):
                for rect in rects:
                    (x, y, w, h) = face_utils.rect_to_bb(rect)
                    cv2.rectangle(framecrop, (x, y), (x + w, y + h),
                                  (0, 0, 255), 2)
                    shape = predictor(graycrop, rect)
                    shape = face_utils.shape_to_np(shape)
                    ns = []
                    for (xs, ys) in shape:
                        # cv2.circle(framecrop, (xs, ys), 1, (0, 0, 255), -1)
                        ns.append(xs - x)
                        ns.append(ys - y)
                        leftEye = shape[lStart:lEnd]
                        rightEye = shape[rStart:rEnd]
                        leftEAR = eye_aspect_ratio(leftEye)
                        rightEAR = eye_aspect_ratio(rightEye)
                        ns.append(leftEAR)
                        ns.append(rightEAR)
                        ns = np.asarray(ns)
                        ys = clf.predict([ns])
                        if (leftEAR + rightEAR < 0.5):
                            cv2.putText(frame, "EYES CLOSED using EAR",
                                        (200, 30), cv2.FONT_HERSHEY_SIMPLEX,
                                        0.4, (0, 0, 255), 2)
                            print("EYES CLOSED USING EAR")
                        if (ys == 1.0):
                            cv2.putText(frame, "NORMAL MODE", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("NORMAL MODE")
                        elif (ys == 2.0):
                            cv2.putText(frame, "Looking DOWN", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking DOWN")
                        elif (ys == 3.0):
                            cv2.putText(frame, "Looking LEFT", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking LEFT")
                        elif (ys == 4.0):
                            cv2.putText(frame, "Looking RIGHT", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking RIGHT")
                        elif (ys == 5.0):
                            cv2.putText(frame, "Looking UP", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking UP")
                        elif (ys == 6.0):
                            cv2.putText(frame, "EYES CLOSED using DT",
                                        (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                                        0.4, (0, 0, 255), 2)
                            print("EYES CLOSED using DT")
                        elif (ys == 7.0):
                            cv2.putText(frame, "YAWNING", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("YAWNING")
            else:
                profile = cv2.CascadeClassifier(config.casc3).detectMultiScale(
                    gray,
                    scaleFactor=1.1,
                    minNeighbors=5,
                    minSize=(30, 30),
                    maxSize=(120, 120),
                    flags=cv2.CASCADE_SCALE_IMAGE)

                if len(profile) > 0:
                    # I detected a right profile
                    for (x, y, w, h) in profile:
                        xlast = x
                        ylast = y
                        hlast = h
                        wlast = w
                        rect = dlib.rectangle(int(x), int(y), int(x + w),
                                              int(y + h))
                        cv2.rectangle(frame, (x, y), (x + w, y + h),
                                      (0, 255, 0), 2)
                        shape = predictor(gray, rect)
                        shape = face_utils.shape_to_np(shape)
                        ns = []
                        for (xs, ys) in shape:
                            # cv2.circle(frame, (xs, ys), 1, (0, 255, 0), -1)
                            ns.append(xs - x)
                            ns.append(ys - y)
                        leftEye = shape[lStart:lEnd]
                        rightEye = shape[rStart:rEnd]
                        leftEAR = eye_aspect_ratio(leftEye)
                        rightEAR = eye_aspect_ratio(rightEye)
                        ns.append(leftEAR)
                        ns.append(rightEAR)
                        ns = np.asarray(ns)
                        ys = clf.predict([ns])
                        if (leftEAR + rightEAR < 0.5):
                            cv2.putText(frame, "EYES CLOSED using EAR",
                                        (200, 30), cv2.FONT_HERSHEY_SIMPLEX,
                                        0.4, (0, 0, 255), 2)
                            print("EYES CLOSED USING EAR")
                        if (ys == 1.0):
                            cv2.putText(frame, "NORMAL MODE", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("NORMAL MODE")
                        elif (ys == 2.0):
                            cv2.putText(frame, "Looking DOWN", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking DOWN")
                        elif (ys == 3.0):
                            cv2.putText(frame, "Looking LEFT", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking LEFT")
                        elif (ys == 4.0):
                            cv2.putText(frame, "Looking RIGHT", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking RIGHT")
                        elif (ys == 5.0):
                            cv2.putText(frame, "Looking UP", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("Looking UP")
                        elif (ys == 6.0):
                            cv2.putText(frame, "EYES CLOSED using DT",
                                        (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                                        0.4, (0, 0, 255), 2)
                            print("EYES CLOSED using DT")
                        elif (ys == 7.0):
                            cv2.putText(frame, "YAWNING", (10, 30),
                                        cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                        (0, 0, 255), 2)
                            print("YAWNING")

                else:
                    # Flip frame, check for left profile
                    flippedFrame = cv2.flip(gray, 1)  # horizontal flip
                    profile = cv2.CascadeClassifier(
                        config.casc3).detectMultiScale(
                            flippedFrame,
                            scaleFactor=1.1,
                            minNeighbors=5,
                            minSize=(30, 30),
                            maxSize=(120, 120),
                            flags=cv2.CASCADE_SCALE_IMAGE)
                    if len(profile):
                        # I detected a left profile
                        for (x, y, w, h) in profile:
                            xlast = x
                            ylast = y
                            hlast = h
                            wlast = w
                            rect = dlib.rectangle(int(x), int(y), int(x + w),
                                                  int(y + h))
                            cv2.rectangle(frame, (x, y), (x + w, y + h),
                                          (0, 255, 0), 2)
                            shape = predictor(gray, rect)
                            shape = face_utils.shape_to_np(shape)
                            ns = []
                            for (xs, ys) in shape:
                                # cv2.circle(frame, (xs, ys), 1, (0, 255, 0), -1)
                                ns.append(xs - x)
                                ns.append(ys - y)
                            leftEye = shape[lStart:lEnd]
                            rightEye = shape[rStart:rEnd]
                            leftEAR = eye_aspect_ratio(leftEye)
                            rightEAR = eye_aspect_ratio(rightEye)
                            ns.append(leftEAR)
                            ns.append(rightEAR)
                            ns = np.asarray(ns)
                            ys = clf.predict([ns])
                            if (leftEAR + rightEAR < 0.5):
                                cv2.putText(frame, "EYES CLOSED using EAR",
                                            (200, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 2)
                                print("EYES CLOSED USING EAR")
                            if (ys == 1.0):
                                cv2.putText(frame, "NORMAL MODE", (10, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 2)
                                print("NORMAL MODE")
                            elif (ys == 2.0):
                                cv2.putText(frame, "Looking DOWN", (10, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 2)
                                print("Looking DOWN")
                            elif (ys == 3.0):
                                cv2.putText(frame, "Looking LEFT", (10, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 2)
                                print("Looking LEFT")
                            elif (ys == 4.0):
                                cv2.putText(frame, "Looking RIGHT", (10, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 2)
                                print("Looking RIGHT")
                            elif (ys == 5.0):
                                cv2.putText(frame, "Looking UP", (10, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 2)
                                print("Looking UP")
                            elif (ys == 6.0):
                                cv2.putText(frame, "EYES CLOSED using DT",
                                            (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                                            0.4, (0, 0, 255), 2)
                                print("EYES CLOSED using DT")
                            elif (ys == 7.0):
                                cv2.putText(frame, "YAWNING", (10, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 2)
                                print("YAWNING")

        # show the frame
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF

        # Saving image for testing
        # timestamp = getTime()
        # myPath = testPath + timestamp + ".jpg"
        # cv2.imwrite(myPath, frame)

        out.write(frame)

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

    # do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()
Exemple #7
0
    def eye_blink_video(self, frame, detector, predictor, COUNTER, TOTAL, ear_top):
        eyes_detect = ''

        rects = detector(frame, 1)

        (left_s, left_e) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
        (right_s, right_e) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

        # andiamo a rilevare il rettangolo del volto e croppiamo l'immagine con 200x200
        # un crop più piccolo rispetto a quello di webcam (200x200) perché l'analisi tramite webcam era abbastanza veloce
        # (molti fps), mentre l'analisi dei video è molto lenta e (200x200) è sufficiente per un buon eyeblink detection.
        for rect in rects:
            (x, y, w, h) = face_utils.rect_to_bb(rect)
            crop = frame[y:y + h, x:x + w]
            try:
                crop = cv2.resize(crop, (DIMENSION, DIMENSION))
            except Exception as e:
                print(str(e))
                break

            rects1 = detector(crop, 1)

            for rect in rects1:
                shape = predictor(crop, rect)
                shape = face_utils.shape_to_np(shape)

                # rilevamento degli occhi e calcolo dell'EAR di entrambe gli occhi
                leftEye = shape[left_s:left_e]
                rightEye = shape[right_s:right_e]
                left_eye_EAR = self.eye_aspect_ratio(leftEye)
                right_eye_EAR = self.eye_aspect_ratio(rightEye)
                ear = (left_eye_EAR + right_eye_EAR) / 2.0

                leftEyeHull = cv2.convexHull(leftEye)
                rightEyeHull = cv2.convexHull(rightEye)
                cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
                cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
                # se ear_top è già stato assegnato allora viene calcolato il threshold
                if ear_top != 0:
                    ear_threshold = (ear_top * 2) / 3
                    print("Ear_th", ear_threshold)
                    print("EAR TOP", ear_top)

                    # se ear < del suo threshold
                    if ear < ear_threshold:
                        # abbiamo lo stato di occhi chiusi e incrementiamo il contatore dei frame
                        eyes_detect = '1'
                        COUNTER += 1
                    else:
                        # quando l'occhio è aperto o di nuovo aperto andiamo a confrontare il contatore
                        # se ha raggiunto i minimi frame consecutivi e se lo è abbiamo avuto un eyeblink
                        # e aumentiamo total (indica il numero di eyeblink) di conseguenza il contatore viene azzerato.
                        eyes_detect = '0'
                        if COUNTER >= CONSEC_FRAMES_NUMBER:
                            TOTAL += 1

                        COUNTER = 0

                cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
                cv2.putText(frame, "EAR: {:.2f}".format(ear), (200, 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

                # ear è maggiore di ear_top lo sostituisce, ricordo che ear_top ad inizio video è a 0 e serve
                # per calcolare il threshold
                if ear > ear_top:
                    ear_top = ear

            cv2.imshow('Frame', frame)
            cv2.waitKey(1)
            return eyes_detect, COUNTER, TOTAL, ear_top
Exemple #8
0
 def hog(self, image):
     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     for face in self.faceDetect(gray, 1):
         (x, y, w, h) = face_utils.rect_to_bb(face)
         self.faces.append([x, y, x + w, y + h])
Exemple #9
0
def gen_emp_face(emp_name):
    vs = cv2.VideoCapture(0)
    time.sleep(3.0)
    face_found = False
    cropped_face = None
    correct_image = None
    time_prev = time.time()
    count = 0

    if os.path.exists(FACES_PARENT + emp_name):
        shutil.rmtree(FACES_PARENT + emp_name)
    os.mkdir(FACES_PARENT + emp_name)

    while True:
        __, frame = vs.read()
        frame = imutils.resize(frame, width=400)
        frame = cv2.flip(frame, 1)
        frame_orig = frame.copy()
        draw_map(frame, count)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        rects = detector(gray, 0)

        if len(rects) == 0:
            text = "No face found"
            cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (0, 0, 255), 2)

        elif len(rects) > 1:
            text = "{} face(s) found. Please be single in the frame.".format(
                len(rects))
            cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (0, 0, 255), 2)

        elif len(rects) == 1:
            text = "Face detected"
            cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (0, 0, 255), 2)
            rect = rects[0]

            (bx, by, bw, bh) = face_utils.rect_to_bb(rect)
            cv2.rectangle(frame, (bx, by), (bx + bw, by + bh), (0, 255, 0), 1)
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)
            face_points = []
            inside = True
            for (i, (x, y)) in enumerate(shape):
                cv2.circle(frame, (x, y), 1, (0, 0, 255), -1)
                face_points.append((x, y))
                if x > 270 or x < 110 or y < 30 or y > 135 and i != 4:
                    inside = False

            eye1_cen = ((face_points[0][0] + face_points[1][0]) // 2,
                        (face_points[0][1] + face_points[1][1]) // 2)
            eye2_cen = ((face_points[2][0] + face_points[3][0]) // 2,
                        (face_points[2][1] + face_points[3][1]) // 2)
            angle = slope(eye1_cen, eye2_cen)
            cv2.putText(frame, str(angle), (10, 50), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (0, 255, 0), 2)

            # print(time_prev-time.time())
            if time.time() - time_prev > 1 and count <= 25 and inside:
                time_capture = time.time()
                time_prev = time_capture
                adjusted = correct_orientation(frame_orig, rect)
                cv2.imwrite(
                    FACES_PARENT + emp_name + "/" + str(count) + ".jpg",
                    adjusted)
                count += 1
            elif not inside:
                cv2.putText(frame, "Be inside the grid", (150, 120),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)

        if count > 25:
            cv2.putText(frame, "Done, Press q", (200, 120),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    if count < 25:
        return False
    vs.release()
    cv2.destroyAllWindows()

    return True
 def face_full_process(self, frame, type, face_detect_on, age_gender_on):
     '''
     full process to extract face, ROI 
     face detection and facial landmark run every 3 frames
     age and gender detection runs every 6 frames
     last values of detections are used in other frames to reduce the time of the process
     ***NOTE: need 2 time facial landmarks, 1 for face alignment and 1 for facial landmarks in aligned face
     ***TODO: find facial landmarks after rotate (find co-ords after rotating) so don't need to do 2 facial landmarks
     Args:
         frame (cv2 image): input frame 
         type (str): 5 or 68 landmarks
         face_detect_on (bool): flag to run face detection and facial landmarks
         age_gender_on (bool): flag to run age gender detection
         
     Outputs:
         rects (array): detected faces as rectangles
         face (cv2 image): face
         (age, gender) (str,str): age and gender
         shape (array): facial landmarks' co-ords in format of tuples (x,y)
         aligned_face (cv2 image): face after alignment
         aligned_shape (array): facial landmarks' co-ords of the aligned face in format of tuples (x,y)
         #mask (cv2 image): mask of the face after fillConvexPoly
     '''
     
     #assign from last params
     age = self.last_age
     gender = self.last_gender
     rects = self.last_rects
     shape = self.last_shape
     aligned_shape = self.last_aligned_shape
     
     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     
     if face_detect_on:
         if(type=="5"):
             shape, rects = self.get_landmarks(frame, "5")
             #mask = None
             
             if shape is None:
                 return None
         else:    
             shape, rects = self.get_landmarks(frame, "68")
             # remapped_landmarks = self.facial_landmarks_remap(shape)
             # mask = np.zeros((face.shape[0], face.shape[1]))
             # cv2.fillConvexPoly(mask, remapped_landmarks[0:27], 1) 
             if shape is None:
                 return None
     
     (x, y, w, h) = face_utils.rect_to_bb(rects[0])
     
     face = frame[y:y+h,x:x+w]        
     
     if age_gender_on:
         age, gender = self.age_gender_detection(face)
     
     aligned_face, aligned_face = self.face_alignment(frame, shape)
     
     # if face_detect_on:
         # if(type=="5"):
             # aligned_shape, rects_2 = self.get_landmarks(aligned_face, "5")
             # if aligned_shape is None:
                 # return None
         # else:    
             # aligned_shape, rects_2 = self.get_landmarks(aligned_face, "68")
             # if aligned_shape is None:
                 # return None
     # print("2: aligned_shape")
     # print(aligned_shape)
     # print("---")
     
     #assign to last params
     self.last_age = age
     self.last_gender = gender
     self.last_rects = rects
     self.last_shape = shape
     self.last_aligned_shape = aligned_shape
     
     #return rects, face, (age, gender), shape, aligned_face, mask
     return rects, face, (age, gender), shape, aligned_face, aligned_shape
     
 
 
 
 
 
 
 
 
     
     
    def run_cam(self, vs, sio):
        @sio.event
        def build_event(data):
            print('into build_face.py  data is:' + data)
            self.input_name = data

        while True:
            frame = vs.read()
            frame_save = frame.copy()
            frame = imutils.resize(frame, width=600)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            # detect faces in the grayscale frame
            rects = self.detector(gray, 0)

            if len(rects) > 0:
                text = "{} face(s) found".format(len(rects))
                cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (0, 0, 255), 2)

            for rect in rects:
                (bX, bY, bW, bH) = face_utils.rect_to_bb(rect)
                cv2.rectangle(frame, (bX, bY), (bX + bW, bY + bH), (0, 255, 0),
                              3)

                # determine the facial landmarks for the face region, then
                # convert the facial landmark (x, y)-coordinates to a NumPy
                # array
                shape = self.predictor(gray, rect)
                shape = face_utils.shape_to_np(shape)

                # loop over the (x, y)-coordinates for the facial landmarks
                # and draw each of them
                for (i, (x, y)) in enumerate(shape):
                    cv2.circle(frame, (x, y), 3, (0, 0, 255), -1)
                    cv2.putText(frame, str(i + 1), (x - 10, y - 10),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)

            key = cv2.waitKey(100) & 0xFF

            name_flag = False
            if (self.input_name != 'q') and (self.input_name != ''):
                name_flag = True

            if key == ord("q"):
                break
            elif (len(rects) == 1) and (name_flag == True):
                lf = Load_face()
                cv2.imwrite('face_pic/{}.jpg'.format(self.input_name),
                            frame_save)
                print('okay face in !')

                cv2.rectangle(frame, (100, 50), (550, 110), (0, 255, 0), -1)
                text = "okay face in !"
                cv2.putText(frame, text, (100, 100), cv2.FONT_HERSHEY_SIMPLEX,
                            2, (255, 0, 0), 3)

                # cv2.imshow("Video", frame)
                self.to_base64_send(frame, 'stream_event', sio)
                key = cv2.waitKey(100) & 0xFF

                lf.load_json()
                lf.update(self.input_name)
                lf.to_json()
                time.sleep(3)
                break

            # cv2.imshow("Video", frame)
            self.to_base64_send(frame, 'stream_event', sio)
Exemple #12
0
    def align(self, image_name, face_loc=None):
        image = cv2.imread(self.image_path + image_name)
        image = imutils.resize(image, width=800)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        if (face_loc == None):
            rects = self.detector(gray, 2)
        else:
            rects = [
                dlib.rectangle(face_loc[0], face_loc[3], face_loc[1],
                               face_loc[2])
            ]
        print(rects)
        # loop over the face detections
        for c, rect in enumerate(rects):
            # extract the ROI of the *original* face, then align the face
            # using facial landmarks
            (x, y, w, h) = rect_to_bb(rect)
            faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256)

            # convert the landmark (x, y)-coordinates to a NumPy array
            shape = self.predictor(gray, rect)
            shape = shape_to_np(shape)

            # extract the left and right eye (x, y)-coordinates
            (lStart, lEnd) = self.FACIAL_LANDMARKS_IDXS["left_eye"]
            (rStart, rEnd) = self.FACIAL_LANDMARKS_IDXS["right_eye"]
            leftEyePts = shape[lStart:lEnd]
            rightEyePts = shape[rStart:rEnd]
            # compute
            leftEyeCenter = leftEyePts.mean(axis=0).astype("int")
            rightEyeCenter = rightEyePts.mean(axis=0).astype("int")
            # compute the angle between the eye centroids
            dY = rightEyeCenter[1] - leftEyeCenter[1]
            dX = rightEyeCenter[0] - leftEyeCenter[0]
            angle = np.degrees(np.arctan2(dY, dX)) - 180
            # compute the desired right eye x-coordinate based on the
            # desired x-coordinate of the left eye
            desiredRightEyeX = 1.0 - self.desiredLeftEye[0]
            # determine the scale of the new resulting image by taking
            # the ratio of the distance between eyes in the *current*
            # image to the ratio of distance between eyes in the
            # *desired* image
            dist = np.sqrt((dX**2) + (dY**2))
            desiredDist = (desiredRightEyeX - self.desiredLeftEye[0])
            desiredDist *= self.desiredFaceWidth
            scale = desiredDist / dist
            # compute center (x, y)-coordinates (i.e., the median point)
            # between the two eyes in the input image
            eyesCenter = ((leftEyeCenter[0] + rightEyeCenter[0]) // 2,
                          (leftEyeCenter[1] + rightEyeCenter[1]) // 2)

            # grab the rotation matrix for rotating and scaling the face
            M = cv2.getRotationMatrix2D(eyesCenter, angle, scale)

            # update the translation component of the matrix
            tX = self.desiredFaceWidth * 0.5
            tY = self.desiredFaceHeight * self.desiredLeftEye[1]
            M[0, 2] += (tX - eyesCenter[0])
            M[1, 2] += (tY - eyesCenter[1])
            # apply the affine transformation
            (w, h) = (self.desiredFaceWidth, self.desiredFaceHeight)
            output = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC)
            # apply the affine transformation
            (w, h) = (self.desiredFaceWidth, self.desiredFaceHeight)
            output = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC)
            print(img_name + " was aligned.")
            cv2.imwrite('../data/aligned/' + 'aligned_' + str(c) + img_name,
                        output)
        return
Exemple #13
0
    def display_front_frame(self, frame, prediction_results):
        """
        Display frame on cv2 window.
        
        Args:
            frame (?): cv2 frame.
            prediction_results (list): a list of tuples. Each item corresponds to
                a face and has (face_rect, age, gender).
        """

        if frame is None:
            return

        height, width = frame.shape[:2]
        img_ratio = float(height) / float(width)
        w_scale_factor = float(width) / float(predictor.PROCESSING_SIZE)
        h_scale_factor = float(height) / (predictor.PROCESSING_SIZE *
                                          img_ratio)

        # write the last person id
        cv2.putText(frame,
                    str(self._last_id) + ' - ' + str(len(prediction_results)),
                    (10, 25), cv2.FONT_HERSHEY_DUPLEX, 1.0, (0, 0, 0), 1)

        if len(prediction_results) > 0:
            # write the results on the frame
            for face_rect, _, age_gender_probas in prediction_results:
                # convert dlib.rectangle to regular bounding box
                (x, y, w, h) = rect_to_bb(face_rect)
                # re-scale based on predictor.PROCESSING_SIZE
                x = int(x * w_scale_factor)
                y = int(y * h_scale_factor)
                w = int(w * w_scale_factor)
                h = int(h * h_scale_factor)
                # draw rectangle on the frame
                cv2.rectangle(frame, (x, y), (x + w, y + h), (179, 178, 179),
                              2)
                # draw a filled rectangle to write text
                cv2.rectangle(frame, (x, y + h - 35), (x + w, y + h),
                              (212, 211, 212), cv2.FILLED)
                # write age and gender if provided
                if not age_gender_probas is None:
                    index = np.argmax(age_gender_probas)
                    text = ''
                    if index == 0:
                        text = 'child'
                    elif index == 1:
                        text = 'M adult'
                    elif index == 2:
                        text = 'F adult'
                    elif index == 3:
                        text = 'senior'
                    else:
                        print("Age / gender out of range.")
                    cv2.putText(frame, text, (x + 2, y + h - 6),
                                cv2.FONT_HERSHEY_DUPLEX, 1.0, (0, 0, 0), 1)

        # display the frame
        cv2.namedWindow('Front Camera', cv2.WINDOW_NORMAL)
        cv2.resizeWindow('Front Camera', predictor.PROCESSING_SIZE,
                         int(img_ratio * predictor.PROCESSING_SIZE))
        cv2.imshow('Front Camera', frame)
        print('LastNameEN')
        print(pytesseract.image_to_string(roiLastNameEN, lang = 'eng',config=tessdata_dir_config))

        id_card = pytesseract.image_to_string(roiID, lang = 'eng',config=tessdata_dir_config)
        nameTH = pytesseract.image_to_string(roiNameTH, lang = 'tha',config=tessdata_dir_config)
        nameEng = pytesseract.image_to_string(roiNameEN, lang = 'eng',config=tessdata_dir_config)
        lastNameEng = pytesseract.image_to_string(roiLastNameEN, lang = 'eng',config=tessdata_dir_config)
        timestr = time.strftime("%d_%m_%Y-%H_%M_%S")

        #detect face
        rects = detector(gray_image,1)
        if len(rects) == 0:
            print "Don't have any face"
        else:
            for(i, rect) in enumerate(rects):
                (xf, yf, wf, hf) = face_utils.rect_to_bb(rect)
                cv2.rectangle(roiImg,(xf,yf),(xf+wf,yf+hf),(0,255,0),2)
                roiface = image[yf:yf+hf,xf:xf+wf]
                f = open('data.txt','a')
                f.write("\nID : "+id_card.encode('utf8')+"\n")
                f.write("Thai Name : "+nameTH.encode('utf8')+"\n")
                f.write("Name : "+nameEng.encode('utf8')+"\n")
                f.write("Last Name : "+lastNameEng.encode('utf8')+"\n")
                f.write ("Image : "+timestr+".jpg"+"\n")
                f.write("\n")
                cv2.imwrite(timestr+".jpg", roiImg)
                f.close()
                cv2.imshow('roiface',roiface)
        
                                              
    def get_frame(self):

        pickle_in = open("New_testing_dlib_normalized.pickle", "rb")

        model = pickle.load(pickle_in)
        while (self.video.isOpened()):
            self.frame += 1
            ret, frame = self.video.read()
            if ret is True:
                if self.frame % 5 == 0:
                    # print(str(self.frame)+" frame")
                    #ret, frame = self.video.read()
                    # if ret == True:
                    self.frame += 1
                    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                    file = open("Expressions.csv", "w")
                    face = detector(gray, 0)
                    # print("Number of Faces {}".format(len(face)))
                    my_list = []
                    count_interested = 0
                    count_bore = 0
                    count_neutral = 0
                    for (J, rect) in enumerate(face):
                        shap = predictor(frame, rect)
                        xlist = []
                        ylist = []
                        shap = face_utils.shape_to_np(shap)
                        Centre = (shap[30])
                        centre_x = Centre[0]
                        centre_y = Centre[1]
                        shap = shap[18:68]
                        for i in shap:
                            xlist.append(i[0])
                            ylist.append(i[1])
                        forx = []
                        fory = []
                        for x in xlist:
                            forx.append((x - centre_x)**2)
                        for y in ylist:
                            fory.append((y - centre_y)**2)
                        listsum = [sum(x) for x in zip(forx, fory)]
                        features = []
                        for i in listsum:
                            k = mpmath.sqrt(float(i))
                            features.append(float(k))
                        maxx = (max(features))
                        final = []
                        for i in features:
                            if (i == 0.0):
                                continue
                            F = i / maxx
                            final.append(F)
                        # print(final)
                        numpy_array = np.array(final)
                        prediction = model.predict([numpy_array])[0]
                        # print(prediction)
                        # print("done")
                        (x, y, w, h) = face_utils.rect_to_bb(rect)
                        cv2.rectangle(frame, (x, y), (x + w, y + h),
                                      (0, 0, 255), 2)
                        # display the image and the prediction
                        cv2.putText(frame, prediction, (x - 7, y - 6),
                                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0),
                                    2)
                        cv2.circle(frame, (centre_x, centre_y), 1, (0, 0, 0),
                                   10)
                        for (x, y) in shap:
                            cv2.circle(frame, (x, y), 1, (0, 0, 255), 2)
                            cv2.line(frame, (centre_x, centre_y), (x, y),
                                     (0, 255, 1))
                        cv2.waitKey(100)
                        if prediction == "INTERESTED":
                            count_interested += 1
                            # self.Expression.append(1)

                        elif prediction == "BORE":
                            count_bore += 1
                        else:
                            count_neutral += 1

                        ret, jpeg = cv2.imencode('.jpg', frame)
                        my_list.append(jpeg.tobytes)

                    if (count_interested > count_bore) and (count_interested >
                                                            count_neutral):
                        self.Expression.append(1)

                    elif (count_bore > count_interested) and (count_bore >
                                                              count_neutral):
                        self.Expression.append(-1)
                    else:
                        self.Expression.append(0)

                    with file:
                        writter = csv.writer(file)
                        writter.writerow(self.Expression)
                    return (my_list)
            else:
                break
Exemple #16
0
writer = imageio.get_writer(args['output_name'] + '.mp4', fps=20.0)

# prompt the user if the face found is a good detection
face_dets = detector(f, 1)
cut = None
mask = None
face_pts = None
while True:
    if len(face_dets) == 0:
        ret, f = cap_f.read()
        face_dets = detector(f, 1)
        continue
    for (i, face) in enumerate(face_dets):
        # face = face.rect
        f_show = copy.deepcopy(f)
        (x, y, w, h) = face_utils.rect_to_bb(face)
        cv2.rectangle(f_show, (x, y), (x + w, y + h), (0, 255, 0), 2)
        shape = predictor(f, face)
        shape = face_utils.shape_to_np(shape)
        for (xs, ys) in shape:
            cv2.circle(f_show, (xs, ys), 1, (0, 0, 255), -1)
    cv2.putText(f_show, 'Press s to select current face', (30, 30), font, 1,
                (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(f_show, 'Press d to detect again', (30, 60), font, 1,
                (255, 255, 255), 2, cv2.LINE_AA)
    winname = 'Faces'
    cv2.namedWindow(winname, cv2.WINDOW_NORMAL)
    cv2.moveWindow(winname, 400, 500)
    cv2.imshow(winname, f_show)
    k = cv2.waitKey(0)
    if k == 115:
    def detect_face(self):

        face_cascade = cv2.CascadeClassifier(self.CASE_PATH)

        # 0 means the default video capture device in OS

        video_capture = cv2.VideoCapture(0)

        # infinite loop, break by key ESC

        while True:

            if not video_capture.isOpened():

                sleep(5)

            # Capture frame-by-frame

            ret, frame = video_capture.read()

            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            ####>>
            rects = self.detector(gray, 2)
            ####~>>>>>>>>>>
            faces = face_cascade.detectMultiScale(gray,
                                                  scaleFactor=1.2,
                                                  minNeighbors=10,
                                                  minSize=(self.face_size,
                                                           self.face_size))

            # placeholder for cropped faces

            face_imgs = np.empty(
                (len(faces), self.face_size, self.face_size, 3))

            for i, face in enumerate(faces):

                face_img, cropped = self.crop_face(frame,
                                                   face,
                                                   margin=40,
                                                   size=self.face_size)

                (x, y, w, h) = cropped
                ########>>>>>>>>
                for rect in rects:
                    (x, y, w, h) = rect_to_bb(rect)
                    faceAligned = self.fa.align(frame, gray, rect)
                    cv2.imwrite("Img" + str(i) + ".png", faceAligned)
                    faceAligned = cv2.cvtColor(faceAligned, cv2.COLOR_BGR2RGB)
                    arr = np.reshape(faceAligned, (1, 100, 100, 3))
                    arr = arr.astype(np.float)
                #############>>>>>>>
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 200, 0), 2)

                face_imgs[i, :, :, :] = face_img

            if len(face_imgs) > 0:

                # predict ages and genders of the detected faces

                results = self.model.predict(face_imgs)

                predicted_genders = results[0]

                ages = np.arange(0, 101).reshape(101, 1)

                predicted_ages = results[1].dot(ages).flatten()
                ##########>>>>>>>>>>>>
                pd = self.model_ethinicity.predict(arr)
                race = self.array[np.asscalar(pd.argmax(axis=1))]
                #########>>>>
                ##########>>>>>
                frontal_faces = sorted(faces,
                                       reverse=True,
                                       key=lambda x: (x[2] - x[0]) *
                                       (x[3] - x[1]))[0]
                (fX, fY, fW, fH) = frontal_faces
                roi = gray[fY:fY + fH, fX:fX + fW]
                roi = cv2.resize(roi, (48, 48))
                roi = roi.astype("float") / 255.0
                roi = img_to_array(roi)
                roi = np.expand_dims(roi, axis=0)
                np.reshape(roi, (48, 48, 1))
                preds = self.emotion_classifier.predict(roi)[0]
                emotion_probability = np.max(preds)
                emotion = self.EMOTIONS[preds.argmax()]

    ########>>>>>>>>
    # draw results

            for i, face in enumerate(faces):

                label = "{}, {} , {},{}".format(
                    emotion, race, int(predicted_ages[i]),
                    "F" if predicted_genders[i][0] > 0.5 else "M")

                self.draw_label(frame, (face[0], face[1]), label)

            cv2.imshow('Keras Faces', frame)

            if cv2.waitKey(5) == 27:  # ESC key press

                break

        # When everything is done, release the capture

        video_capture.release()

        cv2.destroyAllWindows()
    def face_detect(self, frame):
        #frame = imutils.resize(frame, width=400)
        face_frame = np.zeros((10, 10, 3), np.uint8)
        mask = np.zeros((10, 10, 3), np.uint8)
        ROI1 = np.zeros((10, 10, 3), np.uint8)
        ROI2 = np.zeros((10, 10, 3), np.uint8)
        #ROI3 = np.zeros((10, 10, 3), np.uint8)
        status = False
        
        if frame is None:
            return 
            
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # detect faces in the grayscale image
        rects = self.detector(gray, 0)
        

        # loop over the face detections
        #for (i, rect) in enumerate(rects): 
            # determine the facial landmarks for the face region, then
            # convert the facial landmark (x, y)-coordinates to a NumPy
            # array
            
        #assumpion: only 1 face is detected
        if len(rects)>0:
            status = True
            # shape = self.predictor(gray, rects[0])
            # shape = face_utils.shape_to_np(shape)

                # convert dlib's rectangle to a OpenCV-style bounding box
                # [i.e., (x, y, w, h)], then draw the face bounding box
            (x, y, w, h) = face_utils.rect_to_bb(rects[0])
            #cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 1)
            if y<0:
                print("a")
                return frame, face_frame, ROI1, ROI2, status, mask
            #if i==0:
            face_frame = frame[y:y+h,x:x+w]
                # show the face number
                #cv2.putText(frame, "Face #{}".format(i + 1), (x - 10, y - 10),
                #    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                # loop over the (x, y)-coordinates for the facial landmarks
                # and draw them on the image
                
            # for (x, y) in shape:
                # cv2.circle(frame, (x, y), 1, (0, 0, 255), -1) #draw facial landmarks
            if(face_frame.shape[:2][1] != 0):
                face_frame = imutils.resize(face_frame,width=256)
                
            face_frame = self.fa.align(frame,gray,rects[0]) # align face
            
            grayf = cv2.cvtColor(face_frame, cv2.COLOR_BGR2GRAY)
            rectsf = self.detector(grayf, 0)
            
            if len(rectsf) >0:
                shape = self.predictor(grayf, rectsf[0])
                shape = face_utils.shape_to_np(shape)
                
                for (a, b) in shape:
                    cv2.circle(face_frame, (a, b), 1, (0, 0, 255), -1) #draw facial landmarks
                
                cv2.rectangle(face_frame,(shape[54][0], shape[29][1]), #draw rectangle on right and left cheeks
                        (shape[12][0],shape[33][1]), (0,255,0), 0)
                cv2.rectangle(face_frame, (shape[4][0], shape[29][1]), 
                        (shape[48][0],shape[33][1]), (0,255,0), 0)
                
                ROI1 = face_frame[shape[29][1]:shape[33][1], #right cheek
                        shape[54][0]:shape[12][0]]
                        
                ROI2 =  face_frame[shape[29][1]:shape[33][1], #left cheek
                        shape[4][0]:shape[48][0]]    

                # ROI3 = face_frame[shape[29][1]:shape[33][1], #nose
                        # shape[31][0]:shape[35][0]]
                
                #get the shape of face for color amplification
                rshape = np.zeros_like(shape) 
                rshape = self.face_remap(shape)    
                mask = np.zeros((face_frame.shape[0], face_frame.shape[1]))
            
                cv2.fillConvexPoly(mask, rshape[0:27], 1) 
                # mask = np.zeros((face_frame.shape[0], face_frame.shape[1],3),np.uint8)
                # cv2.fillConvexPoly(mask, shape, 1)
                
            #cv2.imshow("face align", face_frame)
            
            # cv2.rectangle(frame,(shape[54][0], shape[29][1]), #draw rectangle on right and left cheeks
                    # (shape[12][0],shape[54][1]), (0,255,0), 0)
            # cv2.rectangle(frame, (shape[4][0], shape[29][1]), 
                    # (shape[48][0],shape[48][1]), (0,255,0), 0)
            
            # ROI1 = frame[shape[29][1]:shape[54][1], #right cheek
                    # shape[54][0]:shape[12][0]]
                    
            # ROI2 =  frame[shape[29][1]:shape[54][1], #left cheek
                    # shape[4][0]:shape[48][0]]   
                
        else:
            cv2.putText(frame, "No face detected",
                       (200,200), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255),2)
            status = False
        return frame, face_frame, ROI1, ROI2, status, mask    
Exemple #19
0
    def eye_blink_cam(self, frame, rect, detector, predictor, COUNTER, TOTAL, ear_top):
        eyes_detect = ''

        # resize del frame a width a 450
        frame = imutils.resize(frame, width=450)
        # conversione in scala di grigi
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        dets = detector(gray, 1)  # Detect the faces in the image
        (left_s, left_e) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
        (right_s, right_e) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

        # andiamo a rilevare il rettangolo del volto e croppiamo l'immagine con 200x200
        for det in dets:
            (x, y, w, h) = face_utils.rect_to_bb(det)
            crop = gray[y:y + h, x:x + w]
            try:
                crop = cv2.resize(crop, (DIMENSION, DIMENSION))
            except Exception as e:
                print(str(e))
                break

            dets1 = detector(crop, 1)

            # rilevamento degli occhi e calcolo dell'EAR di entrambe gli occhi
            for det in dets1:
                shape = predictor(crop, det)
                shape = face_utils.shape_to_np(shape)
                leftEye = shape[left_s:left_e]
                rightEye = shape[right_s:right_e]
                left_eye_EAR = self.eye_aspect_ratio(leftEye)
                right_eye_EAR = self.eye_aspect_ratio(rightEye)
                ear = (left_eye_EAR + right_eye_EAR) / 2.0

                # se ear_top è già stato assegnato allora viene calcolato il threshold
                if ear_top != 0:
                    ear_threshold = (ear_top * 2) / 3
                    print("Ear_th", ear_threshold)
                    print("EAR TOP", ear_top)

                    # se ear < del suo threshold
                    if ear < ear_threshold:
                        # abbiamo lo stato di occhi chiusi e incrementiamo il contatore dei frame
                        eyes_detect = '1'
                        COUNTER += 1
                    else:
                        # quando l'occhio è aperto o di nuovo aperto, andiamo a confrontare il contatore
                        # se ha raggiunto i minimi frame consecutivi e se lo è abbiamo avuto un eyeblink
                        # di conseguenza il contatore viene azzerato.
                        eyes_detect = '0'

                        if COUNTER >= CONSEC_FRAMES_NUMBER:
                            TOTAL += 1

                        COUNTER = 0

                cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
                cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
                # ear è maggiore di ear_top lo sostituisce, ricordo che ear_top ad inizio video è a 0 e serve per
                # calcolare il threshold
                if ear > ear_top:
                    ear_top = ear

        return eyes_detect, TOTAL, COUNTER, ear_top
Exemple #20
0
    # for l in landmrk:
    #     for key,val in l.items():
    #         for (x,y) in val:
    #             cv2.circle(frame, (x, y), 1, (255,0, 0), -1)


    gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    faces = detector(rgb_image)
    # face_locations = face_recognition.face_locations(rgb_image)
    # print (reversed(face_locations))
    face_name = face_compare(rgb_image,process_this_frame)
    for face_coordinates, fname in zip(faces,face_name):
        print ("forrrrr")
        x1, x2, y1, y2 = apply_offsets(face_utils.rect_to_bb(face_coordinates), emotion_offsets)
        gray_face = gray_image[y1:y2, x1:x2]
        try:
            gray_face = cv2.resize(gray_face, (emotion_target_size))
        except:
            continue


        gray_face = preprocess_input(gray_face, True)
        gray_face = np.expand_dims(gray_face, 0)
        gray_face = np.expand_dims(gray_face, -1)
        emotion_prediction = emotion_classifier.predict(gray_face)
        emotion_probability = np.max(emotion_prediction)
        emotion_label_arg = np.argmax(emotion_prediction)
        emotion_text = emotion_labels[emotion_label_arg]
        emotion_window.append(emotion_text)
Exemple #21
0
    def eye_blink_video_fixedTh(self, frame, detector, predictor, COUNTER, TOTAL, ear_th):

        eyes_detect = ''

        rects = detector(frame, 1)  # Detect the faces in the image
        (left_s, left_e) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
        (right_s, right_e) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]


        # andiamo a rilevare il rettangolo del volto e croppiamo l'immagine (200x200)
        for rect in rects:
            (x, y, w, h) = face_utils.rect_to_bb(rect)
            crop = frame[y:y + h, x:x + w]

            try:
                crop = cv2.resize(crop, (DIMENSION, DIMENSION))
            except Exception as e:
                print(str(e))
                break

            rects1 = detector(crop, 1)

            for rect in rects1:
                shape = predictor(crop, rect)
                shape = face_utils.shape_to_np(shape)

                # rilevamento degli occhi e calcolo dell'EAR di entrambe gli occhi
                leftEye = shape[left_s:left_e]
                rightEye = shape[right_s:right_e]
                left_eye_EAR = self.eye_aspect_ratio(leftEye)
                right_eye_EAR = self.eye_aspect_ratio(rightEye)
                ear = (left_eye_EAR + right_eye_EAR) / 2.0

                leftEyeHull = cv2.convexHull(leftEye)
                rightEyeHull = cv2.convexHull(rightEye)
                cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
                cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)

                # confrontiamo EAR con i threshold che vanno da 0.10 a 0.29 e mettiamo 1 al relavito threshold se EAR è
                # minore di esso altrimenti il valore resta a 0.
                count = 0

                for threshold in np.arange(0.10, 0.30, 0.01):

                    th = np.round(threshold, 2)
                    if ear < th:
                        COUNTER += 1
                        eyes_detect = '1'
                        ear_th[count] = 1
                    else:
                        eyes_detect = '0'

                        COUNTER = 0
                    count += 1
                    # print(count)

                    cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
                    cv2.putText(frame, "EAR: {:.2f}".format(ear), (200, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)


            cv2.imshow('Frame', frame)
            cv2.waitKey(1)
            return eyes_detect, COUNTER, ear_th
def alignFace(frame, gray, rect, aligner):
    (x, y, w, h) = face_utils.rect_to_bb(rect)
    faceOrig = imutils.resize(frame[y:y + h, x:x + w], width=256)
    faceAligned = aligner.align(frame, gray, rect)

    return faceOrig, faceAligned
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # detect faces in the grayscale frame
    rects = detector(gray, 0)

    # check to see if a face was detected, and if so, draw the total
    # number of faces on the frame
    if len(rects) > 0:
        text = "{} face(s) found".format(len(rects))
        cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (0, 0, 255), 2)
# loop over the face detections
    for rect in rects:
        # compute the bounding box of the face and draw it on the
        # frame
        (bX, bY, bW, bH) = face_utils.rect_to_bb(rect)
        cv2.rectangle(frame, (bX, bY), (bX + bW, bY + bH), (0, 255, 0), 1)

        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)[indices]
        shape = np.array([tuple(i) for i in shape], dtype="double")
        image_points = shape
        #print(type(shape), shape)
        rotation_vector, translation_vector = get_rotation(
            shape, model_points, camera_matrix)
        (nose_end_point2D,
         jacobian) = cv2.projectPoints(np.array([(0.0, 0.0, 1000.0)]),
                                       rotation_vector, translation_vector,
                                       camera_matrix, dist_coeffs)
        print('projected')
        #for p in image_points:
    def face_detect(self, frame):
        #frame = imutils.resize(frame, width=400)
        face_frame = np.zeros((10, 10, 3), np.uint8)
        mask = np.zeros((10, 10, 3), np.uint8)
        ROI1 = np.zeros((10, 10, 3), np.uint8)
        ROI2 = np.zeros((10, 10, 3), np.uint8)
        status = False
        
        if frame is None:
            return 
            
        rgb_frame = frame[:, :, ::-1]
        # detect faces in the rgb image
        rects = api.face_detector(rgb_frame)



        if len(rects)>0:
            status = True

            (x, y, w, h) = face_utils.rect_to_bb(rects[0])

            if y<0:
                print("a")
                return frame, face_frame, ROI1, ROI2, status, mask

            face_frame = frame[y:y+h,x:x+w]

            if(face_frame.shape[:2][1] != 0):
                face_frame = imutils.resize(face_frame,width=256)
                
            face_frame = self.fa.align(frame,rgb_frame,rects[0])
            
            rgb_frame_f = face_frame[:, :, ::-1]
            rectsf = api.face_detector(rgb_frame_f)
            
            if len(rectsf) >0:

                shape = self.predictor(rgb_frame_f, rectsf[0])
                shape = face_utils.shape_to_np(shape)
                
                for (a, b) in shape:
                    cv2.circle(face_frame, (a, b), 1, (0, 0, 255), -1) #draw facial landmarks

                cv2.rectangle(frame, (x, y), (w + x, h + y), (0, 0, 255), 1)

                cv2.rectangle(face_frame,(shape[54][0], shape[29][1]), #draw rectangle on right and left cheeks
                        (shape[12][0],shape[33][1]), (0,255,0), 0)
                cv2.rectangle(face_frame, (shape[4][0], shape[29][1]), 
                        (shape[48][0],shape[33][1]), (0,255,0), 0)
                
                ROI1 = face_frame[shape[29][1]:shape[33][1], #right cheek
                        shape[54][0]:shape[12][0]]
                        
                ROI2 =  face_frame[shape[29][1]:shape[33][1], #left cheek
                        shape[4][0]:shape[48][0]]    


                rshape = np.zeros_like(shape) 
                rshape = self.face_remap(shape)    
                mask = np.zeros((face_frame.shape[0], face_frame.shape[1]))
            
                cv2.fillConvexPoly(mask, rshape[0:27], 1) 

        else:
            cv2.putText(frame, "No face detected",
                       (200,200), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255),2)
            status = False
        frame = cv2.resize(frame, (640, 480))
        return frame, face_frame, ROI1, ROI2, status, mask    
Exemple #25
0
def upload_file():
    file = request.files['image']

    image_path = os.path.sep.join([UPLOAD_FOLDER, file.filename])
    file.save(image_path)
    # image_url = uploader.upload(image_path)
    # image = AgeGenderHelper.url_to_image(image_url['url'])

    # initialize dlib's face detector (HOG-based), then create facial landmark predictor and face aligner
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(deploy.DLIB_LANDMARK_PATH)
    fa = FaceAligner(predictor)

    # initialize image preprocessors
    sp, cp, iap = SimplePreprocessor(
        256, 256, inter=cv2.INTER_CUBIC), CropPreprocessor(
            config.IMAGE_SIZE, config.IMAGE_SIZE,
            horiz=False), ImageToArrayPreprocessor()

    # loop over image paths
    # load image fron disk, resize it and convert it to grayscale
    print(f'[INFO] processing {file.filename}')
    image = cv2.imread(image_path)
    image = imutils.resize(image, width=1024)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    clone = image.copy()

    # detect faces in grayscale image
    rects = detector(gray, 1)

    # loop over face detections
    for rect in rects:
        # determine facial landmarks for face region, then align face
        shape = predictor(gray, rect)
        face = fa.align(image, gray, rect)

        # draw bounding box around face
        x, y, w, h = face_utils.rect_to_bb(rect)
        cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 255, 0), 2)

        if config.DATASET == 'IOG':
            # load Label Encoder and mean files
            print('[INFO] loading label encoders and mean files...')
            age_le = pickle.loads(open(deploy.AGE_LABEL_ENCODER, 'rb').read())
            gender_le = pickle.loads(
                open(deploy.GENDER_LABEL_ENCODER, 'rb').read())
            age_means = json.loads(open(deploy.AGE_MEAN).read())
            gender_means = json.loads(open(deploy.GENDER_MEAN).read())

            # initialize image preprocessors
            age_mp = MeanPreprocessor(age_means['R'], age_means['G'],
                                      age_means['B'])
            gender_mp = MeanPreprocessor(gender_means['R'], gender_means['G'],
                                         gender_means['B'])

            age_preds, gender_preds = predict(face, sp, age_mp, gender_mp, cp,
                                              iap, deploy.AGE_NETWORK_PATH,
                                              deploy.GENDER_NETWORK_PATH,
                                              age_le, gender_le)

        elif config.DATASET == 'ADIENCE':
            # age_preds_cross, gender_preds_cross = [], []

            i = 0
            # load Label Encoder and mean files
            print(
                f'[INFO] loading label encoders and mean files for cross validation {i}...'
            )
            age_le = pickle.loads(
                open(deploy.AGE_LABEL_ENCODERS[i], 'rb').read())
            gender_le = pickle.loads(
                open(deploy.GENDER_LABEL_ENCODERS[i], 'rb').read())
            age_means = json.loads(open(deploy.AGE_MEANS[i]).read())
            gender_means = json.loads(open(deploy.GENDER_MEANS[i]).read())

            # initialize image preprocessors
            age_mp = MeanPreprocessor(age_means['R'], age_means['G'],
                                      age_means['B'])
            gender_mp = MeanPreprocessor(gender_means['R'], gender_means['G'],
                                         gender_means['B'])

            age_preds, gender_preds = predict(face, sp, age_mp, gender_mp, cp,
                                              iap, deploy.AGE_NETWORK_PATHS[i],
                                              deploy.GENDER_NETWORK_PATHS[i],
                                              age_le, gender_le)
            # age_preds_cross.append(age_pred)
            # gender_preds_cross.append(gender_pred)

            # age_preds, gender_preds = np.mean(age_preds_cross, axis = 0), np.mean(gender_preds_cross, axis = 0)

        clone = AgeGenderHelper.visualize_video(age_preds, gender_preds,
                                                age_le, gender_le, clone,
                                                (x, y))

    # path = image_path.split('.')
    # pred_path = '.'.join([f'{path[0]}_predict', path[1]])
    # pred_filename = pred_path.split(os.path.sep)[-1]
    pred_path = '.'.join([f"{image_path.split('.')[0]}_1", 'jpg'])
    cv2.imwrite(pred_path, clone)
    # image_url = uploader.upload(pred_path)
    gc.collect()
    K.clear_session()

    return render_template('index.html',
                           filename=pred_path.split(os.path.sep)[-1])
Exemple #26
0
def register_yourself(student_id, frame_num, image_num, id_idx):

    try:
        os.makedirs(DATA_PATH)
    except:
        pass

    #Loading the stored face encodings and corresponding IDs
    try:
        with open(os.path.join(STORAGE_PATH, "known_face_ids.pickle"),
                  "rb") as fp:
            known_face_ids = pickle.load(fp)
        with open(os.path.join(STORAGE_PATH, "known_face_encodings.pickle"),
                  "rb") as fp:
            known_face_encodings = pickle.load(fp)
    except:
        known_face_encodings = []
        known_face_ids = []

    mpl.rcParams['toolbar'] = 'None'

    print("[INFO] Loading Face Detector")
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(LANDMARK_PATH)
    fa = FaceAligner(predictor, desiredFaceWidth=96)

    print("[INFO] Initializing Video stream")
    vs = cv2.VideoCapture(0)  #,cv2.CAP_DSHOW)

    #Uncommnet below to create the folder for the images
    '''
    IMAGE_PATH = os.path.join(DATA_PATH, student_id)
    try:
        os.makedirs(IMAGE_PATH)
    except:
        pass
    '''
    #Entry time
    tin = time.time()

    #frame = vs.read()
    #fig = plt.figure()
    #plot = plt.subplot(1,1,1)
    #plt.title("Detecting Face")
    #plt.axis('off')
    #im1 = plot.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

    while image_num < 10:  # Take 10 images

        _, frame = vs.read()
        frame_num += 1

        #Resize each image
        #frame = cv2.resize(frame ,(600,600))

        #Applying face enhancement steps
        frame = imageEnhancement.adjust_gamma(frame, gamma=1.5)
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        #Detecting faces in the frame
        faces = detector(gray_frame, 0)

        for face in faces:

            if face is None:
                print("face is none")
                continue

            #Capture the face and align it using the face aligner
            (x, y, w, h) = rect_to_bb(face)
            face_aligned = fa.align(frame, gray_frame, face)
            face_aligned = cv2.resize(face_aligned, (600, 600))

            # @params the initial point of the rectangle will be x,y and
            # @params end point will be x+width and y+height
            # @params along with color of the rectangle
            # @params thickness of the rectangle
            #Put a bounding box over detected face
            frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0),
                                  1)

            #cv2.imshow("Image Captured",frame)
            #cv2.waitKey(50)

        #plt.ion()
        #im1.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        #plt.pause(0.001)
        #plt.show()

        if (frame_num % 30 == 0):

            #Uncommnet the line below to store the face images
            #cv2.imwrite(IMAGE_PATH + "/{}_".format(student_id) + str(j) + ".jpg", face_aligned)

            #Appending the face encodings and corresponding IDs
            try:
                known_face_encodings.append(
                    face_recognition.face_encodings(frame)[0])
                known_face_ids.append(student_id)
            except:
                continue
            image_num += 1

        #OpenCV's implementation to show an image in window(doesn't work on production server)
        #cv2.imshow("Capturing Images for registration (PRESS Q TO QUIT",frame)

        #Encoding the frame to be stream into browser
        frame = cv2.imencode('.jpg', frame)[1].tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

        #if(cv2.waitKey(1) == ord("q")):
        #    break

    #Storing the face encodings and corresponding IDs to disk
    with open(os.path.join(STORAGE_PATH, "known_face_ids.pickle"), "wb") as fp:
        pickle.dump(known_face_ids, fp)
    with open(os.path.join(STORAGE_PATH, "known_face_encodings.pickle"),
              "wb") as fp:
        pickle.dump(known_face_encodings, fp)

    #Noting the number of pictures already captured and storing the index
    id_idx[student_id] = image_num
    with open(os.path.join(STORAGE_PATH, "id_idx.json"), "w") as outfile:
        json.dump(id_idx, outfile)

    #Exit time
    tout = time.time()
    print(tout - tin)

    #plt.close()

    #Releasing the videostream
    vs.release()
    cv2.destroyAllWindows()
    return True
Exemple #27
0
def analyze(img):
    outputArray = []
    detector = dlib.get_frontal_face_detector()
    predictor_path = 'shape_predictor_68_face_landmarks.dat'
    predictor = dlib.shape_predictor(predictor_path)

    image = cv2.imread(img)
    image = imutils.resize(image, width=500)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    rects = detector(gray, 1)

    for (i, rect) in enumerate(rects):
        # determine the facial landmarks for the face region, then
        # convert the facial landmark (x, y)-coordinates to a NumPy
        # array
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)

        # convert dlib's rectangle to a OpenCV-style bounding box
        # [i.e., (x, y, w, h)], then draw the face bounding box
        (x, y, w, h) = face_utils.rect_to_bb(rect)
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # show the face number
        cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

        # loop over the (x, y)-coordinates for the facial landmarks
        # and draw them on the image
        index = 1
        coordsArray = []
        for (x, y) in shape:
            # print("Point: " + str(index) + "   " +
            #      "x: " + str(x) + " ,y: " + str(y))
            index += 1
            coords = tuple([x, y])
            coordsArray.append(coords)

            cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

        distances = [((x1 - x2)**2 + (y1 - y2)**2)**0.5
                     for (x1, y1) in coordsArray for (x2, y2) in coordsArray]
        sum_of_distances = sum(x1 / x2 for x1 in distances for x2 in distances
                               if x1 != x2 and x2 != 0)
        point_37 = coordsArray[36]
        point_40 = coordsArray[39]
        left_eye_length = ((point_37[0] - point_40[0])**2 +
                           (point_37[1] - point_40[1])**2)**0.5
        print(f"Left eye length is: {left_eye_length}")

        right_eye_length = calc_distance(coordsArray, 43, 46)
        eye_distance = calc_distance(coordsArray, 40, 43)
        nose_length = calc_distance(coordsArray, 28, 34)
        left_eyebrow = calc_distance(coordsArray, 18, 22)
        right_eyebrow = calc_distance(coordsArray, 23, 27)
        mouth_length = calc_distance(coordsArray, 49, 55)

        print(f"Right eye length is: {right_eye_length}")
        print(f"Distance between eyes: {eye_distance}")
        print(f"Nose length is: {nose_length}")
        print(f"Left eyebrow is: {left_eyebrow}")
        print(f"Right eyebrow is: {right_eyebrow}")
        print(f"Nose length is: {nose_length}")
        print(f"Mouth length is: {mouth_length}")

        score = sum_of_distances / 1000000
        print(score)

        outputArray.append(score)
        outputArray.append(right_eye_length)
        outputArray.append(left_eye_length)
        outputArray.append(eye_distance)
        outputArray.append(right_eyebrow)
        outputArray.append(left_eyebrow)
        outputArray.append(nose_length)
        outputArray.append(mouth_length)

    # return cv2.imshow("Output", image)
    print(outputArray)
    image = Image.fromarray(image)
    buff = BytesIO()
    image.save(buff, format="JPEG")
    img_str = base64.b64encode(buff.getvalue())

    if not outputArray:
        return "Could not process image"
    else:
        return json.dumps({'photo': str(img_str), 'data': outputArray})
                if len(rects) == 1:
                    face_rect = rects[0]
                    force_detect_face = False
            # we have got an face rect
            if not force_detect_face:
                # detect the face and the landmarks
                shape_array = face_utils.shape_to_np(
                    predictor(image, face_rect))  # ndarray (68, 2)
                lip_array = shape_array[48:68]  # mouth index [48: 68)
                # get the bounds of landmarks
                (shape_x, shape_y, shape_w,
                 shape_h) = cv2.boundingRect(shape_array)  # new face region
                (mouth_x, mouth_y, mouth_w,
                 mouth_h) = cv2.boundingRect(lip_array)  # mouth region
                (face_x, face_y, face_w,
                 face_h) = face_utils.rect_to_bb(face_rect)  # last face region

                # first use gap_feature to check is the user is speaking
                # gap_distance: mean distance between lip inner points
                gap_distance = (
                    math.hypot(lip_array[13][0] - lip_array[19][0],
                               lip_array[13][1] - lip_array[19][1]) +
                    math.hypot(lip_array[14][0] - lip_array[18][0],
                               lip_array[14][1] - lip_array[18][1]) +
                    math.hypot(lip_array[15][0] - lip_array[17][0],
                               lip_array[15][1] - lip_array[17][1])) / 3
                # width_distance: mean distance between lip inner corner points
                width_distance = math.hypot(
                    lip_array[12][0] - lip_array[16][0],
                    lip_array[12][1] - lip_array[16][1])
                # use their ratio to detect if is speaking or not
def bpmrpm():
    count = 0
    rates = []
    video = True

    if video == False:
        cap = cv2.VideoCapture(0)
    else:
        cap = cv2.VideoCapture("video.webm")

    fu = Face_utilities()
    sp = Signal_processing()

    i=0
    last_rects = None
    last_shape = None
    last_age = None
    last_gender = None

    face_detect_on = False
    age_gender_on = False

    t = time.time()

    #for signal_processing
    BUFFER_SIZE = 100

    fps=0 #for real time capture
    video_fps = cap.get(cv2.CAP_PROP_FPS) # for video capture
    #print(video_fps)

    times = []
    data_buffer = []

    # data for plotting
    filtered_data = []

    fft_of_interest = []
    freqs_of_interest = []

    bpm = 0

    #plotting
    app = QtGui.QApplication([])

    win = pg.GraphicsWindow(title="plotting")
    p1 = win.addPlot(title="FFT")
    p2 = win.addPlot(title ="Signal")
    win.resize(1200,600)

    def update():
        p1.clear()
        p1.plot(np.column_stack((freqs_of_interest,fft_of_interest)), pen = 'g')

        p2.clear()
        p2.plot(filtered_data[20:],pen='g')
        app.processEvents()

    timer = QtCore.QTimer()
    timer.timeout.connect(update)
    timer.start(300)

    while True:
        # grab a frame -> face detection -> crop the face -> 68 facial landmarks -> get mask from those landmarks

        # calculate time for each loop
        t0 = time.time()

        if(i%1==0):
            face_detect_on = True
            if(i%10==0):
                age_gender_on = True
            else:
                age_gender_on = False
        else:
            face_detect_on = False

        ret, frame = cap.read()
        #frame_copy = frame.copy()

        if frame is None:
            #print("End of video")
            cv2.destroyAllWindows()
            timer.stop()
            #sys.exit()
            break

        #display_frame, aligned_face = flow_process(frame)


        ret_process = fu.no_age_gender_face_process(frame, "68")

        if ret_process is None:
            cv2.putText(frame, "No face detected", (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),2)
            cv2.imshow("frame",frame)
            #print(time.time()-t0)

            cv2.destroyWindow("face")
            if cv2.waitKey(1) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                timer.stop()
                #sys.exit()
                break
            continue

        rects, face, shape, aligned_face, aligned_shape = ret_process

        (x, y, w, h) = face_utils.rect_to_bb(rects[0])
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)

        #overlay_text = "%s, %s" % (gender, age)
        #cv2.putText(frame, overlay_text ,(x,y-15), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,0,0),2,cv2.LINE_AA)

        if(len(aligned_shape)==68):
            cv2.rectangle(aligned_face,(aligned_shape[54][0], aligned_shape[29][1]), #draw rectangle on right and left cheeks
                    (aligned_shape[12][0],aligned_shape[33][1]), (0,255,0), 0)
            cv2.rectangle(aligned_face, (aligned_shape[4][0], aligned_shape[29][1]),
                    (aligned_shape[48][0],aligned_shape[33][1]), (0,255,0), 0)
        else:
            #print(shape[4][1])
            #print(shape[2][1])
            #print(int((shape[4][1] - shape[2][1])))
            cv2.rectangle(aligned_face, (aligned_shape[0][0],int((aligned_shape[4][1] + aligned_shape[2][1])/2)),
                        (aligned_shape[1][0],aligned_shape[4][1]), (0,255,0), 0)

            cv2.rectangle(aligned_face, (aligned_shape[2][0],int((aligned_shape[4][1] + aligned_shape[2][1])/2)),
                        (aligned_shape[3][0],aligned_shape[4][1]), (0,255,0), 0)

        for (x, y) in aligned_shape:
            cv2.circle(aligned_face, (x, y), 1, (0, 0, 255), -1)


        #for signal_processing
        ROIs = fu.ROI_extraction(aligned_face, aligned_shape)
        green_val = sp.extract_color(ROIs)
        #print(green_val)

        data_buffer.append(green_val)

        if(video==False):
            times.append(time.time() - t)
        else:
            times.append((1.0/video_fps)*i)

        L = len(data_buffer)
        #print("buffer length: " + str(L))

        if L > BUFFER_SIZE:
            data_buffer = data_buffer[-BUFFER_SIZE:]
            times = times[-BUFFER_SIZE:]
            #bpms = bpms[-BUFFER_SIZE//2:]
            L = BUFFER_SIZE
        #print(times)
        if L==100:
            fps = float(L) / (times[-1] - times[0])
            cv2.putText(frame, "fps: {0:.2f}".format(fps), (30,int(frame.shape[0]*0.95)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
            #
            detrended_data = sp.signal_detrending(data_buffer)
            #print(len(detrended_data))
            #print(len(times))
            interpolated_data = sp.interpolation(detrended_data, times)

            normalized_data = sp.normalization(interpolated_data)

            fft_of_interest, freqs_of_interest = sp.fft(normalized_data, fps)

            max_arg = np.argmax(fft_of_interest)
            bpm = freqs_of_interest[max_arg]
            cv2.putText(frame, "HR: {0:.2f}".format(bpm), (int(frame.shape[1]*0.8),int(frame.shape[0]*0.95)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
            #print(detrended_data)
            filtered_data = sp.butter_bandpass_filter(interpolated_data, (bpm-20)/60, (bpm+20)/60, fps, order = 3)
            #print(fps)
            #filtered_data = sp.butter_bandpass_filter(interpolated_data, 0.8, 3, fps, order = 3)

        #write to txt file
        with open("a.txt",mode = "a+") as f:

            rates.append(bpm)
            f.write("time: {0:.4f} ".format(times[-1]) + ", HR: {0:.2f} ".format(bpm) + "\n")

        # display

        #cv2.imshow("mask",mask)
        i = i+1
        #print("time of the loop number "+ str(i) +" : " + str(time.time()-t0))
        count = count + 1


    rates = [i for i in rates if i!=0]
    #print(rates)

    avg = sum(rates)/len(rates)
    resp = avg/4.5
    print("Heart Rate: ",avg)
    print("Respiratory Rate: ", resp)
    l=[]
    l.append(avg)
    l.append(resp)
    return l
def main():
    # return
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("Unable to connect to camera.")
        return
    
    counter = 0
    frame_count = 0
    record = False

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(face_landmark_path)
    fa = face_utils.FaceAligner(predictor, desiredFaceWidth=256)

    while cap.isOpened():

        ret, img = cap.read()
        #flip img
        img = cv2.flip(img, 1)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        res = img.copy()

        if ret:
            #detect faces from image
            face_rects = detector(img, 0)
            
            for i in range(len(face_rects)):
                mask = np.zeros(img.shape[:2], dtype = np.uint8)

                #get face landmarks
                face_rect = face_rects[i]
                shape = predictor(img, face_rect)
                shape = face_utils.shape_to_np(shape)
                reprojectdst, euler_angle = get_head_pose(shape)
                
                #detect hull to crop face
                hull = cv2.convexHull(shape)
                cv2.fillPoly(mask, pts =[hull], color=1)


                #visualize
                cv2.polylines(res,[hull],True,(255, 0, 0))
                
                for (x, y) in shape:
                    cv2.circle(res, (x, y), 1, (0, 255, 0), -1)

                for start, end in line_pairs:
                    cv2.line(res, reprojectdst[start], reprojectdst[end], (0, 0, 255))

                (x, y, w, h) = face_utils.rect_to_bb(face_rect)

                x = np.clip(x, 0, img.shape[1])
                y = np.clip(y, 0, img.shape[0])
                w = np.clip(w, 1, img.shape[1]-x)
                h = np.clip(h, 1, img.shape[0]-y)

                img_masked = cv2.bitwise_and(img, img, mask = mask)

                faceOrig = cv2.resize(img_masked[y:y + h, x:x + w], (256, 256))
                faceAligned = cv2.resize(fa.align(img_masked, gray, face_rect), (img.shape[1], img.shape[0]))

                if record:
                    if frame_count % 2 == 0:
                        path = './face_database/'+person_name+'/'+person_name+str(counter)+'.jpg'
                        counter+=1
                        cv2.imwrite(path, faceAligned)

                res = np.hstack((res, faceAligned))

            frame_count += 1
            cv2.imshow("demo", res)
            key = cv2.waitKey(1) & 0xFF
            if  key == ord('q'):
                break
            elif key == ord('r'):
                if not record:
                    record = True
                    print("recording...")
                else:
                    record = False
                    print("stop recording")
Exemple #31
0
import cv2
import dlib
from imutils import face_utils

detector = dlib.get_frontal_face_detector()
cap = cv2.VideoCapture("udpsrc port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! videoconvert ! appsink sync=false")
while True:
    f, frame = cap.read()
    dets = detector(frame, 1)
    for (i, face) in enumerate(dets):
        (x,y,w,h) = face_utils.rect_to_bb(face)
        cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 2)

    cv2.imshow('cap', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Exemple #32
0
import cv2
import numpy as np
from imutils import face_utils as fu

detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

img = cv2.imread('test1.jpg')
img = imutils.resize(img, width=500)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

rect = detect(gray, 1)  # Face detections in the grayscale img

for i, rct in enumerate(rect):
    shape = predict(gray, rct)
    shape = fu.shape_to_np(shape)

    x, y, w, h = fu.rect_to_bb(rct)
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 1)
    #roiColor = img[y:y +h, x:x + w]
    cv2.putText(img, "Face {}".format(i + 1), (x - 10, y - 10),
                cv2.FONT_HERSHEY_COMPLEX, 0.75, (0, 0, 255), 2)

    for x, y in shape:
        cv2.circle(img, (x, y), 1, (0, 255, 0), -1)

    #cv2.imshow('Crop Face', roiColor)

cv2.imshow('Show Img', img)
cv2.waitKey(0)