'img is not a numpy array, neither a scalar

I try combine haar cascade code with histogram code

I try this code :

import cv2
import numpy as np 
from matplotlib import pyplot as plt

#Cascade jeruk
jeruk_cascade = cv2.CascadeClassifier('cascade.xml')

camera = cv2.VideoCapture(1)

base1 = cv2.imread('base1.png')
base2 = cv2.imread('base2.png')
base3 = cv2.imread('base3.png')

#Set hist parameters
hist_height = 64
hist_width = 256
nbins = 32
bin_width = hist_width/nbins
hrange = [0,180]
srange = [0,256]
ranges = hrange+srange                                  # ranges =   [0,180,0,256]

#Create an empty image for the histogram
h = np.zeros((hist_height,hist_width))

while 1:
    grabbed, img = camera.read()
    cam = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    if not grabbed:
      "Camera could not be started."
      break

    # add this
    # image, reject levels level weights.
    jeruks = jeruk_cascade.detectMultiScale(cam, 1.03, 5)

    # add this
    for (x,y,w,h) in jeruks:

    #    cv2.rectangle(img,(x,y),(x+w,y+h),(17,126,234),2)
    font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img,'Jeruk',(x+w,y+h), font, 1, (17,126,234), 2, cv2.LINE_AA) #---write the text

        roi_gray = cam[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

I add this histogram code

    histcam = cv2.calcHist([cam], [0], None, [nbins], [0,256])
    cv2.normalize(histcam,histcam, hist_height, cv2.NORM_MINMAX)
    hist=np.int32(np.around(histcam))

    for x,y in enumerate(hist):
        cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)

    #Flip upside down
    h=np.flipud(h)

#Show the histogram
    cv2.imshow('Color Histogram',h)
    h = np.zeros((hist_height,hist_width))

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

camera.release()
cv2.destroyAllWindows()

after I run all of them, I got this error :

Traceback (most recent call last):

File "/home/arizal/Documents/cascade jeruk/histogram/project1.py", line 52, in

cv2.rectangle(h,(xbin_width,y),(xbin_width + bin_width-1,hist_height), (255), -1)

TypeError: img is not a numpy array, neither a scalar

the error in line 52 :

cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)

but if I remove cascade code, the histogram code can run without error with the same histogram code ? how to fix it ?



Solution 1:[1]

You have:

h = np.zeros((hist_height,hist_width))

which is indeed a valid array, however it should be specified the dtype to make sure it will be visible in imshow later as it is intended like:

h = np.zeros((hist_height,hist_width), dtype=np.uint8)

for a normal grey-scale image. However the error comes because you write:

for (x,y,w,h) in jeruks:

which will put a number in h replacing the array you had.

Solution:

change the name of h, also, try to avoid one letter names, it is bad practice and prone to error, specially in python where the types are not set for a variable.

By the way, this could have been spotted faster and easier, if you write as the comments suggested :

print(type(h))
print(h.dtype)

before the lines

for x,y in enumerate(hist):
    cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)

You would have obtained something like int and an error saying that h does not have dtype attribute.

Solution 2:[2]

camera = cv2.VideoCapture(1) this part should be changed. Try , camera = cv2.VideoCapture(0) like this, it worked for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeru Luke
Solution 2 Yashar