mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
commit
b8e338138c
@ -7,7 +7,7 @@ import sensor, image, time
|
||||
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() # Let new settings take affect.
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
clock = time.clock() # Tracks FPS.
|
||||
|
||||
while(True):
|
||||
|
||||
@ -11,11 +11,23 @@ usb = pyb.USB_VCP() # This is a serial port object that allows you to
|
||||
# communciate with your computer. While it is not open the code below runs.
|
||||
|
||||
while(not usb.isconnected()):
|
||||
led.on()
|
||||
time.sleep(150)
|
||||
led.off()
|
||||
time.sleep(100)
|
||||
led.on()
|
||||
time.sleep(150)
|
||||
led.off()
|
||||
time.sleep(600)
|
||||
led.on()
|
||||
time.sleep(150)
|
||||
led.off()
|
||||
time.sleep(100)
|
||||
led.on()
|
||||
time.sleep(150)
|
||||
led.off()
|
||||
time.sleep(600)
|
||||
|
||||
led = pyb.LED(2) # Switch to using the green LED.
|
||||
|
||||
while(usb.isconnected()):
|
||||
led.on()
|
||||
time.sleep(150)
|
||||
led.off()
|
||||
time.sleep(100)
|
||||
led.on()
|
||||
time.sleep(150)
|
||||
led.off()
|
||||
time.sleep(600)
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
import sensor, time
|
||||
|
||||
# Reset sensor
|
||||
sensor.reset()
|
||||
|
||||
# Set sensor settings
|
||||
sensor.set_brightness(0)
|
||||
sensor.set_saturation(0)
|
||||
sensor.set_gainceiling(16)
|
||||
sensor.set_contrast(1)
|
||||
|
||||
# Set sensor to QQVGA/RGB565
|
||||
sensor.set_framesize(sensor.QVGA)
|
||||
sensor.set_pixformat(sensor.RGB565)
|
||||
sensor.set_colorbar(True)
|
||||
|
||||
# Skip a few frames to allow the sensor settle down
|
||||
# Note: This takes more time when exec from the IDE.
|
||||
for i in range(0, 30):
|
||||
sensor.snapshot()
|
||||
|
||||
clock = time.clock()
|
||||
# Take snapshot
|
||||
img = sensor.snapshot()
|
||||
|
||||
# Compress Image
|
||||
clock.tick()
|
||||
img = img.compress(50)
|
||||
print(clock.avg(),"\n")
|
||||
|
||||
with open("/test.jpeg", "w") as f:
|
||||
f.write(img)
|
||||
@ -1,34 +0,0 @@
|
||||
import sensor, pyb, time
|
||||
|
||||
# Reset sensor
|
||||
sensor.reset()
|
||||
|
||||
# Set sensor settings
|
||||
sensor.set_brightness(0)
|
||||
sensor.set_saturation(0)
|
||||
sensor.set_gainceiling(16)
|
||||
sensor.set_contrast(1)
|
||||
sensor.set_framesize(sensor.QVGA)
|
||||
|
||||
# Enable JPEG and set quality
|
||||
sensor.set_pixformat(sensor.JPEG)
|
||||
sensor.set_quality(98)
|
||||
|
||||
# Red LED
|
||||
led = pyb.LED(1)
|
||||
|
||||
# Skip a few frames to allow the sensor settle down
|
||||
# Note: This takes more time when exec from the IDE.
|
||||
for i in range(0, 30):
|
||||
sensor.snapshot()
|
||||
|
||||
# Turn on red LED and wait for a second
|
||||
led.on()
|
||||
time.sleep(1000)
|
||||
|
||||
# Write JPEG image to file
|
||||
with open("/test.jpeg", "w") as f:
|
||||
f.write(sensor.snapshot())
|
||||
|
||||
led.off()
|
||||
print("Reset the camera to see the saved image.")
|
||||
@ -1,33 +0,0 @@
|
||||
import sensor, pyb, time
|
||||
|
||||
# Reset sensor
|
||||
sensor.reset()
|
||||
|
||||
# Set sensor settings
|
||||
sensor.set_brightness(0)
|
||||
sensor.set_saturation(0)
|
||||
sensor.set_gainceiling(16)
|
||||
sensor.set_contrast(1)
|
||||
sensor.set_framesize(sensor.QVGA)
|
||||
|
||||
# Set sensor to RGB565
|
||||
sensor.set_pixformat(sensor.RGB565)
|
||||
|
||||
# Red LED
|
||||
led = pyb.LED(1)
|
||||
|
||||
# Skip a few frames to allow the sensor settle down
|
||||
# Note: This takes more time when exec from the IDE.
|
||||
for i in range(0, 10):
|
||||
sensor.snapshot()
|
||||
|
||||
# Turn on red LED and wait for a second
|
||||
led.on()
|
||||
time.sleep(1000)
|
||||
|
||||
# Write image to file
|
||||
img = sensor.snapshot()
|
||||
img.save("/test.ppm")
|
||||
|
||||
led.off()
|
||||
print("Reset the camera to see the saved image.")
|
||||
@ -1,6 +1,6 @@
|
||||
# Save Image Example
|
||||
# Snapshot Example
|
||||
#
|
||||
# Note: You will need an SD card to run this demo.
|
||||
# Note: You will need an SD card to run this example.
|
||||
#
|
||||
# You can use your OpenMV Cam to save image files.
|
||||
|
||||
@ -12,7 +12,7 @@ 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() # Let new settings take affect.
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
sensor.skip_frames(30) # Give the user time to get ready.
|
||||
@ -21,7 +21,7 @@ pyb.LED(RED_LED_PIN).off()
|
||||
pyb.LED(BLUE_LED_PIN).on()
|
||||
|
||||
print("You're on camera!")
|
||||
sensor.snapshot().save("demo.jpg") # or "demo.bmp" (or others)
|
||||
sensor.snapshot().save("example.jpg") # or "example.bmp" (or others)
|
||||
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Done! Reset the camera to see the saved recording.")
|
||||
print("Done! Reset the camera to see the saved image.")
|
||||
51
usr/examples/05-Snapshot/snapshot_on_face_detection.py
Normal file
51
usr/examples/05-Snapshot/snapshot_on_face_detection.py
Normal file
@ -0,0 +1,51 @@
|
||||
# Snapshot on Face Detection Example
|
||||
#
|
||||
# Note: You will need an SD card to run this example.
|
||||
#
|
||||
# This example demonstrates using face tracking on your OpenMV Cam to take a
|
||||
# picture.
|
||||
|
||||
import sensor, image, pyb
|
||||
|
||||
RED_LED_PIN = 1
|
||||
BLUE_LED_PIN = 3
|
||||
|
||||
sensor.reset() # Initialize the camera sensor.
|
||||
sensor.set_pixformat(sensor.GRAYSCALE)
|
||||
sensor.set_framesize(sensor.HQVGA) # or sensor.QQVGA (or others)
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
|
||||
# Load up a face detection HaarCascade. This is object that your OpenMV Cam
|
||||
# can use to detect faces using the find_features() method below. Your OpenMV
|
||||
# Cam has fontalface HaarCascade built-in. By default, all the stages of the
|
||||
# HaarCascade are loaded. However, You can adjust the number of stages to speed
|
||||
# up processing at the expense of accuracy. The frontalface HaarCascade has 25
|
||||
# stages.
|
||||
face_cascade = image.HaarCascade("frontalface", stages=25)
|
||||
|
||||
while(True):
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
print("About to start detecting faces...")
|
||||
sensor.skip_frames(60) # Give the user time to get ready.
|
||||
|
||||
pyb.LED(RED_LED_PIN).off()
|
||||
print("Now detecting faces!")
|
||||
pyb.LED(BLUE_LED_PIN).on()
|
||||
|
||||
diff = 10 # We'll say we detected a face after 10 frames.
|
||||
while(diff):
|
||||
img = sensor.snapshot()
|
||||
# Threshold can be between 0.0 and 1.0. A higher threshold results in a
|
||||
# higher detection rate with more false positives. The scale value
|
||||
# controls the matching scale allowing you to detect smaller faces.
|
||||
faces = img.find_features(face_cascade, threshold=0.5, scale=1.5)
|
||||
|
||||
if faces:
|
||||
diff -= 1
|
||||
for r in faces:
|
||||
img.draw_rectangle(r)
|
||||
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Face detected! Saving image...")
|
||||
sensor.snapshot().save("snapshot-%d.jpg" % pyb.rng()) # Save Pic.
|
||||
43
usr/examples/05-Snapshot/snapshot_on_movement.py
Normal file
43
usr/examples/05-Snapshot/snapshot_on_movement.py
Normal file
@ -0,0 +1,43 @@
|
||||
# Snapshot on Movement Example
|
||||
#
|
||||
# Note: You will need an SD card to run this example.
|
||||
#
|
||||
# This example demonstrates using frame differencing with your OpenMV Cam to do
|
||||
# motion detection. After motion is detected your OpenMV Cam will take picture.
|
||||
|
||||
import sensor, image, pyb, os
|
||||
|
||||
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(10) # Let new settings take affect.
|
||||
sensor.set_whitebal(False) # Turn off white balance.
|
||||
|
||||
if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory
|
||||
|
||||
while(True):
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
print("About to save background image...")
|
||||
sensor.skip_frames(60) # 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")
|
||||
img.binary([(20, 100, -128, 127, -128, 127)])
|
||||
sum = img.pixels()
|
||||
if sum > 100: # Over 100 pixels need to change to detect motion.
|
||||
diff -= 1
|
||||
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Movement detected! Saving image...")
|
||||
sensor.snapshot().save("temp/snapshot-%d.jpg" % pyb.rng()) # Save Pic.
|
||||
@ -14,7 +14,7 @@ BLUE_LED_PIN = 3
|
||||
sensor.reset() # Initialize the camera sensor.
|
||||
sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
|
||||
sensor.set_framesize(sensor.QQVGA) # or sensor.QVGA (or others)
|
||||
sensor.skip_frames() # Let new settings take affect.
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
clock = time.clock() # Tracks FPS.
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
@ -23,15 +23,15 @@ sensor.skip_frames(30) # Give the user time to get ready.
|
||||
pyb.LED(RED_LED_PIN).off()
|
||||
pyb.LED(BLUE_LED_PIN).on()
|
||||
|
||||
gif = gif.Gif("demo.gif", loop=True)
|
||||
g = gif.Gif("example.gif", loop=True)
|
||||
|
||||
print("You're on camera!")
|
||||
for i in range(100):
|
||||
clock.tick()
|
||||
# clock.avg() returns the milliseconds between frames - gif delay is in
|
||||
gif.add_frame(sensor.snapshot(), delay=int(clock.avg()/10)) # centiseconds.
|
||||
g.add_frame(sensor.snapshot(), delay=int(clock.avg()/10)) # centiseconds.
|
||||
print(clock.fps())
|
||||
|
||||
gif.close()
|
||||
g.close()
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Done! Reset the camera to see the saved recording.")
|
||||
|
||||
65
usr/examples/06-Video-Recording/gif_on_face_detection.py
Normal file
65
usr/examples/06-Video-Recording/gif_on_face_detection.py
Normal file
@ -0,0 +1,65 @@
|
||||
# GIF Video Recording on Face Detection Example
|
||||
#
|
||||
# Note: You will need an SD card to run this example.
|
||||
#
|
||||
# You can use your OpenMV Cam to record gif files. You can either feed the
|
||||
# recorder object RGB565 frames or Grayscale frames. Use photo editing software
|
||||
# like GIMP to compress and optimize the Gif before uploading it to the web.
|
||||
#
|
||||
# This example demonstrates using face tracking on your OpenMV Cam to take a
|
||||
# gif.
|
||||
|
||||
import sensor, image, time, gif, pyb
|
||||
|
||||
RED_LED_PIN = 1
|
||||
BLUE_LED_PIN = 3
|
||||
|
||||
sensor.reset() # Initialize the camera sensor.
|
||||
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.
|
||||
sensor.set_framesize(sensor.QQVGA) # or sensor.HQVGA (or others)
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
|
||||
# Load up a face detection HaarCascade. This is object that your OpenMV Cam
|
||||
# can use to detect faces using the find_features() method below. Your OpenMV
|
||||
# Cam has fontalface HaarCascade built-in. By default, all the stages of the
|
||||
# HaarCascade are loaded. However, You can adjust the number of stages to speed
|
||||
# up processing at the expense of accuracy. The frontalface HaarCascade has 25
|
||||
# stages.
|
||||
face_cascade = image.HaarCascade("frontalface", stages=25)
|
||||
|
||||
while(True):
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
print("About to start detecting faces...")
|
||||
sensor.skip_frames(60) # Give the user time to get ready.
|
||||
|
||||
pyb.LED(RED_LED_PIN).off()
|
||||
print("Now detecting faces!")
|
||||
pyb.LED(BLUE_LED_PIN).on()
|
||||
|
||||
diff = 10 # We'll say we detected a face after 10 frames.
|
||||
while(diff):
|
||||
img = sensor.snapshot()
|
||||
# Threshold can be between 0.0 and 1.0. A higher threshold results in a
|
||||
# higher detection rate with more false positives. The scale value
|
||||
# controls the matching scale allowing you to detect smaller faces.
|
||||
faces = img.find_features(face_cascade, threshold=0.5, scale=1.5)
|
||||
|
||||
if faces:
|
||||
diff -= 1
|
||||
for r in faces:
|
||||
img.draw_rectangle(r)
|
||||
|
||||
g = gif.Gif("example-%d.gif" % pyb.rng(), loop=True)
|
||||
|
||||
clock = time.clock() # Tracks FPS.
|
||||
print("You're on camera!")
|
||||
for i in range(100):
|
||||
clock.tick()
|
||||
# clock.avg() returns the milliseconds between frames - gif delay is in
|
||||
g.add_frame(sensor.snapshot(), delay=int(clock.avg()/10)) # centiseconds.
|
||||
print(clock.fps())
|
||||
|
||||
g.close()
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Restarting...")
|
||||
57
usr/examples/06-Video-Recording/gif_on_movement.py
Normal file
57
usr/examples/06-Video-Recording/gif_on_movement.py
Normal file
@ -0,0 +1,57 @@
|
||||
# GIF Video Recording on Movement Example
|
||||
#
|
||||
# Note: You will need an SD card to run this example.
|
||||
#
|
||||
# You can use your OpenMV Cam to record gif files. You can either feed the
|
||||
# recorder object RGB565 frames or Grayscale frames. Use photo editing software
|
||||
# like GIMP to compress and optimize the Gif before uploading it to the web.
|
||||
#
|
||||
# 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, gif, pyb, os
|
||||
|
||||
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.QQVGA) # or sensor.QVGA (or others)
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
sensor.set_whitebal(False) # Turn off white balance.
|
||||
|
||||
if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory
|
||||
|
||||
while(True):
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
print("About to save background image...")
|
||||
sensor.skip_frames(60) # 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")
|
||||
img.binary([(20, 100, -128, 127, -128, 127)])
|
||||
sum = img.pixels()
|
||||
if sum > 100: # Over 100 pixels need to change to detect motion.
|
||||
diff -= 1
|
||||
|
||||
g = gif.Gif("example-%d.gif" % pyb.rng(), loop=True)
|
||||
|
||||
clock = time.clock() # Tracks FPS.
|
||||
print("You're on camera!")
|
||||
for i in range(100):
|
||||
clock.tick()
|
||||
# clock.avg() returns the milliseconds between frames - gif delay is in
|
||||
g.add_frame(sensor.snapshot(), delay=int(clock.avg()/10)) # centiseconds.
|
||||
print(clock.fps())
|
||||
|
||||
g.close()
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Restarting...")
|
||||
@ -15,7 +15,7 @@ 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() # Let new settings take affect.
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
clock = time.clock() # Tracks FPS.
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
@ -24,14 +24,14 @@ sensor.skip_frames(30) # Give the user time to get ready.
|
||||
pyb.LED(RED_LED_PIN).off()
|
||||
pyb.LED(BLUE_LED_PIN).on()
|
||||
|
||||
mjpeg = mjpeg.Mjpeg("demo.mjpeg")
|
||||
m = mjpeg.Mjpeg("example.mjpeg")
|
||||
|
||||
print("You're on camera!")
|
||||
for i in range(200):
|
||||
clock.tick()
|
||||
mjpeg.add_frame(sensor.snapshot())
|
||||
m.add_frame(sensor.snapshot())
|
||||
print(clock.fps())
|
||||
|
||||
mjpeg.close(clock.fps())
|
||||
m.close(clock.fps())
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Done! Reset the camera to see the saved recording.")
|
||||
|
||||
65
usr/examples/06-Video-Recording/mjpeg_on_face_detection.py
Normal file
65
usr/examples/06-Video-Recording/mjpeg_on_face_detection.py
Normal file
@ -0,0 +1,65 @@
|
||||
# MJPEG Video Recording on Face Detection 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 face tracking on your OpenMV Cam to take a
|
||||
# mjpeg.
|
||||
|
||||
import sensor, image, time, mjpeg, pyb
|
||||
|
||||
RED_LED_PIN = 1
|
||||
BLUE_LED_PIN = 3
|
||||
|
||||
sensor.reset() # Initialize the camera sensor.
|
||||
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.
|
||||
sensor.set_framesize(sensor.QQVGA) # or sensor.HQVGA (or others)
|
||||
sensor.skip_frames(10) # Let new settings take affect.
|
||||
|
||||
# Load up a face detection HaarCascade. This is object that your OpenMV Cam
|
||||
# can use to detect faces using the find_features() method below. Your OpenMV
|
||||
# Cam has fontalface HaarCascade built-in. By default, all the stages of the
|
||||
# HaarCascade are loaded. However, You can adjust the number of stages to speed
|
||||
# up processing at the expense of accuracy. The frontalface HaarCascade has 25
|
||||
# stages.
|
||||
face_cascade = image.HaarCascade("frontalface", stages=25)
|
||||
|
||||
while(True):
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
print("About to start detecting faces...")
|
||||
sensor.skip_frames(60) # Give the user time to get ready.
|
||||
|
||||
pyb.LED(RED_LED_PIN).off()
|
||||
print("Now detecting faces!")
|
||||
pyb.LED(BLUE_LED_PIN).on()
|
||||
|
||||
diff = 10 # We'll say we detected a face after 10 frames.
|
||||
while(diff):
|
||||
img = sensor.snapshot()
|
||||
# Threshold can be between 0.0 and 1.0. A higher threshold results in a
|
||||
# higher detection rate with more false positives. The scale value
|
||||
# controls the matching scale allowing you to detect smaller faces.
|
||||
faces = img.find_features(face_cascade, threshold=0.5, scale=1.5)
|
||||
|
||||
if faces:
|
||||
diff -= 1
|
||||
for r in faces:
|
||||
img.draw_rectangle(r)
|
||||
|
||||
m = mjpeg.Mjpeg("example-%d.mjpeg" % pyb.rng())
|
||||
|
||||
clock = time.clock() # Tracks FPS.
|
||||
print("You're on camera!")
|
||||
for i in range(200):
|
||||
clock.tick()
|
||||
m.add_frame(sensor.snapshot())
|
||||
print(clock.fps())
|
||||
|
||||
m.close(clock.fps())
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Restarting...")
|
||||
57
usr/examples/06-Video-Recording/mjpeg_on_movement.py
Normal file
57
usr/examples/06-Video-Recording/mjpeg_on_movement.py
Normal file
@ -0,0 +1,57 @@
|
||||
# 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
|
||||
|
||||
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(10) # Let new settings take affect.
|
||||
sensor.set_whitebal(False) # Turn off white balance.
|
||||
|
||||
if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory
|
||||
|
||||
while(True):
|
||||
|
||||
pyb.LED(RED_LED_PIN).on()
|
||||
print("About to save background image...")
|
||||
sensor.skip_frames(60) # 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")
|
||||
img.binary([(20, 100, -128, 127, -128, 127)])
|
||||
sum = img.pixels()
|
||||
if sum > 100: # Over 100 pixels need to change to detect motion.
|
||||
diff -= 1
|
||||
|
||||
m = mjpeg.Mjpeg("example-%d.mjpeg" % pyb.rng())
|
||||
|
||||
clock = time.clock() # Tracks FPS.
|
||||
print("You're on camera!")
|
||||
for i in range(200):
|
||||
clock.tick()
|
||||
m.add_frame(sensor.snapshot())
|
||||
print(clock.fps())
|
||||
|
||||
m.close(clock.fps())
|
||||
pyb.LED(BLUE_LED_PIN).off()
|
||||
print("Restarting...")
|
||||
@ -1,146 +0,0 @@
|
||||
# Copy this module to storage and import it if you want
|
||||
# to use it in your own scripts. See example usage below.
|
||||
import ustruct as struct
|
||||
|
||||
class AVI:
|
||||
def __init__(self, path, w, h, codec="MJPG"):
|
||||
self.w = w
|
||||
self.h = h
|
||||
self.codec = codec
|
||||
self.size = 0
|
||||
self.frames = 0
|
||||
self.fp = open(path, "w")
|
||||
self.fp.seek(224) #skip headers
|
||||
|
||||
def avi_hdr(self):
|
||||
hdr = struct.pack("I", int(1000/self.fps)) # Time delay between frames
|
||||
hdr += struct.pack("I", 0) # Data rate of AVI data
|
||||
hdr += struct.pack("I", 1) # Size of single unit of padding
|
||||
hdr += struct.pack("I", 0) # Flags
|
||||
hdr += struct.pack("I", self.frames)# Number of video frame stored
|
||||
hdr += struct.pack("I", 0) # Number of intial frames
|
||||
hdr += struct.pack("I", 1) # Number of data streams in chunk
|
||||
hdr += struct.pack("I", 0) # Minimum playback buffer size
|
||||
hdr += struct.pack("I", self.w) # Width of video frame in pixels
|
||||
hdr += struct.pack("I", self.h) # Height of video frame in pixels
|
||||
hdr += struct.pack("I", 1) # Time scale
|
||||
hdr += struct.pack("I", self.fps) # Data rate of playback
|
||||
hdr += struct.pack("I", 0) # Starting time of AVI data
|
||||
hdr += struct.pack("I", 0) # Size of AVI data chunk
|
||||
return hdr;
|
||||
|
||||
def str_hdr(self):
|
||||
hdr = struct.pack("4s", "vids") # Stream type
|
||||
hdr += struct.pack("4s", self.codec)# Stream codec
|
||||
hdr += struct.pack("I", 0) # Flags
|
||||
hdr += struct.pack("I", 0) # Priority
|
||||
hdr += struct.pack("I", 0) # Number of first frame
|
||||
hdr += struct.pack("I", 1) # Time scale
|
||||
hdr += struct.pack("I", self.fps) # Data rate of playback
|
||||
hdr += struct.pack("I", 0) # Starting time of AVI data
|
||||
hdr += struct.pack("I", 0) # Data length
|
||||
hdr += struct.pack("I", 0) # Buffer size
|
||||
hdr += struct.pack("I", 0) # Sample quailty factor
|
||||
hdr += struct.pack("I", 0) # Size of the sample in bytes
|
||||
hdr += struct.pack("II",0,0) # Rect
|
||||
return hdr;
|
||||
|
||||
def str_fmt(self):
|
||||
#BITMAPINFOHEADER
|
||||
hdr = struct.pack("I", 40) # Size in bytes
|
||||
hdr += struct.pack("I", self.w) # Width
|
||||
hdr += struct.pack("I", self.h) # Height
|
||||
hdr += struct.pack("H", 1) # Planes
|
||||
hdr += struct.pack("H", 16) # Bits per pixel
|
||||
hdr += struct.pack("4s", self.codec) # This should be BI_JPEG, but ffmpeg writes "MJPG"
|
||||
hdr += struct.pack("I", 0) # Image size (which one?)
|
||||
hdr += struct.pack("I", 0) # X pixels-per-meter
|
||||
hdr += struct.pack("I", 0) # Y pixels-per-meter
|
||||
hdr += struct.pack("I", 0) # color indexes in the color table
|
||||
hdr += struct.pack("I", 0) # required color indexes in the color table
|
||||
return hdr;
|
||||
|
||||
def new_chunk(self, c_id, c_data):
|
||||
return c_id +\
|
||||
struct.pack("I", len(c_data)) +\
|
||||
c_data
|
||||
|
||||
def new_list(self, l_id, l_4cc, l_size, l_data):
|
||||
return l_id +\
|
||||
struct.pack("I", l_size+len(l_data)+4) +\
|
||||
struct.pack("4s", l_4cc) +\
|
||||
l_data
|
||||
|
||||
def add_frame(self, img):
|
||||
self.frames +=1
|
||||
self.size += img.size()
|
||||
self.fp.write(struct.pack("4sI", "00dc", img.size()))
|
||||
self.fp.write(img)
|
||||
|
||||
def flush(self, fps):
|
||||
self.fps = fps
|
||||
self.fp.seek(0)
|
||||
self.fp.write(
|
||||
self.new_list(b"RIFF", b"AVI ", self.size,
|
||||
self.new_list(b"LIST", b"hdrl", 0,
|
||||
self.new_chunk(b"avih", self.avi_hdr())
|
||||
+ self.new_list(b"LIST", b"strl", 0,
|
||||
self.new_chunk(b"strh", self.str_hdr())
|
||||
+ self.new_chunk(b"strf", self.str_fmt())
|
||||
)
|
||||
+ self.new_list(b"LIST", b"movi", self.size, b"")
|
||||
)
|
||||
)
|
||||
)
|
||||
self.fp.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sensor, time, pyb
|
||||
#from avi import AVI
|
||||
|
||||
# Recording length in seconds
|
||||
REC_LENGTH = 10
|
||||
|
||||
# Reset sensor
|
||||
sensor.reset()
|
||||
|
||||
# Set sensor settings
|
||||
sensor.set_contrast(2)
|
||||
sensor.set_framesize(sensor.VGA)
|
||||
|
||||
# Enable JPEG and set quality
|
||||
sensor.set_pixformat(sensor.JPEG)
|
||||
sensor.set_quality(95)
|
||||
|
||||
# Skip a few frames to allow the sensor settle down
|
||||
# Note: This takes more time when exec from the IDE.
|
||||
for i in range(0, 30):
|
||||
sensor.snapshot()
|
||||
|
||||
# Create red LED object
|
||||
led = pyb.LED(1)
|
||||
# Create video file
|
||||
video = AVI("%d.mjpeg"%pyb.rng(), 640, 480)
|
||||
|
||||
# Recording clocks
|
||||
led.on()
|
||||
clock = time.clock()
|
||||
start = time.ticks()
|
||||
|
||||
# Start recording
|
||||
while ((time.ticks()-start) < (REC_LENGTH*1000)):
|
||||
clock.tick()
|
||||
img = sensor.snapshot()
|
||||
video.add_frame(img)
|
||||
|
||||
led.off()
|
||||
# Flush video file
|
||||
video.flush(int(clock.fps()))
|
||||
|
||||
# Done, flash blue LED
|
||||
led = pyb.LED(3)
|
||||
while (True):
|
||||
led.on()
|
||||
time.sleep(500)
|
||||
led.off()
|
||||
time.sleep(500)
|
||||
@ -1,41 +0,0 @@
|
||||
# Motion Detection Example:
|
||||
#
|
||||
# This example demonstrates using frame differencing with your OpenMV Cam to do
|
||||
# motion detection. After motion is detected your OpenMV Cam will take picture.
|
||||
|
||||
import os, pyb, sensor, image, time
|
||||
|
||||
if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory
|
||||
|
||||
sensor.reset()
|
||||
sensor.set_framesize(sensor.QVGA)
|
||||
|
||||
while(True):
|
||||
sensor.set_pixformat(sensor.GRAYSCALE) # Grayscale is much faster than RGB.
|
||||
|
||||
# Warm up the cam
|
||||
for i in range(10):
|
||||
sensor.snapshot()
|
||||
|
||||
for i in [5, 4, 3, 2, 1]:
|
||||
print("Saving background in... %d" % i)
|
||||
time.sleep(1000)
|
||||
|
||||
print("Saving background...")
|
||||
sensor.snapshot().save("temp/bg.bmp")
|
||||
|
||||
diff = 30 # wait 30 snapshot before taking picture
|
||||
while(diff):
|
||||
img = sensor.snapshot()
|
||||
img.difference("temp/bg.bmp")
|
||||
img.binary([(32, 255)])
|
||||
sum, x, y = img.centroid()
|
||||
if sum > 100: # 100 pixels detected
|
||||
img.draw_cross(x, y, color = 127)
|
||||
diff -= 1
|
||||
|
||||
sensor.set_pixformat(sensor.RGB565)
|
||||
# Warm up the cam
|
||||
for i in range(10):
|
||||
sensor.snapshot()
|
||||
sensor.snapshot().save("temp/movement-%d" % pyb.rng()) # Save movement
|
||||
Loading…
Reference in New Issue
Block a user