【问题标题】:I want to stream a webcam feed using socket programming in Python我想在 Python 中使用套接字编程流式传输网络摄像头提要
【发布时间】:2013-02-06 20:47:56
【问题描述】:

这是我的代码:

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

我哪里做错了?

【问题讨论】:

  • 你的相机分辨率好像不是640x480,试试用image.get_size()代替(640,480)
  • 我说的对吗?在 server.py 中?我不明白,如果数据相同,那么两个程序之间的大小如何变化?
  • 我的意思是,在发送图像之前,您应该考虑发送相机图像分辨率的大小。这可能有效。

标签: python sockets camera streaming pygame


【解决方案1】:

问题不在于相机。

问题是您通过套接字发送了一个非常大的字符串,并且您错误地假设您可以使用conn.recv(921637) 一次读取整个字符串。

您必须多次致电recv 才能接收所有数据。尝试在调用pygame.image.fromstring 之前打印client.py 中发送的data 的长度并在server.py 中打印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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2013-05-14
    • 2014-06-18
    • 2018-04-05
    • 2016-10-23
    • 2012-10-08
    相关资源
    最近更新 更多