我想用Python的套接字编程流式传输摄像头视频

7 投票
1 回答
15892 浏览
提问于 2025-04-17 16:38

这是我的代码:

server.py:

#The server receives the data

import socket
from PIL import Image
import pygame,sys
import pygame.camera
from pygame.locals import *
import time

host = "localhost"
port = 1890
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print "connected by",addr

screen = pygame.display.set_mode((640,480))

while 1:
        data = conn.recv(921637)
        image = pygame.image.fromstring(data,(640,480),"RGB")
        screen.blit(image,(0,0))
        pygame.display.update()
        if not image: 
               break;
        conn.send(data)
conn.close()

client.py

#The client sends the data

import socket
from PIL import Image
import pygame,sys
import pygame.camera
from pygame.locals import *
import time

host = "localhost"
port = 1890
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))


pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()

while 1:
        image = cam.get_image()
        data = pygame.image.tostring(image,"RGB")
        s.sendall(data)     

s.close()
print "recieved", repr(data)

为了测试,我试了以下代码,运行得很好,但上面的代码却不行...

没有使用套接字时能正常工作的代码:camcapture.py

import sys
import time
import pygame
import pygame.camera
from pygame.locals import *

pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()

screen = pygame.display.set_mode((640,480))

while 1:
        image = cam.get_image()
        data = pygame.image.tostring(image,"RGB")
        img = pygame.image.fromstring(data,(640,480),"RGB")
        screen.blit(img,(0,0))
        pygame.display.update()

错误信息是:

image = pygame.image.fromstring(data,(640,480),"RGB")
ValueError: String length does not equal format and resolution size

我哪里出错了?

1 个回答

10

问题不在于相机。

问题在于你通过网络发送了一个非常大的字符串,而你错误地认为可以一次性用 conn.recv(921637) 读取整个字符串。

你需要多次调用 recv 来接收所有的数据。试着在 client.py 中打印你发送的 data 的长度,然后在 server.py 中调用 pygame.image.fromstring 之前打印 data 的长度,你就会明白了。

解决这个问题有几种方法:

  • 每发送一张图片就建立一个新的连接
  • 发送数据的大小,这样服务器就知道要读取多少数据
  • 使用某种结束标记

下面是一个简单的例子:

发送方:

import socket
import pygame
import time

host = "localhost"
port = 1890
pygame.init()
image = pygame.surface.Surface((640, 480))
i=0
j=0
while 1:
    image.fill((i,j,0))
    i+=10
    j+=5
    if i >= 255: i = 0
    if j >= 255: j = 0
    data = pygame.image.tostring(image,"RGB")
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    s.sendall(data)
    s.close()
    time.sleep(0.5)

接收方:

import socket
import pygame

host="localhost"
port=1890

screen = pygame.display.set_mode((640,480))

while 1:
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host,port))
    s.listen(1)
    conn, addr = s.accept()
    message = []
    while True:
        d = conn.recv(1024*1024)
        if not d: break
        else: message.append(d)
    data = ''.join(message)
    image = pygame.image.fromstring(data,(640,480),"RGB")
    screen.blit(image,(0,0))
    pygame.display.update()

撰写回答