'How can I use parent variable in child file at flask blueprint mode?
I want to access same webcam resource and dispatch the frame to different router. And support multiple clients.
To achieve this I reference flasky and pyimage.
This is my code simple architecture

My code snippet as following: - init.py
from flask import Blueprint
import threading
import cv2
from flask import Response
import time
api = Blueprint('api', __name__)
outputFrame = None
lock = threading.Lock()
camera = cv2.VideoCapture(0)
time.sleep(2.0)
def get_frames():
global camera, outputFrame, lock
while True:
success, frame = camera.read()
if not success:
print("If not success")
break
else:
with lock:
outputFrame = frame.copy()
from . import fileA
- fileA.py
from . import api ,outputFrame, lock
from flask import render_template, Response
import cv2
import threading
def gen_frames():
while True:
with lock:
if outputFrame is None:
continue
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
if not flag:
continue
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
@api.route('/templateA/')
def page():
return render_template("fileA.html")
@api.route('/video_feed')
def video_feed():
return Response(gen_frames(),
mimetype='multipart/x-mixed-replace; boundary=frame')
The output is : outputFrame is None.
How can I get this frame information in fileA?
Any help will appreciate.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
