-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from FelixNgFender/main
Examples and restructure python server code
- Loading branch information
Showing
46 changed files
with
3,426 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,38 @@ | ||
{ | ||
"notebook.formatOnSave.enabled": true, | ||
"notebook.codeActionsOnSave": { | ||
"notebook.source.fixAll":"explicit", | ||
"notebook.source.organizeImports": "explicit" | ||
"notebook.formatOnSave.enabled": true, | ||
"notebook.codeActionsOnSave": { | ||
"notebook.source.fixAll": "explicit", | ||
"notebook.source.organizeImports": "explicit" | ||
}, | ||
"[python]": { | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": "explicit", | ||
"source.organizeImports": "explicit" | ||
}, | ||
"[python]": { | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": "explicit", | ||
"source.organizeImports": "explicit" | ||
}, | ||
"editor.defaultFormatter": "charliermarsh.ruff", | ||
}, | ||
"python.analysis.typeCheckingMode": "off", // TODO: Enable strict type checking | ||
"python.analysis.autoImportCompletions": true, | ||
"[csharp]": { | ||
"editor.formatOnSave": true, | ||
"editor.maxTokenizationLineLength": 2500, | ||
"editor.inlineSuggest.suppressSuggestions": false | ||
}, | ||
"cSpell.words": [ | ||
"editor.defaultFormatter": "charliermarsh.ruff" | ||
}, | ||
"python.analysis.typeCheckingMode": "off", // TODO: Enable strict type checking | ||
"python.analysis.autoImportCompletions": true, | ||
"[csharp]": { | ||
"editor.formatOnSave": true, | ||
"editor.maxTokenizationLineLength": 2500, | ||
"editor.inlineSuggest.suppressSuggestions": false | ||
}, | ||
"cSpell.words": [ | ||
"arange", | ||
"arflow", | ||
"astype", | ||
"Behaviour", | ||
"arflow", | ||
"astype", | ||
"Behaviour", | ||
"dtype", | ||
"flipud", | ||
"frombuffer", | ||
"gmtime", | ||
"flipud", | ||
"frombuffer", | ||
"gmtime", | ||
"meshgrid", | ||
"ndarray", | ||
"Protobuf", | ||
"thecakelab", | ||
"Xihe" | ||
], | ||
"conventionalCommits.scopes": [ | ||
"server", | ||
"client", | ||
"others" | ||
], | ||
"ndarray", | ||
"Protobuf", | ||
"thecakelab", | ||
"Xihe" | ||
], | ||
"conventionalCommits.scopes": ["server", "client", "examples", "others"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# ARFlow Server Examples | ||
|
||
The simplest example is [`minimal`](minimal/minimal.py). You may want to start there! | ||
|
||
## Setup | ||
|
||
If you're using `pip`, you should create and activate a virtual environment before installing any example's dependencies: | ||
|
||
```sh | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
``` | ||
|
||
If you're using `poetry` instead, you can just install the dependencies directly, as shown below. | ||
|
||
## Installing the example | ||
|
||
Each example is packaged as a regular Python package, with a `pyproject.toml` file specifying its required dependencies. To run an example, it must first be installed. | ||
|
||
For example, to install dependencies and run the toy `minimal` example (which doesn't need to download any data) run: | ||
|
||
```sh | ||
# Using pip: | ||
pip install -e python/examples/minimal | ||
|
||
# Using poetry: | ||
cd python/examples/minimal | ||
poetry install | ||
``` | ||
|
||
**Note**: it is import to install example in editable mode, which is done using the `-e` flag (short for `--editable`). | ||
|
||
Once installed, the example can be run as a regular Python module: | ||
|
||
```shell | ||
python3 -m minimal | ||
|
||
# or, if you're using poetry: | ||
poetry run minimal | ||
``` | ||
|
||
Examples also declare console script, so they can also be run directly: | ||
|
||
```shell | ||
minimal | ||
``` | ||
|
||
## Contributions welcome | ||
Feel free to open a PR to add a new example! | ||
|
||
See the [`CONTRIBUTING.md`](https://github.com/cake-lab/ARFlow/blob/main/CONTRIBUTING.md) file for details on how to contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
.. include:: ./README.md | ||
""" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env python3 | ||
"""Demonstrates the usage of ARFlow with Depth Anything v2.""" | ||
|
||
from __future__ import annotations | ||
|
||
import sys | ||
from threading import Thread | ||
from typing import Any, Dict | ||
|
||
import numpy as np | ||
import torch | ||
from PIL import Image | ||
from transformers import pipeline | ||
|
||
import arflow | ||
|
||
|
||
class DepthAnythingV2Service(arflow.ARFlowService): | ||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__() | ||
self.device = "cuda" if torch.cuda.is_available() else "cpu" | ||
self.pipe = pipeline( | ||
"depth-estimation", | ||
model="depth-anything/Depth-Anything-V2-base-hf", | ||
device=self.device, | ||
) | ||
|
||
def on_register(self, request: arflow.RegisterRequest): | ||
self.num_frame = 0 | ||
|
||
def on_frame_received(self, frame_data: Dict[str, Any]): | ||
color_rgb = frame_data["color_rgb"] | ||
if self.num_frame % 50 == 0: | ||
thread = Thread(target=lambda: (self.run_depth_estimation(color_rgb.copy())) ) | ||
thread.start() | ||
|
||
self.num_frame = self.num_frame + 1 | ||
|
||
def run_depth_estimation(self, color_rgb: np.ndarray): | ||
"""Run depth estimation on the given image. The pipeline returns a dictionary with two entries. | ||
The first one, called predicted_depth, is a tensor with the values being the depth expressed in | ||
meters for each pixel. The second one, depth, is a PIL image that visualizes the depth estimation result.""" | ||
|
||
image = Image.fromarray(np.flipud(color_rgb)) | ||
|
||
predictions = self.pipe(image) | ||
self.record_predictions(predictions) | ||
return predictions | ||
|
||
def record_predictions(self, predictions: dict): | ||
self.recorder.log( | ||
"DepthAnythingV2/depth", self.recorder.Image(predictions["depth"]) | ||
) | ||
|
||
|
||
def main() -> None: | ||
# sanity-check since all other example scripts take arguments: | ||
assert len(sys.argv) == 1, f"{sys.argv[0]} does not take any arguments" | ||
arflow.create_server(DepthAnythingV2Service, port=8500, path_to_save=None) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.