python - 实例化后对象仍然没有?
问题描述
我正在使用树莓派和相机将提要流式传输到网络浏览器。我的 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()
解决方案
推荐阅读
- flutter - 输入'_InternalLinkedHashMap
' 不是类型 'FutureOr 的子类型 - >'颤振API问题
- go - 如何在golang中检查字符串是否包含大写字母?
- ios - 有谁知道这个 FIRMessaging 错误?
- opencv - 为什么当我使用 CPU 而不是 GPU 时 cv2.dnn 工作得更快?
- sql - 具有递归和更新内连接的 MariaDB SQL
- python - PyOpenGL 光照颜色格式问题
- kubernetes - Kubernetes 活性探测失败是自愿还是非自愿中断?
- java - 如何在按下按钮重定向到原始页面之前获取 url?春季MVC
- file - 致命错误:Python.h:没有这样的文件或目录 - 安装了 python3 开发工具包
- java - 如何从用户的 ID 中获取用户