我需要在 GPU 中使用cupy
而不是numpy
. 所以,我只对这一行进行了注释,# import numpy as np
并使用了这一行而不是它import cupy as np
完整代码:
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
如何修复此错误以便使用 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
我认为我们不能将 numpy 的某些应用程序与 cupy 一起使用。
请,您的想法或任何建议?