Overview:
This project is about Shape Based Object Detection & Tracking with OpenCV on Raspberry Pi 4 Computer.
In the world of computer vision, object tracking is a crucial task that involves monitoring the movement of specific objects over time using a camera. Shape-based object tracking is a technique that recognizes and tracks objects based on their geometric shapes. OpenCV, an open-source computer vision library, provides a wide range of functions that make this form of tracking both feasible and efficient.
Pairing this with the Raspberry Pi, a compact and affordable mini-computer known for its adaptability in DIY projects, creates a potent combination. By running OpenCV on Raspberry Pi, both hobbyists and professionals can craft real-time tracking systems without heavy investments in hardware. From surveillance and robotics to interactive art installations, shape-based object tracking with OpenCV on Raspberry Pi offers innovative solutions across various fields.
Components Required
We need the following components for this project. You can purchase all items from given links:
| S.N. | Components | Quantity | Purchase Link |
|---|---|---|---|
| 1 | Raspberry Pi 4 | 1 | Amazon | SunFounder |
| 2 | Raspberry Pi Camera | 1 | Amazon | SunFounder |
| 2 | SD Card 16/32 GB | 1 | Amazon | SunFounder |
| 3 | 5V, 3A DC Adapter for RPi | 1 | Amazon | SunFounder |
| 4 | LCD Display (Optional) | 1 | Amazon | SunFounder |
| 5 | Mouse & Keyboard (Optional) | 1 | Amazon | SunFounder |
Raspberry Pi Camera Connection
The Raspberry Pi Camera is a peripheral device developed by the Raspberry Pi Foundation to be used with their series of Raspberry Pi single-board computers. The camera module provides a way to add video/photo capabilities to Raspberry Pi projects.
For this project, we can use a 5 mega-pixel Raspberry Pi Camera.
Simply connect the Camera Module to the Raspberry Pi 4 Board using the Camera Connector.
To use the Camera you need to enable the Camera Module first. Open the Raspberry Pi Configuration Tool by typing sudo raspi-config in the terminal. Navigate to Interfacing Options > Camera and enable it.
Raspberry Pi Setup, Libraries & Dependencies Installation
OpenCV is required for Object Detection and Tracking, and other image processing tasks present in the code. Hence you need to install the OpenCV first. Follow the following guide to install OpenCV in your system:
The next step is to install picamera. Therefore install it using pip.
|
1 2 |
pip3 install picamera |
The setup part is complete now. We can move to the Shape Based Object Detection & Tracking Project with Raspberry Pi and OpenCV.
Raspberry Pi Python Code for Object Tracking with OpenCV
Now let’s develop a Python Code that helps in the Shape Based Object Tracking with OpenCV library and Raspberry Pi 4 Camera.
Python Code
Open Thonny IDE and paste the following code to the Thonny Editor. Save this file with any name such as “Object_Tracking.py” to any location.
Here is a complete Python Code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
import cv2 import numpy as np import time CAMERA_DEVICE_ID = 0 IMAGE_WIDTH = 320 IMAGE_HEIGHT = 240 fps = 0 def set_camera_properties(cap, width, height): """ Set resolution properties for the camera """ cap.set(3, width) cap.set(4, height) def capture_frame(cap): """ Capture a frame from the video source """ ret, frame = cap.read() if not ret: raise ValueError("Failed to read a frame from the camera") return frame def detect_circles(gray_frame): """ Detect circles using Hough transform and return the circles found """ return cv2.HoughCircles(gray_frame, cv2.HOUGH_GRADIENT, 1.2, 100) def process_frame(frame): """ Blur, convert to grayscale and detect circles """ frame = cv2.blur(frame, (3, 3)) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) circles = detect_circles(gray) return frame, gray, circles def draw_circles_on_frame(frame, circles): """ Draw circles and center rectangles on the given frame """ output = frame.copy() if circles is not None: circles = np.round(circles[0, :]).astype("int") for (x, y, r) in circles: cv2.circle(output, (x, y), r, (0, 255, 0), 4) cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1) return output def visualize_fps(image, fps: float) -> np.ndarray: """Overlay the FPS value onto the given image.""" if len(np.shape(image)) < 3: text_color = (255, 255, 255) # white else: text_color = (0, 255, 0) # green row_size = 20 # pixels left_margin = 24 # pixels font_size = 1 font_thickness = 1 fps_text = 'FPS: {:.1f}'.format(fps) text_location = (left_margin, row_size) cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN, font_size, text_color, font_thickness) return image def main(): try: cap = cv2.VideoCapture(CAMERA_DEVICE_ID) if not cap.isOpened(): raise ValueError("Could not open the camera") set_camera_properties(cap, IMAGE_WIDTH, IMAGE_HEIGHT) print("Press 'Esc' to exit...") fps = 0 # Initialize the fps variable while True: start_time = time.time() frame = capture_frame(cap) frame, _, circles = process_frame(frame) output = draw_circles_on_frame(frame, circles) end_time = time.time() seconds = end_time - start_time fps = 1.0 / seconds # Overlay FPS and display frames cv2.imshow("Frame", np.hstack([visualize_fps(frame, fps), visualize_fps(output, fps)])) if cv2.waitKey(33) == 27: # Escape key break except Exception as e: print(e) finally: cv2.destroyAllWindows() cap.release() if __name__ == "__main__": main() |
Code Explanation
Let’s break down the code into sections and explain each:
|
1 2 3 4 |
import cv2 import numpy as np import time |
These lines import necessary libraries. cv2 is for OpenCV functions, numpy provides support for arrays and matrices, and time is used for timing operations.
|
1 2 3 4 5 |
CAMERA_DEVICE_ID = 0 IMAGE_WIDTH = 320 IMAGE_HEIGHT = 240 fps = 0 |
Here, constants and variables are defined. CAMERA_DEVICE_ID specifies the camera to use. IMAGE_WIDTH and IMAGE_HEIGHT set the video resolution, and fps initializes the frames per second variable.
|
1 2 3 4 5 |
def set_camera_properties(cap, width, height): """ Set resolution properties for the camera """ cap.set(3, width) cap.set(4, height) |
This function sets the resolution of the camera. The cap.set() method is used to modify camera properties, with 3 and 4 being the property IDs for width and height, respectively.
|
1 2 3 4 5 6 7 |
def capture_frame(cap): """ Capture a frame from the video source """ ret, frame = cap.read() if not ret: raise ValueError("Failed to read a frame from the camera") return frame |
This function captures a frame from the camera. If the frame isn’t captured successfully, it raises an error.
|
1 2 3 4 |
def detect_circles(gray_frame): """ Detect circles using Hough transform and return the circles found """ return cv2.HoughCircles(gray_frame, cv2.HOUGH_GRADIENT, 1.2, 100) |
This function detects circles in a grayscale frame using the Hough transform.
|
1 2 3 4 5 6 7 |
def process_frame(frame): """ Blur, convert to grayscale and detect circles """ frame = cv2.blur(frame, (3, 3)) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) circles = detect_circles(gray) return frame, gray, circles |
This function processes the frame by blurring it, converting it to grayscale, and detecting circles.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def main(): try: cap = cv2.VideoCapture(CAMERA_DEVICE_ID) ... while True: ... frame = capture_frame(cap) frame, _, circles = process_frame(frame) ... if cv2.waitKey(33) == 27: # Escape key break except Exception as e: print(e) finally: cv2.destroyAllWindows() cap.release() |
The main() function is the primary execution function. It initializes the camera, sets its properties, and enters a loop to capture, process, and display frames. The loop continues until the ‘Esc’ key is pressed. If any exceptions occur, they’re printed, and the program proceeds to the cleanup phase, where all OpenCV windows are destroyed, and the camera is released.
|
1 2 3 |
if __name__ == "__main__": main() |
This conditional checks if the script is being run as a standalone program (not imported as a module). If true, the main() function is executed.
Testing & Results of Shape Based Object Tracking
Now we need to run the code for Shape Based Object Tracking with OpenCV on Raspberry Pi.
After running the code, 2 windows will appear. The first window will show the image fed from Camera. The 2nd window wil show the tracked object.
On the 2nd window, the round object is detected and tracked using HoughCircles().
You may bring any round object in front of camera. A green circle will appear on the outer surface of the circle.
Conclusion
To sum up, the project combines OpenCV’s capabilities in shape-based object tracking with the Raspberry Pi 4’s compact and cost-effective nature. This blend allows for efficient monitoring of objects based on their geometric shapes in real-time. It’s a practical solution for various applications, from surveillance to art installations, making advanced computer vision accessible to both enthusiasts and professionals.










