def ovalChange(self, i):
        if i > 0:
            prevOval = ovalList[i - 1]
            self.canvas.itemconfigure(prevOval, fill="black")
        if i < (len(ovalList)):
            currentOval = ovalList[i]
            self.canvas.itemconfigure(currentOval, fill="red")
        self.canvas.update()
        time.sleep(3)
        
        if i < (len(ovalList)):
            #Call Edge Detection of binary frame
            ret, frame = cam.read()
            threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
            cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil,threshGlint)
            calIteration = 0
            while not successfullyDetected:
                ret, frame = cam.read()
                threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
                cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil,threshGlint)
                calIteration += 1
                #print calIteration
                if calIteration > 100:
                    self.canvas.itemconfigure(currentOval, fill="orange")
                    self.canvas.update()
                    threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
                    cv2.imshow('feed',threshPupil)
            if successfullyDetected:
                cv2.imwrite('pic{:>05}.png'.format(i), frame)
                calIteration = 0
                pupilX.append(cpX)
                pupilY.append(cpY)
                glintX.append(ccX)
                glintY.append(ccY)
                print 'pupilX current'
                print pupilX
            
        if i  < (len(ovalList)):
            j = i + 1
            self.ovalChange(j)

        if i == (len(ovalList)-1):
            print 'Pupil x'
            print pupilX
            print 'Pupil y'
            print pupilY
            print 'glint x'
            print glintX
            print 'glint y'
            print glintY
            print 'ScreenCoordsX'
            print screenCoordinatesX
            print 'ScreenCoordsY'
            print screenCoordinatesY
            aOriginal, bOriginal =  GCU.calibration(pupilX, pupilY, glintX, glintY, screenCoordinatesX, screenCoordinatesY)
            print aOriginal
            print "Here's a gap"
            print bOriginal
            print 'Calibration done'
            return aOriginal, bOriginal
from matplotlib import pyplot as plt

#Solutions obtained from 'Eye.MOV'
#aOriginal = [402.84482, -20.6854858, -7.40409644, -0.139460511, 
#-0.0316878766, -0.0595331782]
#bOriginal = [496.58251, 16.006643, 8.2113024, 0.2931556,
#0.27153592, 0.075294837]

pupilX = [275, 264, 244, 280, 261, 239, 277, 259, 240]
pupilY = [178, 178, 178, 183, 183, 182, 188, 188, 190]
glintX = [278, 273, 264, 281, 272, 262, 279, 270, 259]
glintY = [190, 188, 190, 191, 189, 190, 190, 191, 192]
calibrationX = [213, 639, 1065, 213, 639, 1065, 213, 639, 1065]
calibrationY = [133, 133, 133, 399, 399, 399, 665, 665, 665]

aOriginal, bOriginal =  GCU.calibration(pupilX, pupilY, glintX, glintY, calibrationX, calibrationY)

# Open video capture
cap = cv2.VideoCapture(1)
i = 0
print(cap)
print(cap.isOpened())

while(cap.isOpened()):

    # Read a frame from feed
    ret, frame = cap.read()
    
    threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
    
    # Edge Detection of binary frame
 def ovalChange(self):
     global iteration
     global quitButtonClick
     print "clicked start calibration %d" % iteration
     #As long as the quit button hasnt been pressed
     #Colour the previous dot black, if there is a previous dot
     #Colour the current dot red
     if (not quitButtonClick):
         if iteration > 0:
             prevOval = ovalList[iteration - 1]
             self.canvas.itemconfigure(prevOval, fill="black")
         if iteration < (len(ovalList)):
             currentOval = ovalList[iteration]
             self.canvas.itemconfigure(currentOval, fill="red")
         self.canvas.update()
         time.sleep(3)
         
         if iteration < (len(ovalList)):
             if iteration > 0:
                 prevOval = ovalList[iteration - 1]
                 self.canvas.itemconfigure(prevOval, fill="black")
             
             currentOval = ovalList[iteration]
             self.canvas.itemconfigure(currentOval, fill="red")
             self.canvas.update()
             
             #Cause a delay between dots, so the user has time to move gaze
             count = 0
             while(count < 12):
                 count+=1
                 ret, frame = cap.read()
             
             gazeCount = 0
             
             curPupilX = []
             curPupilY = []
             curGlintX = []
             curGlintY = []
             #Collect 3 sets of gaze points for each dot
             while (gazeCount < 3):
                 countGap = 0
                 #Cause delay between collection of data sets
                 while(countGap < 4):
                     countGap+=1
                     ret, frame = cap.read()
                 #Read frame, threshold, edge detect
                 ret, frame = cap.read()
                 threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
                 cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil,threshGlint)
                 calIteration = 0
                 #If edges have not been detected, try again
                 #If edges are not detected for a prolonged perios, turn calibration dot orange
                 #and show the camera feed
                 while not successfullyDetected:
                     ret, frame = cap.read()
                     threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
                     cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil,threshGlint)
                     calIteration += 1
                     if calIteration > 100:
                         self.canvas.itemconfigure(currentOval, fill="orange")
                         self.canvas.update()
                         threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
                         self.checkFeedFrame()
                 
                 #If the edges have been successfully detected, store the pupil and glint data
                 if successfullyDetected:
                     self.removewindow()
                     cv2.imwrite('pic{:>05}{}.png'.format(iteration, gazeCount), frame)
                     print ("saved image %d " % iteration)
                     calIteration = 0
                     curPupilX.append(cpX)
                     curPupilY.append(cpY)
                     curGlintX.append(ccX)
                     curGlintY.append(ccY)
                     # self.canvas.itemconfigure(currentOval, fill="green")
                     # self.canvas.update()
                 
                 gazeCount += 1
             
             #Once there are three sets of gaze data for each dot
             #find the average values of each bit of data and store the average
             #in the corresponding list
             countPupilX = 0
             for e in curPupilX:
                 countPupilX += e
             avgPupilX = countPupilX/len(curPupilX)
                 
             countPupilY = 0
             for e in curPupilY:
                 countPupilY += e
             avgPupilY = countPupilY/len(curPupilY)
             
             countGlintX = 0
             for e in curGlintX:
                 countGlintX += e
             avgGlintX = countGlintX/len(curGlintX)
                 
             countGlintY = 0
             for e in curGlintY:
                 countGlintY += e
             avgGlintY = countGlintY/len(curGlintY)
             
             pupilX.append(avgPupilX)
             pupilY.append(avgPupilY)
             glintX.append(avgGlintX)
             glintY.append(avgGlintY)
             
             iteration += 1
             
             #If all the dots have been visited pass the data collected to the
             #calibraiton equations and open the user frame
             if iteration == (len(ovalList)):
                 global aOriginal
                 global bOriginal
                 aOriginal, bOriginal =  GCU.calibration(pupilX, pupilY, glintX, glintY,screenCoordinatesX, screenCoordinatesY)
                 self.canvas.itemconfigure(currentOval, fill="black")
                 iteration += 1
                 self.openUserFrame()
             else:
                 self.ovalChange()