Home > Net >  OpenCV/Hough Circles: How to apply a condition if circles is None
OpenCV/Hough Circles: How to apply a condition if circles is None

Time:07-28

I have the following code:

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

(this is the variable 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)

Currently when i try use (if red_circles is None) this does not work.. i'm guessing the program throws an error if circles is none however i put the code inside a try, except commands. Any help will be very much appreciated. EDIT: I have added the full code to the post

CodePudding user response:

You didn't show FULL error message in question so I don't know which line makes problem.

But if red_circles can be None then you should check it at first - before you use red_circles in np.uint16(np.around(red_circles)) and for i in red_circles[0, :]: and if (len(red_circles[0, :]) == 7)

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 ...

And if you use red_circles in other functions then you should also check it at first:

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 ...

If you don't want to print text then use 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 ...
  • Related