Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8037

Python • Re: Detect motion with pi camera v1

$
0
0
Thanks a lot.
The output file if very low res and low framerate.
I tried a few different option with no luck.

Code:

#!/usr/bin/python3import timeimport numpy as npfrom picamera2 import Picamera2from picamera2.encoders import H264Encoderfrom picamera2.outputs import FileOutputlsize = (320, 240)picam2 = Picamera2()video_config = picam2.create_video_configuration(main={"size": (1280, 720), "format": "RGB888"},                                                 lores={"size": lsize, "format": "YUV420"})picam2.configure(video_config)encoder = H264Encoder(1000000)picam2.start()w, h = lsizeprev = Noneencoding = Falseltime = 0while True:    cur = picam2.capture_buffer("lores")    cur = cur[:w * h].reshape(h, w)    if prev is not None:        # Measure pixels differences between current and        # previous frame        mse = np.square(np.subtract(cur, prev)).mean()        if mse > 7:            if not encoding:                encoder.output = FileOutput(f"{int(time.time())}.h264")                picam2.start_encoder(encoder)                encoding = True                print("New Motion", mse)            ltime = time.time()        else:            if encoding and time.time() - ltime > 2.0:                picam2.stop_encoder()                encoding = False    prev = cur

Statistics: Posted by dror_israel — Sat Oct 26, 2024 12:03 pm



Viewing all articles
Browse latest Browse all 8037

Trending Articles