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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
- from ultralytics import YOLO
- #import streamlit as st
- #import cv2
- #from pytube import YouTube
- import settings
- import os
- import io
- from settings import *
- def load_model(model_path):
- # Chargement du modele
- model = YOLO(model_path)
- return model
- def _display_detected_frames(conf, model, st_frame, image, is_display_tracking=None, tracker=None , ıou=0.5 ):
- """
- Affichage de l'objet détecté dans la vidéo
- """
- # 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 ,iou=ıou )
- 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()
- st_frame.image(res_plotted,
- caption='Detected Video',
- channels="BGR",
- use_column_width=True
- )
- def play_youtube_video(conf, model):
- """
- Affichage d'objet détecté dans les vidéos youtube
- """
- source_youtube = st.sidebar.text_input("YouTube Video url")
- #is_display_tracker, tracker ,ıou = display_tracker_options()
- if st.sidebar.button('Detect Objects'):
- try:
- yt = YouTube(source_youtube)
- stream = yt.streams.filter(file_extension="mp4", res=720).first()
- vid_cap = cv2.VideoCapture(stream.url)
- st_frame = st.empty()
- while (vid_cap.isOpened()):
- success, image = vid_cap.read()
- if success:
- _display_detected_frames(conf,
- model,
- st_frame,
- image
- )
- else:
- vid_cap.release()
- break
- except Exception as e:
- st.sidebar.error("Error loading video: " + str(e))
- def play_webcam(conf, model):
- """
- Affichage d'objet détecté depuis une webcam
- """
- source_webcam = settings.WEBCAM_PATH
- #is_display_tracker, tracker ,ıou = display_tracker_options()
- if st.sidebar.button('Detect Objects'):
- try:
- vid_cap = cv2.VideoCapture(source_webcam)
- 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,
- #ıou
- )
- else:
- vid_cap.release()
- break
- except Exception as e:
- st.sidebar.error("Error loading video: " + str(e))
- def play_stored_video(conf, model):
- """
- Affichage d'objet depuis une simple vidéo uploadée
- """
- uploaded_files = st.file_uploader("Upload video files", key="video_uploader", type=["mp4", "avi", "mov"],
- accept_multiple_files=True)
- try:
- if uploaded_files is not None:
- for uploaded_file in uploaded_files:
- video_name = uploaded_file.name
- if video_name is not None:
- video_path = find_video_path_by_name(str(video_name), uploaded_files)
- except:
- st.warning("Please upload a video file available on your computer for inspection or tracking.")
- #is_display_tracker, tracker , ıou = display_tracker_options()
- if st.sidebar.button('Detect Video Objects'):
- try:
- vid_cap = cv2.VideoCapture(video_path)
- 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 ,
- #ıou
- )
- else:
- vid_cap.release()
- break
- except Exception as e:
- st.sidebar.error("Error loading video: " + str(e))
- def show_model_not_loaded_warning(model):
- """
- Affichage de message quand il est impossible de charger le model
- """
- if model is None:
- st.warning("The model has not been loaded. Please upload a valid model weight file.")
- def find_video_path_by_name(video_name , uploaded_files):
- """
- Retrouver le chemin des vidéos
- """
- if uploaded_files is not None:
- for uploaded_file in uploaded_files:
- if uploaded_file.name == video_name:
- # If the uploaded file matches the desired name, save it to a temporary location
- temp_location = save_uploaded_file(uploaded_file)
- if temp_location is not None:
- return temp_location
- # If the desired video name was not found among the uploaded files
- return None
- def save_uploaded_file(uploaded_file):
- try:
- temp_dir = io.BytesIO()
- temp_location = os.path.join(os.path.expanduser("detections/"), "Videos", uploaded_file.name)
- with open(temp_location, 'wb') as out:
- out.write(uploaded_file.read())
- return temp_location
- except Exception as e:
- st.error(f"Error saving uploaded file: {e}")
- return None
|