I have a requirement where I have to deploy this model on Azure container registry and publish it as a Web Service. In order to do that, I need to save the OpenCV model as a pickle file so I can register the model on Azure.
Here is my code to detect faces:
import numpy as np import cv2 import matplotlib.pyplot as plt %matplotlib inline
load the cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") image = cv2.imread('cricketteam.jpg')
Load the image
plt.figure(figsize=(12,8)) plt.imshow(image, cmap='gray') plt.show()
Convert into grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") faces = faceCascade.detectMultiScale( gray, scaleFactor=1.3, minNeighbors=3, minSize=(30, 30) )
print("Found {0} Faces!".format(len(faces)))
for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
status = cv2.imwrite('faces_detected.jpg', image)
print ("Image faces_detected.jpg written to filesystem: ",status)
facesdetected = cv2.imread('faces_detected.jpg')
Load the image
plt.figure(figsize=(12,8)) plt.imshow(image, cmap='gray') plt.show()