-1

I'm working on a image analysis project. encountering this error and not sure hot to solve it , cuz the function 'detect_balls' returns three values but couldn't assign it to variables whre it is called out to . Would really appriciate your help . the code is attached below

import numpy as np
import cv2
import sys
import time
bgr_color = 29,2,128 #129,119,195
color_threshold = 50 #color range

hsv_color = cv2.cvtColor( np.uint8([[bgr_color]] ), cv2.COLOR_BGR2HSV)[0][0]
HSV_lower = np.array([hsv_color[0] - color_threshold, hsv_color[1] - color_threshold, hsv_color[2] - color_threshold])
HSV_upper = np.array([hsv_color[0] + color_threshold, hsv_color[1] + color_threshold, hsv_color[2] + color_threshold])


def detect_ball(frame):
    x, y, radius = -1, -1, -1

    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    mask = cv2.inRange(hsv_frame, HSV_lower, HSV_upper)
    mask = cv2.erode(mask, None, iterations=1)
    mask = cv2.dilate(mask, None, iterations=1)

    im2, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    center = (-1, -1)

    
    if len(contours) > 0:
       
        c = max(contours, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(mask)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) #position of the ball

        if radius > 10:
            #outline ball
            cv2.circle(frame, (int(x), int(y)), int(radius), (255, 0, 0), 2)
            #show ball center
            cv2.circle(frame, center, 5, (0, 255, 0), -1)

    return center[0], center[1], radius


if __name__ == "__main__":
    filepath = '/home/venkat-com/Desktop/track/golf01_preview.mp4'

    cap = cv2.VideoCapture(filepath)

    dist = []
    times = []
    count = 0
    kalmanFilter = []
    variance = []
    initialEstimatedVariance = 1
    deltaX = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    deltat = 524  
    noise = 1  
    sensorCovariance = 83791.65209996712



    variance.append(initialEstimatedVariance)
 
    while(cap.isOpened()):
        count+=1
        ret, frame = cap.read()

        #detect_ball(frame)
        [x,y,r] = detect_ball(frame)
        if (len(dist) != 0):
            x_prev = dist[len(dist) - 1]

            velocity = float(deltaX)/deltat

            dist.append(x)

            estimatedX = x_prev + velocity
            estimatedVariance = variance[len(variance)-1] + noise

            kalmanGain = float(estimatedVariance)/(estimatedVariance + sensorCovariance)

            kalmanPosition = estimatedX + (kalmanGain*(x - estimatedX))

            kalmanFilter.append(kalmanPosition)

            updatedVariance = estimatedVariance-(kalmanGain*estimatedVariance)
            variance.append(updatedVariance)

            print (kalmanFilter)
            #print(variance)

        else: #don't do any calculations on the 1st point
            x_initial = detect_ball(frame)[0]
            dist.append(x_initial)
            kalmanFilter.append(x_initial)

        # Display the resulting frame
        cv2.namedWindow('frame', cv2.WINDOW_NORMAL)  # Create window with freedom of dimensions
        imS = cv2.resizeWindow('frame', (960, 540))  # Resize image

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

Tried to check the values given out by the function, and all of them were correct and no null values

This is the terminal's output trying to run the code :

Traceback (most recent call last):
  File "/home/venkat-com/trajectory.py", line 67, in <module>
    [x,y,r] = detect_ball(frame)
  File "/home/venkat-com/trajectory.py", line 22, in detect_ball
    im2, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

1 Answers1

0

cv2.findCountours seems to return only two values, contours and hierarchy as per the documentation. You need to remove im2 from that line.

Arun Kumar Nagarajan
  • 2,347
  • 3
  • 17
  • 28