Cesar Troya Sherdek
Cesar Troya Sherdek

Reputation: 67

OpenCV ERROR - <unknown> is not a numpy array

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

Upvotes: 3

Views: 2350

Answers (2)

Cesar Troya Sherdek
Cesar Troya Sherdek

Reputation: 67

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)),)

Upvotes: 1

jojonas
jojonas

Reputation: 1675

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

Upvotes: 1

Related Questions