How do i stream a video with openCV into my pytorch neural network?
I wrote YOLOv3 in Pytorch from scratch. If i send an image through the model with trained weights, it kinda works. Next step is to use my camera to make YOLO do its magic in real time.
I think the correct working pipeline is to catch a single frame of the video and feed it to the network. Than, write the boxes on the very same frame.
checkpoint = torch.load(&quot;\my_checkpoint_40.pth.tar&quot;)
model = YOLOv3(in_channels = 3, num_classes = 20).to(config.DEVICE)
model.load_state_dict(checkpoint[&quot;state_dict&quot;])
ip_camera = &quot;http://192.168.1.70:4500/mjpegfeed?640x480&quot;
outputFile = &quot;yolo_out_py.avi&quot;

In this way, i load the weights into the net. Then, i wrote the function to use my camera (it's droidCamera from my mobile, because on my PC i have not any camera device, so i use the ip of the mobile device) and the code itself works: the video appears on screen.
The outputFile should be the destination path of the wrote video.
Problems are when i try to load a single frame into the net and do the rest of the process.
def streaming(model, thresh, iou_thresh, anchors, ip_camera):
    stream = cv2.VideoCapture(ip_camera)

    # Corrective actions printed in the even of failed connection.
    if stream.isOpened() is not True:
        print('Not opened.')
        print('Please ensure the following:')
        print('1. DroidCam is not running in your browser.')
        print('2. The IP address given is correct.')
    # Resizing the image to be in hte same dimension of the YOLOv3 Network
    width = 416
    height = 416
    # Connection successful. Proceeding to display video stream.
    while stream.isOpened() is True:
        # Capture frame-by-frame
        ret, f = stream.read()
        dim = (width, height)
        image = cv2.resize(f, dim, interpolation = cv2.INTER_AREA)
        cv2.imshow('frame', image)
        if cv2.waitKey(1) &amp; 0xFF == ord('q'):
            break
        for frame in image:
            model.eval()
            anchors = torch.tensor(anchors)
            anchors = anchors.to(config.DEVICE) 
            x = torch.tensor(frame)
            x = x.to(&quot;cuda&quot;)

           # from this line to the nms_boxes, it's the same code i used for plotting a single               image
            with torch.no_grad():
                out = model(x)
                bboxes = [[] for _ in range(x.shape[0])]
                for i in range(1):
                    batch_size, A, S, _, _ = out[i].shape
                    anchor = anchors[i]
                    boxes_scale_i = cells_to_bboxes(
                        out[i], anchor, S = S, is_preds = True
                    )
                    for idx, (box) in enumerate(boxes_scale_i):
                        bboxes[idx] += box

                model.train()

            for i in range(batch_size):
                nms_boxes = non_max_suppression(
                    bboxes[i], iou_threshold = iou_thresh, threshold = thresh, box_format =                   &quot;midpoint&quot;,
                )
           # cells_to_boxes and non_max_suppression are functions that return boxes coordinates
           # and the &quot;better&quot; box

                #now it's time to write things on the frame
                frame = cv2.VideoWriter(outputFile, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30,
                                        (round(stream.get(cv2.CAP_PROP_FRAME_WIDTH)), round(stream.get(cv2.CAP_PROP_FRAME_HEIGHT))))

                frame.write(nms_boxes)
    stream.release()
    cv2.destroyAllWindows()

the code doesn't work for several reasons: model.eval() gives me an error: missing 1 required positional argument: 'self'
then, i have several errors that i think are about the right pipiline of work on stream of video. It's the first time i work with openCV.
If i delete model.eval(), i have another error on:
out = model(x)

This is the traceback
Traceback (most recent call last):
  File &quot;C:/Python_Project/YOLOV3/openVid.py&quot;, line 97, in &lt;module&gt;
    streaming(YOLOv3, 0.6, 0.6, config.ANCHORS, ip_camera)
  File &quot;C:/Python_Project/YOLOV3/openVid.py&quot;, line 72, in streaming
    out = model(x)
  File &quot;C:\Python_Project\YOLOV3\model.py&quot;, line 106, in __init__
    self.layers = self.create_conv_layers()
  File &quot;C:\Python_Project\YOLOV3\model.py&quot;, line 141, in create_conv_layers
    CNNBlock(
  File &quot;C:\Python_Project\YOLOV3\model.py&quot;, line 46, in __init__
    self.conv = nn.Conv2d(in_channels, out_channels, bias = not bn_act, **kwargs)
  File &quot;C:\Users\Simone\anaconda3\envs\Pytorch\lib\site-packages\torch\nn\modules\conv.py&quot;, line 430, in __init__
    super(Conv2d, self).__init__(
  File &quot;C:\Users\Simone\anaconda3\envs\Pytorch\lib\site-packages\torch\nn\modules\conv.py&quot;, line 83, in __init__
    if in_channels % groups != 0:
RuntimeError: Boolean value of Tensor with more than one value is ambiguous

I have no clue about what doing next.
I saw i should convert the model in ONNX, but i really don't know what to do. I couldn't find any tutorial on internet, and i'm stucked.
Could you help me?

Based on the error message, model is not a class instance. Note, in the Traceback, that
out = model(x)

is calling the __init__ function. Therefore, model is probably YOLOV3 rather than YOLOV3(...). Based on the init signature, x is being taken as in_channels, and as x an image, the

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

makes sense. This also explains the .eval() error. Besides that, I believe you'll need to add a batch dimension to your frame (e.g., x.unsqueeze(0)), otherwise you'll get another error.
