Base on Opencv and Grip vision machine [TypeError: src is not a numpy array, neither a scalar]

Base on Opencv and Grip vision machine [TypeError: src is not a numpy array, neither a scalar]

我是opencv的新手,想了解更多。 这是我的管道:

import cv2
import numpy
import math
from enum import Enum

class GripPipeline:
    """
    An OpenCV pipeline generated by GRIP.
    """

    def __init__(self):
        """initializes all values to presets or None if need to be set
        """

        self.__cv_resize_dsize = (0, 0)
        self.__cv_resize_fx = 0.25
        self.__cv_resize_fy = 0.25
        self.__cv_resize_interpolation = cv2.INTER_LINEAR

        self.cv_resize_output = None

        self.__hsv_threshold_input = self.cv_resize_output
        self.__hsv_threshold_hue = [0.4556876738819602, 93.45444422147858]
        self.__hsv_threshold_saturation = [37.56692583788217, 145.8721694981769]
        self.__hsv_threshold_value = [1.7210414835158088, 187.92607745473873]

        self.hsv_threshold_output = None

        self.__cv_erode_src = self.hsv_threshold_output
        self.__cv_erode_kernel = None
        self.__cv_erode_anchor = (-1, -1)
        self.__cv_erode_iterations = 1.0
        self.__cv_erode_bordertype = cv2.BORDER_CONSTANT
        self.__cv_erode_bordervalue = (-1)

        self.cv_erode_output = None

        self.__mask_input = self.cv_resize_output
        self.__mask_mask = self.cv_erode_output

        self.mask_output = None

        self.__find_blobs_input = self.mask_output
        self.__find_blobs_min_area = 16.0
        self.__find_blobs_circularity = [0.0, 1.0]
        self.__find_blobs_dark_blobs = False

        self.find_blobs_output = None


    def process(self, source0):
        """
        Runs the pipeline and sets all outputs to new values.
        """
        # Step CV_resize0:
        self.__cv_resize_src = source0
        (self.cv_resize_output) = self.__cv_resize(self.__cv_resize_src, self.__cv_resize_dsize, self.__cv_resize_fx, self.__cv_resize_fy, self.__cv_resize_interpolation)

        # Step HSV_Threshold0:
        /code/
        # Step CV_erode0:
        /code/
        # Step Mask0:
        /code/
        # Step Find_Blobs0:
        /code/

    @staticmethod
    def __cv_resize(src, d_size, fx, fy, interpolation):
        """Resizes an Image.
        Args:
            src: A numpy.ndarray.
            d_size: Size to set the image.
            fx: The scale factor for the x.
            fy: The scale factor for the y.
            interpolation: Opencv enum for the type of interpolation.
        Returns:
            A resized numpy.ndarray.
        """
        return cv2.resize(src, d_size, fx=fx, fy=fy, interpolation=interpolation)

下面有很多 "def" 而我的创造对象 使用我的笔记本电脑相机 我确定这不是相机问题,因为我试图从中捕获 img,但我成功了。 :

    My_Pipeline = GripPipeline()
    My_Pipeline.process(cv2.VideoCapture(0))

它引发的错误:

     Traceback (most recent call last):
     File "<pyshell#1>", line 1, in <module>
     My_Pipeline.process(cv2.VideoCapture(0))
     File "C:\Users\lenovo\Desktop\grip.py", line 57, in process
    (self.cv_resize_output) = self.__cv_resize(self.__cv_resize_src, 
    self.__cv_resize_dsize, self.__cv_resize_fx, self.__cv_resize_fy, 
    self.__cv_resize_interpolation)
    File "C:\Users\lenovo\Desktop\grip.py", line 89, in __cv_resize
    return cv2.resize(src, d_size, fx=fx, fy=fy, 
    interpolation=interpolation)
    TypeError: src is not a numpy array, neither a scalar

我是 Opencv 的新手,只想了解更多! 非常感谢您查看此问题!

您正在用 cv2.VideoCapture(0) 呼叫 My_Pipeline.process()。与您的假设相反,cv2.VideoCapture(0) returns VideoCapture 对象,稍后在脚本中您将此参数传递给 cv2.resize()。所以目前 cv2.resize() 正在接收一个 VideoCapture 对象。您可以使用 cap.read()VideoCapture 获取框架或 numpy 矩阵,这将 return 一个 (success_code, frame) 的元组,您需要将此 frame 传递给 cv2.resize().

因此您的 class 初始化例程可能如下所示;

My_Pipeline = GripPipeline()
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if cap.isOpened() and ret:
    My_Pipeline.process(frame)

详细解释参考OpenCV docs