3

I get this error:

<unknown> is not a numpy array

when executing this code:

import cv2
import numpy as np

try:

    cap = cv2.VideoCapture(0)

    while (cap.isOpened()):
        ret,img=cap.read()
        cv2.imshow('output',img)
        img2=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
        cv2.imshow('gray_scale',img2)
        imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),)
        cv2.imshow('thresholded',imgthreshold)

        k=cv2.waitKey(10)
        if k==27:
            break

    cap.release()
    cv2.destroyAllWindows()


except Exception as inst:

    cap.release()
    cv2.destroyAllWindows()
    print("Eroor!!")
    print(inst)
    raise

Here's the traceback:

Traceback (most recent call last):
    File "C:\Users\... ...\camara.py", line 14, in <module>
        imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),)
TypeError: <unknown> is not a numpy array

I hope you can help me to solve this. I already check all the dependencies and they work fine also if I delete the line

imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),)

and the next one, the program work without problems

7
  • You have a comma at the end of your cv2.inRange(...) function call, turning the three arguments into a single tuple. Try removing that trailing comma.
    – user707650
    Sep 6, 2015 at 22:37
  • @Evert, adding a trailing comma in a function call will not cause the arguments to be packed into a single tuple. Sep 6, 2015 at 22:44
  • Thanks for the answer but it didn't work, the problem remains the same Sep 6, 2015 at 22:44
  • Can you provide the full stack trace?
    – jojonas
    Sep 6, 2015 at 22:45
  • @jojonas here it is: Traceback (most recent call last): File "C:\Users\... ...\camara.py", line 14, in <module> imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),) TypeError: <unknown> is not a numpy array Sep 6, 2015 at 22:50

2 Answers 2

1

You need to check the status returned by the VideoCapture::read function. It returns a boolean flag indicating whether the returned image is valid at all.

import cv2
import numpy as np

try:

    cap = cv2.VideoCapture(0)

    while (cap.isOpened()):
        ret,img=cap.read()
        if not ret:
            continue

        cv2.imshow('output',img)
        img2=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
        cv2.imshow('gray_scale',img2)
        imgthreshold=cv2.inRange(img,cv2.cv.Scalar(3,3,125),cv2.cv.Scalar(40,40,255),)
        cv2.imshow('thresholded',imgthreshold)

        k=cv2.waitKey(10)
        if k==27:
            break

    cap.release()
    cv2.destroyAllWindows()


except Exception as inst:

    cap.release()
    cv2.destroyAllWindows()
    print("Eroor!!")
    print(inst)
    raise
1
  • The VideoCapture function is working without any problem, as I say if, you delete the trouble line, the program show tow displays with the correct image capture Sep 7, 2015 at 1:40
1

I find the solution, it is to convert the top parameters of colors to find into a numpy array -->

imgthreshold=cv2.inRange(img,np.array(cv2.cv.Scalar(3,3,125)),np.array(cv2.cv.Scalar(40,40,255)),)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.