i got the official Raspberry PI 5MP Camera Board Module from 2014. it is plugged into the rpi 3 via the ribbon cable. the OS is 32 bit Bookworm.
my python script calls for:
import picamera
import picamera.array
this command does not work: sudo apt install python3-picamera
this command does work: sudo apt install python3-picamera2
so i change my python script to:
import picamera2
import picamera2.array
and then i got this error because it does not recognize picamera2.array:
pi@mirror:~ $ sudo python3 /home/pi/cam_relay2.py
Traceback (most recent call last):
File "/home/pi/cam_relay2.py", line 5, in <module>
import picamera2.array
ModuleNotFoundError: No module named 'picamera2.array'
any idea how i can get my script working? basically when camera detects motion, turn on relay.
here is the full python script i got from claude.ai
my python script calls for:
import picamera
import picamera.array
this command does not work: sudo apt install python3-picamera
this command does work: sudo apt install python3-picamera2
so i change my python script to:
import picamera2
import picamera2.array
and then i got this error because it does not recognize picamera2.array:
pi@mirror:~ $ sudo python3 /home/pi/cam_relay2.py
Traceback (most recent call last):
File "/home/pi/cam_relay2.py", line 5, in <module>
import picamera2.array
ModuleNotFoundError: No module named 'picamera2.array'
any idea how i can get my script working? basically when camera detects motion, turn on relay.
here is the full python script i got from claude.ai
Code:
import timeimport ioimport threadingimport picamera2import picamera2.arrayimport numpy as npimport RPi.GPIO as GPIO# Set up GPIORELAY_PIN = 17 # Change this to the GPIO pin you're using for the relayGPIO.setmode(GPIO.BCM)GPIO.setup(RELAY_PIN, GPIO.OUT)# Camera settingsRESOLUTION = (640, 480)FRAMERATE = 10THRESHOLD = 30 # Adjust this value to change motion sensitivity# Initialize variableslast_motion_time = time.time()relay_state = Falsedef detect_motion(camera): global last_motion_time, relay_state stream = picamera.array.PiRGBArray(camera) for f in camera.capture_continuous(stream, format='rgb', use_video_port=True): frame = np.frombuffer(stream.getvalue(), dtype=np.uint8) frame = frame.reshape((RESOLUTION[1], RESOLUTION[0], 3)) # Simple motion detection gray = np.dot(frame[..., :3], [0.299, 0.587, 0.114]) if 'last_frame' in locals(): diff = np.abs(gray - last_frame) motion_detected = np.sum(diff > THRESHOLD) > 200 # Adjust 200 for sensitivity if motion_detected: last_motion_time = time.time() if not relay_state: GPIO.output(RELAY_PIN, GPIO.HIGH) relay_state = True print("Motion detected! Relay turned ON") elif time.time() - last_motion_time > 300: # 5 minutes = 300 seconds if relay_state: GPIO.output(RELAY_PIN, GPIO.LOW) relay_state = False print("No motion for 5 minutes. Relay turned OFF") last_frame = gray stream.seek(0) stream.truncate()def main(): with picamera.PiCamera() as camera: camera.resolution = RESOLUTION camera.framerate = FRAMERATE print("Warming up...") time.sleep(2) print("Motion detection started") try: detect_motion(camera) except KeyboardInterrupt: print("Stopping motion detection") finally: GPIO.cleanup()if __name__ == "__main__": main()
Statistics: Posted by tung256 — Wed Sep 18, 2024 12:32 am