예제 #1
0
def draw(photo,initital_time):
    image = photo.copy()
    image_to_show = image
    global time_to_next
    global current_time
    global previous_time
    global duration
    eyes = detect_objects(image, eyeCascadeClassifier)
    ## Select the rightmost eye ##
    rightmost_eye = None
    if eyes is not None:
        for eye in eyes:
            (x,y) = eye[0], eye[1]
            (x2,y2) = (eye[0] + eye[2]), (eye[1] + eye[3])
            if (rightmost_eye is None) or (x < rightmost_eye.x):
                rightmost_eye = Eye(x, y, x2, y2)

    ####### Extract the image of the rightmost eye ################
    eye_image = None
    if rightmost_eye is not None:
        eye_image = image[rightmost_eye.y:rightmost_eye.y2, rightmost_eye.x:rightmost_eye.x2]


    # edge detection
    binary_eye_image = None
    if eye_image is not None:
        eye_histogram = [0]*256
        eye_image = cv2.cvtColor(eye_image, cv2.COLOR_RGB2GRAY)
        for i in xrange(256):
            value_count = (eye_image == i).sum()
            eye_histogram[i] = value_count
        count = 0
        index = 255
        while count < (eye_image.size*3/4):
            count += eye_histogram[index]
            index -= 1
        quarter_threshold = index
        #Multiply all parts of eye above bottom 1/4 brightness by 0
        binary_eye_image = cv2.equalizeHist((eye_image < quarter_threshold) * eye_image)

    #Look for circle (the iris)
    relative_iris_coordinates = None
    if binary_eye_image is not None:
        eye_circles = cv2.HoughCircles(binary_eye_image, cv2.cv.CV_HOUGH_GRADIENT, 3, 500, maxRadius = binary_eye_image.shape[0]/5)
        if eye_circles is not None:
            #Usually gets the job done. Messy.
            circle = eye_circles[0][0]
            relative_iris_coordinates = (circle[0], circle[1])

    ######### Put dot on eye in big picture ##################
    absolute_iris_coordinates = None
    if relative_iris_coordinates is not None and rightmost_eye is not None:
        absolute_iris_coordinates = (int(relative_iris_coordinates[0]+rightmost_eye.x), int(relative_iris_coordinates[1]+rightmost_eye.y))
        cv2.circle(image_to_show, absolute_iris_coordinates, 5, (0,0,255), thickness=10)
    current_time = int(time.clock())-int(initital_time)
    #print "Current Time: {0}".format(current_time)
    #print "Previous Time: {0}".format(previous_time)
    if current_time != previous_time:
        time_to_next += 1
        if duration == time_to_next:
            if absolute_iris_coordinates is not None:
                print absolute_iris_coordinates[0]
            '''if absolute_iris_coordinates is not None:
                if absolute_iris_coordinates[0]<=250:
                    print "LEFT"
                elif absolute_iris_coordinates[0]>=300:
                    print "RIGHT"
                else:
                    print "MIDDLE"
            '''

            #else:
                #print "STOP"
            time_to_next = 0
        else:
            previous_time = current_time
    else:
        previous_time = current_time
    if eyes is None:
        print "Eye Not found"
    # Extract image of iris
    iris_image = None
    if absolute_iris_coordinates is not None:
        x = absolute_iris_coordinates[0]
        y = absolute_iris_coordinates[1]
        #Change the value depending upon the size of the iris
        w = 40
        h = 40
        iris_image = photo[y-h:y+h,x-w:x+w]
        iris_image_to_show = cv2.resize(iris_image, (iris_image.shape[1]*2, iris_image.shape[0]*2))
        image_to_show[0:iris_image_to_show.shape[0], 00:00+iris_image_to_show.shape[1]] = iris_image_to_show


    ### Draw red circle around iris. Draw green around pupil
    iris_picture = None
    if iris_image is not None:
        iris_gray = cv2.cvtColor(iris_image, cv2.COLOR_RGB2GRAY)
        iris_circles_image = iris_image.copy()
        iris_circles = cv2.HoughCircles(iris_gray, cv2.cv.CV_HOUGH_GRADIENT, 2, 100)
        if iris_circles is not None:
            circle=iris_circles[0][0]
            cv2.circle(iris_circles_image, (circle[0], circle[1]), circle[2], (0,0,255), thickness=2)
        pupil_coords = pupil_detect.find_pupil(iris_gray)
        if pupil_coords is not None:
            cv2.circle(iris_circles_image, pupil_coords[:2], pupil_coords[2], (0,255,0),4)
        if iris_circles is not None and pupil_coords is not None:
            ic = iris_circles[0][0]
            pc = pupil_coords
            #Check if pupil is within iris
            if abs(ic[0]-pc[0])<ic[2] and abs(ic[1]-pc[1])<ic[2] and pc[2]<ic[2]:
                iris_picture = iris_circles_image
        iris_circles_to_show = cv2.resize(iris_circles_image, (iris_circles_image.shape[1]*2,iris_circles_image.shape[0]*2))
        image_to_show[0:iris_circles_to_show.shape[0], 200:200+iris_circles_to_show.shape[1]] = iris_circles_to_show

    cv2.imshow('Image', image_to_show)
    if cv2.waitKey(10) > 0: #If we got a key press in less than 10ms
        #Also, cv2 has some weirdness where the image won't update without a waitKey()
        return True, iris_picture
    return False, iris_picture
예제 #2
0
def draw(photo):
    image = photo.copy()
    image_to_show = image
    eyes = detect_objects(image, olho_cascade)
    #################### Select the rightmost eye ##################
    rightmost_eye = None
    if eyes is not None:
        for eye in eyes:
            (x, y) = eye[0], eye[1]
            (x2, y2) = (eye[0] + eye[2]), (eye[1] + eye[3])
            if (rightmost_eye is None) or (x < rightmost_eye.x):
                rightmost_eye = Eye(x, y, x2, y2)
    ###############################################################
    ####### Extract the image of the rightmost eye ################
    eye_image = None
    if rightmost_eye is not None:
        eye_image = image[rightmost_eye.y:rightmost_eye.y2,
                          rightmost_eye.x:rightmost_eye.x2]
    ###############################################################
    ### To assist in edge detection, try to "black out" sclera ####
    binary_eye_image = None
    if eye_image is not None:
        eye_histogram = [0] * 256
        eye_image = cv2.cvtColor(eye_image, cv2.COLOR_RGB2GRAY)
        for i in range(256):
            value_count = (eye_image == i).sum()
            eye_histogram[i] = value_count
        count = 0
        index = 255
        while count < (eye_image.size * 3 / 4):
            count += eye_histogram[index]
            index -= 1
        quarter_threshold = index
        #Multiply all parts of eye above bottom 1/4 brightness by 0
        #Might not work on people with light irises. Have not tried.
        binary_eye_image = cv2.equalizeHist(
            (eye_image < quarter_threshold) * eye_image)
    ###############################################################
    ####### Look for circle (the iris). Save coordinates. ########
    relative_iris_coordinates = None
    if binary_eye_image is not None:
        eye_circles = cv2.HoughCircles(binary_eye_image,
                                       cv2.HOUGH_GRADIENT,
                                       3,
                                       500,
                                       maxRadius=40)
        if eye_circles is not None:
            #Usually gets the job done. Messy.
            circle = eye_circles[0][0]
            relative_iris_coordinates = (circle[0], circle[1])
    ###############################################################
    ######### Put blue dot on eye in big picture ##################
    absolute_iris_coordinates = None
    if relative_iris_coordinates is not None and rightmost_eye is not None:
        absolute_iris_coordinates = (int(relative_iris_coordinates[0] +
                                         rightmost_eye.x),
                                     int(relative_iris_coordinates[1] +
                                         rightmost_eye.y))
        cv2.circle(image_to_show,
                   absolute_iris_coordinates,
                   5, (255, 0, 0),
                   thickness=10)
    ###############################################################
    ####### Extract image of iris (and some surrounding) ##########
    iris_image = None
    if absolute_iris_coordinates is not None:
        x = absolute_iris_coordinates[0]
        y = absolute_iris_coordinates[1]
        #Should find a way to make these numbers adaptable.
        #It really messes things up when these arbitrary numbers don't work with
        #whatever image size or eye distance is actually being used.
        #YOU, DEAR READER, MUST CHANGE THESE FOR YOUR CAMERA
        #These numbers describe the guestimated shape of a captured image of an iris.
        #This program makes no effort to automatically find the size of the iris in
        #the captured image of the iris
        w = 60
        h = 60
        iris_image = photo[y - h:y + h, x - w:x + w]
        iris_image_to_show = cv2.resize(
            iris_image, (iris_image.shape[1] * 2, iris_image.shape[0] * 2))
        image_to_show[0:iris_image_to_show.shape[0],
                      00:00 + iris_image_to_show.shape[1]] = iris_image_to_show
    ###############################################################
    ### Draw blue circle around iris. Draw green around pupil #####
    #Also, if the pupil and iris seem to be sensible shapes/sizes,
    #return the current iris_image (picture of the iris+surroundings)
    iris_picture = None
    if iris_image is not None:
        iris_gray = cv2.cvtColor(iris_image, cv2.COLOR_RGB2GRAY)
        iris_circles_image = iris_image.copy()
        iris_circles = cv2.HoughCircles(iris_gray, cv2.HOUGH_GRADIENT, 2, 100)
        if iris_circles is not None:
            circle = iris_circles[0][0]
            cv2.circle(iris_circles_image, (circle[0], circle[1]),
                       circle[2], (255, 0, 0),
                       thickness=2)
        pupil_coords = pupil_detect.find_pupil(iris_gray)
        if pupil_coords is not None:
            cv2.circle(iris_circles_image, pupil_coords[:2], pupil_coords[2],
                       (0, 255, 0), 4)
        if iris_circles is not None and pupil_coords is not None:
            ic = iris_circles[0][0]
            pc = pupil_coords
            #Check if pupil is within iris
            if abs(ic[0] - pc[0]) < ic[2] and abs(
                    ic[1] - pc[1]) < ic[2] and pc[2] < ic[2]:
                iris_picture = iris_circles_image
        iris_circles_to_show = cv2.resize(
            iris_circles_image,
            (iris_circles_image.shape[1] * 2, iris_circles_image.shape[0] * 2))
        image_to_show[0:iris_circles_to_show.shape[0], 200:200 +
                      iris_circles_to_show.shape[1]] = iris_circles_to_show
    ###############################################################
    cv2.imshow('Video de captura', image_to_show)
    if (cv2.waitKey(1)
            & 0xFF == ord('q')):  #If we got a key press in less than 10ms
        #Also, cv2 has some weirdness where the image won't update without a waitKey()
        return True
    return False
