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
    def show_frame(self):
        #Read the input feed, flip it, resize it and show it in the corresponding label
        ret, frame = cap.read()
        flipFrame = cv2.flip(frame, 1)
        cv2image = cv2.resize(flipFrame, (vidWidth, vidHeight));
        img1 = Image.fromarray(cv2image)
        imgtk1 = ImageTk.PhotoImage(image=img1)
        videoStream1.imgtk1 = imgtk1
        videoStream1.configure(image=imgtk1)
        
        #Call the threholding function
        threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
        
        #Call Edge Detection of binary frame
        cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil,threshGlint)
        #Draw around the pupil and glint
        print('cpX: ', cpX, ' cpY: ', cpY, ' ccX: ', ccX, ' ccY: ', ccY)
        #print successfullyDetected
        if cpX is None or cpY is None or ccX is None or ccY is None:
            print('pupil or corneal not detected, skipping...')
        else:
            # Ellipse Fitting
            frameCopy = frame.copy()
            
            #draw pupil centre
            cv2.circle(frameCopy, (cpX,cpY),3,(0,255,0),-1)
            
            #draw pupil circumference
            cv2.drawContours(frameCopy,cp,-1,(0,0,255),3)
            
            #draw corneal centre
            cv2.circle(frameCopy, (ccX,ccY),3,(0,255,0),-1)
            
            #draw corneal circumference
            cv2.drawContours(frameCopy,cc,-1,(0,0,255),3)
           
            #Show detected pupil
            if(frameCopy != None):
                frameC_resized = cv2.resize(frameCopy, (vidWidth, vidHeight), interpolation = cv2.INTER_AREA)
                frameC_resized = cv2.flip(frameC_resized, 1)
                img1 = Image.fromarray(frameC_resized)
                imgtk1 = ImageTk.PhotoImage(image=img1)
                videoStream1.imgtk1 = imgtk1
                videoStream1.configure(image=imgtk1)
            
            #If mouseToggle is true implement the cursor control code
            #Allows user to interact with computer wiht their eyes
            if mouseToggle:
                if 'aOriginal' in globals() and 'bOriginal' in globals():
                    # Centre points of glint and pupil pass to vector
                    gazeX, gazeY = GGP.getGazePoint(aOriginal, bOriginal, cpX, cpY, ccX, ccY)
                    ATE.move_mouse(gazeX, gazeY)
                    infoLabel.configure(text = "Now tracking your eye!")
                else:
                    infoLabel.configure(text = "You have not calibrated yet, please do so")

        videoStream1.after(5, self.show_frame)
    def show_frameInit(self):
        #Read the input feed, flip it, resize it and show it in the corresponding label
        if cap.isOpened():
            ret, frame = cap.read()
            flipFrame = cv2.flip(frame, 1)
            cv2image = cv2.resize(flipFrame, (vidWidth, vidHeight))
            img1 = Image.fromarray(cv2image)
            imgtk1 = ImageTk.PhotoImage(image=img1)
            videoStreamInit.imgtk1 = imgtk1
            videoStreamInit.configure(image=imgtk1)
            
            #Call the threholding function
            threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
            
            #Call Edge Detection of binary frame
            cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil,threshGlint)
            #Draw around the pupil and glint, if they are detected
            print('cpX: ', cpX, ' cpY: ', cpY, ' ccX: ', ccX, ' ccY: ', ccY)
            if cpX is None or cpY is None or ccX is None or ccY is None:
                print('pupil or corneal not detected, skipping...')
            else:
                # Ellipse Fitting
                frameCopy = frame.copy()
                
                #draw pupil centre
                cv2.circle(frameCopy, (cpX,cpY),3,(0,255,0),-1)
                
                #draw pupil circumference
                cv2.drawContours(frameCopy,cp,-1,(0,0,255),3)
                
                #draw corneal centre
                cv2.circle(frameCopy, (ccX,ccY),3,(0,255,0),-1)
                
                #draw corneal circumference
                cv2.drawContours(frameCopy,cc,-1,(0,0,255),3)
                
                #Code that will show the detected pupil, if it is detected
                if(frameCopy != None):
                    frameC_resized = cv2.resize(frameCopy, (vidWidth, vidHeight), interpolation = cv2.INTER_AREA)
                    frameC_resized = cv2.flip(frameC_resized, 1)
                    img1 = Image.fromarray(frameC_resized)
                    imgtk1 = ImageTk.PhotoImage(image=img1)
                    videoStreamInit.imgtk1 = imgtk1
                    videoStreamInit.configure(image=imgtk1)
        else:
            #Alerts the suer if the camera is not plugged in, attempts to open camera feed
            videoStreamInit.configure(text = "You have not plugged in the camera")
            global cap
            cap = cv2.VideoCapture(capVal)

        #Continue showing feed until calibration button is pressed.
        if (not calButton):
                videoStreamInit.after(5, self.show_frameInit)
    def show_frame(self):
        #Read the input feed, flip it, resize it and show it in the corresponding label
        ret, frame = cap.read()
        flipFrame = cv2.flip(frame, 1)
        cv2image = cv2.cvtColor(flipFrame, cv2.COLOR_BGR2GRAY)
        cv2image = cv2.resize(flipFrame, (vidWidth, vidHeight));
        img1 = Image.fromarray(cv2image)
        imgtk1 = ImageTk.PhotoImage(image=img1)
        videoStream1.imgtk1 = imgtk1
        videoStream1.configure(image=imgtk1)
        
        #Call the threholding function
        threshPupil, threshGlint = imgThreshold.imgThreshold(frame)
        
        #Call Edge Detection of binary frame
        cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil,threshGlint)
        #Draw around the pupil and glint
        print('cpX: ', cpX, ' cpY: ', cpY, ' ccX: ', ccX, ' ccY: ', ccY)
        if cpX is None or cpY is None or ccX is None or ccY is None:
            print('pupil or corneal not detected, skipping...')
        else:
            
            # Ellipse Fitting
            frameCopy = frame.copy()
            
            #draw pupil centre
            cv2.circle(frameCopy, (cpX,cpY),3,(0,255,0),-1)
            
            #draw pupil circumference
            cv2.drawContours(frameCopy,cp,-1,(0,0,255),3)
            
            #draw corneal centre
            cv2.circle(frameCopy, (ccX,ccY),3,(0,255,0),-1)
            
            #draw corneal circumference
            cv2.drawContours(frameCopy,cc,-1,(0,0,255),3)
            #Show the detected pupil
            if(frameCopy != None):
                frameC_resized = cv2.resize(frameCopy, (vidWidth, vidHeight), interpolation = cv2.INTER_AREA)
                frameC_resized = cv2.flip(frameC_resized, 1)
                img1 = Image.fromarray(frameC_resized)
                imgtk1 = ImageTk.PhotoImage(image=img1)
                videoStream1.imgtk1 = imgtk1
                videoStream1.configure(image=imgtk1)

        videoStream1.after(5, self.show_frame)
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
    cpX,cpY,cp,ccX,ccY,cc,successfullyDetected = edgeDet.edgeDetectionAlgorithm(threshPupil, threshGlint)
#    print('cpX: ', cpX, ' cpY: ', cpY, ' ccX: ', ccX, ' ccY: ', ccY)
    if cpX is None or cpY is None or ccX is None or ccY is None:
        print('pupil or corneal not detected, skipping...')
    else:   
        # Ellipse Fitting
        frameCopy = frame.copy()
    
        #draw pupil centre
        cv2.circle(frameCopy, (cpX,cpY),3,(0,255,0),-1)
        
        #draw pupil circumference
        cv2.drawContours(frameCopy,cp,-1,(0,0,255),3)
 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()