将捕获的视频帧(numpy 数组对象)从网络摄像头传输到 caffe 模型

Passing captured video frame (numpy array object) from webcam feed to caffe model

我是Caffe初学者,正在尝试使用Imagenet模型进行对象分类。我的要求是我想从网络摄像头源中使用它并检测来自网络摄像头的对象 feed.For 这个,我使用以下代码

    cap = cv2.VideoCapture(0)
    while(True):    
        ret, frame = cap.read() #frame is of type numpy array       
        #frame = caffe.io.array_to_datum(frame) 
        img = caffe.io.load_image(frame)

显然这不起作用,因为 caffe.io.load_image 需要图像路径。 如您所见,我还尝试使用 caffe io.pyarray_to_datum 函数(从 this Whosebug question 获取)并将框架传递给 caffe io load_image 但这也不起作用. 如何将捕获的视频帧从网络摄像头直接传递到 caffe io load_image ? 如果那不可能,那么将框架加载到 caffe io 的方法是什么?请帮忙。提前致谢。

caffe.io.load_image 作用不大。它仅执行以下操作:

  1. 从磁盘读取图像(给定路径)
  2. 确保返回的图像具有 3 维(HxWx1 或 HxWx3)

(参见来源 caffe.io.load_image

所以它不会加载图像到你的模型,它只是一个从磁盘加载图像的辅助函数。要将图像加载到内存中,您可以根据需要加载它(从磁盘、网络摄像头等)。要加载网络,将图像输入其中并进行推理,您可以执行以下操作:

# Load pre-trained network
net=caffe.Net(deployprototxt_path, caffemodel_path, caffe.TEST)
# Feed network with input
net.blobs['data'].data[0,...] = frame
# Do inference
net.forward()
# Access prediction
probabilities = net.blobs['prob'].data

确保框架尺寸与 deploy.prototxt 中指定的预期输入尺寸相匹配(参见 example for CaffeNet

如果您使用 OpenCV 读取相机帧,则需要将颜色 space 从 OpenCV 的默认 space (BGR) 重新排序为 Caffe 输入顺序 RGB,然后将所有值作为单个值精度浮点数:

# load caffe model
net = caffe.Net(model_def,  # defines the structure of the model
            model_weights,  # contains the trained weights
            caffe.TEST)     # use test mode (e.g., don't perform dropout)

cap = cv2.VideoCapture(1)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280);
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT,720);

while(True):

    ret,frame = cap.read()
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)/255.0
    # you can now pass image to Caffe
    net.blobs['data'].data[...] = image
    # forward pass, obtain detections
    detections = net.forward()['detection_out']