How to convert Mat from opencv to caffe format

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

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:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!