将图像作为参数传递给python中的函数

时间:2018-02-01 10:14:05

标签: python opencv

如何创建一个在python中获取图像文件(而不是图像文件名)的函数。简单地说,如下所示:

FaceController.py

import cv2
from Computer_Vision import Face_Detector as FD


def detectface():
    img = cv2.imread('DSC_1902.JPG')
    FD.detect(img)


detectface()

Face_Detector.py

import cv2

def detect(img):
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.namedWindow('img',cv2.WINDOW_NORMAL)
    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.imwrite('messigray.png', img)
    return img

错误:

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    cv2.error: C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

1 个答案:

答案 0 :(得分:0)

您可以传递指向图像的指针,而不是图像或图像的文件名

修改

def image_function(imagePointer):
    #DO SOMETHING WITH THE IMAGE

#HERE IS THE IMAGE POINTER
image = open('your_image.png')

#CALLING THE FUNCTION
image_function(image)

对不起,我不知道opencv,所以我无法帮助你的代码:(