How to save openCV as a pickle file?

asked 2019-09-27 16:55:20 -0600

updated 2019-09-27 20:31:14 -0600

supra56 gravatar image

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()
edit retag flag offensive close merge delete

Comments

I need to save the OpenCV model as a pickle file so I can register the model on Azure.

you need explain this. why a pickle file, exactly ? (it's impossible)

berak gravatar imageberak ( 2019-09-27 23:53:43 -0600 )edit

Thanks for the reply. I'm trying to deploy the OpenCV model (detects face) to Azure. During this process, I will have to register the model using the below Python code.

register(workspace, model_path, model_name, tags=None, properties=None, description=None, datasets=None, model_framework=None, model_framework_version=None, child_paths=None)

What would I give for the model_path. I usually give the pickle file name in the model path. What should I be giving for the OpenCV model?

sharmila gravatar imagesharmila ( 2019-09-30 13:55:31 -0600 )edit

what would an "opencv model" be ?

please understand, that opencv is mainly a c++ library, and that the python api are just shallow wrappers around it.

please explain the need for "pickling". again, i doubt, that it is nessecary, and again, you can only pickle pure python state (not c++ pointers)

then, the code above does not make any sense on a webserver (using matplotlib for visualization) -- it's somehow quite obvious, that you did not think this project through to the end.

berak gravatar imageberak ( 2019-09-30 14:11:05 -0600 )edit

Okay got it. I don't need to pickle the model. But is it possible to deploy the OpenCV model to Azure?

sharmila gravatar imagesharmila ( 2019-09-30 14:26:11 -0600 )edit

I'm basically trying to Dockerise a Python application, which is an OpenCV module.

sharmila gravatar imagesharmila ( 2019-09-30 14:40:10 -0600 )edit

I'm trying to register the model in Azure Machine Learning workspace using the SDK below.

model = run.register_model(model_name='sklearn_mnist', model_path='outputs/sklearn_mnist_model.pkl')
print(model.name, model.id, model.version, sep='\t')

My question is how can I register the OpenCV model if it cannot be pickled since I need to mention the pkl file in the model_path parameter.

Or how do I save and load a OpenCV model?

Is there any other way to deploy OpenCV model on Azure.

Thank you.

sharmila gravatar imagesharmila ( 2019-09-30 16:00:08 -0600 )edit