-
-
Save befinitiv/fd44a597a48460c100a54649c97d5984 to your computer and use it in GitHub Desktop.
#!/usr/bin/python | |
import glob | |
import socket | |
import time | |
from threading import Timer | |
import picamera | |
import RPi.GPIO as GPIO | |
SENSOR_PIN = 18 | |
W = 1296 | |
H = 972 | |
FPS = 18 | |
IP = '192.168.0.21' | |
PORT = 5000 | |
def start_recording(): | |
global camera_data | |
global camera | |
camera_data = CameraData() | |
camera.start_recording(camera_data, format='h264') | |
print('start') | |
def stop_recording(): | |
global camera_data | |
global camera | |
camera.stop_recording() | |
camera_data.stop() | |
print('stop') | |
def timer_stop(): | |
global stop_timer_running | |
stop_timer_running = False | |
stop_recording() | |
last_sensor_event = 0 | |
stop_timer_running = False | |
def sensor_change(pin): | |
global last_sensor_event | |
global stop_timer | |
global stop_timer_running | |
now = time.time() | |
if abs(last_sensor_event - now) > 0.5: | |
start_recording() | |
last_sensor_event = now | |
if stop_timer_running: | |
stop_timer.cancel() | |
stop_timer = Timer(0.5, timer_stop) | |
stop_timer.start() | |
stop_timer_running = True | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(SENSOR_PIN, GPIO.IN) | |
GPIO.add_event_detect(SENSOR_PIN, GPIO.BOTH, sensor_change) | |
# This class writes data to file and streams it in parallel | |
class CameraData(object): | |
def __init__(self): | |
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
self.client_socket.connect((IP, PORT)) | |
self.connection = self.client_socket.makefile('wb') | |
self.file = open(self.create_unique_file_name(), 'wb') | |
def create_unique_file_name(self): | |
return '%d.h264' % len(glob.glob('*')) | |
def write(self, s): | |
self.connection.write(s) | |
self.file.write(s) | |
def flush(self): | |
pass | |
def stop(self): | |
self.connection.close() | |
self.client_socket.close() | |
self.file.close() | |
# Make a file-like object out of the connection | |
try: | |
camera = picamera.PiCamera() | |
camera.resolution = (W, H) | |
camera.framerate = FPS | |
while True: | |
time.sleep(1) | |
#camera.wait_recording(60) | |
finally: | |
pass |
[Unit] | |
Description=Analog camera service | |
After=network.target | |
StartLimitIntervalSec=0 | |
[Service] | |
Type=simple | |
Restart=always | |
RestartSec=1 | |
User=pi | |
ExecStart=/home/pi/cam.py | |
WorkingDirectory=/home/pi/videos | |
[Install] | |
WantedBy=multi-user.target |
antisaint81
commented
Apr 29, 2024
via email
christian@pizero:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 12 (bookworm)"
NAME="Raspbian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
ok its working i think im no longer getting errors and im running Bullseye. The camera is showing a small red LED when running the script now im trying to see the live feed from my computer via web browser and nothing .. i had tried changing the ip address to 192.168.1.117:5000 but that didnt change anything considering the PI is on that address already @antisaint81
actually i believe the issue is i haven't set up the
[camera.service]
(https://gist.github.com/befinitiv/fd44a597a48460c100a54649c97d5984#file-camera-service) where would i implement this..?