树莓派实时视频传输

 1 # -*- coding: utf-8 -*-
 2 # __author__ = "赵鹏"
 3 # Date: 2019/8/16
 4 
 5 import socket
 6 import threading
 7 import struct
 8 import time
 9 import cv2
10 import numpy
11 
12 
13 class Carame_Accept_Object:
14     def __init__(self, S_addr_port=("127.0.0.1", 8880)):
15         self.resolution = (640, 480)  # 分辨率
16         self.img_fps = 15  # 每秒传输多少帧数
17         self.addr_port = S_addr_port
18         self.Set_Socket(self.addr_port)
19 
20     # 设置套接字
21     def Set_Socket(self, S_addr_port):
22         self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23         self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # 端口可复用
24         self.server.bind(S_addr_port)
25         self.server.listen(5)
26         # print("the process work in the port:%d" % S_addr_port[1])
27 
28 
29 def check_option(object, client):
30     # 按格式解码,确定帧数和分辨率
31     info = struct.unpack('lhh', client.recv(8))
32     if info[0] > 888:
33         object.img_fps = int(info[0]) - 888  # 获取帧数
34         object.resolution = list(object.resolution)
35         # 获取分辨率
36         object.resolution[0] = info[1]
37         object.resolution[1] = info[2]
38         object.resolution = tuple(object.resolution)
39         return 1
40     else:
41         return 0
42 
43 
44 def RT_Image(object, client, D_addr):
45     if (check_option(object, client) == 0):
46         return
47     camera = cv2.VideoCapture(0)  # 从摄像头中获取视频
48     img_param = [int(cv2.IMWRITE_JPEG_QUALITY), object.img_fps]  # 设置传送图像格式、帧数
49     while 1:
50         time.sleep(0.1)  # 推迟线程运行0.1s
51         _, object.img = camera.read()  # 读取视频每一帧
52         gray = cv2.cvtColor(object.img, cv2.COLOR_BGR2GRAY)
53         object.img = cv2.resize(object.img, object.resolution)  # 按要求调整图像大小(resolution必须为元组)
54         ret, img_encode = cv2.imencode('.jpg', object.img, img_param)  # 按格式生成图片
55         img_code = numpy.array(img_encode)  # 转换成矩阵
56         object.img_data = img_code.tostring()  # 生成相应的字符串
57         try:
58             # 按照相应的格式进行打包发送图片
59             client.send(
60                 struct.pack("lhh", len(object.img_data), object.resolution[0], object.resolution[1]) + object.img_data)
61         except:
62             camera.release()  # 释放资源
63             return
64 
65 
66 if __name__ == '__main__':
67     camera = Carame_Accept_Object()
68     while 1:
69         client, D_addr = camera.server.accept()
70         clientThread = threading.Thread(None, target=RT_Image, args=(camera, client, D_addr,))
71         clientThread.start()
 1 # -*- coding: utf-8 -*-
 2 # __author__ = "赵鹏"
 3 # Date: 2019/8/16
 4 
 5 
 6 import socket
 7 import cv2
 8 import threading
 9 import struct
10 import numpy
11 
12 
13 class Camera_Connect_Object:
14     def __init__(self, D_addr_port=["", 8880]):
15         self.resolution = [640, 480]
16         self.addr_port = D_addr_port
17         self.src = 888 + 15  # 双方确定传输帧数,(888)为校验值
18         self.interval = 0  # 图片播放时间间隔
19         self.img_fps = 100  # 每秒传输多少帧数
20 
21     def Set_socket(self):
22         self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23         self.client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
24 
25     def Socket_Connect(self):
26         self.Set_socket()
27         self.client.connect(self.addr_port)
28         print("IP is %s:%d" % (self.addr_port[0], self.addr_port[1]))
29 
30     def RT_Image(self):
31         # 按照格式打包发送帧数和分辨率
32         self.name = self.addr_port[0] + " Camera"
33         self.client.send(struct.pack("lhh", self.src, self.resolution[0], self.resolution[1]))
34         while (1):
35             info = struct.unpack("lhh", self.client.recv(8))
36             buf_size = info[0]  # 获取读的图片总长度
37             if buf_size:
38                 try:
39                     self.buf = b""  # 代表bytes类型
40                     temp_buf = self.buf
41                     while (buf_size):  # 读取每一张图片的长度
42                         temp_buf = self.client.recv(buf_size)
43                         buf_size -= len(temp_buf)
44                         self.buf += temp_buf  # 获取图片
45                         data = numpy.fromstring(self.buf, dtype='uint8')  # 按uint8转换为图像矩阵
46                         self.image = cv2.imdecode(data, 1)  # 图像解码
47                         gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
48                         cv2.imshow(self.name, self.image)  # 展示图片
49                 except:
50                     pass;
51                 finally:
52                     if (cv2.waitKey(10) == 27):  # 每10ms刷新一次图片,按‘ESC’(27)退出
53                         self.client.close()
54                         cv2.destroyAllWindows()
55                         break
56 
57     def Get_Data(self, interval):
58         showThread = threading.Thread(target=self.RT_Image)
59         showThread.start()
60 
61 
62 if __name__ == '__main__':
63     camera = Camera_Connect_Object()
64     camera.addr_port[0] = input("Please input IP:")
65     camera.addr_port = tuple(camera.addr_port)
66     camera.Socket_Connect()
67     camera.Get_Data(camera.interval)

 

posted @ 2019-08-16 15:28  過去  阅读(3476)  评论(4编辑  收藏  举报