Ask Your Question

Revision history [back]

How to save openCV as a pickle file?

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()

How to save openCV as a pickle file?

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

inline # load the cascade

cascade face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") image = cv2.imread('cricketteam.jpg')

cv2.imread('cricketteam.jpg') # Load the image

image plt.figure(figsize=(12,8)) plt.imshow(image, cmap='gray') plt.show()

plt.show() # Convert into grayscale

grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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)))

Faces!".format(len(faces))) for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

2) status = cv2.imwrite('faces_detected.jpg', image)

image) print ("Image faces_detected.jpg written to filesystem: ",status)

",status) facesdetected = cv2.imread('faces_detected.jpg')

cv2.imread('faces_detected.jpg') # Load the image

image plt.figure(figsize=(12,8)) plt.imshow(image, cmap='gray') plt.show()

plt.show()