''bytes' object has no attribute 'condition'
I am trying to stream my Rpi (zero) camera (v2.0) to HTML using cv2.VideoCapture.
However, I am facing a lot of errors and I'm currently stuck on this one as there are no similar issues online currently. Below is my full code (ps this code is from many different sources combined together):
import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server
import cv2
camera = cv2.VideoCapture(0)
PAGE="""\
<html>
<head>
<title>Raspberry Pi - Surveillance Camera</title>
</head>
<body>
<center><h1>Raspberry Pi - Surveillance Camera</h1></center>
<center><img src="stream.mjpg" width="640" height="480"></center>
</body>
</html>
"""
class StreamingHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = PAGE.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', len(content))
self.end_headers()
self.wfile.write(content)
elif self.path == '/stream.mjpg':
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
try:
while True:
with output.condition:
output.condition.wait()
frame = output.frame
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
def generate_frames():
while True:
ret, frame = camera.read()
if not ret:
break
else:
ret, buffer = cv2.imencode('.jpg',frame)
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
if __name__=="__main__":
output = generate_frames()
output = output.__next__()
try:
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
finally:
camera.release()
Error Message:
192.168.18.17 - - [07/May/2022 11:38:11] "GET /index.html HTTP/1.1" 200 -
192.168.18.17 - - [07/May/2022 11:38:11] "GET /stream.mjpg HTTP/1.1" 200 -
WARNING:root:Removed streaming client ('192.168.18.17', 51120): 'bytes' object has no attribute 'condition'
It seems that there is no problem with the server as the page is able to load, however, there is no camera stream available. I believe I have made some errors in my generate_frames() function but I don't know where and why.
Any help would be greatly appreciated. Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
