如何将Mat从OpenCV转换为Caffe格式 [英] How to convert Mat from opencv to caffe format

查看:134
本文介绍了如何将Mat从OpenCV转换为Caffe格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv从相机上修剪脸部.然后,我使用Caffe来预测图像是属于男性还是女性.我有一个从静态图像加载图像的原始代码.但是,我想使用相机中的图像.这是caffe中的原始代码

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

现在,我将使用opencv捕获帧并调用预测方法

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.

具有resize_image后,我会将其转换为caffe类型,例如函数

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?

谢谢您的帮助!

推荐答案

您可以在caffe.io中使用 CVMatToDatum 函数.此处提供更多信息: https://github.com/BVLC /caffe/blob/master/src/caffe/util/io.cpp

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

我认为您可以使用 array_to_datum https://github.com/BVLC/caffe/blob/master /python/caffe/io.py , 尽管可能有必要先将Mat转换为ndarray

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

这篇关于如何将Mat从OpenCV转换为Caffe格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