'TypeError: lowerb is not a numpy array, neither a scalar
I needed to run some parts of the code in GPU using cupy
instead of numpy
. So, I only made comment out for this line # import numpy as np
and used this line instead of it import cupy as np
the full code:
import cv2
# import numpy as np
import cupy as np
import time
cap = cv2.VideoCapture(0)
while (1):
_, img = cap.read()
if _ is True:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
else:
continue
# red color
red_lower = np.array([0, 0, 0], np.uint8)
red_upper = np.array([180,255,30], np.uint8)
mask = cv2.inRange(hsv, red_lower, red_upper)
# #
kernal = np.ones((15, 15), "uint8")
# # # # #
mask = cv2.dilate(mask, kernal , iterations=1)
mask = cv2.GaussianBlur(mask, (11, 11), cv2.BORDER_DEFAULT)
(_, contours, hierarchy) = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if (area > 300):
x, y, w, h = cv2.boundingRect(contour)
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
cv2.putText(img, "black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))
cv2.imshow("Color Tracking", img)
if cv2.waitKey(10) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
how to fix this error so that use cupy.
Traceback (most recent call last):
File "/home/redhwan/learn.py", line 18, in <module>
mask = cv2.inRange(hsv, red_lower, red_upper)
TypeError: lowerb is not a numpy array, neither a scalar
I think we can't use some applications of numpy with cupy.
please, your ideas or any suggestions?
Solution 1:[1]
thanks for your question.
Opencv does not work with cupy arrays, you will need to convert back and from cupy when calling Opencv using the cupy.asnumpy
and cupy.asarray
functions.
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 | emcastillo |