How to convert Mat from opencv to caffe format

3.1k views Asked by At

I am using opencv to crop face from my camera. And then I used caffe to predict that image belongs to male or female. I have a original code that load image from static image. However, I want to use image from camera for it. This is original code in caffe

    model = caffe.Classifier(...)
    image_path = './static_image.jpg'
    input_image = caffe.io.load_image(image_path )
    prediction =model.predict([input_image]) 

Now, I will use opencv to capture frame and call predict method

  val, image = cap.read()    
  image = cv2.resize(image, (320,240))
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(30,30))
  for f in faces:
      x,y,w,h = f
      cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,255))        
       face_image = gray[y:y+h, x:x+w]
       resized_img = cv2.resize(face_image, (45,45))/255.

After having resized_image, I will conver it to caffe type such as function

def format_frame(self,frame):
    img = frame.astype(np.float32)/255.
    img = img[...,::-1]
    return img

However, when I call that function. I don't know what is self. Could you help me to fix it?

Thank you for help!

1

There are 1 answers

2
LiberiFatali On

You can use CVMatToDatum function in caffe.io. More info here: https://github.com/BVLC/caffe/blob/master/src/caffe/util/io.cpp

Edit: I think you can use array_to_datum from https://github.com/BVLC/caffe/blob/master/python/caffe/io.py, though it might be necessary to convert Mat to ndarray first