我正在使用树莓派和相机将提要流式传输到网络浏览器。我的 Camera 类尝试两次实例化我的 CameraThread 类(如果 None 则尝试)并停止(因为 picamera 试图被两个不同的对象使用),但理论上这不应该发生。
我试图在 Camera 类的 if 语句中更新一个临时变量,只是为了查看,但该变量没有更新……我不确定这里的问题是什么。
这是我正在使用的代码:
class CameraThread(threading.Thread):
current_image = None
def __init__(self):
super().__init__()
self.event = threading.Event()
self.have_image_event = threading.Event()
self.start()
def stop(self):
self.event.set()
self.join()
def run(self):
try:
with picamera.PiCamera() as camera:
camera.resolution = (1296, 730)
capture_image = io.BytesIO()
temp1, temp2 = io.BytesIO(), io.BytesIO(),
default_time = 0.033
for _ in camera.capture_continuous(capture_image, format='jpeg'):
# Truncate the stream to the current position (in case
# prior iterations output a longer image)
capture_image.truncate()
capture_image.seek(0)
temp1.seek(0)
temp1.write(capture_image.read())
capture_image.seek(0)
temp1.truncate()
temp1.seek(0)
self.current_image = temp1
self.have_image_event.set()
temp1, temp2 = temp2, temp1
if self.event.wait(default_time):
break
except Exception as ex:
import traceback
traceback.print_exc()
class Camera(tornado.web.RequestHandler):
camera = None
def get(self):
if self.camera is None:
print(self.camera)
self.camera = CameraThread()
self.camera.have_image_event.wait()
self.set_header('Content-type', 'image/jpeg')
self.set_header('Content-length',
self.camera.current_image.getbuffer().nbytes)
self.set_header('Cache-Control', 'no-cache')
self.write(self.camera.current_image.read())
self.finish()