Skip to content

Commit

Permalink
new roboflow python package
Browse files Browse the repository at this point in the history
  • Loading branch information
SamratSahoo committed Aug 6, 2021
1 parent 960caee commit 12dbb8a
Show file tree
Hide file tree
Showing 33 changed files with 849 additions and 172 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
test.py

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/roboflow-python.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 1 addition & 79 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,79 +1 @@
# Roboflow Python Library

This is a helper library to load your [Roboflow](https://roboflow.ai) datasets
into your python scripts and Jupyter notebooks.

## Requirements

This package requires python >=3.6 and a (free)
[Roboflow](https://roboflow.ai) account.

## Installing

#### With PIP
```sh
pip install roboflow
```

#### With Anaconda
```sh
conda install roboflow
```

## Setup

The `roboflow` package works in conjunction with your
[Roboflow](https://roboflow.ai) account.

From your `Account` page, click `Roboflow Keys` to get your API key.
You can then use the `roboflow` python package to manage downloading
your datasets in various formats.

```python
import roboflow
roboflow.auth("<<YOUR API KEY>>")
info = roboflow.load("chess-sample", 1, "tfrecord")

# dataset is now downloaded and unzipped in your current directory
# and info contains the paths you need to load it into your favorite
# machine learning libraries
```

By default the folder is named
```
${dataset-name}.${version-number}-${version-name}.${format}
```
(For example, `Chess Sample.v1-small-gray.coco`).

The file hierarchy is three folders containing the `train`, `valid`, and `test`
data you selected in the Roboflow upload flow (and the format you specified
in `roboflow.load` above). There is also a `README.roboflow.txt` describing the preprocessing and augmentation steps and, optionally, a `README.dataset.txt`
provided by the person who shared the dataset.

![Example file layout](img/file-layout.png)

## Doing Inference
It's important to pre-process your images for inference the same way you
pre-processed your training images. For this, get a pre-processor via the
`roboflow.infer` method which will return a function you can use to pre-process
your images.

```python
import roboflow
roboflow.auth("<<YOUR API KEY>>")
process = roboflow.preprocessor("chess-sample", 1)
images = process.fromFile("example.jpg") # returns a numpy array (of 1 image, unless you used tiling)
```

## Benefits

This package currently provides two main benefits over downloading and loading
your datasets manually.

1. If you have previously loaded your dataset, it will automatically use the local copy rather than re-downloading.
2. You can dynamically choose the export format at runtime rather than export-time.

## Roadmap

We plan to include more features in the future to allow you (for example,
to let you easily do inference on your trained models).
# Roboflow Python Library
1 change: 0 additions & 1 deletion build.sh

This file was deleted.

Binary file removed img/file-layout.png
Binary file not shown.
2 changes: 1 addition & 1 deletion meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ requirements:
about:
license: Apache
license_file: LICENSE
summary: Loader for Roboflow datasets.
summary: The official Roboflow Python Package
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build-system]
requires = [
"setuptools>=57",
"wheel"
]
build-backend = "setuptools.build_meta"
15 changes: 15 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
certifi==2021.5.30
chardet==4.0.0
cycler==0.10.0
idna==2.10
kiwisolver==1.3.1
matplotlib==3.4.2
numpy==1.21.0
opencv-python==4.5.3.56
Pillow==8.3.0
pyparsing==2.4.7
python-dateutil==2.8.1
python-dotenv==0.18.0
requests==2.25.1
six==1.16.0
urllib3==1.26.6
94 changes: 35 additions & 59 deletions roboflow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,53 @@
import json
import time

import requests

API_URL = "https://api.roboflow.ai"
_token = None
_token_expires = 0
from roboflow.core.project import Project

def token():
global _token
return _token
TOKEN = None
API_URL = "http://localhost:5000"
TOKEN_EXPIRES = None
USER_API_KEY = ""

def auth(api_key):
global _token

def auth(api_key):
global TOKEN, TOKEN_EXPIRES
global USER_API_KEY
USER_API_KEY = api_key
response = requests.post(API_URL + "/token", data=({
"api_key": api_key
}))

r = response.json();
if "error" in r:
raise RuntimeError(response.text)

_token = r["token"]
_token_expires = time.time() + r["expires_in"]

return r

def dataset(name):
global _token

if not _token:
raise Exception("You must first auth with your API key to call this method.")

response = requests.get(API_URL + "/dataset/" + name, params=({
"access_token": _token
}))

r = response.json();
if "error" in r:
raise RuntimeError(response.text)

return r

def version(dataset_name, version_id):
global _token

if not _token:
raise Exception("You must first auth with your API key to call this method.")

response = requests.get(API_URL + "/dataset/" + dataset_name + '/' + str(version_id), params=({
"access_token": _token
}))

r = response.json();
if "error" in r:
r = response.json()
if "error" in r or response.status_code != 200:
raise RuntimeError(response.text)

return r
TOKEN = r['token']
TOKEN_EXPIRES = r['expires_in']
return Roboflow(api_key, TOKEN, TOKEN_EXPIRES)

def export(dataset_name, version_id, format):
global _token

if not _token:
raise Exception("You must first auth with your API key to call this method.")
class Roboflow():
def __init__(self, api_key, access_token, token_expires):
self.api_key = api_key
self.access_token = access_token
self.token_expires = token_expires

response = requests.get(API_URL + "/dataset/" + dataset_name + '/' + str(version_id) + '/' + format, params=({
"access_token": _token
}))
def list_workspaces(self):
workspaces = requests.get(API_URL + '/workspaces?access_token=' + self.access_token).json()
print(json.dumps(workspaces, indent=2))
return workspaces

r = response.json();
if "error" in r:
raise RuntimeError(response.text)
def load_workspace(self):
pass

return r
def load(self, dataset_slug):
# TODO: Change endpoint once written
LOAD_ENDPOINT = "ENDPOINT_TO_GET_DATSET_INFO" + dataset_slug
response = requests.get(LOAD_ENDPOINT).json()
return Project(self.api_key, response['dataset_slug'], response['type'], response['exports'])

def load(dataset, *args):
print(f"loading {dataset} {args}")
def __str__(self):
json_value = {'api_key': self.api_key, 'auth_token': self.access_token, 'token_expires': self.token_expires}
return json.dumps(json_value, indent=2)
Empty file added roboflow/archive/__init__.py
Empty file.
Loading

0 comments on commit 12dbb8a

Please sign in to comment.