OpenCV/Hough Circles:如果Circles为None,如何应用条件



我有以下代码:

def Tracking():
red_lower = np.array([35, 192, 65])
red_upper = np.array([179, 255, 255])
yellow_lower = np.array([16, 215, 177])
yellow_upper = np.array([179, 255, 255])
video = cv2.VideoCapture(1, 0)
times = []
total = 0
is_round = False
average = str(datetime.timedelta(seconds=0))[2:7]
while True:
try:
success, img = video.read()
image = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
red_mask = cv2.inRange(image, red_lower, red_upper)
yellow_mask = cv2.inRange(image, yellow_lower, yellow_upper)
red_blur = cv2.GaussianBlur(red_mask, (15, 15), 0)
yellow_blur = cv2.GaussianBlur(yellow_mask, (15, 15), 0)
red_circles = cv2.HoughCircles(red_blur, cv2.HOUGH_GRADIENT, 1, 14,
param1=34, param2=10, minRadius=4, maxRadius=10)
red_circles = np.uint16(np.around(red_circles))
yellow_circles = cv2.HoughCircles(yellow_blur, cv2.HOUGH_GRADIENT, 1, 14,
param1=34, param2=10, minRadius=4, maxRadius=10)
yellow_circles = np.uint16(np.around(yellow_circles))
if (len(red_circles[0, :]) == 7) and not is_round:
start_time = time.time()
is_round = True
curr_count = 0
round_total = 0
elif is_round:
if red_circles is None: ------> PROBLEM
end_time = time.time()
is_round = False
time_taken = end_time - start_time
times.append(time_taken)
average1 = sum(times) / len(times)
average = str(datetime.timedelta(seconds=average1))[2:7]
elif len(red_circles[0, :]) < 7 and len(yellow_circles[0, :]) < 7:
curr_count = (14 - round_total) - 
len(red_circles[0, :]) - len(yellow_circles[0, :])
total += curr_count
round_total += curr_count
previous_total = 0
previous_average = 0
if red_circles is None:
previous_total = total
previous_average = average
for i in red_circles[0, :]:
cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
cv2.circle(img, (i[0], i[1]), 2, (0, 0, 255), 3)
for i in yellow_circles[0, :]:
cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
cv2.circle(img, (i[0], i[1]), 2, (0, 0, 255), 3)
yield dict(total=total, average=average)
except:
yield dict(total=previous_total, average=previous_average)
pass

(这是变量red_circles(

red_circles = cv2.HoughCircles(red_blur, cv2.HOUGH_GRADIENT, 1, 14,
param1=34, param2=10, minRadius=4, maxRadius=10)
red_circles = np.uint16(np.around(red_circles))
for i in red_circles[0, :]:
cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
cv2.circle(img, (i[0], i[1]), 2, (0, 0, 255), 3)

目前,当我尝试使用时(如果red_circles为None(,这不起作用。。如果circles为none,我猜程序会抛出一个错误。但是我把代码放在了try中,除了命令。任何帮助都将不胜感激。编辑:我已经将完整的代码添加到后

您并没有显示问题中的FULL错误消息,所以我不知道哪一行有问题。

但如果red_circles可以是None,那么在np.uint16(np.around(red_circles))for i in red_circles[0, :]:if (len(red_circles[0, :]) == 7)中使用red_circles之前,您应该先检查它

red_circles = cv2.HoughCircles(red_blur, cv2.HOUGH_GRADIENT, 1, 14,
param1=34, param2=10, minRadius=4, maxRadius=10)
if red_circles is None:  #  shorter `if not red_circles:`
print("didn't find circles")
else:
red_circles = np.uint16(np.around(red_circles))
for i in red_circles[0, :]:
# ... rest ...

如果你在其他功能中使用red_circles,那么你也应该首先检查它:

if red_circles is None:  #  shorter `if not red_circles:`
print("didn't find circles")
else:
if (len(red_circles[0, :]) == 7) and not is_round:
# ... rest ...

如果不想打印文本,请使用not None

red_circles = cv2.HoughCircles(red_blur, cv2.HOUGH_GRADIENT, 1, 14,
param1=34, param2=10, minRadius=4, maxRadius=10)
if red_circles is not None:  # shorter `if red_circles:`
red_circles = np.uint16(np.around(red_circles))
for i in red_circles[0, :]:
# ... rest ...
if red_circles is not None:  # shorter `if red_circles:`
if (len(red_circles[0, :]) == 7) and not is_round:
# ... rest ...

相关内容

最新更新