【问题标题】:Python webcam over socket套接字上的 Python 网络摄像头
【发布时间】:2021-01-27 14:13:02
【问题描述】:

我得到了从服务器到客户端的套接字屏幕共享脚本,有人能告诉我如何将它从屏幕转换为网络摄像头捕获(流)吗? 只是我如何将 sct.grab 行转换为网络摄像头并使其工作。 我尝试捕获图片,然后在图像上使用 open() 并发送其像素,但 pygame 说:“字符串长度不等于格式和分辨率大小”

服务器:

from socket import socket
from threading import Thread
from zlib import compress
from mss import mss



WIDTH = 1900
HEIGHT = 1000

def retreive_screenshot(conn):
    with mss() as sct:
        # The region to capture
        rect = {'top': 0, 'left': 0, 'width': WIDTH, 'height': HEIGHT}

        while 'recording':
            # Capture the screen
            img = sct.grab(rect)
            data = compress(img.rgb, 6)

            # Send the size of the pixels length
            size = len(data)
            size_len = (size.bit_length() + 7) // 8
            conn.send(bytes([size_len]))

            # Send the actual pixels length
            size_bytes = size.to_bytes(size_len, 'big')
            conn.send(size_bytes)

            # Send pixels
            conn.sendall(data)

def main(host='127.0.0.1', port=5555):
    sock = socket()
    sock.bind((host, port))
    try:
        sock.listen(5)
        print('Server started.')

        while 'connected':
            conn, addr = sock.accept()
            print('Client connected IP:', addr)
            thread = Thread(target=retreive_screenshot, args=(conn,))
            thread.start()
    finally:
        sock.close()


if __name__ == '__main__':
    main()

客户:

from socket import socket
from zlib import decompress
import pygame


WIDTH = 1900
HEIGHT = 1000


def recvall(conn, length):
    """ Retreive all pixels. """
    buffer = b''
    while len(buffer) < length:
        data = conn.recv(length - len(buffer))
        if not data:
            return data
        buffer += data
    return buffer



def main(host='127.0.0.1', port=5555):

    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    watching = True    

    s = socket()
    s.connect((host, port))
    try:
        while watching:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    watching = False
                    break

            # Retreive the size of the pixels length, the pixels length and pixels
            size_len = int.from_bytes(s.recv(1), byteorder='big')
            size = int.from_bytes(s.recv(size_len), byteorder='big')
            pixels = decompress(recvall(s, size))

            img = pygame.image.fromstring(pixels, (WIDTH, HEIGHT), 'RGB')
            screen.blit(img, (0, 0))
            pygame.display.flip()
            clock.tick(60)
    finally:
        s.close()

if __name__ == '__main__':
    main()

谢谢

【问题讨论】:

    标签: python sockets stream webcam


    【解决方案1】:

    好吧,我找到了一个解决方案,我只是尝试了它,就像最初提出一个新想法一样。我现在使用文件(缓存),转移像素,创建一个新的图像文件并使用 PyGame.image 中的 load() 方法。 仅供参考,这是一个基础代码,您可以按照自己的方式使用它并添加更多内容,并使其变得更好。

    这里是代码, 服务器:

    from socket import socket
    from threading import Thread
    from zlib import compress
    from cv2 import *
    
    
    WIDTH = 1900
    HEIGHT = 1000
    
    def retreive_screenshot(conn):
        cam = VideoCapture(0)
        while 'recording':
            # initialize the camera
            s, img = cam.read()
            imwrite("filename.jpg", img)  # save image
            f = open('filename.jpg', 'rb')
            data = compress(f.read(), 6)
            f.close()
    
            # Send the size of the pixels length
            size = len(data)
            size_len = (size.bit_length() + 7) // 8
            conn.send(bytes([size_len]))
    
            # Send the actual pixels length
            size_bytes = size.to_bytes(size_len, 'big')
            conn.send(size_bytes)
    
            # Send pixels
            conn.sendall(data)
    
    def main(host='127.0.0.1', port=5555):
        sock = socket()
        sock.bind((host, port))
        try:
            sock.listen(5)
            print('Server started.')
    
            while 'connected':
                conn, addr = sock.accept()
                print('Client connected IP:', addr)
                thread = Thread(target=retreive_screenshot, args=(conn,))
                thread.start()
        finally:
            sock.close()
    
    
    if __name__ == '__main__':
        main()
    

    客户:

    from socket import socket
    from zlib import decompress
    import pygame
    
    
    WIDTH = 1900
    HEIGHT = 1000
    
    
    def recvall(conn, length):
        """ Retreive all pixels. """
        buffer = b''
        while len(buffer) < length:
            data = conn.recv(length - len(buffer))
            if not data:
                return data
            buffer += data
        return buffer
    
    
    
    def main(host='127.0.0.1', port=5555):
    
        pygame.init()
        screen = pygame.display.set_mode((WIDTH, HEIGHT))
        clock = pygame.time.Clock()
        watching = True
    
        s = socket()
        s.connect((host, port))
        try:
            while watching:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        watching = False
                        break
    
                # Retreive the size of the pixels length, the pixels length and pixels
                size_len = int.from_bytes(s.recv(1), byteorder='big')
                size = int.from_bytes(s.recv(size_len), byteorder='big')
                pixels = decompress(recvall(s, size))
                ff = open('filenam.jpg', 'wb')
                ff.write(pixels)
                ff.close()
                img = pygame.image.load('filenam.jpg')
                screen.blit(img, (0, 0))
                pygame.display.flip()
                clock.tick(60)
        finally:
            s.close()
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 2015-07-28
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 2012-06-16
      相关资源
      最近更新 更多