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

General discussion • Issue with 2 python scripts running simultaneously

$
0
0
RPi 3 B+ Bookworm 64bit
OV5647 camera + PIR motion sensor

File1.py records video when motion is detected and saves it as vid.h264
File2.py runs, in my test, every 3 minutes from cron. It converts vid.h264 into vid.mp4, deletes vid.h264 and finally it invokes another python script to upload vid.mp4 to a cloud.

They work okay independently but when File1.py is running without motion detection and File2.py starts executing from cron, File1.py starts detecting motion like
Motion detected!
(Elapsed time : 3 secs)
Ready
Motion detected!
(Elapsed time : 3 secs)
Ready
Motion detected!
(Elapsed time : 4 secs)
Ready
Motion detected!
(Elapsed time : 2 secs)
Ready
Motion detected!
(Elapsed time : 4 secs)
Ready
Motion detected!
(Elapsed time : 4 secs)
Ready
Motion detected!
(Elapsed time : 4 secs)
Ready
despite no motion could have been detected.
This happens every 3 minutes when cron triggers File2.py

I don't realize what could be happening here.
Thanks in advance for any clue.


File1.py

Code:

from picamera2.encoders import H264Encoder, Qualityfrom picamera2.outputs import CircularOutputfrom picamera2 import Picamera2import time, osfrom datetime import datetimeimport RPi.GPIO as GPIOpicam2 = Picamera2()video_config = picam2.create_video_configuration(main={"size":(1280,960)})picam2.configure(video_config)encoder = H264Encoder(bitrate=10000000)output = CircularOutput()picam2.start_recording(encoder, output) #####GPIO.setmode(GPIO.BCM)GPIO_PIR = 4#print("PIR Module Holding Time Test (CTRL-C to exit)")#GPIO.setup(GPIO_PIR,GPIO.IN)Current_State  = 0Previous_State = 0try:  print("Waiting for PIR to settle ...")  # Loop until PIR output is 0  while GPIO.input(GPIO_PIR)==1:    Current_State  = 0  print("  Ready")  # Loop until users quits with CTRL-C  while True :    # Read PIR state    Current_State = GPIO.input(GPIO_PIR)    if Current_State==1 and Previous_State==0: # PIR is triggered      start_time=time.time()      print("  Motion detected!")      fecha = datetime.now().strftime("%a-%d.%m.%Y-%H_%M_%S")      nombre = '/home/pi/Videos/PIR-IR-' + fecha + '.h264'      nombre2 = '/home/pi/Videos/PIR-IR-' + fecha + '.mp4'      output.fileoutput = nombre ####      output.start()      # Record previous state      Previous_State=1    elif Current_State==0 and Previous_State==1:      # PIR has returned to ready state      stop_time=time.time()      ####picam2.stop_recording()      output.stop()      elapsed_time=int(stop_time-start_time)      print(" (Elapsed time : " + str(elapsed_time) + " secs)")      print("  Ready ")      Previous_State=0except KeyboardInterrupt:  print("  Quit")  # Reset GPIO settings  GPIO.cleanup()
File2.py
import fnmatch
import os

base = '/home/pi/Videos/'
for fileH264 in os.listdir(base):
if fnmatch.fnmatch(fileH264, '*.h264'):
print(fileH264)
fileMP4 = fileH264[:-4] + 'mp4'
os.system("ffmpeg -i " + base +fileH264 + " -c copy " + base + fileMP4)
os.system("rm " + base + fileH264)
os.system("/home/pi/venv/bin/python /home/pi/apps/pcloud.py " + base + fileMP4)

Statistics: Posted by marciano — Tue Apr 23, 2024 10:09 pm



Viewing all articles
Browse latest Browse all 2501

Trending Articles