Advertisement
j0h

MicroFische.py

j0h
Mar 5th, 2024 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import cv2
  3. import time
  4. from time import sleep
  5. import os
  6. import threading
  7. """
  8. OMG Finlay. You can't blink the LED and run the video stream
  9. Without threading. I mean, you can, but it doesn't produce a
  10. Quality video.
  11.  
  12. This program reads an EAZY CAM usb capture device attached to
  13. Raspberry Pi 3b+ to capture video coming off a modified
  14. Pulse Data PDI SmartView micro fische reader, using
  15. a composite LCD screen with composite video output
  16.  
  17. Video is saved to a web accessible location, using an integrated
  18. webserver.
  19.  
  20. Possible improvements:
  21. ditch the print statements
  22. do more testing.
  23. ditch imshow, there's no screen output in this setup,
  24. video goes to the saved files.
  25.  
  26. Other notes:
  27. OpenCV kinda a memory hog, I edited
  28. /etc/dphys-swapfile
  29. from CONF_SWAPSIZE=100
  30. to CONF_SWAPSIZE=2048
  31. """
  32. # Define Some IO
  33. record_LED = 21
  34. capVidBtn = 20
  35.  
  36. # Set GPIO
  37. GPIO.setmode(GPIO.BCM)
  38. GPIO.setwarnings(False)
  39. GPIO.setup(record_LED, GPIO.OUT)
  40. GPIO.output(record_LED, GPIO.OUT)
  41. GPIO.setup(capVidBtn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  42. GPIO.output(record_LED, 0)  # Make sure LED is off to start
  43.  
  44. BTN_State = False
  45.  
  46. def debounce_callback(channel):
  47.     global last_interrupt
  48.     current_time = time.time()
  49.     if current_time - last_interrupt > 0.3:
  50.         grabVid(channel)
  51.     last_interrupt = current_time
  52.  
  53. last_interrupt = time.time()
  54. GPIO.add_event_detect(capVidBtn, GPIO.FALLING, callback=debounce_callback, bouncetime=300)
  55.  
  56. def blink_LED():
  57.     global BTN_State
  58.     cnt = 20
  59.     while BTN_State == True:
  60.         for i in range(cnt):
  61.             GPIO.output(record_LED, 1)
  62.             print(i)
  63.             sleep(0.5)
  64.             GPIO.output(record_LED, 0)
  65.             sleep(0.5)
  66.         if (i == cnt - 1):
  67.             BTN_State = False
  68.             break
  69.     GPIO.output(record_LED, 0)  # Make sure LED is off
  70.  
  71.  
  72. def grabVid(channel):
  73.     global BTN_State
  74.     BTN_State = not BTN_State
  75.     # OCV BS
  76.     cap = cv2.VideoCapture('/dev/video0')
  77.     current_time = int(time.time())
  78.     output_filename = f"{current_time}.mp4"
  79.     File_path = '/home/ch/img'
  80.     output_path = os.path.join(File_path, output_filename)
  81.     fourcc = cv2.VideoWriter_fourcc(*'H264')
  82.     out = cv2.VideoWriter(output_path, fourcc, 20.0, (640, 480))
  83.  
  84.     if BTN_State:
  85.         print("Capturing Video btn State is True")
  86.         # Start Capturing Video here
  87.         if not cap.isOpened():
  88.             print("Capture device not found")
  89.             exit()
  90.        
  91.         # Start a thread for blinking the LED
  92.         t = threading.Thread(target=blink_LED)
  93.         t.start()
  94.  
  95.         # Start Capturing Video
  96.         while cap.isOpened():
  97.             ret, frame = cap.read()
  98.             if not ret:
  99.                 print("BOoo no frame read")
  100.                 break
  101.  
  102.             # Write Data
  103.             out.write(frame)
  104.             cv2.imshow("MicroFische", frame)
  105.  
  106.             if not BTN_State:
  107.                 # Clean up BTN_State
  108.                 print("Button pressed: quitting")
  109.                 cap.release()
  110.                 out.release()
  111.                 cv2.destroyAllWindows()
  112.                 break
  113.  
  114. print("Make my dman video")
  115.  
  116. try:
  117.     while True:
  118.         sleep(0.1)
  119. except KeyboardInterrupt:
  120.     GPIO.cleanup()
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement