title: Passing an image as an argument to a function in python
body: <p>How can I create a function that takes an image file (not image filename) in python. Simply, like the following:</p>
  
  <p>FaceController.py</p>
  
  <pre><code>import cv2
  from Computer_Vision import Face_Detector as FD
  
  
  def detectface():
      img = cv2.imread('DSC_1902.JPG')
      FD.detect(img)
  
  
  detectface()
  </code></pre>
  
  <p>Face_Detector.py</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>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
  </code></pre>