Python视频流初学者初始化相机时出错
我想在我的raspberry pi usb摄像头和笔记本电脑之间制作一个视频流 客户:Python视频流初学者初始化相机时出错,python,camera,video-streaming,pydev,Python,Camera,Video Streaming,Pydev,我想在我的raspberry pi usb摄像头和笔记本电脑之间制作一个视频流 客户: import io import socket import struct import time import pygame.camera # Connect a client socket to my_server:8000 (change my_server to the # hostname of your server) client_socket = socket.socket() client
import io
import socket
import struct
import time
import pygame.camera
# Connect a client socket to my_server:8000 (change my_server to the
# hostname of your server)
client_socket = socket.socket()
client_socket.connect(('192.168.1.12', 8000))
# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
with pygame.camera.Camera ()as camera:
camera.resolution = (640, 480)
# Start a preview and let the camera warm up for 2 seconds
camera.start_preview()
time.sleep(2)
# Note the start time and construct a stream to hold image data
# temporarily (we could write it directly to connection but in this
# case we want to find out the size of each capture first to keep
# our protocol simple)
start = time.time()
stream = io.BytesIO()
for foo in camera.capture_continuous(stream, 'jpeg'):
# Write the length of the capture to the stream and flush to
# ensure it actually gets sent
connection.write(struct.pack('<L', stream.tell()))
connection.flush()
# Rewind the stream and send the image data over the wire
stream.seek(0)
connection.write(stream.read())
# If we've been capturing for more than 30 seconds, quit
if time.time() - start > 30:
break
# Reset the stream for the next capture
stream.seek(0)
stream.truncate()
# Write a length of zero to the stream to signal we're done
connection.write(struct.pack('<L', 0))
finally:
connection.close()
client_socket.close()
导入io
导入套接字
导入结构
导入时间
导入pygame.camera
#将客户端套接字连接到my_服务器:8000(将my_服务器更改为
#服务器的主机名)
client_socket=socket.socket()
客户端_套接字连接('192.168.1.12',8000))
#从连接中生成类似文件的对象
connection=client\u socket.makefile('wb'))
尝试:
使用pygame.camera.camera()作为摄影机:
相机分辨率=(640480)
#开始预览,让相机预热2秒钟
camera.start_预览()
时间。睡眠(2)
#记下开始时间并构造一个流来保存图像数据
#暂时(我们可以直接将其写入连接,但在这种情况下
#我们首先要找出每次捕获的大小,以便保存
#我们的协议(简单)
开始=时间。时间()
stream=io.BytesIO()
对于相机中的foo.capture_continuous(流,'jpeg'):
#将捕获的长度写入流并刷新到
#确保它确实被发送
连接.写入(结构包('30:
打破
#为下一次捕获重置流
stream.seek(0)
stream.truncate()
#在流中写入一个长度为零的值,表示我们完成了
connection.write(struct.pack(“发生这种情况是因为在调用Camera()之前没有初始化pygame)
首先,确保导入所需的所有内容:
import pygame
import pygame.camera
from pygame.locals import *
现在您必须初始化pygame和pygame.camera:
pygame.init()
pygame.camera.init()
最后,如您所做的那样创建摄影机对象:
with pygame.camera.Camera ()as camera:
blablabla...
with pygame.camera.Camera ()as camera:
blablabla...