A pedestrian tracking system using probabilistic graph models to assign bounding rectangles (Bounding Boxes) to people in consecutive video frames
Clone repository:
git clone https://github.com/2acholsk1/pedestrian_tracker.git
cd pedestrian_trackerAfter clone repository, you need to setup virtual environemnt, with which commands in the Makefile will help:
make setup-venvRemember to activate venv with command: source bin/activate.
Then, you can easily build and install package by using:
make setupIf you want to use example data unzip it in data folder:
unzip c6s1To start the program type, remeber to add path which contain frames of video to analyze:
python3 src/__main__.py path/to/frames/folderIf you want to ocheck accuracy, remember to add path to file with correct answers:
python3 src/result_accuracy.py path/to/fileOverall accuracy for c6s1 data folder is 95%, 2585 correct answers out of 2733 total.
![]()
The project consists of two main classes BoundBox and Histogram.
BoundBox class manages rectangular regions (bounding boxes) on images. Methods:
- init(self, coordinates: list, image: cv2.Mat): Initializes with bounding box coordinates and an image.
- compute_coords(self, coordinates: list): Converts and stores coordinates.
- compute_bb(self): Computes and extracts bounding boxes from the image.
- return_bb(self) -> list: Returns the list of bounding boxes.
- return_nodes(self) -> list: Returns the list of node identifiers.
Histogram class computes and compares histograms of bounding boxes. Methods:
- init(self, bb: list, new_obj_prob: float, factor_graph: DiscreteFactor): Initializes with bounding boxes, new object probability, and a factor graph.
- hist_bb_calc(self) -> list: Computes histograms for each bounding box.
- hist_bb_compare(self, bb_current_hist: list, bb_previous_hist: list) -> DiscreteFactor: Compares histograms between frames and updates the factor graph.
There is also an auxiliary class - Visualize, which, as the name suggest, is used to visualize effects of the project.
Main function processes the images and bounding boxes, computes histograms, compares them, and performs belief propagation to track or identify objects across frames.
Below is a diagram describing how to build a graph with two images and their bounding boxes:
![]()
- Initialization:
- Initialize variables for current and previous histograms (hist_curr, hist_prev), current bounding boxes (bb_curr), and a flag (bb_none) to handle cases with no bounding boxes.
- Set a probability for new objects (prob_new).
- Prepare a list to store results.
- Iterate Over Images:
- Loop through each image in the provided image paths.
- For each image, read and discard the first line (usually the image filename).
- Read Bounding Box Information:
- Read the number of bounding boxes (bb_num) for the current image.
- If no bounding boxes are present (bb_num is "0"), set the bb_none flag and skip further processing for the current image.
- Compute Bounding Boxes:
- If bounding boxes are present, create an instance of BoundBox with the coordinates and image.
- Call compute_bb method to extract bounding boxes.
- Retrieve the bounding boxes (bb_curr) and node identifiers (nodes).
- Add nodes to the factor graph.
- Compute Histograms:
- Create an instance of Histogram with the current bounding boxes, new object probability, and factor graph.
- Call hist_bb_calc method to compute histograms for the current bounding boxes.
- Handle No Bounding Boxes in Previous Image:
- If bb_none flag is set (no bounding boxes in the previous image), reset the flag, append -1 results for the current image, and skip further processing.
- Setup Node Possibility Matrix:
- Create a matrix to represent the possibilities of node connections.
- Initialize the matrix with ones and set diagonal elements to zero (to prevent self-connections).
- Compare Histograms and Update Factor Graph:
- If previous histograms are available, call hist_bb_compare method to compare current and previous histograms, and update the factor graph.
- Use combinations of current and previous histogram indices to create and add factors to the factor graph.
- Add edges to the factor graph based on histogram comparisons.
- Perform Belief Propagation:
- Create an instance of BeliefPropagation with the factor graph.
- Calibrate the factor graph using the calibrate method.
- Query the factor graph to determine the most probable states of the variables (bounding boxes).
- Store and print the results, adjusting the indices as needed.
- Handle No Previous Histograms:
- If no previous histograms are available, append -1 results for the current image.
- Write Results to File:
- After processing all images, write the results to a text file (
data/check/results.txt).
Please report bugs and request features using the Issue Tracker.