Skip to main content

No project description provided

Project description

RFCloud Python SDK

Also See RFCloud Website

Also See Documentation for RFCloud

Prerequisite

Installing

Using pip:

$ pip install rfc-sdk

Using poetry:

$ poetry add rfc-sdk

To use:

from rfc_sdk import Account
from rfc_sdk import Flow

Account-Level Operations

Start Using Your Account in the SDK

Pass your RFCloud user key

Obtain your user key from the RFCloud Dashboard https://portal.randomforest.cloud/account

account = Account(userKey)

Get All Flows in the Account

for flow in account.getFlows():
    print(flow)
    pass

Get A Flow in the Account

This is a syntactic sugar for Flow(flowId, userKey=userKey)

Note: This does not verify your access to the Flow

flow2 = account.getFlow("00000000-0000-0000-0000-000000000000")

Create a Flow in the Account

flow3 = account.createFlow(
    f"New Stream At {datetime.now().isoformat()} (SDK)",
    "PUSH",
    # pullUrl is not applicable for PUSH mode
)
flow4 = account.createFlow(
    f"New Stream At {datetime.now().isoformat()} (SDK)",
    "PULL",
    "rtmp://example.com/live1")

Flow-Level Operations

Start Using Your Account in the SDK

Pass your RFCloud flow key

Obtain your flow key from the RFCloud Dashboard https://portal.randomforest.cloud/ > Select a flow

flow1 = Flow(flowId, flowKey=flowKey)
flow2 = Flow(flowId, userKey=userKey)

Also see Get A Flow in the Account

Get the Stream URLs

If this is a push flow, check the containing pushUrls e.g.

[
  "rtmp://stream.randomforest.cloud/FLOW-...",
  "rtsp://stream.randomforest.cloud/FLOW-...",
]

If this is a pull flow, check the containing pullUrl e.g.

"rtmp://camera.example.com/live1"
streamUrls = flow1.getStreamUrls()
streamUrls['pushUrls']
streamUrls['pullUrl']

One-Shot Call to Get the Output

Note: Your flow should be inferencing

batchResult = flow1.getOutput(engine='object_detection',
                              classes=['person'],
                              withFrames=True)

for singleResult in batchResult:
    print(singleResult)

    # base64-encoded JPEG, e.g. /9j/4AAQSkZJRgABAQAAA... | None
    singleResult.get("frame")
    if (singleResult.get("frame") is not None):
        decodedFrame = base64.b64decode(singleResult['frame'])
        # image_2024-01-23T080123.456000Z.jpg
        with open(f"image_{datetime.fromtimestamp(singleResult['inferenceTimestamp']/1000).strftime('%Y-%m-%dT%H:%M:%S.%fZ').replace(":", "")}.jpg", "wb") as f:
            f.write(decodedFrame)
    singleResult.inferenceTimestamp  # string
    for p in singleResult.payload:
        p.trackingId  # number
        p.get("class")  # string
        p.bounds[0]  # x, number
        p.bounds[1]  # y, number
        p.bounds[2]  # w, number
        p.bounds[3]  # h, number
        p.centroid[0]  # x, number
        p.centroid[1]  # y, number

Accepted parameters:

  • engine
    • Engine to use. Only accepted value: "object_detection"
  • classes
  • withFrames
    • Whether you want to get the latest video frame if available. Video frame is not guaranteed even if you pass true

Returns: See above code block

Subscribe to the Flow Inference Output

Note: You should always unsubscribe when you're done in order to avoid unintentional costs

def callbackBatchResult(batchResult):
    # This callback would repeatedly be called until flowUnsubscribe(), or flow.stop() is called
    if (len(batchResult) == 0):
        # No result
        return
    for singleResult in batchResult:
        print(singleResult)


def callbackSingleResult(singleResult):
    # This callback would repeatedly be called until flowUnsubscribe(), or flow.stop() is called
    if (singleResult is None):
        # No result
        return
    print(singleResult)


# Subscribe to the flow inference output
flowUnsubscribe = flow1.subscribe(
    minimumInterval=1000,
    requestOptions={"engine": "object_detection",
                    "classes": ["person"],
                    "withFrames": True
                    },
    # Provide at least one of callbackBatchResult or callbackSingleResult
    callbackBatchResult=callbackBatchResult,
    callbackSingleResult=callbackSingleResult
)

Accepted parameters:

  • minimumInterval
    • Minimum interval in milliseconds, minimum 1000ms. Calculated with (time taken to get output + time taken for all callbacks to finish)
  • callbackBatchResult
    • Callback function with batch result (empty array if no result). See the output array of flow.getOutput
  • callbackSingleResult
    • Callback function with single result (null if no result). See the output array element of flow.getOutput
  • requestOptions

Sample of single result:

{
  "payload": [
    {
      "trackingId": 11,
      "class": "truck",
      "bounds": [909, 340, 935, 364],
      "centroid": [1376, 522]
    }
  ],
  "inferenceTimestamp": 1714449453312
}
{
  "payload": [
    {
      "trackingId": 11,
      "class": "car",
      "bounds": [372, 392, 409, 421],
      "centroid": [576, 602]
    },
    {
      "trackingId": 22,
      "class": "truck",
      "bounds": [910, 339, 936, 363],
      "centroid": [1378, 520]
    }
  ],
  "inferenceTimestamp": 1714449452614
}
{
    "payload": [
        {
            "trackingId": 11,
            "class": "truck",
            "bounds": [
                912,
                339,
                937,
                363
            ],
            "centroid": [
                1380,
                520
            ]
        }
    ],
    "frame": "/9j/4AAQSkZJRgABAQAAA...",
    "inferenceTimestamp": 1714449452614
}

Provide at least one of callbackBatchResult or callbackSingleResult

Returns: Function to unsubscribe from the flow inference output

Unsubscribe From the Flow Inference Output

flow1.unsubscribe()

Or call the return function of flow.subscribe

flowUnsubscribe()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rfc_sdk-0.0.1a2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rfc_sdk-0.0.1a2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file rfc_sdk-0.0.1a2.tar.gz.

File metadata

  • Download URL: rfc_sdk-0.0.1a2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for rfc_sdk-0.0.1a2.tar.gz
Algorithm Hash digest
SHA256 993cec88603229d816388be29c348a89005c04910bd8d67680260dcf8151cbf5
MD5 2feb6a4904411e1f76e7a827946b8831
BLAKE2b-256 63b51984fe262173ef61d15c4fe1cfc6626255de3233200e2f4a7f2cabdbf51f

See more details on using hashes here.

File details

Details for the file rfc_sdk-0.0.1a2-py3-none-any.whl.

File metadata

  • Download URL: rfc_sdk-0.0.1a2-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for rfc_sdk-0.0.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 49d4fd956cca7f92150d66f6f935f1090ad314603eeeab5b0a011e4978a21ced
MD5 31abbc2e0ba5d615f82f2d96c0134f83
BLAKE2b-256 6e84a5d20cd5a51b830b880faa6cf47a3ed63d040393aa08b2586b443c892d1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page