Python Package
Navigating the pip package
Most Roboflow users will interact with the platform through their web UI, but often times, workflows require Python integrations. The gold-standard for python packages is pip provided by PyPi.
The python package, over time, will be able to handle all actions you can perform on the web UI programmatically; although, for now a select number of operations are able to be performed. Please request any functionality you might need on the roboflow-python repository.

Installation

You can either install Roboflow through the PyPi package:
1
pip install roboflow
Copied!
or manually through the GitHub:
1
git clone https://github.com/roboflow-ai/roboflow-python.git
2
cd roboflow-python
3
python3 -m venv
4
source venv/bin/activate
5
pip3 install -r requirements.txt
Copied!

Overview

The Python Package allows you to:
  1. 1.
    List information about workspaces, projects, and versions.
  2. 2.
    Upload images to your projects.
  3. 3.
    Perform inference with specific model versions.
  4. 4.
    Visualize, and save predictions that you've made on the model.

Quick start sample code direct from your project

Looking for your projectID on the app.roboflow.com website or want a jumpstart with some code scaffolding? A pre-built code snippet can be found within your workspace using the following steps:
  1. 1.
    select the export button on the generated dataset you need to work with
2. Select show download code
3. Copy-paste the code snippet into your code editor of choice. Note: this can also be an effective method to find your projectID.

Methods

Methods follow the follow cascading pattern:
  • Roboflow()
  • Roboflow().workspace()
  • Roboflow().workspace().project()

Roboflow()

Everything in the package works through the Roboflow() object. You must pass your private API key to the object, which can be found under the settings of your workspace. Full instructions for obtaining your API Key can be found here.
1
from roboflow import Roboflow
2
3
rf = Roboflow(api_key="YOUR_PRIVATE_API_KEY")
Copied!

workspace()

Workspace details such as name, URL, and a list of projects can be found the workspace() method:
1
workspace = rf.workspace()
2
3
# name
4
workspace.name
5
6
# URL
7
workspace.url
8
9
# Projects
10
workspace.projects()
Copied!

project()

Workspace objects can provide reference to a specific project using the project(projectID) method. See step 3 of Quick start sample code direct from your project for an easy way to find the projectID.
1
project = workspace.project("YOUR_PROJECT_ID")
Copied!
You can upload a local image to this project by doing:
1
project.upload("UPLOAD_IMAGE.jpg")
2
3
# if you want to attempt reuploading image on failure
4
project.upload("UPLOAD_IMAGE.jpg"), num_retry_uploads=3)
Copied!

version()

Next, if you want to access the versions of a specific project:
1
all_versions = project.versions() # or
2
version = project.version(1)
Copied!
If you've trained a model on this version, you can perform inference on the model with either a local or hosted image:
1
model = version.model
2
prediction = model.predict("YOUR_IMAGE.jpg") # or
3
prediction_hosted = model.predict("https://www.yourimageurl.com", hosted=True)
Copied!
You can plot and save a visualization of the prediction.
1
prediction.plot()
2
prediction.save(output_path="predictions.jpg")
Copied!
Or access your model's prediction as JSON.
1
prediction.json()
Copied!

To Be Continued

Just like the Roboflow REST API, our vision for the Roboflow Python pip package is to eventually reach feature parity with our web UI so you can automate and integrate any part of your Roboflow workflow into your own codebase.
Last modified 1mo ago