CCCPaste

MicroFische.py

import RPi.GPIO as GPIO
import cv2
import time
from time import sleep
import os
import threading
"""
OMG Finlay. You can't blink the LED and run the video stream
Without threading. I mean, you can, but it doesn't produce a 
Quality video. 

This program reads an EAZY CAM usb capture device attached to 
Raspberry Pi 3b+ to capture video coming off a modified 
Pulse Data PDI SmartView micro fische reader, using 
a composite LCD screen with composite video output

Video is saved to a web accessible location, using an integrated
webserver. 

Possible improvements:
ditch the print statements
do more testing.
ditch imshow, there's no screen output in this setup,
video goes to the saved files. 

Other notes:
OpenCV kinda a memory hog, I edited 
 /etc/dphys-swapfile
from CONF_SWAPSIZE=100
to CONF_SWAPSIZE=2048
"""
# Define Some IO
record_LED = 21
capVidBtn = 20

# Set GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(record_LED, GPIO.OUT)
GPIO.output(record_LED, GPIO.OUT)
GPIO.setup(capVidBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(record_LED, 0)  # Make sure LED is off to start

BTN_State = False

def debounce_callback(channel):
    global last_interrupt
    current_time = time.time()
    if current_time - last_interrupt > 0.3:
        grabVid(channel)
    last_interrupt = current_time

last_interrupt = time.time()
GPIO.add_event_detect(capVidBtn, GPIO.FALLING, callback=debounce_callback, bouncetime=300)

def blink_LED():
    global BTN_State
    cnt = 20
    while BTN_State == True:
        for i in range(cnt):
            GPIO.output(record_LED, 1)
            print(i)
            sleep(0.5)
            GPIO.output(record_LED, 0)
            sleep(0.5)
        if (i == cnt - 1):
            BTN_State = False
            break
    GPIO.output(record_LED, 0)  # Make sure LED is off


def grabVid(channel):
    global BTN_State
    BTN_State = not BTN_State
    # OCV BS
    cap = cv2.VideoCapture('/dev/video0')
    current_time = int(time.time())
    output_filename = f"{current_time}.mp4"
    File_path = '/home/ch/img'
    output_path = os.path.join(File_path, output_filename)
    fourcc = cv2.VideoWriter_fourcc(*'H264')
    out = cv2.VideoWriter(output_path, fourcc, 20.0, (640, 480))

    if BTN_State:
        print("Capturing Video btn State is True")
        # Start Capturing Video here
        if not cap.isOpened():
            print("Capture device not found")
            exit()
        
        # Start a thread for blinking the LED
        t = threading.Thread(target=blink_LED)
        t.start()

        # Start Capturing Video
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                print("BOoo no frame read")
                break

            # Write Data
            out.write(frame)
            cv2.imshow("MicroFische", frame)

            if not BTN_State:
                # Clean up BTN_State
                print("Button pressed: quitting")
                cap.release()
                out.release()
                cv2.destroyAllWindows()
                break

print("Make my dman video")

try:
    while True:
        sleep(0.1)
except KeyboardInterrupt:
    GPIO.cleanup()