從局域網監控到廣域網實時監控的實現




1. 局域網視頻監控

  • socket 實現
  • 測試可在同一臺電腦運行
  • 條件:處在同一局域網,並知道被監控端的 ip
  • 觀看監控視頻時同步保存視頻到本地


服務端 :等待接受視頻信號

import socket
import cv2
import struct
import numpy
import threading
import time


class Camera_Connect_Object(object):
    def __init__(self, TargetIP=('', 6666)): #端口可以自己選
        self.TargetIP = TargetIP
        self.resolution = (640, 480)
        self.img_fps = 25

        now_time = str(time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        avi_name = now_time
        self.out = cv2.VideoWriter(avi_name+ '.avi' ,fourcc, 25.0, (640,480))

        self.Server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.Server.bind(self.TargetIP)
        self.Server.listen(5)

    def RT_Image(self):
        self.client, self.addr = self.Server.accept()
        self.name = self.addr[0] + " Camera"
        print(self.name)
        while True:
            # time.sleep(0.3)  # sleep for 0.3 seconds
            tempdata = self.client.recv(8)
            if len(tempdata) == 0:
                print("+1")
                continue
            info = struct.unpack('lhh', tempdata)
            buf_size = int(info[0])

            if buf_size:
                try:
                    self.buf = b""
                    self.temp_buf = self.buf
                    while buf_size:
                        self.temp_buf = self.client.recv(buf_size)
                        buf_size -= len(self.temp_buf)
                        self.buf += self.temp_buf
                    data = numpy.frombuffer(self.buf, dtype='uint8')

                    self.image = cv2.imdecode(data, 1)
                    print(self.image.shape)
                    self.out.write(self.image)
                    cv2.imshow(self.name, self.image)
                except Exception as e:
                    print(e.args)
                    pass
                finally:
                    if cv2.waitKey(10) == 27:
                        self.client.close()
                        self.out.release()
                        cv2.destroyAllWindows()
                        break

    def Get_data(self):
        showThread = threading.Thread(target=self.RT_Image)
        showThread.start()
        showThread.join()


if __name__ == '__main__':
    camera = Camera_Connect_Object()
    camera.Get_data()


客戶端 :向服務端發送視頻信號

import socket
import struct
import time
import cv2
import numpy


class Config(object):
    def __init__(self):
        self.TargetIP = ('192.168.xx.xxx', 6666) #填自己電腦的ip
        self.resolution = (640, 480)  # 分辨率
        self.img_fps = 25  # each second send pictures
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.connect(self.TargetIP)
        self.img = ''
        self.img_data = ''

    def RT_Image(self):
        camera = cv2.VideoCapture(0)
        img_param = [int(cv2.IMWRITE_JPEG_QUALITY), self.img_fps]

        while True:
            time.sleep(0.1)  # sleep for 0.1 seconds
            _, self.img = camera.read()

            self.img = cv2.resize(self.img, self.resolution)

            _, img_encode = cv2.imencode('.jpg', self.img, img_param)
            img_code = numpy.array(img_encode)
            self.img_data = img_code.tostring()  # bytes data
            try:

                packet = struct.pack(b'lhh', len(self.img_data), self.resolution[0],
                                     self.resolution[1])
                self.server.send(packet)
                self.server.send(self.img_data)
            
            except Exception as e:
                print(e.args)
                camera.release()
                return


if __name__ == '__main__':
    config = Config()
    config.RT_Image()


2. 與雲服務器遠程通信

  • socket 實現
  • 服務端在雲服務器運行,等待客戶端連接
  • 通信過程,客戶端發送信息至雲端,雲端將接收到的信息返回到客戶端,並加上系統本地時間


服務端 :接受客戶端的消息並回復

import socket
import time

# 創建一個tcp/ip協議的套接字
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = "172.17.xx.xx" #填寫雲服務器的私網 ip
port = 8888 #記得要先開放端口
buffer_size = 1024
address = (host, port)
# 開始綁定地址
tcpSocket.bind(address)
tcpSocket.listen(5)

while True:
    print("服務器等待被連接中。。。。。")
    clientSock, client_addr = tcpSocket.accept()
    print("已經連接到客戶端,連接地址:{0}".format(client_addr))
    try:
        while True:
            data = clientSock.recv(buffer_size)
            print("接收到客戶端的數據爲:{0}".format(str(data, encoding="utf8")))
            if data.upper() == "EXIT":
                break
            now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            clientSock.send(bytes("時間:{0},接受的內容爲:{1}".format(now_time, str(data, encoding="utf8")), encoding="utf8"))

    except Exception as e:
        print(e)
    finally:
        clientSock.close()

tcpSocket.close()


客戶端 :向服務器發送數據,也可接收服務器返回的數據

import socket
import time

# 創建一個tcp/ip協議的套接字
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = "39.97.xx.xx" #填寫自己的公網 ip
port = 8888 #與服務端的端口一致
buffer_size = 1024
address = (host, port)
# 開始連接服務器地址
clientSocket.connect(address)

if clientSocket is None:
    print("無法連接當前的服務器!\n")
else:
    print("已經連接服務器---> oK\n")
    while True:
        data = input("(q 退出)發送數據到服務器:")
        if data== "q":
            clientSocket.send(bytes("EXIT", encoding="utf8"))
            # 關閉當前的客戶端
            clientSocket.close()
            break

        clientSocket.send(bytes(data, encoding="utf8"))
        # 接收服務器發送的數據
        data = clientSocket.recv(buffer_size)
        # 打印接收的數據
        print("服務器返回:{0}\n".format(str(data, encoding="utf8")))


3. 雲服務器中轉實現遠程監控

  • 先在雲服務器運行服務端,等待被連接
  • 雲服務器可以接收被監控方客戶端的視頻信號,並保存爲 avi 格式存放在雲服務器上
  • 雲服務器同樣可以接收用戶客戶端的請求,將實時的視頻信號轉發給用戶
  • 用戶在遠程觀看視頻的同時,視頻也將被保存到本地
  • 順序是這樣的:服務端等待、相機連接到服務器併發送一幀畫面、服務器接收一幀畫面並保存爲 avi 格式、用戶連接到服務器查看正在傳輸的數據並保存視頻到本地


客戶端 1 :向服務器發送數據,經壓縮的圖像

import socket
import struct
import time
import cv2
import numpy


class Config(object):
    def __init__(self):
        self.TargetIP = ('39.97.xx.xx', 8888) #使用雲服務器公網 ip
        self.resolution = (640, 480)  # 分辨率
        self.img_fps = 70  # each second send pictures
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.connect(self.TargetIP)
        print('採集者已連接服務器!')

    def RT_Image(self):
        camera = cv2.VideoCapture(0)
        img_param = [int(cv2.IMWRITE_JPEG_QUALITY), self.img_fps]
        ret, frame = camera.read()
        while ret:
            time.sleep(0.1)  # sleep for 0.1 seconds
            ret, frame = camera.read()
            
            frame = cv2.resize(frame, self.resolution)
            _, img_encode = cv2.imencode('.jpg', frame, img_param)
            img_code = numpy.array(img_encode)
            self.img_data = img_code.tostring()  # bytes data
            try:
                buf_size = len(self.img_data)
                if buf_size>0 and buf_size<500000:
                    packet = struct.pack(b'i',buf_size)
                    self.server.send(packet)
                    self.server.send(self.img_data)
            
            except Exception as e:
                print(e.args)
                camera.release()
                return

if __name__ == '__main__':
    config = Config()
    config.RT_Image()


客戶端 2 :從服務器讀取實時數據並解析,保存視頻到本地

import socket
import struct
import time
import cv2
import numpy
import threading
import sys


class FromServer(object):
    def __init__(self):
        self.TargetIP = ('39.97.xx.xx', 9999) #公網 ip 端口不要與客戶端 1 的重複了
        self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.clientSocket.connect(self.TargetIP)

        self.resolution = (640, 480)
        self.img_fps = 70
    

    def RT_Image(self):
        video_name = bytes.decode(struct.unpack('14s', (self.clientSocket.recv(14)))[0])
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(video_name+ '.avi' ,fourcc, 35.0, (640,480))
        try:
            while True:
                tempdata = self.clientSocket.recv(4)
                if cv2.waitKey(10) == 27:
                    self.clientSocket.close()
                    out.release()
                    sys.exit()
                    break
                if len(tempdata) == 4:
                    info = struct.unpack('i', tempdata)
                    buf_size = int(info[0])
                    if buf_size>0 and buf_size<500000:
                        try:
                            buf = b""
                            temp_buf = buf
                            while buf_size:
                                temp_buf = self.clientSocket.recv(buf_size)
                                buf_size -= len(temp_buf)
                                buf += temp_buf
                            data = numpy.frombuffer(buf, dtype='uint8')
                            self.image = cv2.imdecode(data, 1)
                            out.write(self.image)
                            cv2.imshow(video_name, self.image)
                        except Exception as e:
                            print(e)
                            pass
                    else:
                        buf_size = 0
        except Exception as e:
            self.clientSocket.close()
            out.release()

    def Get_data(self):
        showThread = threading.Thread(target=self.RT_Image)
        showThread.start()
        showThread.join()


if __name__ == '__main__':
    data = FromServer()
    data.Get_data()


服務端 :中轉視頻信號並保存數據在雲端

import socket
import cv2
import struct
import numpy
import threading
import time


class Transstation(object):
    def __init__(self, cam_ip, user_ip):
        self.cam_ip = cam_ip
        self.cam_Server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.cam_Server.bind(self.cam_ip)
        self.cam_Server.listen(5)
        print('服務器接收工作準備初始化完成!')

        self.user_ip = user_ip
        self.tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.tcpSocket.bind(self.user_ip)
        self.tcpSocket.listen(5)
        print('服務器發送工作準備初始化完成!')

        self.now_time = ''
        self.resolution = (640, 480)
        self.img_fps = 70

    def receiveVideoThread(self):     
        print("等待相機中。。。。。")
        clientSock, client_addr = self.cam_Server.accept()
        print("已經連接到相機,連接地址:{0}".format(client_addr))
        self.now_time = str(time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(self.now_time+ '.avi' ,fourcc, 35.0, (640,480))
        print('正在保存視頻 %s.avi'%self.now_time)
        flag = 1
        while True:
            tempdata = clientSock.recv(4)
            if cv2.waitKey(10) == 27:
                clientSock.close()
                out.release()
                break
            if len(tempdata) == 4:
                info = struct.unpack('i', tempdata)
                buf_size = int(info[0])
                if buf_size>0 and buf_size<500000:
                    try:
                        buf = b""
                        temp_buf = buf
                        while buf_size:
                            temp_buf = clientSock.recv(buf_size)
                            buf_size -= len(temp_buf)
                            buf += temp_buf
                        data = numpy.frombuffer(buf, dtype='uint8')
                        img = cv2.imdecode(data, 1)
                        self.image = img
                        out.write(img)
                        
                    except Exception as e:
                        print(e.args)
                        pass
    
    def sendVideoThread(self):
        print("等待用戶中。。。。。")
        clientSock, client_addr = self.tcpSocket.accept()
        print("已經連接到用戶,地址:{0}".format(client_addr))
        img_param = [int(cv2.IMWRITE_JPEG_QUALITY), self.img_fps]
        s_name = struct.pack(b'14s',str.encode(self.now_time))
        clientSock.send(s_name)
        while True:
            if cv2.waitKey(10) == 27:
                clientSock.close()
                break
            _, img_encode = cv2.imencode('.jpg', self.image, img_param)
            img_code = numpy.array(img_encode)
            img_data = img_code.tostring()
            try:
                packet = struct.pack(b'i',len(img_data))
                clientSock.send(packet)
                clientSock.send(img_data)
            except Exception as e:
                pass

    
    def start_work(self):
        camThread = threading.Thread(target=self.receiveVideoThread)
        usrThread = threading.Thread(target=self.sendVideoThread)
        camThread.start()
        usrThread.start()
        camThread.join()
        usrThread.join()

if __name__ == '__main__':
    cam_ip = ('172.17.xx', 8888) #私網 ip
    usr_ip = ('172.17.xx', 9999)#私網 ip
    server = Transstation(cam_ip,usr_ip)
    server.start_work()




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章