Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将捕获的视频帧(numpy数组对象)从网络摄像头馈送传递到caffe模型_Python_Opencv_Video_Caffe_Pycaffe - Fatal编程技术网

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

Python 将捕获的视频帧(numpy数组对象)从网络摄像头馈送传递到caffe模型,python,opencv,video,caffe,pycaffe,Python,Opencv,Video,Caffe,Pycaffe,我是Caffe的初学者,我正在尝试使用Imagenet模型进行对象分类。我的要求是,我想从网络摄像头提要中使用它,并从网络摄像头提要中检测对象 cap = cv2.VideoCapture(0) while(True): ret, frame = cap.read() #frame is of type numpy array #frame = caffe.io.array_to_datum(frame) i

我是Caffe的初学者,我正在尝试使用Imagenet模型进行对象分类。我的要求是,我想从网络摄像头提要中使用它,并从网络摄像头提要中检测对象

    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\u image
需要一个映像路径。 如您所见,我还尝试使用
caffe io.py
array\u to\u datum
函数(从中获取)并将帧传递给caffe io load\u image,但这也不起作用。 如何将捕获的视频帧从网络摄像头直接传送到
caffe io load_image

如果不可能,那么如何将帧加载到
caffe io
?请帮忙。提前感谢。

caffe.io.load\u图像没有太大作用。它只执行以下操作:

# 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
  • 从磁盘读取映像(给定路径)
  • 确保返回的图像具有3个维度(HxWx1或HxWx3)
  • (见资料来源)

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

    # 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(请参阅)中指定的预期输入尺寸相匹配。如果使用OpenCV读取相机帧,则需要将颜色空间从OpenCV的默认空间(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']
    

    为什么是caffe.io?你可以将numpy数组直接传递到你的网络。你的回答拯救了我的一天!非常感谢,做得很好。请注意,对于像我这样的第一次使用的用户,您必须调整框架的大小,以匹配您正在使用的模型。在我的例子中,对于caffeNet和AlexNet模型,我将其大小调整为(227 x 227),并且您可能需要转换尺寸,如frame=frame.transpose((2,0,1))#将尺寸从H-W-C转换为C-H-W