I am attempting to record video using my OpenMV camera that can later be used for training in ML contexts. I would like to be able to trigger video recording start using a TTL input to the OpenMV camera from an external application (Matlab). I would also like to output TTLs from OpenMV to an external application (again, Matlab) at the time of each frame grab. Based on the examples I found online and the topics in the forum, I created the following code to do this:
import pyb
import sensor
import image
import time
import mjpeg
import machine
import os
from pyb import Pin, ExtInt
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.set_windowing((121,101,100,80)) # Set windowing
sensor.skip_frames(time=2000) # Wait for settings take effect.
led = machine.LED("LED_RED")
# Prepare the voltage output
pin0 = Pin('P0', Pin.OUT_PP)
# Set external trigger
trigger = 0
def triggerline (line):
global trigger
trigger = 1
ext = ExtInt(Pin('P7'), ExtInt.IRQ_RISING, Pin.PULL_DOWN, triggerline)
ext.enable()
# Set trigger pin
pin7 = Pin('P7', Pin.IN)
# led.on()
m = mjpeg.Mjpeg("openMVvideo.mjpeg")
clock = time.clock() # Create a clock object to track the FPS.
while(True):
if (trigger == 1):
sensor.skip_frames(time=1000) # Wait for the trigger to reset
trigger = 0
while(trigger == 0):
clock.tick()
led.on()
pin0.high()
m.write(sensor.snapshot())
time.sleep(1) # Included to slow pulses/flashes and make them visible
pin0.low()
led.off()
print(clock.fps())
break
m.close()
# led.off()
raise (Exception("Please reset the camera to see the new file."))
This code successfully starts recording following an external TTL and then stops recordings following a second TTL. However, I am not receiving an output TTL from P0 for some reason and the LED is not blinking. I would appreciate any suggestions on how to fix this problem.
Also, is there a more efficient way to record the video? As it is, the video is recorded in mjpeg format to the SD card on the OpenMV camera. The camera has to be reset to see the video file. Is there a way to automate the reset process or include it in the code?
Also, transfer of this file off the SD card to the computer is very slow. Can the video be directly written to the computer?
Also, I’m having a hard time working with the mjpeg format file. I see that it can be converted to mp4 in the IDE, but is there a way to automate this process or write to mp4 during the original recording? I’m also finding that following conversion to mp4, the video file is not the proper length.
I would greatly appreciate any advice people can provide. I attempted to look through the github examples and the forum to find answers to these issues. If I missed the answers, I apologize. Thank you!
~Nick