File size: 2,569 Bytes
f8ce4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from ultralytics import YOLO
import streamlit as st
import cv2
#import pafy
import requests
import settings
import numpy as np
from test_api.algorithm import *

def load_model(model_path):

    model = YOLO(model_path)
    return model

    
def display_tracker_options():
    col1, col2 = st.columns(2)
    with col1:
        display_tracker = st.radio("显示追踪", ('Yes', 'No'))
        is_display_tracker = True if display_tracker == 'Yes' else False
        if is_display_tracker:
            with col2:
                tracker_type = st.radio("追踪器", ("bytetrack.yaml", "botsort.yaml"))
                return is_display_tracker, tracker_type
    return is_display_tracker, None



def _display_detected_frames(conf, model, st_frame, image, is_display_tracking=None, tracker=None):


    # Resize the image to a standard size
    image = cv2.resize(image, (720, int(720*(9/16))))

    # Display object tracking, if specified
    if is_display_tracking:
        res = model.track(image, conf=conf, persist=True, tracker=tracker)
    else:
        # Predict the objects in the image using the YOLOv8 model
        res = model.predict(image, conf=conf)

    # # Plot the detected objects on the video frame
    res_plotted = res[0].plot()
    try:
       st_frame.image(res_plotted,
                   caption='实时检测',
                   channels="BGR",
                   use_column_width=True
                   )
    except requests.exceptions.RequestException as e:
        st.write("Unable to get image, using placeholder")
        st.image("placeholder.png")
        

def play_rtsp_stream(conf, model):
    source_rtsp = st.sidebar.text_input("rtsp stream url")
    is_display_tracker, tracker = display_tracker_options()
    if st.sidebar.button('检测目标'):
        try:
            vid_cap = cv2.VideoCapture(source_rtsp)
            st_frame = st.empty()
            while (vid_cap.isOpened()):
                success, image = vid_cap.read()
                if success:
                    _display_detected_frames(conf,
                                             model,
                                             st_frame,
                                             image,
                                             is_display_tracker,
                                             tracker
                                             )
                else:
                    vid_cap.release()
                    break
        except Exception as e:
            st.sidebar.error("Error loading RTSP stream: " + str(e))