Ejemplo n.º 1
0
def mainForTraining():
    import pygamestuff
    crosshair = pygamestuff.Crosshair([7, 2], quadratic=False)
    vc = cv2.VideoCapture(0)  # Initialize the default camera
    if vc.isOpened():  # try to get the first frame
        (readSuccessful, frame) = vc.read()
    else:
        print("Could not open camera.")
        return

    MAX_SAMPLES_TO_RECORD = 999999
    recordedEvents = 0
    HT = None
    while readSuccessful and recordedEvents < MAX_SAMPLES_TO_RECORD and not crosshair.userWantsToQuit:
        pupilOffsetXYList = getOffset(frame, allowDebugDisplay=False)
        if pupilOffsetXYList is not None:  #If we got eyes, check for a click. Else, wait until we do.
            if crosshair.pollForClick():
                crosshair.clearEvents()
                #print( (xOffset,yOffset) )
                #do learning here, to relate xOffset and yOffset to screenX,screenY
                crosshair.record(pupilOffsetXYList)
                print "recorded something"
                crosshair.remove()
                recordedEvents += 1
                if recordedEvents > RANSAC_MIN_INLIERS:
                    ##                    HT = fitTransformation(np.array(crosshair.result))
                    resultXYpxpy = np.array(crosshair.result)
                    features = getFeatures(resultXYpxpy[:, :-2])
                    featuresAndLabels = np.concatenate(
                        (features, resultXYpxpy[:, -2:]), axis=1)
                    HT = RANSACFitTransformation(featuresAndLabels)
                    print HT
            if HT is not None:  # draw predicted eye position
                currentFeatures = getFeatures(
                    np.array((pupilOffsetXYList[0], pupilOffsetXYList[1])))
                gazeCoords = currentFeatures.dot(HT)
                crosshair.drawCrossAt((gazeCoords[0, 0], gazeCoords[0, 1]))
        readSuccessful, frame = vc.read()

    print "writing"
    crosshair.write()  #writes data to a csv for MATLAB
    vc.release()  #close the camera
    crosshair.close()
    print "HT:\n"
    print HT
    resultXYpxpy = np.array(crosshair.result)
    print "eyeData:\n"
    print getFeatures(resultXYpxpy[:, :-2])
    print "resultXYpxpy:\n"
    print resultXYpxpy[:, -2:]
Ejemplo n.º 2
0
def mainForTraining():
    import pygamestuff
    global port
    crosshair = pygamestuff.Crosshair([7, 2], quadratic=False)
    connection = socketConnection(port)
    frame = receiveFrame(connection)
    MAX_SAMPLES_TO_RECORD = 999999  # Number of samples to record before crashing
    recordedEvents = 0  # Numbers of times you have clicked
    HT = None
    try:
        while recordedEvents < MAX_SAMPLES_TO_RECORD and not crosshair.userWantsToQuit:
            pupilOffsetXYList = getOffset(frame, allowDebugDisplay=False)
            if pupilOffsetXYList is not None:  # If we got eyes, check for a click. Else, wait until we do.
                if crosshair.pollForClick():  # Looks for click
                    print(crosshair.resetRansac)

                    crosshair.clearEvents()  # Resets any events
                    crosshair.record(
                        pupilOffsetXYList
                    )  # Relates the XY Offset to the XY Screen
                    crosshair.remove()  # Removes the corsshair
                    recordedEvents += 1  # Increase recorded event counter
                    if recordedEvents > RANSAC_MIN_INLIERS:  # If enough points have been used for the ransac
                        resultXYpxpy = np.array(
                            crosshair.result
                        )  # Creates an array of all the click locations
                        features = getFeatures(resultXYpxpy[:, :-2])
                        featuresAndLabels = np.concatenate(
                            (features, resultXYpxpy[:, -2:]), axis=1)
                        HT = RANSACFitTransformation(featuresAndLabels)
                    ##                        print (HT)
                    if crosshair.resetRansac == True:
                        recordedEvents = 0
                        HT = None
                        print('everything reset')
                if HT is not None:  # draw predicted eye position
                    currentFeatures = getFeatures(
                        np.array((pupilOffsetXYList[0], pupilOffsetXYList[1])))
                    gazeCoords = currentFeatures.dot(HT)
                    crosshair.drawCrossAt((gazeCoords[0, 0], gazeCoords[0, 1]))
            frame = receiveFrame(connection)
        crosshair.write()  # writes data to a csv for MATLAB
        crosshair.close()
        resultXYpxpy = np.array(
            crosshair.result
        )  # This should be able to be saved and inputed in to create a pre-calibrated method
    finally:
        pass
Ejemplo n.º 3
0
def mainForTraining2():
    import pygamestuff
    crosshair = pygamestuff.Crosshair([7, 2], quadratic=False)
    vc = cv2.VideoCapture(0)  # Initialize the default camera
    if vc.isOpened():  # try to get the first frame
        (readSuccessful, frame) = vc.read()
    else:
        raise (Exception("failed to open camera."))
        return

    MAX_SAMPLES_TO_RECORD = 999999
    recordedEvents = 0
    HT = None
    try:
        while readSuccessful and recordedEvents < MAX_SAMPLES_TO_RECORD and not crosshair.userWantsToQuit:
            pupilOffsetXYList = getOffset2(frame)
            if pupilOffsetXYList is not None:  #If we got eyes, check for a click. Else, wait until we do.
                if crosshair.pollForClick():
                    crosshair.clearEvents()
                    #print( (xOffset,yOffset) )
                    #do learning here, to relate xOffset and yOffset to screenX,screenY
                    crosshair.record(pupilOffsetXYList)
                    print("recorded something")
                    crosshair.remove()
                    recordedEvents += 1
                    print("Recorded Events", recordedEvents)
                    if recordedEvents > RANSAC_MIN_INLIERS:
                        resultXYpxpy = np.array(crosshair.result)
                        minSeedSize = 5
                        iterations = 800
                        maxInlierError = 240  #**2
                        pointX, pointY = ransac.ransac2(
                            resultXYpxpy, minSeedSize, iterations,
                            maxInlierError, RANSAC_MIN_INLIERS,
                            pupilOffsetXYList[0], pupilOffsetXYList[1])
                        crosshair.drawCrossAt((pointX, pointY))
            readSuccessful, frame = vc.read()
        print("writing")
        crosshair.write()  #writes data to a csv for MATLAB
        crosshair.close()

    finally:
        vc.release()  #close the camera
Ejemplo n.º 4
0
def mainEyeTrack():
    import pygamestuff
    crosshair = pygamestuff.Crosshair([7, 2], quadratic = False)
    #crosshair.result = np.genfromtxt('1700wxoffsetyoffsetxy.csv',delimiter=',')
    with open("1700wxoffsetyoffsetxy.csv", "rb") as fp:   # Unpickling
        crosshair.result = pickle.load(fp)
    vc = cv2.VideoCapture(1) # Initialize the default camera
    if vc.isOpened(): # try to get the first frame
        (readSuccessful, frame) = vc.read()
    else:
        raise(Exception("failed to open camera."))
        return
    HT = None
    try:
	file = open("collectedData.txt", "w")
        while readSuccessful and not crosshair.userWantsToQuit:
            crosshair.checkEsc()
            crosshair.clearEvents()
            pupilOffsetXYList = getOffset(frame, allowDebugDisplay=False)
            if pupilOffsetXYList is not None: #If we got eyes, check for a click. Else, wait until we do.
		print "Found eyes"
                crosshair.remove()
                resultXYpxpy =np.array(crosshair.result)
                features = getFeatures(resultXYpxpy[:,:-2])
                featuresAndLabels = np.concatenate( (features, resultXYpxpy[:,-2:] ) , axis=1)
                HT = RANSACFitTransformation(featuresAndLabels)
		print HT
                if HT is not None: # draw predicted eye position
		    print "PREDICTING EYE POSITION"
                    currentFeatures =getFeatures( np.array( (pupilOffsetXYList[0], pupilOffsetXYList[1]) ))
                    gazeCoords = currentFeatures.dot(HT)
                    crosshair.drawCrossAt( (gazeCoords[0,0], gazeCoords[0,1]) )
		    naive_dt = datetime.now()
		    file.write(str(gazeCoords[0,0]) + "," + str(gazeCoords[0,1]) + "," + str(naive_dt) + "\n")
            readSuccessful, frame = vc.read()
    
        crosshair.close()
	file.close()

    finally:
        vc.release() #close the camera
