简体   繁体   中英

Control picamera with different scripts

I would like to control a raspberry camera with picamera with python. I created a QT interface with buttons and I want to start and stop recording with them, for example, clicking on a button1, I call a python script that start recording, clicking on button2 the record is stopped. Of course I don't know if it makes sense, in both scripts I have to allocate a "picamera" object but I don't know if they will refer to the same object.. Ideas? Otherwise I'm able only to record for a determined amount of time starting, waiting a time and stopping the record in the same script. Thanks

Simply put your code which will start recording and stop recording in a python script. Make them available via REST API (HTTP endpoints). For this Python-Flask will help you to create picamera object globally and then define two methods (functions) in a python script that will start your recording and stop your recording. Map those two method with a URL (HTTP endpoint).

On client side (QT Interface), when you click buttons simply call your REST APIs (Your HTTP URLs).

This is how I am doing, I have web application (HTML and JS) that controls my camera and in back-end I have python, picamera and flask.

I created a file python and it runs with Flask. It could help someone else, so I report here my code:

import datetime as dt

from picamera import PiCamera
from time import sleep
from datetime import datetime

from flask import Flask
app = Flask(__name__)


camera = PiCamera()

@app.route('/')
def hello_word():
    return 'Camera control ready'

@app.route('/StartRecord')
def start_record():
    global camera
    timestamp = datetime.now().strftime("%y%m%d_%H%M%S")
    camera.resolution = (1920, 1080)
    camera.rotation = 180
    camera.start_recording('/home/pi/Videos/Video_{}.h264'.format(timestamp))

    return 'Camera recording...'

@app.route('/StopRecord')
def stop_record():
    global camera
    camera.stop_recording()
        return 'Camera video stopped!'

@app.route('/TakePicture')
def take_picture():
        global camera
        timestamp = datetime.now().strftime("%y%m%d_%H%M%S")
        camera.resolution = (1920, 1080)
    camera.rotation = 180
        camera.capture('/home/pi/Pictures/Photo_{}.jpg'.format(timestamp))

        return 'Camera photo captured!'

@app.route('/StartLive')
def start_live():
        global camera
        camera.preview_fullscreen=False
        camera.resolution =(944, 600)
    camera.rotation = 180
        camera.preview_window=(0,0,944,600)

        preview = camera.start_preview()

    return 'Camera start live!'

@app.route('/StopLive')
def stop_live():
        global camera
        camera.stop_preview()
        return 'Camera stop live!'

if __name__ == '__main__':
    app.run(host='0.0.0.0')

@webDev, please, if you have any additional hints, let me know. thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM