2017-10-12 63 views
1

我是opencv的新手,想要了解更多信息。 這裏是我的管道:基於Opencv和Grip視覺機器[TypeError:src不是一個numpy陣列,也不是一個標量]

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) 

有大量的「高清」那裏 我的創建對象 使用我的筆記本電腦攝像頭 我相信這是不是相機的問題,因爲我試圖從中捕捉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的,只是想了解更多信息! 很高興看到這個問題!

回答

1

您正在致電My_Pipeline.process()cv2.VideoCapture(0)。與你的假設相反,cv2.VideoCapture(0)返回VideoCapture對象,稍後在腳本中將此參數傳遞給cv2.resize()。所以目前cv2.resize()正在接收VideoCapture對象。您可以使用cap.read()VideoCapture獲取框架或numpy矩陣,該矩陣將返回(success_code, frame)的元組,並且您需要將此frame轉換爲cv2.resize()

所以你的類初始化例程可能看起來像;

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

有關詳細說明,請參閱OpenCV docs

+0

很多的感謝,並沒有意識到對象的問題!大聲笑 – Billvace

+0

和通知,cv2.read應cap.read() – Billvace

+0

@Billvace更正我的壞 – ZdaR

相關問題