生成器无法在Python中使用字节连接
我有一个类,它使用生成器无法在Python中使用字节连接,python,flask,generator,Python,Flask,Generator,我有一个类,它使用get\u frame方法从相机获取帧。在web环境中,我需要在每个帧周围添加一些数据,然后再将其流式传输到浏览器。当我尝试将额外信息(一些字节)添加到帧时,我得到类型错误:无法将字节添加到生成器。如何连接此数据 def gen(): camera = VideoCamera() while True: frame = camera.get_frame() yield (b'--frame\r\n'
get\u frame
方法从相机获取帧。在web环境中,我需要在每个帧周围添加一些数据,然后再将其流式传输到浏览器。当我尝试将额外信息(一些字节
)添加到帧时,我得到类型错误:无法将字节添加到生成器
。如何连接此数据
def gen():
camera = VideoCamera()
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
class VideoCamera():
def __init__(self):
self.video = cv2.VideoCapture(0)
def get_frame(self):
while(True):
ret, frame = self.video.read()
#that face is the list, which has all the detected faces in the frame, using dlib library
face = detector(gray, 0)
for (J, rect) in enumerate(face):
ret, jpeg = cv2.imencode('.jpg', frame)
yield jpeg.tobytes()
如前所述,调用
get_frame
将返回一个生成器,而不是单个帧。您需要迭代该生成器以获得各个帧,然后可以将这些帧和其他数据一起生成
def gen():
camera = VideoCamera()
for frame in camera.get_frame():
yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n'
yield frame
yield b'\r\n\r\n'