|
import streamlit as st |
|
import cv2 |
|
import matplotlib.pyplot as plt |
|
from inference import Stream |
|
import supervision as sv |
|
import os |
|
|
|
import os |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
api_key = os.getenv('ROBOFLOW_API_KEY') |
|
|
|
|
|
|
|
|
|
|
|
annotator = sv.BoxAnnotator() |
|
|
|
def main(): |
|
st.title("RTSP Stream with Hugging Face Streamlit") |
|
|
|
|
|
ip_address = '65.108.95.124' |
|
port = '554' |
|
username = '' |
|
password = '' |
|
|
|
|
|
rtsp_url = f"rtsp://{username}:{password}@{ip_address}:{port}/testia/live" |
|
|
|
|
|
stframe = st.empty() |
|
predictions_frame = st.empty() |
|
|
|
with Stream( |
|
source=rtsp_url, |
|
model="jakojdnfin/1", |
|
output_channel_order="BGR", |
|
use_main_thread=True, |
|
on_prediction=lambda predictions, image: update_predictions(predictions, image, predictions_frame) |
|
) as stream: |
|
while True: |
|
image = stream.read() |
|
if image is not None and not image.empty(): |
|
|
|
fig, ax = plt.subplots() |
|
ax.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) |
|
ax.axis('off') |
|
stframe.pyplot(fig) |
|
|
|
def update_predictions(predictions, image, predictions_frame): |
|
|
|
annotated_image = annotator.annotate(scene=image, detections=sv.Detections.from_roboflow(predictions)) |
|
predictions_frame.image(cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB), use_column_width=True) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|