简体   繁体   English

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

[英]Passing an image as an argument to a function in python

How can I create a function that takes an image file (not image filename) in python. 我如何创建一个函数,该函数在python中使用图像文件(而不是图像文件名)。 Simply, like the following: 简单来说,如下所示:

FaceController.py 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 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

Error: 错误:

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

You can pass a pointer pointing to the image instead of the image or the filename of the image 您可以传递指向图像的指针,而不是图像或图像的文件名

EDIT 编辑

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)

Sorry, I don't know opencv so I can not help in your code :( 抱歉,我不知道opencv,所以我不能帮您的代码:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM