Pygame webcam streaming client can't execute with python 3.4 -
i'm using pygame , python3.4 stream webcam in odroid board. server (extracted of post: using pygame stream on 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 string, use rgb color scheme connection.sendall(data) time.sleep(0.1) connection.close() the server works ok in python , python 3.4. when execute client python 3.4 following error:
traceback (most recent call last): file "client.py", line 30, in image = pygame.image.fromstring(dataset,(320,240),"rgb") # convert received image string typeerror: must bytes, not str
the client following:
#!/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, returns empty string when done, transmitted data 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) v in received) image = pygame.image.fromstring(dataset,(320,240),"rgb") # convert received image string screen.blit(image,(0,0)) # "show image" on screen pygame.display.update() # check quit events event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() sys.exit() i changed line dataset = ''.join(received) dataset = ','.join(str(v) v in received) because read somwhere in python3.x mst this. error line is: image = pygame.image.fromstring(dataset,(320,240),"rgb")
thanks!
these 2 lines appear plain wrong:
dataset = ','.join(received) image = pygame.image.fromstring(dataset,(320,240),"rgb") # convert received if dataset contain binary pxel data, should not concatenating bytes receive ",": add lost of extraneous "," (decimal 44) bytes garbage in pixel data - previous line, using "join" empty string work (in python 2.x) becuase called upon empty string, join concatenates various piece of data, want.
in python3, handling of binary data (such pixel data receiving) has been separated text handling - , '""' empty string using object representing empty text - different empty bytes python 2.x - can prefix b denoting bytes-string (which want).
all in all, try using:
dataset = b''.join(str(v) v in received) image = pygame.image.fromstring(dataset,(320,240),"rgb")
Comments
Post a Comment