简体   繁体   English

视频转换:h.264从UDP套接字到Mat对象

[英]Video conversion: h.264 from a UDP socket to a Mat object

I have a Raspberry pi with a picam and I wanto to stream video to a server to process it and run object detection software. 我有一个带有picam的Raspberry pi,我想将视频流传输到服务器以对其进行处理并运行对象检测软件。 Im sending a video stream over a UDP socket to the server and reciving byte arrays that I can't turn into Mats. 我通过UDP套接字将视频流发送到服务器,并接收无法转换为Mats的字节数组。

The python sender: python发送者:

import socket
import time
import picamera
import sys

# msg = b'test'
addr = ("192.168.1.79", 7777)
start = time.time()
# Connect a client socket to my_server:8000 (change my_server to the
# hostname of your server)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('192.168.1.79', 7777))
con = s.makefile('wb');
print("Conectado")
# Make a file-like object out of the connection
# connection = s.makefile('wb')
try:
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 24
        print("Cargando camara")
        camera.start_preview()
        time.sleep(2)
        print("Enviando")
        camera.start_recording(con, format='h264')
        camera.wait_recording(10)
        camera.stop_recording()        
finally:
    con.close()
    s.close()

The c# reciver: C#接收器:

using Emgu.CV;
using Emgu.CV.UI;
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;


namespace EgmuCv {
    class Decoder{
        private const int listenPort = 7777;


        public static void streamToImage(ImageBox img) {
            UdpClient listener = new UdpClient(listenPort);
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            Mat mat = new Mat(new Size(640, 480), Emgu.CV.CvEnum.DepthType.Cv8U, 3);

            try {
                while (true) {
                    Console.WriteLine("Waiting for broadcast");
                    byte[] bytes = listener.Receive(ref groupEP);
                    // CvInvoke.Imdecode(bytes, Emgu.CV.CvEnum.ImreadModes.ReducedColor8, mat);
                    /** Code to turn bytes into Mat **/
                    // img.Image = mat;
                }
            } catch (SocketException e) {
                Console.WriteLine(e);
            } finally {
                listener.Close();
            }

        }
    }
}

After some research I found thath h.264 shold no be streamed trought UDP but TCP, and h.264 is not a good idea at all if you want to use the video in a Object Detection program. 经过一番研究,我发现h.264信息流不能流经UDP,而可以流经TCP,如果要在对象检测程序中使用视频,h.264根本不是个好主意。 A better idea is to send individual MJPEG frames with a TCP socket, is a little bit slower but im geting 640X480@30 which is more than enouht and the conversion to a Mat object way easyer 更好的主意是使用TCP套接字发送单个MJPEG帧,虽然速度稍慢,但是却获得了640X480 @ 30的好处,这远远不够,并且转换为Mat对象的方式更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM