Skip to content

Commit

Permalink
Merge pull request #40 from tryolabs/remove_dependencies
Browse files Browse the repository at this point in the history
Make OpenCV and motmetrics optional dependencies
  • Loading branch information
joaqo committed Feb 17, 2021
2 parents 46ceefe + 5b8ee6c commit e062198
Show file tree
Hide file tree
Showing 18 changed files with 672 additions and 553 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
max-parallel: 3
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2

Expand Down
3 changes: 3 additions & 0 deletions .isort.cfg
@@ -0,0 +1,3 @@
[tool.isort]
profile = "black"
multi_line_output = 3
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -20,12 +20,24 @@ Norfair is built, used and maintained by [Tryolabs](https://tryolabs.com).

## Installation

Norfair currently supports Python 3.6+.
Norfair currently supports Python 3.7+.

For the pure Python version, install as:

```bash
pip install norfair
```

To make Norfair install the dependencies to support more features, install as:

```bash
pip install norfair[video] # Adds several video helper features running on OpenCV
pip install norfair[metrics] # Supports running MOT metrics evaluation
pip install norfair[metrics,video] # Everything included
```

If the needed dependencies are already present in the system, installing the pure Python version of Norfair is enough for enabling the extra features. This is particuarly useful for embedded devices, where installing compiled dependencies can be difficult.

## How it works

Norfair works by estimating the future position of each point based on its past positions. It then tries to match these estimated positions with newly detected points provided by the detector. For this matching to occur, Norfair can rely on any distance function specified by the user of the library. Therefore, each object tracker can be made as simple or as complex as needed.
Expand Down Expand Up @@ -114,4 +126,4 @@ If you are interested, please [contact us](https://tryolabs.com/#contact).

## License

Copyright © 2020, [Tryolabs](https://tryolabs.com). Released under the [BSD 3-Clause](LICENSE).
Copyright © 2021, [Tryolabs](https://tryolabs.com). Released under the [BSD 3-Clause](LICENSE).
5 changes: 3 additions & 2 deletions demos/alphapose/README.md
Expand Up @@ -4,8 +4,9 @@ An example of how to integrate Norfair into the video inference loop of a pre ex

## Instructions

1. [Follow the instructions](https://github.com/MVIG-SJTU/AlphaPose/tree/pytorch#installation) to install the Pytorch version of AlphaPose.
2. Apply this diff to this [commit](https://github.com/MVIG-SJTU/AlphaPose/commit/ded84d450faf56227680f0527ff7e24ab7268754) on AlphaPose and use their [video_demo.py](https://github.com/MVIG-SJTU/AlphaPose/blob/ded84d450faf56227680f0527ff7e24ab7268754/video_demo.py) to process your video.
1. Install Norfair with `pip install norfair[video]`.
2. [Follow the instructions](https://github.com/MVIG-SJTU/AlphaPose/tree/pytorch#installation) to install the Pytorch version of AlphaPose.
3. Apply this diff to this [commit](https://github.com/MVIG-SJTU/AlphaPose/commit/ded84d450faf56227680f0527ff7e24ab7268754) on AlphaPose and use their [video_demo.py](https://github.com/MVIG-SJTU/AlphaPose/blob/ded84d450faf56227680f0527ff7e24ab7268754/video_demo.py) to process your video.

```diff
diff --git a/dataloader.py b/dataloader.py
Expand Down
5 changes: 3 additions & 2 deletions demos/detectron2/README.md
Expand Up @@ -6,8 +6,9 @@ Simplest possible example of tracking. Based on [Detectron2](https://github.com/

Assuming Norfair is installed:

1. [Follow the instructions](https://detectron2.readthedocs.io/tutorials/install.html) to install Detectron2.
2. Run `python detectron2_cars.py`. For the demo, we are using [this traffic footage](https://www.youtube.com/watch?v=aio9g9_xVio).
1. Install Norfair with `pip install norfair[video]`.
2. [Follow the instructions](https://detectron2.readthedocs.io/tutorials/install.html) to install Detectron2.
3. Run `python detectron2_cars.py`. For the demo, we are using [this traffic footage](https://www.youtube.com/watch?v=aio9g9_xVio).

## Explanation

Expand Down
34 changes: 19 additions & 15 deletions demos/motmetrics4norfair/README.md
@@ -1,24 +1,28 @@
# Compute MOTChallenge metrics

Demo on how to evaluate a Norfair tracker on the [MOTChallenge](https://motchallenge.net).

## Instructions

1. Download the [MOT17](https://motchallenge.net/data/MOT17/) dataset running
1. Install Norfair with `pip install norfair[metrics,video]`.
2. Download the [MOT17](https://motchallenge.net/data/MOT17/) dataset running:

```bash
curl -O https://motchallenge.net/data/MOT17.zip # To download Detections + Ground Truth + Images (5.5GB)
unzip MOT17.zip
```

or

```bash
curl -O https://motchallenge.net/data/MOT17.zip # To download Detections + Ground Truth + Images (5.5GB)
unzip MOT17.zip
```
or
```bash
curl -O https://motchallenge.net/data/MOT17Labels.zip # To download Detections + Ground Truth (9.7 MB)
unzip MOT17Labels.zip
```

```bash
curl -O https://motchallenge.net/data/MOT17Labels.zip # To download Detections + Ground Truth (9.7 MB)
unzip MOT17Labels.zip
```
Given that the ground truth files for the testing set are not publicly available, you will only be able to use motmetrics4norfair with the training set.

Given that the ground truth files for the testing set are not publicly available, you will only be able to use motmetrics4norfair with the training set.
3. Display the instructions:

2. Display the motmetrics4norfair instructions:
```bash
python motmetrics4norfair.py --help
```
```bash
python motmetrics4norfair.py --help
```
7 changes: 4 additions & 3 deletions demos/motmetrics4norfair/motmetrics4norfair.py
@@ -1,8 +1,9 @@
import argparse
import os.path

import numpy as np
import norfair
from norfair import Detection, Tracker, metrics, video, drawing
import argparse

from norfair import Tracker, drawing, metrics, video

frame_skip_period = 1
detection_threshold = 0.01
Expand Down
5 changes: 3 additions & 2 deletions demos/openpose/README.md
Expand Up @@ -4,8 +4,9 @@ Demo for extrapolating detections through skipped frames. Based on [OpenPose](ht

## Instructions

1. Install [OpenPose version 1.4](https://github.com/CMU-Perceptual-Computing-Lab/openpose/releases/tag/v1.4.0).
2. Run `python openpose_extrapolation.py`.
1. Install Norfair with `pip install norfair[video]`.
2. Install [OpenPose version 1.4](https://github.com/CMU-Perceptual-Computing-Lab/openpose/releases/tag/v1.4.0).
3. Run `python openpose_extrapolation.py`.

## Explanation

Expand Down
6 changes: 3 additions & 3 deletions demos/yolov4/README.md
Expand Up @@ -4,12 +4,12 @@ Simplest possible example of tracking. Based on [pytorch YOLOv4](https://github.

## Instructions

1. Clone [pytorch YOLOv4](https://github.com/Tianxiaomo/pytorch-YOLOv4/tree/master) and download the [weights](https://drive.google.com/open?id=1wv_LiFeCRYwtpkqREPeI13-gPELBDwuJ) published on the repo into your local clone of the repo.
2. Copy `yolov4demo.py` into your local clone of the repo and run `python yolov4demo.py <video file>`.
1. Install Norfair with `pip install norfair[video]`.
2. Clone [pytorch YOLOv4](https://github.com/Tianxiaomo/pytorch-YOLOv4/tree/master) and download the [weights](https://drive.google.com/open?id=1wv_LiFeCRYwtpkqREPeI13-gPELBDwuJ) published on the repo into your local clone of the repo.
3. Copy `yolov4demo.py` into your local clone of the repo and run `python yolov4demo.py <video file>`.

## Explanation

This example tracks objects using a single point per detection: the centroid of the bounding boxes around cars returned by YOLOv4.

![Norfair YOLOv4 demo](../../docs/yolo_cars.gif)

6 changes: 3 additions & 3 deletions demos/yolov4/yolov4demo.py
Expand Up @@ -3,13 +3,13 @@
import cv2
import numpy as np
import torch

import norfair
from models import Yolov4
from norfair import Detection, Tracker, Video
from tool.torch_utils import do_detect
from tool.utils import load_class_names, plot_boxes_cv2

import norfair
from norfair import Detection, Tracker, Video

max_distance_between_points = 30


Expand Down
2 changes: 1 addition & 1 deletion norfair/__init__.py
@@ -1,4 +1,4 @@
from .drawing import *
from .tracker import Detection, Tracker
from .utils import print_objects_as_table, get_cutout
from .utils import get_cutout, print_objects_as_table
from .video import Video
13 changes: 9 additions & 4 deletions norfair/drawing.py
@@ -1,10 +1,15 @@
from typing import Optional, Tuple, Sequence
from typing import Optional, Sequence, Tuple

import cv2
import numpy as np
try:
import cv2
except ImportError:
from .utils import DummyOpenCVImport

cv2 = DummyOpenCVImport()
import random

from .tracker import Detection, TrackedObject
import numpy as np

from .utils import validate_points


Expand Down
21 changes: 17 additions & 4 deletions norfair/metrics.py
@@ -1,11 +1,21 @@
import os

import numpy as np
from rich import print
from rich.progress import track

from norfair import Detection
import motmetrics as mm

try:
import motmetrics as mm
import pandas as pd
except ImportError:
from .utils import DummyMOTMetricsImport

mm = DummyMOTMetricsImport()
pandas = DummyMOTMetricsImport()
from collections import OrderedDict
import pandas as pd



class InformationFile:
Expand Down Expand Up @@ -157,6 +167,9 @@ def __init__(self):
self.paths = []

def create_accumulator(self, input_path, information_file=None):
# Check that motmetrics is installed here, so we don't have to process
# the whole dataset before failing out if we don't.
mm.metrics

file_name = os.path.split(input_path)[1]

Expand Down Expand Up @@ -220,9 +233,9 @@ def compute_metrics(
return self.summary

def save_metrics(self, save_path=".", file_name="metrics.txt"):

if not os.path.exists(save_path):
os.makedirs(save_folder)
os.makedirs(save_path)

metrics_path = os.path.join(save_path, file_name)
metrics_file = open(metrics_path, "w+")
metrics_file.write(self.summary)
Expand Down
4 changes: 2 additions & 2 deletions norfair/tracker.py
@@ -1,10 +1,10 @@
import math
from typing import Callable, Optional, Sequence, List
from typing import Callable, List, Optional, Sequence

import numpy as np
from filterpy.kalman import KalmanFilter

from rich import print

from .utils import validate_points


Expand Down
22 changes: 21 additions & 1 deletion norfair/utils.py
@@ -1,5 +1,5 @@
import os
from typing import Tuple, Sequence
from typing import Sequence, Tuple

import numpy as np
from rich import print
Expand Down Expand Up @@ -68,3 +68,23 @@ def get_cutout(points, image):
max_y = int(max(points[:, 1]))
min_y = int(min(points[:, 1]))
return image[min_y:max_y, min_x:max_x]


class DummyOpenCVImport:
def __getattribute__(self, name):
print(
"""[bold red]Missing dependency:[/bold red] You are trying to use Norfair's video features. However, OpenCV is not installed.
Please, make sure there is an existing installation of OpenCV or install Norfair with `pip install norfair\[video]`."""
)
exit()


class DummyMOTMetricsImport:
def __getattribute__(self, name):
print(
"""[bold red]Missing dependency:[/bold red] You are trying to use Norfair's metrics features without the required dependencies.
Please, install Norfair with `pip install norfair\[metrics]`, or `pip install norfair\[metrics,video]` if you also want video features."""
)
exit()
11 changes: 8 additions & 3 deletions norfair/video.py
@@ -1,11 +1,16 @@
import os
import time
from typing import Optional, List, Union
from typing import List, Optional, Union

import cv2
try:
import cv2
except ImportError:
from .utils import DummyOpenCVImport

cv2 = DummyOpenCVImport()
import numpy as np
from rich import print
from rich.progress import BarColumn, Progress, TimeRemainingColumn, ProgressColumn
from rich.progress import BarColumn, Progress, ProgressColumn, TimeRemainingColumn

from .utils import get_terminal_size

Expand Down

0 comments on commit e062198

Please sign in to comment.