Pygame 网络摄像头流客户端无法使用 python 3.4 执行
Pygame webcam streaming client can't execute with python 3.4
我正在使用 pygame 和 python3.4 在 Odroid 开发板上流式传输网络摄像头。服务器是这样的(提取自 post: using pygame to stream over sockets in python error ):
import socket
import pygame
import pygame.camera
import sys
import time
port = 5000
pygame.init()
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("",port))
serversocket.listen(1)
pygame.camera.init()
webcam = pygame.camera.Camera("/dev/video1",(320,240))
webcam.start()
while True:
connection, address = serversocket.accept()
image = webcam.get_image() # capture image
data = pygame.image.tostring(image,"RGB") # convert captured image to string, use RGB color scheme
connection.sendall(data)
time.sleep(0.1)
connection.close()
服务器在 python 和 python 3.4 中工作正常。
但是当我使用 python 3.4 执行客户端时,出现以下错误:
Traceback (most recent call last):
File "client.py", line 30, in
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
TypeError: must be bytes, not str
客户如下:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import socket
import pygame
import sys
host = "192.168.45.103"
port=5000
screen = pygame.display.set_mode((320,240),0)
while True:
clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect((host, port))
received = []
# loop .recv, it returns empty string when done, then transmitted data is completely received
while True:
#print("esperando receber dado")
recvd_data = clientsocket.recv(230400)
if not recvd_data:
break
else:
received.append(recvd_data)
#dataset = ''.join(received)
dataset = ','.join(str(v) for v in received)
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
screen.blit(image,(0,0)) # "show image" on the screen
pygame.display.update()
# check for quit events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
我更改了行 dataset = ''.join(received) for dataset = ','.join(str(v) for v in received) 因为我在某处读到 python3.x 它必须是这个。
现在的错误行是:image = pygame.image.fromstring(dataset,(320,240),"RGB")
谢谢!
这两行显然是错误的:
dataset = ','.join(received)
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received
如果 dataset
包含二进制 pxel 数据,则不应将接收到的字节与“,”连接起来:它只会添加丢失的无关“,”(十进制 44)字节作为像素数据中的垃圾 - 前一行,使用 "join" 和空字符串会起作用(在 Python 2.x 中)因为调用空字符串,join 只是连接各种数据,这就是你想要的。
在 Python3 中,二进制数据(例如您接收的像素数据)的处理已与文本处理分开 - 并且您使用的 '""' 空字符串是表示空的对象text - 不同于 Python 2.x 中的空字节 - 但你可以简单地用 b
作为前缀来表示字节串(这是你想要的)。
总而言之,尝试使用:
dataset = b''.join(str(v) for v in received)
image = pygame.image.fromstring(dataset,(320,240),"RGB")
我正在使用 pygame 和 python3.4 在 Odroid 开发板上流式传输网络摄像头。服务器是这样的(提取自 post: using pygame to stream over sockets in python error ):
import socket
import pygame
import pygame.camera
import sys
import time
port = 5000
pygame.init()
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(("",port))
serversocket.listen(1)
pygame.camera.init()
webcam = pygame.camera.Camera("/dev/video1",(320,240))
webcam.start()
while True:
connection, address = serversocket.accept()
image = webcam.get_image() # capture image
data = pygame.image.tostring(image,"RGB") # convert captured image to string, use RGB color scheme
connection.sendall(data)
time.sleep(0.1)
connection.close()
服务器在 python 和 python 3.4 中工作正常。 但是当我使用 python 3.4 执行客户端时,出现以下错误:
Traceback (most recent call last): File "client.py", line 30, in image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string TypeError: must be bytes, not str
客户如下:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import socket
import pygame
import sys
host = "192.168.45.103"
port=5000
screen = pygame.display.set_mode((320,240),0)
while True:
clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect((host, port))
received = []
# loop .recv, it returns empty string when done, then transmitted data is completely received
while True:
#print("esperando receber dado")
recvd_data = clientsocket.recv(230400)
if not recvd_data:
break
else:
received.append(recvd_data)
#dataset = ''.join(received)
dataset = ','.join(str(v) for v in received)
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received image from string
screen.blit(image,(0,0)) # "show image" on the screen
pygame.display.update()
# check for quit events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
我更改了行 dataset = ''.join(received) for dataset = ','.join(str(v) for v in received) 因为我在某处读到 python3.x 它必须是这个。 现在的错误行是:image = pygame.image.fromstring(dataset,(320,240),"RGB")
谢谢!
这两行显然是错误的:
dataset = ','.join(received)
image = pygame.image.fromstring(dataset,(320,240),"RGB") # convert received
如果 dataset
包含二进制 pxel 数据,则不应将接收到的字节与“,”连接起来:它只会添加丢失的无关“,”(十进制 44)字节作为像素数据中的垃圾 - 前一行,使用 "join" 和空字符串会起作用(在 Python 2.x 中)因为调用空字符串,join 只是连接各种数据,这就是你想要的。
在 Python3 中,二进制数据(例如您接收的像素数据)的处理已与文本处理分开 - 并且您使用的 '""' 空字符串是表示空的对象text - 不同于 Python 2.x 中的空字节 - 但你可以简单地用 b
作为前缀来表示字节串(这是你想要的)。
总而言之,尝试使用:
dataset = b''.join(str(v) for v in received)
image = pygame.image.fromstring(dataset,(320,240),"RGB")