This repository provides a method to track a moving object and access its location in real time using the OpenCV template matching method. To ensure efficiency, the code runs in three threads:
- The first thread collects frames from the camera.
- The second thread detects the location of the object in each frame.
- The third thread saves the object locations to a
.npyfile on your local device, allowing real-time access to the object locations.
An example of how to use this code can be found in the main branch of this repository in three_threads.py.
-
Connect the Camera:
- Connect the camera and adjust the settings to get a good image of the object being tracked.
-
Initialize Queues:
- Start two queues: one to hold the frames from the camera, and another to hold the location of the object.
-
Load the Template:
- Import the template using the
load_templatefunction.
- Import the template using the
-
Set Parameters:
- Determine the number of frames to be captured and processed.
- Decide how often location data will be saved locally.
- Specify the file path for saving locations. If the file does not exist, it will be automatically created.
-
Create Threads:
- Create each thread using
threading.Thread(target=, args=()).
- Create each thread using
-
Start Threads:
- Start each thread using
.start().
- Start each thread using
-
Stop Threads:
- Stop the threads using
.join().
- Stop the threads using
-
Disconnect the Camera:
- Disconnect the camera when done.
To read the data while locations are being collected, use the read_locations function. An example is provided in how_to_access_locations.py.
-
Overwriting Data:
- If the file chosen to save the data already exists, the new data will overwrite the old data.
-
Image Format:
- The code assumes that all images are in grayscale.
-
Batch Size:
- The batch size must be a multiple of the number of frames to be collected.
-
Python Version:
- The Baumer camera used in this project requires Python version 3.10.2.
This repository provides a comprehensive solution for real-time object tracking using OpenCV, ensuring efficient and effective tracking through multi-threading.