GithubHelp home page GithubHelp logo

Comments (3)

miguelgrinberg avatar miguelgrinberg commented on September 27, 2024

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.

root-hal9000 avatar root-hal9000 commented on September 27, 2024

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.

hyansuper avatar hyansuper commented on September 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.