예제 #3
0
def draw(photo):
    image = photo.copy()
    image_to_show = image
    eyes = detect_objects(image, eyeCascadeClassifier)
    #################### Select the rightmost eye ##################
    rightmost_eye = None
    if eyes is not None:
        for eye in eyes:
            (x,y) = eye[0], eye[1]
            (x2,y2) = (eye[0] + eye[2]), (eye[1] + eye[3])
            if (rightmost_eye is None) or (x < rightmost_eye.x):
                rightmost_eye = Eye(x, y, x2, y2)
    ###############################################################
    ####### Extract the image of the rightmost eye ################
    eye_image = None
    if rightmost_eye is not None:
        eye_image = image[rightmost_eye.y:rightmost_eye.y2, rightmost_eye.x:rightmost_eye.x2]
    ###############################################################
    ### To assist in edge detection, try to "black out" sclera ####
    binary_eye_image = None
    if eye_image is not None:
        eye_histogram = [0]*256
        eye_image = cv2.cvtColor(eye_image, cv2.COLOR_RGB2GRAY)
        for i in xrange(256):
            value_count = (eye_image == i).sum()
            eye_histogram[i] = value_count
        count = 0
        index = 255
        while count < (eye_image.size*3/4):
            count += eye_histogram[index]
            index -= 1
        quarter_threshold = index
        #Multiply all parts of eye above bottom 1/4 brightness by 0
        #Might not work on people with light irises. Have not tried.
        binary_eye_image = cv2.equalizeHist((eye_image < quarter_threshold) * eye_image)
    ###############################################################
    ####### Look for circle (the iris). Save coordinates. ########
    relative_iris_coordinates = None
    if binary_eye_image is not None:
        eye_circles = cv2.HoughCircles(binary_eye_image, cv2.HOUGH_GRADIENT, 3, 500, maxRadius = binary_eye_image.shape[0]/5)
        if eye_circles is not None:
            #Usually gets the job done. Messy.
            circle = eye_circles[0][0]
            relative_iris_coordinates = (circle[0], circle[1])
    ###############################################################
    ######### Put blue dot on eye in big picture ##################
    absolute_iris_coordinates = None
    if relative_iris_coordinates is not None and rightmost_eye is not None:
        absolute_iris_coordinates = (int(relative_iris_coordinates[0]+rightmost_eye.x), int(relative_iris_coordinates[1]+rightmost_eye.y))
        cv2.circle(image_to_show, absolute_iris_coordinates, 5, (255,0,0), thickness=10)
    ###############################################################
    ####### Extract image of iris (and some surrounding) ##########
    iris_image = None
    if absolute_iris_coordinates is not None:
        x = absolute_iris_coordinates[0]
        y = absolute_iris_coordinates[1]
        #Should find a way to make these numbers adaptable.
        #It really messes things up when these arbitrary numbers don't work with
        #whatever image size or eye distance is actually being used.
        #YOU, DEAR READER, MUST CHANGE THESE FOR YOUR CAMERA
        #These numbers describe the guestimated shape of a captured image of an iris.
        #This program makes no effort to automatically find the size of the iris in
        #the captured image of the iris
        w = 60
        h = 60
        iris_image = photo[y-h:y+h,x-w:x+w]
        iris_image_to_show = cv2.resize(iris_image, (iris_image.shape[1]*2, iris_image.shape[0]*2))
        image_to_show[0:iris_image_to_show.shape[0], 00:00+iris_image_to_show.shape[1]] = iris_image_to_show
    ###############################################################
    ### Draw blue circle around iris. Draw green around pupil #####
    #Also, if the pupil and iris seem to be sensible shapes/sizes,
    #return the current iris_image (picture of the iris+surroundings)
    iris_picture = None
    if iris_image is not None:
        iris_gray = cv2.cvtColor(iris_image, cv2.COLOR_RGB2GRAY)
        iris_circles_image = iris_image.copy()
        iris_circles = cv2.HoughCircles(iris_gray, cv2.HOUGH_GRADIENT, 2, 100)
        if iris_circles is not None:
            circle=iris_circles[0][0]
            cv2.circle(iris_circles_image, (circle[0], circle[1]), circle[2], (255,0,0), thickness=2)
        pupil_coords = pupil_detect.find_pupil(iris_gray)
        if pupil_coords is not None:
            cv2.circle(iris_circles_image, pupil_coords[:2], pupil_coords[2], (0,255,0),4)
        if iris_circles is not None and pupil_coords is not None:
            ic = iris_circles[0][0]
            pc = pupil_coords
            #Check if pupil is within iris
            if abs(ic[0]-pc[0])<ic[2] and abs(ic[1]-pc[1])<ic[2] and pc[2]<ic[2]:
                iris_picture = iris_circles_image
        iris_circles_to_show = cv2.resize(iris_circles_image, (iris_circles_image.shape[1]*2,iris_circles_image.shape[0]*2))
        image_to_show[0:iris_circles_to_show.shape[0], 200:200+iris_circles_to_show.shape[1]] = iris_circles_to_show
    ###############################################################
    cv2.imshow('Image', image_to_show)
    if cv2.waitKey(10) > 0: #If we got a key press in less than 10ms
        #Also, cv2 has some weirdness where the image won't update without a waitKey()
        return True, iris_picture
    return False, iris_picture