How to detect both eyes Pupil using Python and OpenCV

2.6k Views Asked by At

I have found Github codes for Pupil detection Pupil Detection with Python and OpenCV which explains how to detect eye pupil but explain only one Eye. I would like to detect both eyes. Please give me ideas how I can detect both eyes pupil from the codes.

Thanks

1

There are 1 best solutions below

2
On

Briefly looking over that code, it looks like it finds both eyes but then only gives you the one. Just modify the code as needed to extract the two found blobs rather than just the one. lines 55-72 are where it is pruning your candidate pool from some number of blobs (possible pupils) down to 1.

All of these lines: "if len(contours) >= n" are basically saying, if you still have more than one blob, try to cut one out. The thing is, you want the TWO biggest blobs, not the one. So, you need to rewrite those check statements such that you eliminate all but the two largest blobs, and then draw circles on each of their centroids. As far as I can tell, nothing else should need modification.

here is some sample code (untested) that may help. I don't know python syntax and just modified some stuff from your linked file:

while len(contours) > 2:
    #find smallest  blob and delete it
    minArea = 1000000  #this should be the dimensions of your image to be safe
    MAindex = 0         #to get the unwanted frame 
    currentIndex = 0 
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area < minArea:
            minArea = area
            MAindex = currentIndex
        currentIndex = currentIndex + 1

    del contours[MAindex]       #remove the picture frame contour
    del distanceX[MAindex]

This will get you down to your two eye blobs, then you will still need to add a circle drawing for each blob center. (you need to delete all the "if len..." statements and replace them with this one while statement)