Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions containers/notebooks/app/continuous_alert.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Notebook setup\n",
"Run the following cells to initiate notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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 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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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",
" "
]
}
],
"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
}