Spaces:
Runtime error
Runtime error
Commit ·
0a5d922
0
Parent(s):
fix: gradio example link instead of file
Browse files- README.md +8 -0
- app.py +65 -0
- backend_client.py +39 -0
README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Tracking System Demo
|
| 3 |
+
sdk: gradio
|
| 4 |
+
app_port: 7860
|
| 5 |
+
pinned: false
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
Testing Tracking System on examples images.
|
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import asyncio
|
| 3 |
+
|
| 4 |
+
from backend_client import check_backend_health, send_frame_to_websocket
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def wrapper(image, camera_id):
|
| 8 |
+
return asyncio.run(send_frame_to_websocket(image, camera_id))
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
with gr.Blocks() as demo:
|
| 12 |
+
# On load - Refresh every 60 Seconds
|
| 13 |
+
status_indicator = gr.Markdown("Checking Server Status")
|
| 14 |
+
demo.load(fn=check_backend_health, outputs=status_indicator)
|
| 15 |
+
gr.Timer(60).tick(
|
| 16 |
+
fn=check_backend_health, outputs=status_indicator
|
| 17 |
+
) # Also check backend connectivity every 60 seconds
|
| 18 |
+
|
| 19 |
+
gr.Markdown(
|
| 20 |
+
"""
|
| 21 |
+
# Tracking System demo
|
| 22 |
+
A fast way to test the system just by uploading an image, Please make sure you check the guide to be able to use it.
|
| 23 |
+
"""
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
with gr.Accordion("How to use this system", open=False):
|
| 27 |
+
gr.Markdown(
|
| 28 |
+
"""
|
| 29 |
+
1. Make sure first backend is opened [HF Backend](https://huggingface.co/spaces/e1250/tracking_system_backend).
|
| 30 |
+
* If it is inactive, activate it or reach out to me.
|
| 31 |
+
* There is an indicator at the top of the page showing if the server is online or offline
|
| 32 |
+
2. Open Vercel Dashboard and make sure you create a line and place a camera with an id. [Dashboard Vercel](https://p-tracking-system.vercel.app/)
|
| 33 |
+
3. Open Gradio, Make sure you add the same `camera_id`, upload an image, then click send
|
| 34 |
+
4. Go back into the dashboard, Give it couple of seconds (Due to HF env limitation - CPU). then see the updates
|
| 35 |
+
5. Feel free also to take a look on Prometheus - [Prometheus Metrics](https://e1250-tracking-system-backend.hf.space/metrics/)
|
| 36 |
+
---
|
| 37 |
+
- [Github Repo](https://github.com/E1250/p-tracking_system)
|
| 38 |
+
"""
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
with gr.Column():
|
| 43 |
+
camera_id = gr.Textbox(
|
| 44 |
+
label="Camera ID", placeholder="Enter Camera ID", value="from_gradio"
|
| 45 |
+
)
|
| 46 |
+
outputs = gr.Textbox(label="Results", value="None")
|
| 47 |
+
send_btn = gr.Button("Send")
|
| 48 |
+
|
| 49 |
+
image_input = gr.Image(type="filepath", label="Upload Image")
|
| 50 |
+
|
| 51 |
+
gr.Examples(
|
| 52 |
+
examples=[
|
| 53 |
+
# Each list is just one example
|
| 54 |
+
# TODO: Add values later
|
| 55 |
+
[
|
| 56 |
+
"https://www.tdi.texas.gov/tips/artwork/protecting-family-from-fire-danger-on-page.jpg"
|
| 57 |
+
]
|
| 58 |
+
],
|
| 59 |
+
inputs=[image_input, camera_id],
|
| 60 |
+
cache_examples=False,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
send_btn.click(fn=wrapper, inputs=[image_input, camera_id], outputs=outputs)
|
| 64 |
+
|
| 65 |
+
demo.launch(share=True)
|
backend_client.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import websockets
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def check_backend_health():
|
| 7 |
+
"""Checking if backend is live.."""
|
| 8 |
+
health_url = "https://e1250-tracking-system-backend.hf.space/health/live"
|
| 9 |
+
try:
|
| 10 |
+
response = requests.get(health_url, timeout=5)
|
| 11 |
+
if response.status_code == 200:
|
| 12 |
+
return '<b style="color: green"> Backend Server Online </b>'
|
| 13 |
+
else:
|
| 14 |
+
return '<b style="color: red"> Backend Server Offline </b>'
|
| 15 |
+
except Exception:
|
| 16 |
+
return gr.Warning("Backend Server Error, {e}")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
async def send_frame_to_websocket(image_path: str | None, camera_id: str | None):
|
| 20 |
+
if image_path is None:
|
| 21 |
+
raise gr.Error("No Image provided")
|
| 22 |
+
if camera_id == "":
|
| 23 |
+
raise gr.Error("Camera ID is requried")
|
| 24 |
+
|
| 25 |
+
with open(image_path, "rb") as f:
|
| 26 |
+
image_bytes = f.read()
|
| 27 |
+
|
| 28 |
+
# uri = f"ws://127.0.0.1:8000/detectors/stream/{camera_id}"
|
| 29 |
+
uri = f"wss://e1250-tracking-system-backend.hf.space/detectors/stream/{camera_id}"
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
async with websockets.connect(uri) as websocket:
|
| 33 |
+
await websocket.send(image_bytes)
|
| 34 |
+
gr.Info("Great, now go to the Dashboard to check the updates")
|
| 35 |
+
return f"Frame was sent to {camera_id}.."
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
gr.Error("Error while connecting and sending the frame..")
|
| 39 |
+
return f"Error: {e}"
|