def preprocess_output(self, eye_boxes, face_image):
     """
     Scales the eye bounding boxes from the 0..1 range to the size of the face image.
     Returns a tuple of cropped left and right eye images. 
     """
     if face_image is not None and face_image.size > 0 and eye_boxes and eye_boxes[
             0] and eye_boxes[1]:
         left_eye_box = helpers.fit(eye_boxes[0], face_image)
         left_eye = helpers.crop(face_image, left_eye_box)
         right_eye_box = helpers.fit(eye_boxes[1], face_image)
         right_eye = helpers.crop(face_image, right_eye_box)
         return left_eye, right_eye
     else:
         return None, None
def captureHistogram(source=None):
    source = 0 if source is None else source
    if source is not None and str(type(source)) != "<class 'int'>":
        raise ValueError("source: integer value expected")
    cap = cv2.VideoCapture(source)

    boxSize = 100
    centerX = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) / 2)
    centerY = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) / 3)
    padding = boxSize + 10
    while True:
        _, frame = cap.read()
        frame = cv2.flip(frame, 1)

        box1 = (centerY + padding * 0, centerX + padding * 0, boxSize, boxSize)
        box2 = (centerY + padding * 0, centerX + padding * 1, boxSize, boxSize)
        box3 = (centerY + padding * 1, centerX + padding * 0, boxSize, boxSize)
        box4 = (centerY + padding * 1, centerX + padding * 1, boxSize, boxSize)
        box5 = (centerY + padding * 2, centerX + padding * 0, boxSize, boxSize)
        box6 = (centerY + padding * 2, centerX + padding * 1, boxSize, boxSize)

        helpers.drawBoxes(frame, helpers.PURPLE, [box1, box2, box3, box4])

        boxOne = helpers.crop(frame, box1)
        boxTwo = helpers.crop(frame, box2)
        boxThree = helpers.crop(frame, box3)
        boxFour = helpers.crop(frame, box4)

        boxFourBlurred = helpers.blur(boxFour, 15)

        helpers.copyTo(frame, box5, boxFour)
        helpers.copyTo(frame, box6, boxFourBlurred)

        cv2.imshow("Video Feed", frame)
        key = cv2.waitKey(10)
        if key == 97:
            objectColor = finalHistImage
            cv2.destroyAllWindows()
            break
        if key == 27:
            cv2.destroyAllWindows()
            cap.release()
            exit()

    hsvObjectColor = cv2.cvtColor(objectColor, cv2.COLOR_BGR2HSV)
    objectHist = cv2.calcHist([hsvObjectColor], [0, 1], None, [12, 15],
                              [0, 180, 0, 256])
    cv2.normalize(objectHist, objectHist, 0, 255, cv2.NORM_MINMAX)
    return objectHist
Esempio n. 3
0
def recognize_gesture(image, handDetector, signDetector, label_encoder):

    #find hand
    #please play with the hyperparameters to find a balance between accuracy and runtime
    detected_box = sliding_window(handDetector, image, 50, .9)
    #crop image around hand
    croppedImage = helpers.crop(image, int(detected_box[0]),
                                int(detected_box[2]), int(detected_box[1]),
                                int(detected_box[3]))

    #get HOG of hand
    hogvec = helpers.convertToGrayToHOG(croppedImage)
    #use our trained model to get the probability of each class
    prediction_probs = signDetector.predict_proba([hogvec])[0]
    #get predicted class based on predicted probabilities
    prediction = label_encoder.inverse_transform(
        prediction_probs.tolist().index(max(prediction_probs)))

    return detected_box, prediction_probs, prediction


#handDetector = helpers.loadClassifier('handDetector.pkl')
#
#signDetector = helpers.loadClassifier('signDetector.pkl')
#
#label_encoder = LabelEncoder().fit(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N',
#       'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'])
#
#image = helpers.io.imread('Dataset/user_3/S2.jpg')
#
#loc, probs, pred = recognize_gesture(image, handDetector, signDetector, label_encoder)
#
#print(probs)
Esempio n. 4
0
def captureHistogram(source=None):
    source = 0 if source is None else source
    if source is not None and str(type(source)) != "<class 'int'>":
        raise ValueError("source: integer value expected")
    cap = cv2.VideoCapture(source)

    boxSize = 30
    centerX = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) / 2)
    centerY = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) / 2)
    while True:
        _, frame = cap.read()
        frame = cv2.flip(frame, 1)
        cv2.putText(frame,
                    "Place region of the hand inside the boxes and press `A`",
                    (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1,
                    cv2.LINE_AA)

        box1 = (centerX + 0, centerY + 0, boxSize, boxSize)
        box2 = (centerX + 50, centerY + 0, boxSize, boxSize)
        box3 = (centerX + 0, centerY + 50, boxSize, boxSize)
        box4 = (centerX + 50, centerY + 50, boxSize, boxSize)

        helpers.drawBoxes(frame, helpers.PURPLE, [box1, box2, box3, box4])

        boxOne = helpers.crop(frame, box1)
        boxTwo = helpers.crop(frame, box2)
        boxThree = helpers.crop(frame, box3)
        boxFour = helpers.crop(frame, box4)

        finalHistImage = np.hstack((boxOne, boxTwo, boxThree, boxFour))
        cv2.imshow("Video Feed", frame)
        key = cv2.waitKey(10)
        if key == 97:
            objectColor = finalHistImage
            cv2.destroyAllWindows()
            break
        if key == 27:
            cv2.destroyAllWindows()
            cap.release()
            exit()

    hsvObjectColor = cv2.cvtColor(objectColor, cv2.COLOR_BGR2HSV)
    objectHist = cv2.calcHist([hsvObjectColor], [0, 1], None, [12, 15],
                              [0, 180, 0, 256])
    cv2.normalize(objectHist, objectHist, 0, 255, cv2.NORM_MINMAX)
    return objectHist
Esempio n. 5
0
 def preprocess_output(self, face_box, frame):
     """
     Scales the face bounding box from the range 0..1 to the size of the frame
     and extracts the face image. Returns a tuple containing the face image and
     the scaled bounding box.
     """
     if face_box is None or frame is None or frame.size < 1:
         return None, None
     else:
         face_box = helpers.fit(face_box, frame)
         face_img = helpers.crop(frame, face_box)
         return face_img, face_box
Esempio n. 6
0
def buildhandnothand_lis(frame, imgset):
    poslis = []
    neglis = []

    for nameimg in frame.image:
        tupl = frame[frame['image'] == nameimg].values[0]
        x_tl = tupl[1]
        y_tl = tupl[2]
        side = tupl[5]
        conf = 0

        dic = [0, 0]

        arg1 = [x_tl, y_tl, conf, side, side]
        poslis.append(
            helpers.convertToGrayToHOG(
                helpers.crop(imgset['Dataset/' + nameimg], x_tl, x_tl + side,
                             y_tl, y_tl + side)))
        while dic[0] <= 1 or dic[1] < 1:
            x = random.randint(0, 320 - side)
            y = random.randint(0, 240 - side)
            crp = helpers.crop(imgset['Dataset/' + nameimg], x, x + side, y,
                               y + side)
            hogv = helpers.convertToGrayToHOG(crp)
            arg2 = [x, y, conf, side, side]

            z = overlapping_area(arg1, arg2)
            if dic[0] <= 1 and z <= 0.5:
                neglis.append(hogv)
                dic[0] += 1
            if dic[0] == 1:
                break
    label_1 = [1 for i in range(0, len(poslis))]
    label_0 = [0 for i in range(0, len(neglis))]
    label_1.extend(label_0)
    poslis.extend(neglis)
    return poslis, label_1
Esempio n. 7
0
def get_data(user_list, img_dict, data_directory):
    X = []
    Y = []

    for user in user_list:
        user_images = glob.glob(data_directory + user + '/*.jpg')

        boundingbox_df = pd.read_csv(data_directory + user + '/' + user +
                                     '_loc.csv')

        for rows in boundingbox_df.iterrows():
            cropped_img = helpers.crop(
                img_dict[data_directory + rows[1]['image']],
                rows[1]['top_left_x'], rows[1]['bottom_right_x'],
                rows[1]['top_left_y'], rows[1]['bottom_right_y'])
            hogvector = helpers.convertToGrayToHOG(cropped_img)
            X.append(hogvector.tolist())
            Y.append(rows[1]['image'].split('/')[1][0])
    return X, Y
Esempio n. 8
0
def cacheSteps(imgset, frame, step_x, step_y):
    # print "Cache-ing steps"
    list_dic_of_hogs = []
    dic = {}
    i = 0
    for img in frame.image:
        tupl = frame[frame['image'] == img].values[0]
        x_tl = tupl[1]
        y_tl = tupl[2]
        side = tupl[5]
        conf = 0
        i += 1
        if i % 10 == 0:
            print(i, " images cached")
        image = imgset['Dataset/' + img]
        for x in range(0, 320 - side, step_x):
            for y in range(0, 240 - side, step_y):
                dic[str(img + str(x) + str(y))] = helpers.convertToGrayToHOG(
                    helpers.crop(image, x, x + side, y, y + side))
    return dic
Esempio n. 9
0
            gazeEstimator.feed_input(left_eye, right_eye, head_pose_angles)

        # get a new head pose if available
        head_pose_consumed, head_pose_angles = headPoseEstimator.consume_output(
            wait_needed)
        if head_pose_consumed:
            q[head_poses_produced][3] = head_pose_angles
            head_poses_produced += 1

        # get a new pair of eye images if available
        eyes_consumed, eye_boxes = eyeDetector.consume_output(wait_needed)
        if eyes_consumed:
            frame, face_box, _, _ = q[
                eyes_produced]  # the corresponding frame and the face bounding box for this detection
            eyes = eyeDetector.preprocess_output(eye_boxes,
                                                 face_image=helpers.crop(
                                                     frame, face_box))
            q[eyes_produced][2] = eyes
            eyes_produced += 1

        # get a new face and the bounding box if available
        face_consumed, face_box = faceDetector.consume_output(
            confidence=args.confidence, wait=wait_needed)
        if face_consumed:
            face_img, face_box = faceDetector.preprocess_output(
                face_box, frame=q[faces_produced][0])
            q[faces_produced][1] = face_box
            faces_produced += 1
            eyeDetector.feed_input(face_img)
            headPoseEstimator.feed_input(face_img)

        frame = feed.read_next()  # get the next frame from the input feed
Esempio n. 10
0
 def crop(self):
     size_list = [(128, 128), (256, 256), (512, 512), (1024, 1024), (128, 0), (256, 0), (512, 0), (1024, 0)]
     for size in size_list:
         helpers.crop(self.path, size)
Esempio n. 11
0
 def crop(self):
     size_list = [(128, 128), (256, 256), (512, 512), (1024, 1024),
                  (128, 0), (256, 0), (512, 0), (1024, 0)]
     for size in size_list:
         helpers.crop(self.path, size)