|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "83db9682-cfc4-4cd0-889f-c8747c4033b3", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Inference Pipeline\n", |
| 9 | + "\n", |
| 10 | + "Inference Pipelines are a great way to process video streams with Inference. You can configure different sources that include streams from local devices, RTSP streams, and local video files. You can also configure different sinks that include UDP streaming of results, render of results, and custom callbacks to run your own logic after each new set of predictions is available. " |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "4ec4136f-53e9-4c8c-9217-a2c533d498ae", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "### Roboflow API Key\n", |
| 19 | + "\n", |
| 20 | + "To load models with `inference`, you'll need a Roboflow API Key. Find instructions for retrieving your API key [here](https://docs.roboflow.com/api-reference/authentication). The utility function below attempts to load your Roboflow API key from your enviornment. If it isn't found, it then prompts you to input it. To avoid needing to input your API key for each example, you can configure your Roboflow API key in your environment via the variable `ROBOFLOW_API_KEY`." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "id": "af3aad40-d41b-4bc1-ade8-dac052951257", |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "from utils import get_roboflow_api_key\n", |
| 31 | + "\n", |
| 32 | + "api_key = get_roboflow_api_key()" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "id": "86f3f805-f628-4e94-91ac-3b2f44bebdc0", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "### Inference Pipeline Example\n", |
| 41 | + "\n", |
| 42 | + "In this example we create a new InferencePipeline. We pass the model ID, the video reference, and a method to render our results. Out pipeline does the rest!" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "id": "58dd049c-dcc6-4d0b-85ad-e6d1c0ba805b", |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "from functools import partial\n", |
| 53 | + "\n", |
| 54 | + "import numpy as np\n", |
| 55 | + "from matplotlib import pyplot as plt\n", |
| 56 | + "from IPython import display\n", |
| 57 | + "\n", |
| 58 | + "from inference.core.interfaces.stream.inference_pipeline import InferencePipeline\n", |
| 59 | + "from inference.core.interfaces.stream.sinks import render_boxes\n", |
| 60 | + "\n", |
| 61 | + "# Define source video\n", |
| 62 | + "video_url = \"https://storage.googleapis.com/com-roboflow-marketing/football-video.mp4\"\n", |
| 63 | + "\n", |
| 64 | + "# Prepare to plot results\n", |
| 65 | + "\n", |
| 66 | + "fig, ax = plt.subplots()\n", |
| 67 | + "frame_placeholder = np.zeros((480, 640, 3), dtype=np.uint8) # Adjust the dimensions to match your frame size\n", |
| 68 | + "image_display = ax.imshow(frame_placeholder)\n", |
| 69 | + "\n", |
| 70 | + "# Define our plotting function\n", |
| 71 | + "def update_plot(new_frame):\n", |
| 72 | + " # Update the image displayed\n", |
| 73 | + " image_display.set_data(new_frame)\n", |
| 74 | + " # Redraw the canvas immediately\n", |
| 75 | + " display.display(plt.gcf())\n", |
| 76 | + " display.clear_output(wait=True)\n", |
| 77 | + "\n", |
| 78 | + "# Define our pipeline's sink\n", |
| 79 | + "render = partial(render_boxes, on_frame_rendered=update_plot)\n", |
| 80 | + "\n", |
| 81 | + "# Instantiate the pipeline\n", |
| 82 | + "pipeline = InferencePipeline.init(\n", |
| 83 | + " model_id=\"soccer-players-5fuqs/1\",\n", |
| 84 | + " video_reference=video_url,\n", |
| 85 | + " on_prediction=render,\n", |
| 86 | + " api_key=api_key,\n", |
| 87 | + ")\n", |
| 88 | + "\n", |
| 89 | + "# Start the pipeline\n", |
| 90 | + "pipeline.start()\n", |
| 91 | + "pipeline.join()" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "id": "07762936-ff33-46c0-a4a2-0a8e729053d1", |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [] |
| 101 | + } |
| 102 | + ], |
| 103 | + "metadata": { |
| 104 | + "kernelspec": { |
| 105 | + "display_name": "Python 3 (ipykernel)", |
| 106 | + "language": "python", |
| 107 | + "name": "python3" |
| 108 | + }, |
| 109 | + "language_info": { |
| 110 | + "codemirror_mode": { |
| 111 | + "name": "ipython", |
| 112 | + "version": 3 |
| 113 | + }, |
| 114 | + "file_extension": ".py", |
| 115 | + "mimetype": "text/x-python", |
| 116 | + "name": "python", |
| 117 | + "nbconvert_exporter": "python", |
| 118 | + "pygments_lexer": "ipython3", |
| 119 | + "version": "3.9.18" |
| 120 | + } |
| 121 | + }, |
| 122 | + "nbformat": 4, |
| 123 | + "nbformat_minor": 5 |
| 124 | +} |
0 commit comments