Recently I've been working on tracking an object based on contours with openCV in python. I want my code to only draw the largest contour (greater than 20px).
The code works if I start the program while holding a ball up to the camera, but when I remove the ball from the view the program crashes and give me:
if cv2.contourArea(cnt[0]) > 20:
IndexError: list index out of range
Anyone know how to solve this?
My code:
import cv2
import numpy as np
#Get current frame from camera
cap = cv2.VideoCapture(-1)
#Set size of said frame
cap.set(3, 160)
cap.set(4, 120)
while True:
    _, frame = cap.read()
    #Remove Noise and Grain
    median = cv2.medianBlur(frame,5)
    # Convert BGR to HSV
    hsv = cv2.cvtColor(median, cv2.COLOR_BGR2HSV)
    # define range of red color in HSV
    lower_red = np.array([0,200,0])
    upper_red = np.array([40,255,255])
    # Threshold the HSV image to get only red colors
    thresh = cv2.inRange(hsv, lower_red, upper_red)
    #Eliminate small peices of a thresholded image
    kernel = np.ones((5,5),np.uint8)
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    #Detect contours in an image
    contours, hierarchy =  cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    #Find only the biggest contour
    cnt = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
    #If there is a "biggest contour," then check to make sure it is the right size
    if cnt != None:
        if cv2.contourArea(cnt[0]) > 20:
            target = True
        else:
            target = False;
        if target == True:
            cv2.drawContours(frame, cnt, -1, (0,255,0), 3)
        else:
            print 'False \n'
    else:
         print 'No Contours Detected \n'
    cv2.imshow('Frame',frame)
    cv2.imshow('Threshold', thresh)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
         break
 cv2.destroyAllWindows()
Thanks for any help.
