实时视频传输_切割压缩、丢包统计

UDP实时视频传输

udp连接

原理:

在选择UDP作为传输协议时必须要谨慎。在网络质量令人十分不满意的环境下,UDP协议数据包丢失会比较严重。但是由于UDP的特性:它不属于连接型协议,因而具有资源消耗小,处理速度快的优点,所以通常音频、视频和普通数据在传送时使用UDP较多,因为它们即使偶尔丢失一两个数据包,也不会对接收结果产生太大影响。比如我们聊天用的ICQ和QQ就是使用的UDP协议。

在现场测控领域,面向的是分布化的控制器、监测器等,其应用场合环境比较恶劣,这样就对待传输数据提出了不同的要求,如实时、抗干扰性、安全性等。基于此,现场通信中,若某一应用要将一组数据传送给网络中的另一个节点,可由UDP进程将数据加上报头后传送给IP进程,UDP协议省去了建立连接和拆除连接的过程,取消了重发检验机制,能够达到较高的通信速率。

1.发送端

1
2
3
4
import threading
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
t = threading.Thread(target=mythread, args=(sock, string, ('192.168.201.106', 9999)))

2.接收端
1
2
3
4
5
6
import socket
import time
import threading
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('192.168.201.106', 9999))
data, addr = s.recvfrom(60000)

图像处理

旋转变换

1
2
img = img.transpose(Image.ROTATE_180)
img = img.transpose(Image.FLIP_LEFT_RIGHT)

这分别为图像旋转和左右翻转变换,我们拍到的视频是镜像翻转对称的,因此要进行以下处理,才能看到正像。

图像转Bytes

这个我找到了好几种方法:cv2转Image转bytes、cv2转np转bytes、

cv2转np转Image转bytes

1
2
image = Image.fromarray(cv.cvtColor(frame, cv.COLOR_BGR2RGB))
string = Image.fromarray(np.uint8(image)).tobytes()

cv2转np转bytes

1
2
image = Image.fromarray(cv.cvtColor(frame, cv.COLOR_BGR2RGB))
string=np.uint8(image).tostring()

pygame转bytes

1
2
Img = pygame.image.load('/home/pi/实验/视频传输数据采集/test.jpg')
string = pygame.image.tostring(Img, "RGB")

Bytes转图片

Image

1
img = Image.frombuffer("RGB", (160, 120), data)#, 'raw', "L", 0, 1

pygame

1
img = pygame.image.frombuffer(data, (160, 120), "RGB")

源码

  1. 发送端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# coding:utf-8
import cv2 as cv
import time
import threading
import socket
import numpy as np
from PIL import Image
def mythread(sock, data, addr):
sock.sendto(data, addr)
print("已发送 " + str(len(data)) + " bytes")
sock.close()
def main():
print("begin")
start = time.clock()
capture = cv.VideoCapture(0)
while True:
ret, frame = capture.read()
frame = cv.flip(frame, -1)
frame = cv.resize(frame, (160, 120))
image = Image.fromarray(cv.cvtColor(frame, cv.COLOR_BGR2RGB))
string = Image.fromarray(np.uint8(image)).tobytes()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
t = threading.Thread(target=mythread, args=(sock, string, ('192.168.201.106', 9999)))
t.start()
if cv.waitKey(10) & 0xff == ord('q'):
capture.release()
break
main()
cv.destroyAllWindows()
  1. 接收端
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    #coding=utf-8
    import socket
    import time
    import threading
    import numpy as np
    import cv2
    from PIL import Image
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(('192.168.201.106', 9999))
    print('Waiting for connection...')
    start = time.perf_counter()
    while not False:
    data, addr = s.recvfrom(60000)
    if time.perf_counter() - start > 1:
    start = time.perf_counter()
    print('Received from %s:%s.' % addr)
    img = Image.frombuffer("RGB", (160, 120), data)#, 'raw', "L", 0, 1
    img = img.transpose(Image.ROTATE_180)
    img = img.transpose(Image.FLIP_LEFT_RIGHT)
    crop_im = cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2BGRA)
    cv2.imshow('UDP 视频传输',crop_im)
    if cv2.waitKey(1) == 27: # 按下“ESC”退出
    break
    print('have received one frame')

    udp视频传输结果:

图片的切割与拼接

各种类型转bytes传输

对图片进行jpg压缩与还原

jpg压缩后的切割与传输

传输时延、丢包率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
auto lo
iface lo inet loopback

iface eth0 inet manual


auto wlan0
iface wlan0 inet static
address 192.168.201.165
netmask 255.255.255.0
gateway 192.168.201.1
pre-up wpa_supplicant -Dwext -i wlan0 -c/etc/wpa_supplicant/wpa_supplicant2.conf -B

allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant2.conf

auto wlan1
iface wlan1 inet static
address 192.168.201.165
netmask 255.255.255.0
gateway 192.168.201.1
pre-up wpa_supplicant -Dwext -i wlan1 -c/etc/wpa_supplicant/wpa_supplicant.conf -B

allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

sudo create_ap -n wlan0 car_wifi xmu20111 -c 1

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=CN

network={
ssid=”xmu201_1”
psk=”xmu20111”
key_mgmt=WPA-PSK
}

sudo nano /media/pi/rootfs/etc/network/interfaces
sudo nano /etc/network/interfaces
cd /media/pi/rootfs/etc/wpa_supplicant/

sudo nano /media/pi/rootfs/etc/dhcpcd.conf
sudo nano /etc/dhcpcd.conf

dhcp 域名解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
sudo nano /etc/dhcpcd.conf
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Most distributions have NTP support.
option ntp_servers
# Respect the network MTU.
# Some interface drivers reset when changing the MTU so disabled by default.
#option interface_mtu

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate Stable Private IPv6 Addresses instead of hardware based ones
slaac private

# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname

interface eth0
static ip_address=192.168.3.233/24
static routers=192.168.3.1
static domain_name_servers=114.114.114.114

interface wlan0
#static ip_address=172.16.233.233/16
#static routers=172.16.0.1
static domain_name_servers=114.114.114.114

static ip_address=10.42.0.233/24
static routers=10.42.0.1