Video recording with a TV Shield

Hi there,

I am working with the H7 Plus and I want to combine to example codes together. I want to film and see the result whilst on the ground. At the same time, the video has to be saved on the SD-card.

When I tried to combine the two codes there were a few problems. The code “mpjeg_on_movement” and “tv” both have a while(true): statement. When the tv while statement is on top that part works. But when the other statement is on top the tv doesn’t work. Normally with other programming languages you just place a new code after a closed statement, but with this while statement I don’t see where the statements end.

Thanks in advance,
Dave

Hi, please post the code you have so far.

# MJPEG Video Recording on Movement Example
#
# Note: You will need an SD card to run this example.
#
# You can use your OpenMV Cam to record mjpeg files. You can either feed the
# recorder object JPEG frames or RGB565/Grayscale frames. Once you've finished
# recording a Mjpeg file you can use VLC to play it. If you are on Ubuntu then
# the built-in video player will work too.
#
# This example demonstrates using frame differencing with your OpenMV Cam to do
# motion detection. After motion is detected your OpenMV Cam will take video.

import sensor, image, time, mjpeg, pyb, os, tv

RED_LED_PIN = 1
BLUE_LED_PIN = 3

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 2000) # Let new settings take affect.
sensor.set_auto_whitebal(False) # Turn off white balance.
clock = time.clock()

tv.init(triple_buffer=False)
tv.channel(8)
if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory

while(True):
    clock.tick()
    tv.display(sensor.snapshot())
    print(clock.fps())

    pyb.LED(RED_LED_PIN).on()
    print("About to save background image...")
    sensor.skip_frames(time = 2000) # Give the user time to get ready.

    pyb.LED(RED_LED_PIN).off()
    sensor.snapshot().save("temp/bg.bmp")
    print("Saved background image - Now detecting motion!")
    pyb.LED(BLUE_LED_PIN).on()

    diff = 10 # We'll say we detected motion after 10 frames of motion.
    while(diff):
        img = sensor.snapshot()
        img.difference("temp/bg.bmp")
        stats = img.statistics()
        # Stats 5 is the max of the lighting color channel. The below code
        # triggers when the lighting max for the whole image goes above 20.
        # The lighting difference maximum should be zero normally.
        if (stats[5] > 20):
            diff -= 1

    m = mjpeg.Mjpeg("example-%d.mjpeg" % pyb.rng())

    clock = time.clock() # Tracks FPS.
    print("You're on camera!")
    for i in range(1000):
        clock.tick()
        m.add_frame(sensor.snapshot())
        print(clock.fps())

    m.close(clock.fps())
    pyb.LED(BLUE_LED_PIN).off()
    print("Restarting...")

Hi, you have to call tv.display() on whatever image object is returned by snapshot for it to be displayed. You only call this once at the top of your loop and then you do a bunch of smaller loops inside. You need to add the display command to these loops too.

Note that tv.display() doesn’t return an image object… so, you need to assign the output of sensor.snapshot() to a variable first, then call display on that variable, then do something else with the image object.

Hi, I don’t know where all the smaller loops are running. When I combined the two programms together, I just deleted the While(true): statement, because I thought that two of those statements doesn’t work together. So does that mean that the 2 loops are just the while(true) loop for sending, and the other loop is for recording.

I don’t really know where to put the tv.display() commands, do you have an example code for me, if that is possible?

Thanks in advance,
Dave Stevens

# MJPEG Video Recording on Movement Example
#
# Note: You will need an SD card to run this example.
#
# You can use your OpenMV Cam to record mjpeg files. You can either feed the
# recorder object JPEG frames or RGB565/Grayscale frames. Once you've finished
# recording a Mjpeg file you can use VLC to play it. If you are on Ubuntu then
# the built-in video player will work too.
#
# This example demonstrates using frame differencing with your OpenMV Cam to do
# motion detection. After motion is detected your OpenMV Cam will take video.

import sensor, image, time, mjpeg, pyb, os, tv

RED_LED_PIN = 1
BLUE_LED_PIN = 3

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.QVGA) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 2000) # Let new settings take affect.
sensor.set_auto_whitebal(False) # Turn off white balance.
clock = time.clock()

tv.init(triple_buffer=False)
tv.channel(8)
if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory

while(True):
    clock.tick()
    tv.display(sensor.snapshot())
    print(clock.fps())

    pyb.LED(RED_LED_PIN).on()
    print("About to save background image...")
    sensor.skip_frames(time = 2000) # Give the user time to get ready.

    pyb.LED(RED_LED_PIN).off()
    sensor.snapshot().save("temp/bg.bmp")
    print("Saved background image - Now detecting motion!")
    pyb.LED(BLUE_LED_PIN).on()

    diff = 10 # We'll say we detected motion after 10 frames of motion.
    while(diff):
        img = sensor.snapshot()
        img.difference("temp/bg.bmp")
        stats = img.statistics()
        # Stats 5 is the max of the lighting color channel. The below code
        # triggers when the lighting max for the whole image goes above 20.
        # The lighting difference maximum should be zero normally.
        if (stats[5] > 20):
            diff -= 1
        tv.display(img)

    m = mjpeg.Mjpeg("example-%d.mjpeg" % pyb.rng())

    clock = time.clock() # Tracks FPS.
    print("You're on camera!")
    for i in range(1000):
        clock.tick()
        img = sensor.snapshot()
        m.add_frame(img)
        print(clock.fps())
        tv.display(img)

    m.close(clock.fps())
    pyb.LED(BLUE_LED_PIN).off()
    print("Restarting...")