From 0abd5d3688ccd06c54932e94f37d0208b74f51c2 Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Sat, 2 Apr 2016 11:18:20 -0400 Subject: [PATCH] Working on scripts... Moved feature detection scripts into their own folders and added explict frame_skip value per Ibrahim's request. Finished working on snapshot and video recording scripts for next release. ... From CMUcam4 work I learned that people will just want examples that do "X" thing. So, in general, our examples should include a simple script showing off a feature and then a more complex script that does "X" where "X" is some app that a person would want. For example, we'll get reuqests for face tracking with servos, and movement detection with servos. So, instead of answering this question a million times with an example script we'll just have examples for all kinds of things people will want. Gotta automate dealing with help support at the end of the day... --- usr/examples/01-Basics/helloworld.py | 2 +- usr/examples/01-Basics/main.py | 28 +++- usr/examples/05-Picture-Taking/compress.py | 32 ---- usr/examples/05-Picture-Taking/write_jpeg.py | 34 ---- usr/examples/05-Picture-Taking/write_ppm.py | 33 ---- .../save.py => 05-Snapshot/snapshot.py} | 10 +- .../05-Snapshot/snapshot_on_face_detection.py | 51 ++++++ .../05-Snapshot/snapshot_on_movement.py | 43 ++++++ usr/examples/06-Video-Recording/gif.py | 8 +- .../gif_on_face_detection.py | 65 ++++++++ .../06-Video-Recording/gif_on_movement.py | 57 +++++++ usr/examples/06-Video-Recording/mjpeg.py | 8 +- .../mjpeg_on_face_detection.py | 65 ++++++++ .../06-Video-Recording/mjpeg_on_movement.py | 57 +++++++ usr/examples/06-Video-Recording/mjpeg_py.py | 146 ------------------ .../face_detection.py | 0 .../face_tracking.py | 0 .../face_eye_detection.py | 0 .../iris_detection.py | 0 .../freak.py | 0 .../lbp.py | 0 .../template_matching.py | 0 .../09-Miscellaneous/motion_detection.py | 41 ----- .../blob_detection.py | 0 .../{10-Tests => 15-Tests}/colorbar.py | 0 .../{10-Tests => 15-Tests}/selftest.py | 0 .../{10-Tests => 15-Tests}/test_binary_1.py | 0 .../{10-Tests => 15-Tests}/test_binary_2.py | 0 .../{10-Tests => 15-Tests}/test_drawing_1.py | 0 .../{10-Tests => 15-Tests}/test_drawing_2.py | 0 .../test_erode_and_dilate.py | 0 .../{10-Tests => 15-Tests}/test_save.py | 0 32 files changed, 372 insertions(+), 308 deletions(-) delete mode 100644 usr/examples/05-Picture-Taking/compress.py delete mode 100644 usr/examples/05-Picture-Taking/write_jpeg.py delete mode 100644 usr/examples/05-Picture-Taking/write_ppm.py rename usr/examples/{05-Picture-Taking/save.py => 05-Snapshot/snapshot.py} (65%) create mode 100644 usr/examples/05-Snapshot/snapshot_on_face_detection.py create mode 100644 usr/examples/05-Snapshot/snapshot_on_movement.py create mode 100644 usr/examples/06-Video-Recording/gif_on_face_detection.py create mode 100644 usr/examples/06-Video-Recording/gif_on_movement.py create mode 100644 usr/examples/06-Video-Recording/mjpeg_on_face_detection.py create mode 100644 usr/examples/06-Video-Recording/mjpeg_on_movement.py delete mode 100644 usr/examples/06-Video-Recording/mjpeg_py.py rename usr/examples/{08-Feature-Detection => 07-Face-Detection}/face_detection.py (100%) rename usr/examples/{08-Feature-Detection => 07-Face-Detection}/face_tracking.py (100%) rename usr/examples/{08-Feature-Detection => 08-Eye-Tracking}/face_eye_detection.py (100%) rename usr/examples/{08-Feature-Detection => 08-Eye-Tracking}/iris_detection.py (100%) rename usr/examples/{08-Feature-Detection => 09-Feature-Detection}/freak.py (100%) rename usr/examples/{08-Feature-Detection => 09-Feature-Detection}/lbp.py (100%) rename usr/examples/{08-Feature-Detection => 09-Feature-Detection}/template_matching.py (100%) delete mode 100644 usr/examples/09-Miscellaneous/motion_detection.py rename usr/examples/{08-Feature-Detection => 10-Color-Tracking}/blob_detection.py (100%) rename usr/examples/{10-Tests => 15-Tests}/colorbar.py (100%) rename usr/examples/{10-Tests => 15-Tests}/selftest.py (100%) rename usr/examples/{10-Tests => 15-Tests}/test_binary_1.py (100%) rename usr/examples/{10-Tests => 15-Tests}/test_binary_2.py (100%) rename usr/examples/{10-Tests => 15-Tests}/test_drawing_1.py (100%) rename usr/examples/{10-Tests => 15-Tests}/test_drawing_2.py (100%) rename usr/examples/{10-Tests => 15-Tests}/test_erode_and_dilate.py (100%) rename usr/examples/{10-Tests => 15-Tests}/test_save.py (100%) diff --git a/usr/examples/01-Basics/helloworld.py b/usr/examples/01-Basics/helloworld.py index b1ef8e439..2f0286307 100644 --- a/usr/examples/01-Basics/helloworld.py +++ b/usr/examples/01-Basics/helloworld.py @@ -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): diff --git a/usr/examples/01-Basics/main.py b/usr/examples/01-Basics/main.py index 0a93da4df..97d6d921f 100644 --- a/usr/examples/01-Basics/main.py +++ b/usr/examples/01-Basics/main.py @@ -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) diff --git a/usr/examples/05-Picture-Taking/compress.py b/usr/examples/05-Picture-Taking/compress.py deleted file mode 100644 index 553e11a20..000000000 --- a/usr/examples/05-Picture-Taking/compress.py +++ /dev/null @@ -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) diff --git a/usr/examples/05-Picture-Taking/write_jpeg.py b/usr/examples/05-Picture-Taking/write_jpeg.py deleted file mode 100644 index 3b1383366..000000000 --- a/usr/examples/05-Picture-Taking/write_jpeg.py +++ /dev/null @@ -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.") \ No newline at end of file diff --git a/usr/examples/05-Picture-Taking/write_ppm.py b/usr/examples/05-Picture-Taking/write_ppm.py deleted file mode 100644 index 27ab31eea..000000000 --- a/usr/examples/05-Picture-Taking/write_ppm.py +++ /dev/null @@ -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.") \ No newline at end of file diff --git a/usr/examples/05-Picture-Taking/save.py b/usr/examples/05-Snapshot/snapshot.py similarity index 65% rename from usr/examples/05-Picture-Taking/save.py rename to usr/examples/05-Snapshot/snapshot.py index 83162ec10..37156d70b 100644 --- a/usr/examples/05-Picture-Taking/save.py +++ b/usr/examples/05-Snapshot/snapshot.py @@ -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.") diff --git a/usr/examples/05-Snapshot/snapshot_on_face_detection.py b/usr/examples/05-Snapshot/snapshot_on_face_detection.py new file mode 100644 index 000000000..8ec5ed5fa --- /dev/null +++ b/usr/examples/05-Snapshot/snapshot_on_face_detection.py @@ -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. diff --git a/usr/examples/05-Snapshot/snapshot_on_movement.py b/usr/examples/05-Snapshot/snapshot_on_movement.py new file mode 100644 index 000000000..cc8d3721c --- /dev/null +++ b/usr/examples/05-Snapshot/snapshot_on_movement.py @@ -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. diff --git a/usr/examples/06-Video-Recording/gif.py b/usr/examples/06-Video-Recording/gif.py index 725a009fd..a268f6a00 100644 --- a/usr/examples/06-Video-Recording/gif.py +++ b/usr/examples/06-Video-Recording/gif.py @@ -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.") diff --git a/usr/examples/06-Video-Recording/gif_on_face_detection.py b/usr/examples/06-Video-Recording/gif_on_face_detection.py new file mode 100644 index 000000000..dd2db2a14 --- /dev/null +++ b/usr/examples/06-Video-Recording/gif_on_face_detection.py @@ -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...") diff --git a/usr/examples/06-Video-Recording/gif_on_movement.py b/usr/examples/06-Video-Recording/gif_on_movement.py new file mode 100644 index 000000000..6cf0c0d1b --- /dev/null +++ b/usr/examples/06-Video-Recording/gif_on_movement.py @@ -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...") diff --git a/usr/examples/06-Video-Recording/mjpeg.py b/usr/examples/06-Video-Recording/mjpeg.py index 64d325e62..f3623d756 100644 --- a/usr/examples/06-Video-Recording/mjpeg.py +++ b/usr/examples/06-Video-Recording/mjpeg.py @@ -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.") diff --git a/usr/examples/06-Video-Recording/mjpeg_on_face_detection.py b/usr/examples/06-Video-Recording/mjpeg_on_face_detection.py new file mode 100644 index 000000000..90d295b82 --- /dev/null +++ b/usr/examples/06-Video-Recording/mjpeg_on_face_detection.py @@ -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...") diff --git a/usr/examples/06-Video-Recording/mjpeg_on_movement.py b/usr/examples/06-Video-Recording/mjpeg_on_movement.py new file mode 100644 index 000000000..2bce47814 --- /dev/null +++ b/usr/examples/06-Video-Recording/mjpeg_on_movement.py @@ -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...") diff --git a/usr/examples/06-Video-Recording/mjpeg_py.py b/usr/examples/06-Video-Recording/mjpeg_py.py deleted file mode 100644 index acee349b5..000000000 --- a/usr/examples/06-Video-Recording/mjpeg_py.py +++ /dev/null @@ -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) diff --git a/usr/examples/08-Feature-Detection/face_detection.py b/usr/examples/07-Face-Detection/face_detection.py similarity index 100% rename from usr/examples/08-Feature-Detection/face_detection.py rename to usr/examples/07-Face-Detection/face_detection.py diff --git a/usr/examples/08-Feature-Detection/face_tracking.py b/usr/examples/07-Face-Detection/face_tracking.py similarity index 100% rename from usr/examples/08-Feature-Detection/face_tracking.py rename to usr/examples/07-Face-Detection/face_tracking.py diff --git a/usr/examples/08-Feature-Detection/face_eye_detection.py b/usr/examples/08-Eye-Tracking/face_eye_detection.py similarity index 100% rename from usr/examples/08-Feature-Detection/face_eye_detection.py rename to usr/examples/08-Eye-Tracking/face_eye_detection.py diff --git a/usr/examples/08-Feature-Detection/iris_detection.py b/usr/examples/08-Eye-Tracking/iris_detection.py similarity index 100% rename from usr/examples/08-Feature-Detection/iris_detection.py rename to usr/examples/08-Eye-Tracking/iris_detection.py diff --git a/usr/examples/08-Feature-Detection/freak.py b/usr/examples/09-Feature-Detection/freak.py similarity index 100% rename from usr/examples/08-Feature-Detection/freak.py rename to usr/examples/09-Feature-Detection/freak.py diff --git a/usr/examples/08-Feature-Detection/lbp.py b/usr/examples/09-Feature-Detection/lbp.py similarity index 100% rename from usr/examples/08-Feature-Detection/lbp.py rename to usr/examples/09-Feature-Detection/lbp.py diff --git a/usr/examples/08-Feature-Detection/template_matching.py b/usr/examples/09-Feature-Detection/template_matching.py similarity index 100% rename from usr/examples/08-Feature-Detection/template_matching.py rename to usr/examples/09-Feature-Detection/template_matching.py diff --git a/usr/examples/09-Miscellaneous/motion_detection.py b/usr/examples/09-Miscellaneous/motion_detection.py deleted file mode 100644 index 6d731b57f..000000000 --- a/usr/examples/09-Miscellaneous/motion_detection.py +++ /dev/null @@ -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 diff --git a/usr/examples/08-Feature-Detection/blob_detection.py b/usr/examples/10-Color-Tracking/blob_detection.py similarity index 100% rename from usr/examples/08-Feature-Detection/blob_detection.py rename to usr/examples/10-Color-Tracking/blob_detection.py diff --git a/usr/examples/10-Tests/colorbar.py b/usr/examples/15-Tests/colorbar.py similarity index 100% rename from usr/examples/10-Tests/colorbar.py rename to usr/examples/15-Tests/colorbar.py diff --git a/usr/examples/10-Tests/selftest.py b/usr/examples/15-Tests/selftest.py similarity index 100% rename from usr/examples/10-Tests/selftest.py rename to usr/examples/15-Tests/selftest.py diff --git a/usr/examples/10-Tests/test_binary_1.py b/usr/examples/15-Tests/test_binary_1.py similarity index 100% rename from usr/examples/10-Tests/test_binary_1.py rename to usr/examples/15-Tests/test_binary_1.py diff --git a/usr/examples/10-Tests/test_binary_2.py b/usr/examples/15-Tests/test_binary_2.py similarity index 100% rename from usr/examples/10-Tests/test_binary_2.py rename to usr/examples/15-Tests/test_binary_2.py diff --git a/usr/examples/10-Tests/test_drawing_1.py b/usr/examples/15-Tests/test_drawing_1.py similarity index 100% rename from usr/examples/10-Tests/test_drawing_1.py rename to usr/examples/15-Tests/test_drawing_1.py diff --git a/usr/examples/10-Tests/test_drawing_2.py b/usr/examples/15-Tests/test_drawing_2.py similarity index 100% rename from usr/examples/10-Tests/test_drawing_2.py rename to usr/examples/15-Tests/test_drawing_2.py diff --git a/usr/examples/10-Tests/test_erode_and_dilate.py b/usr/examples/15-Tests/test_erode_and_dilate.py similarity index 100% rename from usr/examples/10-Tests/test_erode_and_dilate.py rename to usr/examples/15-Tests/test_erode_and_dilate.py diff --git a/usr/examples/10-Tests/test_save.py b/usr/examples/15-Tests/test_save.py similarity index 100% rename from usr/examples/10-Tests/test_save.py rename to usr/examples/15-Tests/test_save.py