> For the complete documentation index, see [llms.txt](https://docs.eyepop.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.eyepop.ai/sdks/python/inference.md). Agents can also search and read this documentation through its [MCP server](https://docs.eyepop.ai/~gitbook/mcp).

# Running Inference

Process files, streams, URLs, video, and image groups

Every example opens a session with a Pop, submits media, and reads predictions.

This page is the Python call shapes. What a source is, every kind the platform accepts, and the options that shape how one is processed are covered once in [Sources and Options](https://docs.eyepop.ai/platform/sources-and-options.md).

### A single image

```python
from eyepop import EyePopSdk
from eyepop.worker.worker_types import InferenceComponent, Pop

pop = Pop(components=[InferenceComponent(ability="eyepop.person:latest")])

with EyePopSdk.sync_worker(pop=pop) as endpoint:
    result = endpoint.upload("photo.jpg").predict()
    print(result)
```

### Binary streams

```python
with EyePopSdk.sync_worker(pop=pop) as endpoint:
    with open("photo.jpg", "rb") as file:
        result = endpoint.upload_stream(file, "image/jpeg").predict()
```

### Live streams sent as they are captured

A live upload (`upload_stream(..., is_live=True)`) is paced by the pipeline to its own timestamps, so a recorded file sent as live plays at its own speed. A stream you send as it is captured, such as a camera"s live MPEG-TS read from a pipe, is real time already: pass `real_time=True` and the pipeline does not pace it again. Paced, a real-time stream keeps the length of any stall in the sender, a network hiccup or a slow first frame, on its latency for the rest of the stream (one 0.5 s stall left every later frame 0.5 s late).

```python
with EyePopSdk.sync_worker(pop=pop) as endpoint:
    camera = subprocess.Popen(["ffmpeg", "-i", "rtsp://camera/stream", "-c", "copy", "-f", "mpegts", "-"],
                              stdout=subprocess.PIPE)
    job = endpoint.upload_stream(camera.stdout, "video/mp2t", is_live=True, real_time=True)
    while result := job.predict():
        print(result)
```

* `real_time=True` needs `is_live=True`; without it, it raises `ValueError`. `real_time=False` and leaving it unset are fine on any upload.
* Unset, it is off, except for a stream that says it is real time itself with a `real_time` attribute that is `True`: `eyepop.relay.RtspRelayStream` (from `rtsp_relay_stream()`) does, so a relayed camera is marked without asking. Never set it on a recorded file: unpaced, it plays as fast as it uploads.
* Pipeline mode sends it as `"realTime": true` with the live upload"s `setSource`, which needs eyepop-worker 4.31.2 or later. An older pipeline API refuses the member; the SDK then sends the upload again without it, as every later live upload on the endpoint, and logs a warning once. None of the stream is read before the pipeline API takes the source, so the retry loses no bytes.
* A worker session does not send it.

### URLs

`load_from()` hands the platform a URL and lets it fetch, so nothing uploads from your application. [Source Types](https://docs.eyepop.ai/platform/sources-and-options/sources.md) lists every scheme it accepts.

```python
with EyePopSdk.sync_worker(pop=pop) as endpoint:
    result = endpoint.load_from("https://example.com/image.jpg").predict()
```

### Video

A video yields one prediction per frame, so read until `predict()` returns `None`.

```python
with EyePopSdk.sync_worker(pop=pop) as endpoint:
    job = endpoint.load_from("https://example.com/video.mp4")
    while result := job.predict():
        print(result)
```

Cancel a job mid-stream with `job.cancel()`.

### Image groups

A [group](https://docs.eyepop.ai/platform/sources-and-options/sources.md#image-groups) is one source processed together as a single inference unit, returning one prediction for the whole set — unlike batching below, where each image is independent.

```python
with EyePopSdk.sync_worker(pop=pop) as endpoint:
    # local files
    result = endpoint.upload_group(["a.jpg", "b.jpg", "c.jpg"]).predict()

    # in-memory streams
    with open("a.jpg", "rb") as a, open("b.jpg", "rb") as b:
        result = endpoint.upload_stream_group([a, b]).predict()

    # remote URLs
    result = endpoint.load_from_group([
        "https://example.com/a.jpg",
        "https://example.com/b.jpg",
    ]).predict()
```

[Image groups](https://docs.eyepop.ai/platform/sources-and-options/sources.md#image-groups) covers the size limit, the ordering guarantee, and which abilities accept a group.

### Batching

Queue several uploads, then collect the results. Each image is an independent inference.

```python
with EyePopSdk.sync_worker(pop=pop) as endpoint:
    jobs = [endpoint.upload(p) for p in ["photo1.jpg", "photo2.jpg"]]
    for job in jobs:
        print(job.predict())
```

### Async with callbacks

```python
import asyncio
from eyepop import EyePopSdk, Job

async def main(paths):
    async def on_ready(job: Job):
        print(await job.predict())

    async with EyePopSdk.async_worker(pop=pop) as endpoint:
        for p in paths:
            await endpoint.upload(p, on_ready=on_ready)

asyncio.run(main(["photo1.jpg", "photo2.jpg"]))
```

### Visualizing results

```python
from PIL import Image
import matplotlib.pyplot as plt

with EyePopSdk.sync_worker(pop=pop) as endpoint:
    result = endpoint.upload("photo.jpg").predict()

with Image.open("photo.jpg") as image:
    plt.imshow(image)
EyePopSdk.plot(plt.gca()).prediction(result)
plt.show()
```

### Recordings: thumbnails and clips by mediaId

A source with `media_cache_seconds` records to the media cache. Once it has started, its job's `media_id` names the recording: an opaque string you pass back unchanged. Read a frame, a clip or the range still held through the endpoint, in either mode:

```python
job = endpoint.load_from("rtsp://camera.example.com/stream1", media_cache_seconds=3600)
for _ in range(300):  # a live source never ends on its own: take its first 300 results
    if (result := job.predict()) is None:
        break
    for person in result.get("objects", []):
        # the frame the prediction was made on, cropped to the object's box, fit to 160 x 160, JPEG
        thumbnail = job.media_frame(result, person, max_width=160, max_height=160)

start_ns, end_ns = endpoint.media_range(job.media_id)
with endpoint.media_clip(job.media_id, max(start_ns, end_ns - 10_000_000_000), end_ns) as clip:  # the last 10 s, MP4
    with open("clip.mp4", "wb") as file:
        for chunk in clip:
            file.write(chunk)
job.cancel()  # stop the live source
```

| Method                                                                                                        | Returns                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `media_range(media_id)`                                                                                       | `(start_ns, end_ns)`: the first and last nanosecond the cache still holds.                                                                                                                                                                                                                                                                                                                                                |
| `media_frame(media_id, timestamp_ns, *, bbox=None, max_width=None, max_height=None, png=False, wait_ms=None)` | The frame at a prediction's `timestamp` as JPEG bytes, PNG with `png=True`. `bbox` is `(x, y, width, height)` in the frame's pixels, sent as the smallest box of whole pixels that holds it and clipped to the frame; `max_width` and `max_height` fit the answer, aspect kept, never enlarged.                                                                                                                           |
| `media_clip(media_id, start_ns, end_ns, *, mode="exact", wait_ms=None)`                                       | A `MediaClip` that streams an MP4: iterate it for its bytes or `read()` it whole, and close it (`with`) when you stop early. `mode="exact"` re-encodes to the exact bounds (300 s at most by default); `"keyframe"` copies from the keyframe at or before `start_ns`. `start_ns` and `end_ns` are the clip's real bounds; `milli_compute_units` is what the pipeline API priced it at, None when the answer carries none. |
| `job.media_frame(prediction, obj=None, **options)`                                                            | `media_frame` with the job's `media_id`, the prediction's `timestamp` and the object's `x`, `y`, `width` and `height` (source pixels, which are the recording's). `ValueError` when the job has no `media_id`.                                                                                                                                                                                                            |

The async endpoint has the same methods as coroutines (`await endpoint.media_frame(...)`, `async with await endpoint.media_clip(...) as clip`, `async for chunk in clip`). A session's endpoint reads from its instance, which knows only its own session's recordings (`v2.` ids, from the `recording` event that opens a recording source's results); a pipeline endpoint reads from the pipeline API, which knows every recording of the token's account, a session's too once its instance is gone. A frame read right after its prediction waits on the server for the moment to be cached (`wait_ms`, 2000 by default, at most 10000); an answer `425`, or `503 overloaded` from a pipeline API at capacity, is asked again after its `Retry-After` for up to `EYEPOP_MAX_RETRY_TIME_SECS` (30 s), each retry bounded by what is left of it. Any other refusal raises `MediaReadException` with the `status` and the answer's `error`, `reason` (the code to act on, such as `stream_not_found`, `media_expired` or `clip_too_long`) and `message`. The pipeline API prices frame and clip reads in milli compute units, in dry run; a session's reads are part of its session minutes. Needs eyepop-instance with spec 025 on the instance or the pipeline API.

A job also keeps the pipeline's `warning` events on `job.warnings`, in both modes, and logs each at `WARNING` on `eyepop`; one says a recorded camera sends keyframes too rarely (`media_cache_gop_too_long`), which `media_cache_reencode=True` avoids.

### Target resolution

Every upload and load method takes a `target_resolution`, the box a video, or a group's images, is scaled down to fit inside before the Pop runs on it. The worker scales after `fps` and motion detection, keeps the aspect ratio and never scales up, so a frame that fits already keeps its own size. Each side is a whole number from 64 to 16384 pixels:

```python
from eyepop.worker.worker_types import TargetResolution

job = endpoint.load_from("rtsp://camera.example.com/stream1", target_resolution=TargetResolution(width=1280, height=720))
```

A smaller frame costs less to process; a model still sees its own input size, so scale down only as far as the objects you need stay large enough.

### Source outputs

Every upload and load method takes `outputs`, where the source's results go besides your own call, and `events`, the prediction events your own call gets back:

```python
from eyepop.worker.outputs import Output, OutputEvent, WebhookTarget

crm = Output(name="crm", type="webhook",
             webhook=WebhookTarget(url="https://hooks.example.com/eyepop"),
             events=[OutputEvent.PREDICTION_SELECTED, OutputEvent.STREAM_STATUS_CHANGED])
job = endpoint.load_from("rtsp://camera.example.com/stream1", outputs=[crm],
                         events=[OutputEvent.PREDICTION_TRACKED, OutputEvent.PREDICTION_SELECTED])
```

An output without `events` gets `prediction.made` and `stream.status_changed`; `prediction.made` and `prediction.tracked` do not go together. Pipeline mode delivers webhooks only. A worker session on an instance also delivers MQTT and NATS, on url and asset sources; its uploads take no outputs. See the README's "Source outputs" for every member and rule.

### Relayed pipelines

In local mode, `relay=True` makes an on-premise instance run the pop in the cloud: `EyePopSdk.sync_worker(is_local_mode=True, relay=True, pop=pop)`. The instance needs a compute-API login, `pipeline-api-url`, and no fixed pop (an instance booted with a pipeline refuses it). Predictions are version 4, and a caught-up backlog's carry `"backlog": true`.

### Camera calibration

Every upload and load method takes a `camera`, which is what lets a [depth map](https://docs.eyepop.ai/platform/depth-and-world-coordinates/depth-maps.md) become positions in meters:

```python
from eyepop.worker.camera import Camera

job = endpoint.load_from("rtsp://camera.example.com/stream1", camera=Camera(hfovDegrees=72.0))
```

Set it once for every source with `Pop.defaults` instead — see [Composable Pops](https://docs.eyepop.ai/sdks/python/composable-pops.md#world-coordinates).

`EyePopPlot.depth(result)` overlays a frame's depth map as a heatmap, and `EyePopWorldPlot` scatters world coordinates into a 3D axes.

### Next steps

* [Sources and Options](https://docs.eyepop.ai/platform/sources-and-options.md) — every source the platform accepts, and the options that shape processing
* [Composable Pops](https://docs.eyepop.ai/sdks/python/composable-pops.md) — chain models into a pipeline
* [Depth and World Coordinates](https://docs.eyepop.ai/platform/depth-and-world-coordinates.md) — depth maps, calibration, and meters
* [Data Endpoint](https://docs.eyepop.ai/sdks/python/data-endpoint.md) — datasets, VLM inference, and evaluation


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.eyepop.ai/sdks/python/inference.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
