Comments (3)
It's been a while since I played with picamera, but I would imagine you need to stop the stream, make the necessary changes and then restart the stream with the new settings. So you need to use some signaling mechanism (maybe an event object) to let the background thread know that it needs to exit the streaming loop before you make changes.
from flask-video-streaming.
Hey Miguel,
here are some preliminary results, managed to stop/start thread - see code below. Seems like a dirty method, since I am sure it is probably horrible for performance, but here it is. I have managed to stop and start the thread by setting a 1 or 0 flag in a file using a route for /start & /stop and then checking for it on the base_camera._thread() method. Then I can change settings on camera_pi.frames() - If you have any other suggestions, please let me know. I didn't even know where to look for how to use events as you mentioned (really new on python and flask, first project), so any tips on where to start for a better method than using writing/reading files would be great - tried to use session, but couldn't read it in base_camera._thread(). or is there a way to control the thread from app.py?
app.py
@app.route('/start')
def start():
file = open('status', 'w+')
file.write('1')
file.close()
return render_template('index.html')
@app.route('/stop')
def stop():
file = open('status', 'w+')
file.write('0')
file.close()
return render_template('index.html')
base_camera.py
@classmethod
def _thread(cls):
"""Camera background thread."""
print('Starting camera thread.')
frames_iterator = cls.frames()
for frame in frames_iterator:
BaseCamera.frame = frame
BaseCamera.event.set() # send signal to clients
time.sleep(0)
# check to see if a stop flag has been set
try:
file = open('status', 'r+')
status = file.read()
file.close()
if status == '0':
print('stopping camera')
break
except:
print('no file')
# if there hasn't been any clients asking for frames in
# the last 10 seconds then stop the thread
if time.time() - BaseCamera.last_access > 10:
frames_iterator.close()
print('Stopping camera thread due to inactivity.')
break
BaseCamera.thread = None
then handle files for changing settings here on camera_pi.py
class Camera(BaseCamera):
@staticmethod
def frames():
# with Camera.set_res(1920, 1080) as camera:
with picamera.PiCamera() as camera:
# let camera warm up
time.sleep(2)
# TODO - check files and adjust settings here, tested with hflip and resolution and they work if set in this method
# file = open('flip', 'w+')
# if file.read() == '1':
# camera.hflip = True
# if file.read() == '0':
# camera.hflip = False
# print(file.read())
# file.close()
# camera.resolution = (1024, 768)
stream = io.BytesIO()
for _ in camera.capture_continuous(stream, 'jpeg',
use_video_port=True):
# return current frame
stream.seek(0)
yield stream.read()
# reset stream for next frame
stream.seek(0)
stream.truncate()
from flask-video-streaming.
This may help. picamera settings can be changed via url parameter, see https://github.com/hyansuper/picam-video-streaming
from flask-video-streaming.
Related Issues (20)
- How to send meta data Along with the image stream ? HOT 3
- The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a generator. HOT 3
- How does darknet yolo call the USB camera? HOT 1
- I need this to run in SSL HOT 1
- How to control the speed of video feed flow showing in the webpage? HOT 2
- Can we sync Flask streaming response with OpenCV FPS HOT 5
- IOError: [Errno 104] Connection reset by peer HOT 5
- Allways save the video as mp4 file? HOT 3
- I want to send a string alongwith each frame. HOT 4
- Loading the video stream freezes up all other responses from the Flask server HOT 4
- resetting yield generator during long run HOT 5
- Frames Are Displayed With Delay of One Frame
- Gstreamer backend
- Rotated Pictures HOT 1
- Hello, I don't know why the front-end console can receive a bunch of late results only after the back-end program is finished
- Publish the video through MotionEye HOT 6
- Picamera2/libcamera compatibility HOT 1
- Concurrency question HOT 3
- Stopping/Restarting video stream? HOT 4
- Return still image when requested HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask-video-streaming.