0

I have a function 'draw_humans' in class 'TfPoseEstimator' in 'estimator.py' which is defined as:

  def draw_humans:  
    global cocoDict
        cocoDict = {}
        cocoDict = dict(zip(a,zip(b,c)))
    '''
    '''
    return (npimg, cocoDict, dist_dict)

I call this function in the main.py module and assign the returned values to variables like this:

    image, cocoDict_clone, dist_dict_clone = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)

But I get the error mentioned above.

    Traceback (most recent call last):
    File "run_webcam.py", line 306, in <module>
    image, cocoDict_clone, dist_dict_clone = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
    File "C:\Python\Python37\summer\PoseEstimation\tf_pose\estimator.py", line 772, in draw_humans
    return (npimg, cocoDict, dist_dict)
    NameError: name 'cocoDict' is not defined
    [ WARN:1] terminating async callback

I have even tried to make it global but it did not work. Usually, it does work, can someone figure it out?

Pe Dro
  • 2,651
  • 3
  • 24
  • 44
  • Is there an opencv operation somewhere there??? – Inder Jun 12 '19 at 17:50
  • Yes, its there in both the modules : After I declare cocoDict and after I call it in main.py. How does it matter? – Pe Dro Jun 13 '19 at 02:07
  • kindly consider exploring: https://stackoverflow.com/questions/53888878/cv2-warn0-terminating-async-callback-when-attempting-to-take-a-picture – Inder Jun 13 '19 at 13:19
  • if this doesn't resolve the issue consider adding more detail in the question – Inder Jun 13 '19 at 13:20
  • My case is entirely different. I am stuck due to the Name Error. OpenCV has no relation to it. – Pe Dro Jun 14 '19 at 04:56

1 Answers1

0

Actually, the problem was related to the scope of the variables (cocoDict in my case). This dictionary was initialized within the for loop but was being returned outside it. So, I declared it before the for loop and then after manipulating it within the for loop, returned it with no issues.

  def draw_humans(npimg, humans, imgcopy=False): 

    global cocoDict
    cocoDict = {}

    for human in humans:
      '''
      '''
    return (npimg, cocoDict, dist_dict)

I guess scope in Python is causing me a lot of efforts as I am from a C++ background.

Pe Dro
  • 2,651
  • 3
  • 24
  • 44
  • Python scope is for whole function (not like in C++, where it is for every almost {}). Setting variable inside for loop and outside of it is exactly the same. What matters is order - you need to assign value to a variable before trying to read from it. My guess is had code path in your loop, where you would read from **cocoDict** before assign. – Radosław Cybulski Jun 14 '19 at 05:11