usando pygame para transmitir sobre sockets en error de python

Estoy trabajando con un script de webcam que obtuve de internet en Python y estoy usando el módulo pygame. El código es

import socket import pygame import sys port=5014 #create pygame screen screen = pygame.display.set_mode((800,600),0) while True: s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("",port)) # server is available on the whole network by setting host to "" s.listen(1) connection, addr = s.accept() received = [] # loop .recv, it returns empty string when done, then transmitted data is completely received while True: recvd_data = connection.recv(1440021) if not recvd_data: break else: received.append(recvd_data) dataset = ''.join(received) image = pygame.image.fromstring(dataset,(800,600),"RGB") # convert received image from string #image = pygame.transform.scale(image,(800,600)) # scale image to 800*600 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() 

y el código del cliente es

  import socket import pygame import pygame.camera import sys import time host = "localhost" port = 5014 pygame.init() pygame.camera.init() cam_list = pygame.camera.list_cameras() # list available cameras webcam = pygame.camera.Camera(cam_list[0],(800,600)) # use first camera in list and set resolution webcam.start() # start camera while True: image = webcam.get_image() # capture image data = pygame.image.tostring(image,"RGB") # convert captured image to string, use RGB color scheme #print sys.getsizeof(data) # in case somebody wants to know the size of the captured image # prepare for connection to server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP is used s.connect((host, port)) s.sendall(data) s.close() time.sleep(0.1) 

el error que recibo en el servidor es

  Traceback (most recent call last): File "/root/Desktop/serv.py", line 29, in  image = pygame.image.fromstring(dataset,(800,600),"RGB") # convert received image from string ValueError: String length does not equal format and resolution size 

y el error que recibo en el cliente es

 Traceback (most recent call last): File "/root/Desktop/cli.py", line 28, in  s.sendall(data) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 104] Connection reset by peer 

Alguien sabe qué podría estar mal

Estaba usando el mismo código y obtuve un error similar, la solución fue reducir la resolución de la cámara web porque la mía no podía manejar el 800×600.

También cambié el “servidor” y el “cliente” para que el servidor de weebcam actúe como el “servidor de socket”

intente el siguiente código, asegúrese de que su video sea correcto, en mi ejemplo “/ dev / video0” el suyo podría ser diferente. Inicia primero el servidor de webcam.

Servidor de webcam:

 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/video0",(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() 

Servidor de cliente:

 import socket import pygame import sys host = "10.0.0.13" 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) 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()