Error in inRange () "TypeError: unknown is not a numpy array"

2

I am doing a motion detector program but I have an error in the following part of the code:

rang = cv2.inRange(flow,20,255)

Error:

TypeError: <unknown> is not a numpy array

Annex code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

cap.set(3,320)
cap.set(4,240)
_,prev = cap.read()
prevG = cv2.cvtColor(prev, cv2.COLOR_RGB2GRAY)
kernel = np.ones((5,5),np.uint8)
font = cv2.FONT_HERSHEY_SIMPLEX

while True:
    _,next = cap.read()
    nextG = cv2.cvtColor(next, cv2.COLOR_RGB2GRAY)
    flow = np.array(abs(np.array(nextG,np.float32)-np.array(prevG,np.float32)),np.uint8)
    cv2.imshow('flow',flow)
    rang = cv2.inRange(flow,20,255)
    cv2.imshow('rang',rang)
    opening = cv2.morphologyEx(rang, cv2.MORPH_OPEN, kernel)
    closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
    cv2.imshow('closing',closing)

    contours,_ = cv2.findContours(closing,1,2)

    M = [0,0]
    n = 0

    for cnt in contours:
        x,y,w,h = cv2.boundingRect(cnt)
        if w>15 and h>15 and w<200 and h<200:
            M[0] += x + float(w)/2.
            M[1] += y + float(h)/2.
            n += 1

    if M[0]!=0 and M[0]!=0:
        M = np.array(M)
        NewCen = PrevCen + 0.9*(M-PrevCen)
        cntX = int(NewCen[0]/n)
        cntY = int(NewCen[1]/n)
        cv2.circle(next,(cntX,cntY),5,(130,50,200),-1)
        cv2.putText(next,str(cntX)+','+str(cntY),(cntX+10,cntY+10),font,1,(130,50,200))
        PrevCen = NewCen

    prevG = nextG

    cv2.imshow('next',next)
    k = cv2.waitKey(1) & 0xFF
    if k == 27: break

cap.release()
cv2.destroyAllWindows()
    
asked by Ex70 26.03.2017 в 11:32
source

1 answer

1

I am afraid that without a minimum example I can not reproduce the error, it would also help to know the line that produces the error. I suspect it is some of the 2 cv2.imshow In which case I suppose that at some point the input argument is an empty object and produces that error, in which case maybe you should validate the entry something like:

nextG = cv2.cvtColor(next, cv2.COLOR_RGB2GRAY)
if nextG:
    flow = np.array(abs(np.array(nextG,np.float32)np.array(prevG,np.float32)),np.uint8)

The error simply indicates that you want to create a numpy array with an empty object.

    
answered by 26.03.2017 в 22:02