The code below finds a contour based on its maximum area but when there is no contour to be found I solve the ValueError
by inputting default = None
as shown in the code below. However, when the list is not empty (contour is present) a new ValueError
arises as ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
. Why is that?
contours = cv2.findContours(tframe, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 2:
contours = contours[0]
else:
contours = contours[1]
maxcontour = max(contours, key=cv2.contourArea, default= None) #filter maximum contour based on contour area
if maxcontour == None :
maxarea = 0
circularity.append(0)
area.append(0)
else:
maxarea = cv2.contourArea(maxcontour)
perimeter = cv2.arcLength(maxcontour,True)
M = cv2.moments(maxcontour) #moments of that contour
circ = circularityfunc(maxarea,perimeter) #function that calculates circularity of contour
circularity.append(circ)