Ejemplo n.º 5
0
def mainForTraining():
    import pygamestuff
    crosshair = pygamestuff.Crosshair([7, 2], quadratic = False)
    vc = cv2.VideoCapture(0) # Initialize the default camera
    if vc.isOpened(): # try to get the first frame
        (readSuccessful, frame) = vc.read()
    else:
        raise(Exception("failed to open camera."))
        return

    MAX_SAMPLES_TO_RECORD = 999999
    recordedEvents=0
    HT = None
    try:
        while readSuccessful and recordedEvents < MAX_SAMPLES_TO_RECORD and not crosshair.userWantsToQuit:
            pupilOffsetXYList = getOffset(frame, allowDebugDisplay=False)
            
            if pupilOffsetXYList is not None: #If we got eyes, check for a click. Else, wait until we do.
                if crosshair.pollForClick():
                    crosshair.clearEvents()
                    #print( (xOffset,yOffset) )
                    #do learning here, to relate xOffset and yOffset to screenX,screenY
                    crosshair.record(pupilOffsetXYList)
                    print(len(pupilOffsetXYList),"pupil set")
                    print ("recorded something")
                    crosshair.remove()
                    recordedEvents += 1
                    if recordedEvents > RANSAC_MIN_INLIERS:
    ##                    HT = fitTransformation(np.array(crosshair.result))
                        resultXYpxpy =np.array(crosshair.result)
                        features = getFeatures(resultXYpxpy[:,:-2])
                        featuresAndLabels = np.concatenate( (features, resultXYpxpy[:,-2:] ) , axis=1)
                        HT = RANSACFitTransformation(featuresAndLabels)
                        print (HT)
                if HT is not None: # draw predicted eye position
                    currentFeatures =getFeatures( np.array( (pupilOffsetXYList[0], pupilOffsetXYList[1]) ))
                    gazeCoords = currentFeatures.dot(HT)
                    print(gazeCoords.shape,":Shape of gazecoords")##delete after test
                    crosshair.drawCrossAt( (gazeCoords[0,0], gazeCoords[0,1]) )
                    
                    mouse_x = resultXYpxpy[-1,-2]
                    mouse_y = resultXYpxpy[-1,-1]
                    two=""
                    two += str(mouse_x)+str(',') + str(mouse_y)+str(',') 
                    two += str(gazeCoords[0,0])+str(',') + str(gazeCoords[0,1])+str(',') 
                    one.write(two+"\n")
                    
            readSuccessful, frame = vc.read()
    
        print ("writing")
        crosshair.write() #writes data to a csv for MATLAB
        crosshair.close()
        print ("HT: ")
        print (HT)
        resultXYpxpy =np.array(crosshair.result)
        print ("eyeData:")
        print (getFeatures(resultXYpxpy[:,:-2]))
        print ("resultXYpxpy:")
        print (resultXYpxpy[:,-2:])
        
    finally:
        vc.release() #close the camera
Ejemplo n.º 6
0
def mainForTraining():
    """ 
    En esta sección comienza la aplicación
    
    """


    import pygamestuff
    crosshair = pygamestuff.Crosshair([7, 2], quadratic = False)
    vc = cv2.VideoCapture(0) # Initialize the default camera
    if vc.isOpened(): # try to get the first frame
        (readSuccessful, frame) = vc.read()
    else:
        raise(Exception("failed to open camera."))
        return

    MAX_SAMPLES_TO_RECORD = 999999
    recordedEvents=0 #numero de fijaciones
    HT = None
    try:
        coords = []
        points = 0
        clicks = 0
        while readSuccessful and recordedEvents < MAX_SAMPLES_TO_RECORD and not crosshair.userWantsToQuit:
            points += 1
            pupilOffsetXYList = getOffset(frame, allowDebugDisplay=False)
            if pupilOffsetXYList is not None: #si se obtienen los dos ojos, espera un click
                if crosshair.pollForClick(): #si hace click se agregan los puntos a la calibracion
                    clicks += 1
                    print('clicks '+ str(clicks))
                    crosshair.clearEvents()
                    #print( (xOffset,yOffset) )
                    #do learning here, to relate xOffset and yOffset to screenX,screenY
                    crosshair.record(pupilOffsetXYList)
                    print ("recorded something")
                    crosshair.remove()
                    recordedEvents += 1
                    if recordedEvents > RANSAC_MIN_INLIERS:
            ##      HT = fitTransformation(np.array(crosshair.result))
                        resultXYpxpy =np.array(crosshair.result)
                        features = getFeatures(resultXYpxpy[:,:-2])
                        featuresAndLabels = np.concatenate( (features, resultXYpxpy[:,-2:] ) , axis=1)
                        HT = RANSACFitTransformation(featuresAndLabels)
                        print (HT)
                if HT is not None: # dibujar el circulo estimando la mirada
                    #print('ya empieza la estimacion')
                    #print(messagebox.askyesnocancel(message="Comenzará la calibración", title="Título"))

                    fixations = 0
                    currentFeatures = getFeatures( np.array( (pupilOffsetXYList[0], pupilOffsetXYList[1]) ))
                    gazeCoords = currentFeatures.dot(HT)
                    crosshair.drawCrossAt((gazeCoords[0,0], gazeCoords[0,1]))
                    print(gazeCoords[0,0], gazeCoords[0,1])
                    coords.append({
                    'fixation_number': fixations, 'x': gazeCoords[0,0],'y': gazeCoords[0,1] #las fijaciones son los puntos que detecta la aplicacion que un usuario mira
                })
                    fixations += 1
            readSuccessful, frame = vc.read()
    
        # print ("writing")
        crosshair.write() #writes data to a csv for MATLAB
        crosshair.close()
        # print ("HT:\n")
        # print (HT)
        resultXYpxpy = np.array(crosshair.result)
        # print ("eyeData:\n")
        # print (getFeatures(resultXYpxpy[:,:-2]))
        # print ("coordenadas: \n")

        # with open('1700wxoffsetyoffsetxy.csv') as tracker:
        #     fixation = csv.reader(tracker, delimiter=',', quotechar=',', quoting=csv.QUOTE_MINIMAL)
        #     for index_fixation, row in enumerate(fixation):
        #         item = {
        #             'fixation_number': index_fixation, 'x': row[2],'y': row[3],
        #         }
        #         coords.append(item)
    finally:
        vc.release() #close the camera
        makeModel(coords)