Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While executing the below code i'm getting this 'TypeError: img is not a numerical tuple'

    import cv2
    ram_frames=30
    cam = cv2.VideoCapture(0)
    def get_image():
          cap = cam.read()
          return cap
    for i in xrange(ramp_frames):
              temp = get_image()
    image = get_image()
    cv2.imwrite('bin/color.jpg',image)

The error i'm getting is :

File "C:\modules\imlib.py", line 1035, in __init__
    self.imin = self.WinWebCam()
  File "C:\modules\imlib.py", line 1125, in WinWebCam
    cv2.imwrite('bin/color.jpg',image)
TypeError: img is not a numerical tuple

I have done everything right i didn't change anything the code which when executed in a seperate program it's not showing any error but when run inside my code it's showing error. The code i copied is from this link

like image 268
dinece Avatar asked Sep 03 '25 04:09

dinece


1 Answers

You have changed the code while copying. Obviously, cam.read() returns a tuple. From the documentation:

Python: cv2.VideoCapture.read([image]) → retval, image

You are returning the whole tuple of retval and image, while the example only returns the second part of it (the image). So your image variable in line 9 contains the complete tuple that is returned by read() while the example only returns the second part of it. imwrite then fails because it does not expect a tuple as argument.

Try changing your code like this:

def get_image():
      _, cap = cam.read()
      return cap

or, even better,

def get_image():
    return cam.read()[1]

Additionally, you have misspelled the variable ramp_frames as ram_frames in line 2.

like image 125
hochl Avatar answered Sep 04 '25 21:09

hochl