Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

helper.py 5.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. from ultralytics import YOLO
  2. #import streamlit as st
  3. #import cv2
  4. #from pytube import YouTube
  5. import settings
  6. import os
  7. import io
  8. from settings import *
  9. def load_model(model_path):
  10. # Chargement du modele
  11. model = YOLO(model_path)
  12. return model
  13. def _display_detected_frames(conf, model, st_frame, image, is_display_tracking=None, tracker=None , ıou=0.5 ):
  14. """
  15. Affichage de l'objet détecté dans la vidéo
  16. """
  17. # Resize the image to a standard size
  18. image = cv2.resize(image, (720, int(720*(9/16))))
  19. # Display object tracking, if specified
  20. if is_display_tracking:
  21. res = model.track(image, conf=conf, persist=True, tracker=tracker ,iou=ıou )
  22. else:
  23. # Predict the objects in the image using the YOLOv8 model
  24. res = model.predict(image, conf=conf)
  25. # # Plot the detected objects on the video frame
  26. res_plotted = res[0].plot()
  27. st_frame.image(res_plotted,
  28. caption='Detected Video',
  29. channels="BGR",
  30. use_column_width=True
  31. )
  32. def play_youtube_video(conf, model):
  33. """
  34. Affichage d'objet détecté dans les vidéos youtube
  35. """
  36. source_youtube = st.sidebar.text_input("YouTube Video url")
  37. #is_display_tracker, tracker ,ıou = display_tracker_options()
  38. if st.sidebar.button('Detect Objects'):
  39. try:
  40. yt = YouTube(source_youtube)
  41. stream = yt.streams.filter(file_extension="mp4", res=720).first()
  42. vid_cap = cv2.VideoCapture(stream.url)
  43. st_frame = st.empty()
  44. while (vid_cap.isOpened()):
  45. success, image = vid_cap.read()
  46. if success:
  47. _display_detected_frames(conf,
  48. model,
  49. st_frame,
  50. image
  51. )
  52. else:
  53. vid_cap.release()
  54. break
  55. except Exception as e:
  56. st.sidebar.error("Error loading video: " + str(e))
  57. def play_webcam(conf, model):
  58. """
  59. Affichage d'objet détecté depuis une webcam
  60. """
  61. source_webcam = settings.WEBCAM_PATH
  62. #is_display_tracker, tracker ,ıou = display_tracker_options()
  63. if st.sidebar.button('Detect Objects'):
  64. try:
  65. vid_cap = cv2.VideoCapture(source_webcam)
  66. st_frame = st.empty()
  67. while (vid_cap.isOpened()):
  68. success, image = vid_cap.read()
  69. if success:
  70. _display_detected_frames(conf,
  71. model,
  72. st_frame,
  73. image,
  74. #is_display_tracker,
  75. #tracker,
  76. #ıou
  77. )
  78. else:
  79. vid_cap.release()
  80. break
  81. except Exception as e:
  82. st.sidebar.error("Error loading video: " + str(e))
  83. def play_stored_video(conf, model):
  84. """
  85. Affichage d'objet depuis une simple vidéo uploadée
  86. """
  87. uploaded_files = st.file_uploader("Upload video files", key="video_uploader", type=["mp4", "avi", "mov"],
  88. accept_multiple_files=True)
  89. try:
  90. if uploaded_files is not None:
  91. for uploaded_file in uploaded_files:
  92. video_name = uploaded_file.name
  93. if video_name is not None:
  94. video_path = find_video_path_by_name(str(video_name), uploaded_files)
  95. except:
  96. st.warning("Please upload a video file available on your computer for inspection or tracking.")
  97. #is_display_tracker, tracker , ıou = display_tracker_options()
  98. if st.sidebar.button('Detect Video Objects'):
  99. try:
  100. vid_cap = cv2.VideoCapture(video_path)
  101. st_frame = st.empty()
  102. while (vid_cap.isOpened()):
  103. success, image = vid_cap.read()
  104. if success:
  105. _display_detected_frames(conf,
  106. model,
  107. st_frame,
  108. image
  109. #is_display_tracker,
  110. #tracker ,
  111. #ıou
  112. )
  113. else:
  114. vid_cap.release()
  115. break
  116. except Exception as e:
  117. st.sidebar.error("Error loading video: " + str(e))
  118. def show_model_not_loaded_warning(model):
  119. """
  120. Affichage de message quand il est impossible de charger le model
  121. """
  122. if model is None:
  123. st.warning("The model has not been loaded. Please upload a valid model weight file.")
  124. def find_video_path_by_name(video_name , uploaded_files):
  125. """
  126. Retrouver le chemin des vidéos
  127. """
  128. if uploaded_files is not None:
  129. for uploaded_file in uploaded_files:
  130. if uploaded_file.name == video_name:
  131. # If the uploaded file matches the desired name, save it to a temporary location
  132. temp_location = save_uploaded_file(uploaded_file)
  133. if temp_location is not None:
  134. return temp_location
  135. # If the desired video name was not found among the uploaded files
  136. return None
  137. def save_uploaded_file(uploaded_file):
  138. try:
  139. temp_dir = io.BytesIO()
  140. temp_location = os.path.join(os.path.expanduser("detections/"), "Videos", uploaded_file.name)
  141. with open(temp_location, 'wb') as out:
  142. out.write(uploaded_file.read())
  143. return temp_location
  144. except Exception as e:
  145. st.error(f"Error saving uploaded file: {e}")
  146. return None
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...