updated to version 0.0.3

added vision daemon which runs in the background
added more documentation to vision module
now including opencv dependencies in complib package
This commit is contained in:
Joel 2021-01-21 23:55:26 +01:00
parent b06962e955
commit 77c2354c00
No known key found for this signature in database
GPG key ID: BDDDBECD0808290E
12 changed files with 201 additions and 16 deletions

View file

@ -6,6 +6,7 @@ from flask import Flask, Response
RTMP_SERVER = os.getenv("RTMP_SERVER", "rtmp://localhost/live/stream")
SERVE_VIDEO = os.getenv("SERVER_SRC", "/live")
BUILDING_DOCS = os.getenv("BUILDING_DOCS", "false")
app = Flask(__name__)
@ -27,7 +28,12 @@ HTML = HTML.replace("{{ VIDEO_DST }}", SERVE_VIDEO)
class __Streaming:
"""
Private class for opencv stuff.
Class that handles rtmp streaming for opencv.
DO NOT CREATE AN INSTANCE OF THIS CLASS YOURSELF!
This is automatically done when importing this module. Use Vision.Streaming which is
an instance of this class!
grab frames -> do your own processing -> publish frame -> view on http server
"""
@ -40,7 +46,7 @@ class __Streaming:
create an object of this class. (There can (SHOULD!) only be one VideCapture)
"""
self.__camera_stream = cv2.VideoCapture(RTMP_SERVER)
#self.__camera_stream = cv2.VideoCapture(0)
# self.__camera_stream = cv2.VideoCapture(0)
self.__newest_frame = None
self.__lock = threading.Lock()
@ -79,7 +85,7 @@ class __Streaming:
buffer_frame = self.__newest_frame.copy()
# encode frame for jpeg stream
(flag, encodedImage) = cv2.imencode(".jpg", buffer_frame)
(flag, encoded_image) = cv2.imencode(".jpg", buffer_frame)
# if there was an error try again with the next frame
if not flag:
@ -87,15 +93,17 @@ class __Streaming:
# else yield encoded frame with mimetype image/jpeg
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
bytearray(encoded_image) + b'\r\n')
# instantiate private class __Streaming
Streaming = __Streaming()
Streaming = None
if BUILDING_DOCS == "false":
# instantiate private class __Streaming
Streaming = __Streaming()
@app.route("/live")
def video_feed():
def __video_feed():
"""
Define route for serving jpeg stream.
@ -124,12 +132,13 @@ def __start_flask():
app.run(host="0.0.0.0", port=9898, debug=True, threaded=True, use_reloader=False)
# start flask service in the background
__webserver_thread = threading.Thread(target=__start_flask)
__webserver_thread.start()
if BUILDING_DOCS == "false":
# start flask service in the background
__webserver_thread = threading.Thread(target=__start_flask)
__webserver_thread.start()
# for debugging and testing start processing frames and detecting a 6 by 9 calibration chessboard
if __name__ == '__main__':
if __name__ == '__main__' and BUILDING_DOCS == "false":
while True:
frame = Streaming.get_frame()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
@ -140,5 +149,5 @@ if __name__ == '__main__':
# find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (6, 9), None)
cv2.drawChessboardCorners(frame, (6, 9), corners, ret)
cv2.drawChessboardCorners(frame, (6, 9), corners, ret)
Streaming.publish_frame(frame)