首页 > 解决方案 > a TypeError: Required argument 'object' (pos 1) not found

问题描述

I'm trying to track all feature points with Kalman Filter, The code runs and shows correct results for few seconds.The script then errors out with the message.

TypeError                                 Traceback (most recent call last)
<ipython-input-7-7e2081ff6701> in <module>()
     90         image1 = cv2.circle(image1,(c,d),2,(255,0,0),2)
     91         image1 = cv2.putText(image1,"x:"+str(c),(c-2,d-2),cv2.FONT_HERSHEY_COMPLEX,0.5,(255,0,0),1)
---> 92         x_predict,p_predict = k_predict(state[main_id],P_cov[main_id])
     93         print(x_predict,p_predict)
     94         x_next,p_next = k_update(z,x_predict,p_predict)

TypeError: Required argument 'object' (pos 1) not found

And I don't know what's causing it nor how to fix it. My guess is that the defaultdict function is responsible for the error, but again, i have no idea how to fix it, Any Suggestion ?

Here's the main code

import numpy as np
import cv2
from collections import defaultdict
import time

#feature parameter for the shi tomasi algo
feature_params = dict(maxCorners=50,
                    qualityLevel=0.2,
                    minDistance = 10,
                    blockSize = 10)

#parameters for lucasKanade
lk_params = dict( winSize  = (15,15),
                  maxLevel = 2,criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))


check_id = 0 #it is the id given to each object
main_id = 0
object_ids = dict() #it is the dictionary which can contains the id and the coordinate
disappeared = dict() #it is the dictionary which contains the number of time the object is dissappeared
max_disappeared = 10 #till 10 frames the object will be followed up if it is not visible after 10 frames it will be remove
#state and noise initialization for n corner points
state = defaultdict(np.array)
P_cov = defaultdict(np.array)

max_points = 30
points = defaultdict(list)
points_kalman = defaultdict(list)

cap = cv2.VideoCapture("test.webm")
ret, old_frame = cap.read()

old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
#create the feature points
fp = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)
#state initialization
if fp is not None:
    p = np.int0(fp)

    for i,fppp in enumerate(p):
        (c,d) = fppp.ravel()
        check_id+=1
        object_ids[check_id] = object_ids.get(check_id,(c,d))
        disappeared[check_id] = disappeared.get(check_id,0)
        points[check_id].append((c,d))
        points_kalman[check_id].append((c,d))
        state[check_id] = np.array([c,d,0,0]).reshape(4,1) #[x,y,vx,vy]
        P_cov[check_id] = np.array([[1,0,0,0],[0,1,0,0],[0,0,10,0],[0,0,0,10]])
    #print(fp)

    
while(1):
    ret,frame = cap.read()
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame_no = cap.get(cv2.CAP_PROP_POS_FRAMES)
    if int(frame_no)%5 == 0:
        fp = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)
    # calculate optical flow
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, fp, None, **lk_params)
    
    
    good_new = p1[st==1]
    good_old = fp[st==1]
    image = frame.copy()
    image1 = image.copy()
    image2 = image.copy()

    for i,(old,new) in enumerate(zip(good_old,good_new)):
        a,b = old.ravel().astype(int)
        c,d = new.ravel().astype(int)
        z = np.array([c,d,c-a,d-b]).reshape(4,1)
        try:
            main_id = [key for key,values in object_ids.items() if values[0]==a and values[1]==b][0]
        except Exception as e:
            main_id=-1
            pass

        if main_id<0:
            check_id+=1
            object_ids[check_id] = object_ids.get(check_id,(c,d))
            disappeared[check_id] = disappeared.get(check_id,0)
            points[check_id].append((c,d))
            main_id=check_id
        else:
            object_ids[main_id]=(c,d)
            points[main_id].append((c,d))
            if len(points[main_id])>max_points:
                points[main_id].pop(0)
        for newp,point in enumerate(points[main_id]):
            if newp>=len(points[main_id])-2:
                break
            (rpx,rpy) = point
            (rnx,rny) = points[main_id][newp+1]
            image = cv2.line(frame,(rpx,rpy),(rnx,rny),(255,0,0),2)
        image1 = cv2.circle(image1,(c,d),2,(255,0,0),2)
        image1 = cv2.putText(image1,"x:"+str(c),(c-2,d-2),cv2.FONT_HERSHEY_COMPLEX,0.5,(255,0,0),1)
        x_predict,p_predict = k_predict(state[main_id],P_cov[main_id])
        print(x_predict,p_predict)
        x_next,p_next = k_update(z,x_predict,p_predict)
        pfpx = int(x_predict[0][0])
        pfpy = int(x_predict[1][0])
        pfpvx = int(x_predict[2][0])
        pfpvy = int(x_predict[3][0])
        #pfpx = int(x_next[0][0])
        #pfpy = int(x_next[1][0])

        image2 = cv2.circle(image2,(pfpx,pfpy),2,(0,0,255),2)
        image2 = cv2.putText(image2,"x:"+str(pfpx),(pfpx-2,pfpy-2),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,255),1)
        state[main_id] = x_next
        P_cov[main_id] = p_next
    #new_fp = cv2.goodFeaturesToTrack(gray_frame, mask = None, **feature_params)
    #for nfp in new_fp:
    #    (nfpx,nfpy) = nfp.ravel()
    #    image = cv2.circle(image,(nfpx,nfpy),2,(255,0,0),2)
    fp = good_new.reshape(-1,1,2)
    old_gray = old_gray.copy()
    cv2.imshow('frame',frame)
    #cv2.imshow('main',image)
    cv2.imshow('original',image1)
    cv2.imshow('kalman',image2)
    k = cv2.waitKey(30) & 0xff
    if k==27:
        break

cv2.destroyAllWindows()
cap.release()

标签: pythonopencvcomputer-visionkalman-filter

解决方案


推荐阅读