pythonopencvgrip

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


I am new to opencv and want to learn more about it. Here is my Pipeline:

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)

There is lots of "def" down there And my creating object Using my laptop camera I am sure it is not the camera problem because I tried to capture img from it and i succeed. :

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

The error it raises:

     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

I am new to Opencv and just want to learn more! Lot of thanks for looking at this problem!


Solution

  • You are calling My_Pipeline.process() with cv2.VideoCapture(0). Contrary to your assumption, cv2.VideoCapture(0) returns VideoCapture object, later in the script you are passing this parameter to cv2.resize(). So currently cv2.resize() is receiving a VideoCapture object. You can get the frame or numpy matrix from the VideoCapture using cap.read() which would return a tuple of (success_code, frame) and you need to pass this frame to cv2.resize().

    So your class initialization routine may look like;

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

    For detailed explanation refer to OpenCV docs