I've created a PPE detector on openCV-Python and I want to display image based on the condition that the person is wearing a PPE or not. Code below opens camera and check if the person is wearing the PPE or not?
def generate_frames():
while True:
## read the camera frame
success, frame = camera.read()
if not success:
break
else:
image_np, boxes = get_detection(frame)
if boxes[:, -1].tolist().count(0.) > 0: # not wearing PPE
filename = r'\images\cross.png'
else: # wearing PPE
filename = r'\images\thumbs_up.gif'
return filename
Following is the index and image code.
@app.route('/')
def index():
return render_template('index.html')
@app.route('/image')
def image():
return send_file(generate_frames(), mimetype='image/gif')
The index.html
is below
<!DOCTYPE html>
<html>
<body>
<h1>Live streaming</h1>
<div>
<img src="{{ url_for('image') }}" width="50%"/>
</div>
</body>
</html>
Question
I am having trouble identifying why the image display don't change?
So for example if in the first frame person is wearing the PPE the filename won't switch even if in the next frame the person is not wearing the PPE and similarly the image won't change. The filename remains static based on which condition is fulfilled by first frame.
I tried using yield
instead of return
but I get an error
AttributeError: 'generator' object has no attribute 'read'
Edit Upon refreshing the image do change but I want it to change dynamically.
CodePudding user response:
It's unclear to me what you're trying to do but I believe the whole logic is broken.
Looks like you'd like generate_frames
to generate images while looping and those images would refresh automatically in the browser. This is not the say things work.
You need to find another mechanism to refresh. Perhaps and asynchronous call using JS to the route generating the image, or some page autorefresh mechanism.
Also, in generate_frames
, I think you'll want to replace break
with continue
(and the else
statement is useless).