Week 12: Motion Detection#
Laboratory 9
Last updated Jan 5, 2023
00. Content #
Mathematics
Vector fields
Programming Skills
Function and modules
Embedded Systems
Thonny and Micropython
0. Required Hardware #
Microcontroller: Raspberry Pi Pico
Breadboard
USB connector
Camera
Write your name and email below:
Name: me
Email: me @purdue.edu
You may work in groups of 2-3 for this lab.
Groupmate(s):
import numpy as np
import matplotlib.pyplot as plt
import cv2
1. Detecting Motion #
During the previous lab, you learned how to display the optical flow between the frames of a video and how to set up a camera using the Raspberry Pi Pico. For this lab, we will be focusing on using the camera and analyzing the video that it provides.
Set up your camera using the instructions from last week’s lab. After it is set up, try Exercise 1 using what you learned last week. As a reminder, OpenCV is used for images and videos in Python, so remember to make use of this. You may want to look up commands to help during this lab.
If you run into significant hardware issues, use the test video from Lab 7.
Rerun the following cells as you did in the previous lab:
import time
import serial
from serial.tools import list_ports
PICO_HWID = "2E8A"
def get_pico_port():
pico_ports = list(list_ports.grep(PICO_HWID))
if len(pico_ports) == 0:
raise Exception(
"No Raspberry Pi Pico was detected. Check to make sure it is plugged in, and that no other programs are accessing it"
)
return pico_ports[0].device
print("Here are all the serial devices detected:")
for port in list_ports.comports():
print(port.device, port.hwid)
port = get_pico_port()
print(f"\nselected port {port} as most likely to have a raspberry pi pico")
buffer = bytearray(96 * 96)
img = np.zeros(shape=(96, 96), dtype="uint8")
with serial.Serial(port, timeout=1) as s:
s.read_until(b"\x55\xAA")
s.readinto(buffer)
img.flat[::-1] = buffer
plt.imshow(img, cmap="gray")
plt.show()
%matplotlib widget
fig, ax = plt.subplots()
render = ax.imshow(img, cmap='gray')
plt.show(block=False)
try:
with serial.Serial(port, timeout=1) as s:
while True:
s.read_until(b"\x55\xAA")
s.readinto(buffer)
img.flat[::-1] = buffer
render.set_data(img)
fig.canvas.draw()
except KeyboardInterrupt:
pass
Remember that when you are done, you will have to hit the Interrupt Kernel
button, which can found at the top of the screen (the stop symbol) or under Kernel.
Exercise 1 #
Part 1
Recall the previous lab on optical flow. Can you think of a (mathematical) rule to tell when an object enters the camera’s view?
Hint: Are there any values that would be high/low when there is motion?
Write Answers for Exercise 1 Part 1 Below
Part 2
Using your camera, detect motion in real time (or near real time). You can use any method to do this, whether it be Farneback’s algorithm to determine the differences between frames, your answer to Exercise 1 Part 1, or anything else you can think of. Decide how to display that movement has been detected on the camera, such as having Python print a statement.
Display all code you wrote either by copying and pasting or reading in and printing Python file contents.
Write Answers for Exercise 1 Part 2 Below
Exercise 2 #
Analyze the camera feed in real time and display which direction the movement is occurring in. This can be limited to up, down, right, left.
Hint: Compare neighboring frames and determine where there are significant differences between them.
Part 1
Display all code you wrote either by copying and pasting or reading in and printing Python file contents.
Write Answers for Exercise 2 Part 1 Below
Part 2
Describe how well the motion detector and direction indicator works. What problems arose and were you able to resolve any of them?
Write Answers for Exercise 2 Part 2 Below
Reflection #
1. What parts of the lab, if any, do you feel you did well?
2. What are some things you learned today?
3. Are there any topics that could use more clarification?
4. Do you have any suggestions on parts of the lab to improve?