Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python错误中使用pygame在套接字上传输数据流_Python_Sockets_Networking_Pygame_Webcam - Fatal编程技术网

在python错误中使用pygame在套接字上传输数据流

在python错误中使用pygame在套接字上传输数据流,python,sockets,networking,pygame,webcam,Python,Sockets,Networking,Pygame,Webcam,我正在用python编写一个网络摄像头脚本,我使用的是pygame模块,代码是 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))

我正在用python编写一个网络摄像头脚本,我使用的是pygame模块,代码是

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()
客户端代码是

    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)
我在服务器上遇到的错误是

    Traceback (most recent call last):
  File "/root/Desktop/serv.py", line 29, in <module>
    image = pygame.image.fromstring(dataset,(800,600),"RGB") # convert received image from string
ValueError: String length does not equal format and resolution size
回溯(最近一次呼叫最后一次):
文件“/root/Desktop/serv.py”,第29行,在
image=pygame.image.fromstring(数据集,(800600),“RGB”)#从字符串转换接收到的图像
ValueError:字符串长度不等于格式和分辨率大小
我在客户机上得到的错误是

Traceback (most recent call last):
  File "/root/Desktop/cli.py", line 28, in <module>
    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
回溯(最近一次呼叫最后一次):
文件“/root/Desktop/cli.py”,第28行,在
s、 sendall(数据)
文件“/usr/lib/python2.7/socket.py”,第224行,meth格式
返回getattr(self.\u sock,name)(*args)
socket.error:[Errno 104]对等方重置连接

有人知道会出什么问题吗

我使用了相同的代码,得到了类似的错误,解决方案是降低网络摄像头的分辨率,因为我的摄像头无法处理800x600

我还更改了“服务器”和“客户端”,使weebcam服务器的行为类似于“套接字服务器”

请尝试以下代码,确保您的视频是正确的,在我的示例“/dev/video0”中,您的视频可能会有所不同。首先启动网络摄像头服务器

网络摄像头服务器:

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()
客户端服务器:

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()