From f29c46236d7537c8878954ddb4b4b20dd2f3a440 Mon Sep 17 00:00:00 2001 From: fe51 <55736935+fe51@users.noreply.github.com> Date: Fri, 8 Aug 2025 19:48:31 +0200 Subject: [PATCH 1/2] Added notebook to send detection continuously --- .../notebooks/app/continuous_alert.ipynb | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 containers/notebooks/app/continuous_alert.ipynb diff --git a/containers/notebooks/app/continuous_alert.ipynb b/containers/notebooks/app/continuous_alert.ipynb new file mode 100644 index 0000000..63e0dfd --- /dev/null +++ b/containers/notebooks/app/continuous_alert.ipynb @@ -0,0 +1,158 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Notebook setup\n", + "Run the following cells to initiate notebook" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "from dotenv import load_dotenv\n", + "import os\n", + "from api import get_token, get_camera_token\n", + "from pyroclient import Client\n", + "import glob\n", + "from PIL import Image\n", + "import numpy as np\n", + "import io\n", + "import requests\n", + "import random\n", + "import shutil\n", + "from utils import read_pred_file\n", + "import time" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "API_URL = \"http://api:5050\"\n", + "load_dotenv(\"../.env\")\n", + "SUPERADMIN_LOGIN = os.environ.get(\"SUPERADMIN_LOGIN\")\n", + "SUPERADMIN_PWD = os.environ.get(\"SUPERADMIN_PWD\")\n", + "\n", + "# Get access token\n", + "admin_access_token = get_token(API_URL, SUPERADMIN_LOGIN, SUPERADMIN_PWD)\n", + "\n", + "# Download some relevant images and predictions\n", + "if not os.path.isdir(\"selection-true-positives\"):\n", + " print(\"Images not found, dowloading ...\")\n", + " url = \"https://github.com/pyronear/pyro-envdev/releases/download/v0.0.1/selection-true-positives.zip\"\n", + " output_path = \"selection-true-positives.zip\"\n", + "\n", + " response = requests.get(url, stream=True)\n", + " response.raise_for_status() # Raises an error for bad status codes\n", + "\n", + " with open(output_path, 'wb') as f:\n", + " for chunk in response.iter_content(chunk_size=8192):\n", + " f.write(chunk)\n", + "\n", + " zip_path = \"selection-true-positives.zip\"\n", + " extract_dir = \"selection-true-positives\" # Current directory\n", + "\n", + " shutil.unpack_archive(zip_path, extract_dir, 'zip')\n", + "\n", + " print(\"Extraction completed.\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Send detection continuoulsy\n", + "For a given CAMERA_ID, the following cell will continuously send 1 detection periodicaly (each IMG_FREQ_SECONDS) \n", + "\n", + "To stop the cell from running you can click on the \"stop\" button in the ribbon above the notebook.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "detection sent\n", + "detection sent\n", + "detection sent\n", + "detection sent\n", + "detection sent\n", + "detection sent\n", + "detection sent\n" + ] + } + ], + "source": [ + "CAMERA_ID = 1\n", + "SEQUENCE_CENTER_AZIMUTH = 180\n", + "IMG_FREQ_SECONDS = 30\n", + "\n", + "\n", + "camera_token = get_camera_token(API_URL, CAMERA_ID, admin_access_token)\n", + "camera_client = Client(camera_token, API_URL)\n", + "\n", + "seq_folder = \"selection-true-positives/2025-06-13T14-50-34_mostazal--2-180_sequence-1195\"\n", + "\n", + "imgs = glob.glob(f\"{seq_folder}/images/*\")\n", + "imgs.sort()\n", + "preds = glob.glob(f\"{seq_folder}/labels_predictions/*\")\n", + "preds.sort()\n", + "\n", + "while True:\n", + " for img_file, pred_file in zip(imgs, preds):\n", + " stream = io.BytesIO()\n", + " im = Image.open(img_file)\n", + " im.save(stream, format=\"JPEG\", quality=80)\n", + "\n", + " bboxes = read_pred_file(pred_file)\n", + "\n", + " response = camera_client.create_detection(stream.getvalue(), SEQUENCE_CENTER_AZIMUTH, bboxes)\n", + " # Force a KeyError if the request failed\n", + " response.json()[\"id\"]\n", + " print(\"detection sent\")\n", + " time.sleep(IMG_FREQ_SECONDS)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.20" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} From 901b6a837bcddcb8f75c984232b2d56e05b62efb Mon Sep 17 00:00:00 2001 From: fe51 <55736935+fe51@users.noreply.github.com> Date: Fri, 8 Aug 2025 19:52:06 +0200 Subject: [PATCH 2/2] typo --- .../notebooks/app/continuous_alert.ipynb | 29 +++---------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/containers/notebooks/app/continuous_alert.ipynb b/containers/notebooks/app/continuous_alert.ipynb index 63e0dfd..9802837 100644 --- a/containers/notebooks/app/continuous_alert.ipynb +++ b/containers/notebooks/app/continuous_alert.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -70,7 +70,7 @@ "metadata": {}, "source": [ "# Send detection continuoulsy\n", - "For a given CAMERA_ID, the following cell will continuously send 1 detection periodicaly (each IMG_FREQ_SECONDS) \n", + "For a given CAMERA_ID, the following cell will continuously send 1 detection periodically (each IMG_FREQ_SECONDS) \n", "\n", "To stop the cell from running you can click on the \"stop\" button in the ribbon above the notebook.\n" ] @@ -79,21 +79,7 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "detection sent\n", - "detection sent\n", - "detection sent\n", - "detection sent\n", - "detection sent\n", - "detection sent\n", - "detection sent\n" - ] - } - ], + "outputs": [], "source": [ "CAMERA_ID = 1\n", "SEQUENCE_CENTER_AZIMUTH = 180\n", @@ -125,13 +111,6 @@ " time.sleep(IMG_FREQ_SECONDS)\n", " " ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {