Dipanwita Biswas
Dipanwita Biswas

Reputation: 1

why does my python code using opencv is giving typeerror?

this is my code

import cv2
from cvzone.PoseModule import PoseDetector 

cap=cv2.VideoCapture('Video (1).mp4') 

detector=PoseDetector() 

posList = []
while True: # This line starts an infinite loop. The code inside this loop will continue to run until it encounters a break statement or the program is terminated manually.

    success, img = cap.read() 

    img = detector.findPose(img) 

   lmList,bboxInfo = detector.findPosition(img) 
    
    
    if bboxInfo:
        lmString = ''
        for lm in lmList:
            # print(lm)
            lmString += f'{lm[1]},{img.shape[0]-lm[2]},{lm[3]}'
        posList.append(lmString)
    
    print(posList)
            # num_points = len(lmList)
            # print("Number of Points Detected:", num_points)


    cv2.imshow("Image",img)
    cv2.waitKey(1)   

this is the error:

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Traceback (most recent call last):
  File "d:\py_projects\motionDetector\motionCap.py", line 22, in <module>
    lmString += f'{lm[1]},{img.shape[0]-lm[2]},{lm[3]}'
TypeError: unsupported operand type(s) for -: 'int' and 'list'

the should display the changing position of the landmarks on the video

Upvotes: 0

Views: 83

Answers (1)

Sauron
Sauron

Reputation: 1353

  • In this line lmString += f'{lm[1]},{img.shape[0]-lm[2]},{lm[3]}', you are rying to subtract a list from an integer which causes a TypeError.
  • you need to ensure that lm[2] is an integer before performing the subtraction.
  • By accessing lm[2][0], you're retrieving the first element of the lm[2] list, which should be an integer.
lmString += f'{lm[1]},{img.shape[0]-lm[2][0]},{lm[3]}'

Upvotes: 0

Related Questions