Skip to content

Commit

Permalink
Tp/fix_issues (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanpepinartefact authored Nov 14, 2023
1 parent befb92d commit 7bc79a6
Show file tree
Hide file tree
Showing 26 changed files with 1,000 additions and 402 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ jobs:

- name: Install requirements
run: |
poetry install
make install
- name: Run Pre commit hooks
run: make format-code

- name: Test with pytest
run: make run-tests
6 changes: 4 additions & 2 deletions .github/workflows/deploy_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install poetry
run: |
make download-poetry
- name: Install requirements
run: |
make install_project_requirements
make install
- name: Deploying MkDocs documentation
run: |
mkdocs build
Expand Down
8 changes: 0 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ repos:
types: [file]
files: (.ipynb)$
language: system
- id: pytest-check
name: Tests (pytest)
stages: [push]
entry: pytest tests/
types: [python]
language: system
pass_filenames: false
always_run: true
exclude: ^(.svn|CVS|.bzr|.hg|.git|__pycache__|.tox|.ipynb_checkpoints|assets|tests/assets/|venv/|.venv/)
21 changes: 8 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
PYTHON_VERSION = 3.10
USE_CONDA ?= 1
INSTALL_SCRIPT = install_with_conda.sh
ifeq (false,$(USE_CONDA))
INSTALL_SCRIPT = install_with_venv.sh
endif

PYTHON_VERSION = 3.10.13
# help: help - Display this makefile's help information
.PHONY: help
help:
Expand All @@ -17,6 +11,7 @@ download-poetry:
# help: install - Install python dependencies using poetry
.PHONY: install
install:
@poetry config virtualenvs.create true
@poetry env use $(PYTHON_VERSION)
@poetry lock -n
@poetry install -n
Expand All @@ -27,12 +22,6 @@ install:
install-requirements:
@poetry install -n


.PHONY: install-dev-requirements
# help : install-dev-requirements - Install Python Dependencies for development
install-dev-requirements:
@poetry install -n --with dev

.PHONY: update-requirements
#help: update-requirements - Update Python Dependencies (requirements.txt and requirements-dev.txt)
update-requirements:
Expand All @@ -43,6 +32,12 @@ update-requirements:
format-code:
@poetry run pre-commit run -a

.PHONY: run-tests
#help: run-tests - Run all tests with pytest
run-tests:
@export PYTHONPATH=.
@poetry run pytest

# help: deploy_docs - Deploy documentation to GitHub Pages
.PHONY: deploy_docs
deploy_docs:
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ This Git repository is dedicated to the development of a Python library aimed at

## Installation

First, install poetry:

```bash
make download-poetry
```

To install the required packages in a virtual environment, run the following command:

```bash
make install
```

TODO: Choose between conda and venv if necessary or let the Makefile as is and copy/paste the [MORE INFO installation section](MORE_INFO.md#eased-installation) to explain how to choose between conda and venv.
You can then activate the env with the following command:

```bash
poetry shell
```

A complete list of available commands can be found using the following command:

Expand Down
18 changes: 0 additions & 18 deletions bin/install_with_conda.sh

This file was deleted.

20 changes: 0 additions & 20 deletions bin/install_with_venv.sh

This file was deleted.

1 change: 1 addition & 0 deletions lib/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __next__(self):
raise StopIteration

frame = Image.open(self.frame_paths[self.index])

try:
detection = np.loadtxt(self.detection_paths[self.index], dtype="float")
except OSError: # file doesn't exist not detection return empty file
Expand Down
167 changes: 73 additions & 94 deletions notebooks/starter_kit_reid.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload \n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
Expand All @@ -16,22 +16,21 @@
"metadata": {},
"outputs": [],
"source": [
"import sys \n",
"sys.path.append(\"..\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"import cv2\n",
"import numpy as np\n",
"import pandas as pd\n",
"from trackreid.reid_processor import ReidProcessor\n",
"from bytetracker import BYTETracker\n",
"from bytetracker.basetrack import BaseTrack\n",
"from tqdm import tqdm\n",
"\n",
"from lib.bbox.utils import rescale_bbox, xy_center_to_xyxy\n",
"from lib.sequence import Sequence\n",
"from trackreid.args.reid_args import OUTPUT_POSITIONS\n",
"import cv2\n",
"from tqdm import tqdm \n"
"from trackreid.reid_processor import ReidProcessor\n",
"\n",
"sys.path.append(\"..\")\n"
]
},
{
Expand All @@ -41,18 +40,6 @@
"# Real life data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from lib.sequence import Sequence\n",
"from bytetracker import BYTETracker\n",
"from lib.bbox.utils import xy_center_to_xyxy, rescale_bbox"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -64,7 +51,7 @@
"FRAME_PATH = f\"{DATA_PATH}/frames\"\n",
"VIDEO_OUTPUT_PATH = \"private\"\n",
"\n",
"SEQUENCES = os.listdir(FRAME_PATH)\n"
"SEQUENCES = os.listdir(DETECTION_PATH)\n"
]
},
{
Expand All @@ -83,11 +70,7 @@
" detections = os.listdir(f\"{DETECTION_PATH}/{sequence}\")\n",
" detections = [os.path.join(f\"{DETECTION_PATH}/{sequence}\", detection) for detection in detections]\n",
" detections.sort()\n",
" return detections\n",
"\n",
"frame_path = get_sequence_frames(SEQUENCES[2])\n",
"test_sequence = Sequence(frame_path)\n",
"test_sequence"
" return detections"
]
},
{
Expand Down Expand Up @@ -116,11 +99,10 @@
" processed_detection[idx,:4] = rescaled_bbox\n",
" processed_detection[idx,4] = conf\n",
" processed_detection[idx,5] = clss\n",
" \n",
"\n",
" return processed_detection\n",
" else:\n",
" return detection_output\n",
" "
" return detection_output\n"
]
},
{
Expand All @@ -137,16 +119,16 @@
"\n",
" if not detection_outputs.size :\n",
" return detection_outputs\n",
" \n",
"\n",
" processed_detections = self._pre_process(detection_outputs)\n",
" tracked_objects = self.tracker.update(processed_detections, _ = None)\n",
" processed_tracked = self._post_process(tracked_objects, frame_id)\n",
" tracked_objects = self.tracker.update(processed_detections, frame_id = frame_id)\n",
" processed_tracked = self._post_process(tracked_objects)\n",
" return processed_tracked\n",
"\n",
" def _pre_process(self,detection_outputs : np.ndarray):\n",
" return detection_outputs\n",
"\n",
" def _post_process(self, tracked_objects : np.ndarray, frame_id):\n",
" def _post_process(self, tracked_objects : np.ndarray):\n",
"\n",
" if tracked_objects.size :\n",
" if tracked_objects.ndim == 1:\n",
Expand Down Expand Up @@ -185,60 +167,57 @@
"metadata": {},
"outputs": [],
"source": [
"from bytetracker.basetrack import BaseTrack\n",
"BaseTrack._count = 0\n",
"\n",
"# Define the codec using VideoWriter_fourcc() and create a VideoWriter object\n",
"fourcc = cv2.VideoWriter_fourcc(*'avc1') # or use 'x264'\n",
"out = cv2.VideoWriter('output_corrected.mp4', fourcc, 20.0, (2560, 1440)) # adjust the frame size (640, 480) as per your needs\n",
"\n",
"\n",
"detection_handler = DetectionHandler(image_shape=[2560, 1440])\n",
"tracking_handler = TrackingHandler(tracker=BYTETracker(track_thresh= 0.3, track_buffer = 5, match_thresh = 0.85, frame_rate= 30))\n",
"reid_processor = ReidProcessor(filter_confidence_threshold=0.1, \n",
" filter_time_threshold=0,\n",
" cost_function=bounding_box_distance,\n",
" selection_function=select_by_category,\n",
" max_attempt_to_rematch=1,\n",
" max_frames_to_rematch=100)\n",
"\n",
"frame_id = 0\n",
"\n",
"for frame, detection in tqdm(test_sequence):\n",
" frame = np.array(frame)\n",
"\n",
" frame_id += 1\n",
"\n",
" processed_detections = detection_handler.process(detection)\n",
" processed_tracked = tracking_handler.update(processed_detections, frame_id)\n",
" reid_results = reid_processor.process(processed_tracked, frame_id)\n",
"\n",
" if reid_results.size:\n",
" for res in reid_results:\n",
" object_id =res[OUTPUT_POSITIONS[\"object_id\"]]\n",
" x1, y1, x2, y2 = res[OUTPUT_POSITIONS[\"bbox\"]]\n",
" class_id = res[OUTPUT_POSITIONS[\"category\"]]\n",
" confidence_score = res[OUTPUT_POSITIONS[\"confidence\"]]\n",
"\n",
" frame_id, object_id, class_id, x1, y1, x2, y2 = int(frame_id), int(object_id), int(class_id), int(x1), int(y1), int(x2), int(y2)\n",
" color = (0, 255, 0) if class_id == 0.0 else (0, 0, 255) # green for class 0, red for class 1\n",
" cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)\n",
" cv2.putText(frame, str(object_id), (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)\n",
"\n",
"\n",
" frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
" # Write the frame to the video file\n",
" out.write(frame)\n",
"out.release()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reid_processor.all_tracked_objects"
"for sequence in SEQUENCES :\n",
" frame_path = get_sequence_frames(sequence)\n",
" test_sequence = Sequence(frame_path)\n",
" test_sequence\n",
" frame_id = 0\n",
" BaseTrack._count = 0\n",
"\n",
" # Define the codec using VideoWriter_fourcc() and create a VideoWriter object\n",
" fourcc = cv2.VideoWriter_fourcc(*'avc1') # or use 'x264'\n",
" out = cv2.VideoWriter(f'{sequence}.mp4', fourcc, 20.0, (2560, 1440)) # adjust the frame size (640, 480) as per your needs\n",
"\n",
"\n",
" detection_handler = DetectionHandler(image_shape=[2560, 1440])\n",
" tracking_handler = TrackingHandler(tracker=BYTETracker(track_thresh= 0.3, track_buffer = 5, match_thresh = 0.85, frame_rate= 30))\n",
" reid_processor = ReidProcessor(filter_confidence_threshold=0.1,\n",
" filter_time_threshold=5,\n",
" cost_function=bounding_box_distance,\n",
" #cost_function_threshold=500, # max cost to rematch 2 objects\n",
" selection_function=select_by_category,\n",
" max_attempt_to_match=5,\n",
" max_frames_to_rematch=500)\n",
"\n",
" for frame, detection in tqdm(test_sequence):\n",
" frame = np.array(frame)\n",
"\n",
" frame_id += 1\n",
"\n",
" processed_detections = detection_handler.process(detection)\n",
" processed_tracked = tracking_handler.update(processed_detections, frame_id)\n",
" reid_results = reid_processor.update(processed_tracked, frame_id)\n",
"\n",
" if len(reid_results) > 0:\n",
" for res in reid_results:\n",
" object_id = int(res[OUTPUT_POSITIONS[\"object_id\"]])\n",
" bbox = list(map(int, res[OUTPUT_POSITIONS[\"bbox\"]]))\n",
" class_id = int(res[OUTPUT_POSITIONS[\"category\"]])\n",
" tracker_id = int(res[OUTPUT_POSITIONS[\"tracker_id\"]])\n",
" mean_confidence = float(res[OUTPUT_POSITIONS[\"mean_confidence\"]])\n",
" #mean_confidence_per_object[object_id].append((frame_id, mean_confidence))\n",
" x1, y1, x2, y2 = bbox\n",
" color = (0, 0, 255) if class_id else (0, 255, 0) # green for class 0, red for class 1\n",
" cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)\n",
" cv2.putText(frame, f\"{object_id} ({tracker_id}) : {round(mean_confidence,2)}\", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)\n",
" frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
"\n",
" # Write the frame to the video file\n",
" out.write(frame)\n",
" out.release()\n",
"\n",
" print(sequence, len(reid_processor.seen_objects),reid_processor.nb_corrections)\n",
" print(reid_processor.seen_objects)\n"
]
}
],
Expand Down
Loading

0 comments on commit 7bc79a6

Please sign in to comment.