Spaces:
Sleeping
Sleeping
sergey21000
commited on
Commit
•
a3c6281
1
Parent(s):
41e4f3c
Update utils.py
Browse files
utils.py
CHANGED
@@ -3,18 +3,25 @@ import glob
|
|
3 |
import json
|
4 |
import urllib.request
|
5 |
from pathlib import Path
|
6 |
-
from typing import
|
7 |
|
8 |
-
import torch
|
9 |
-
import pandas as pd
|
10 |
import numpy as np
|
11 |
import cv2
|
12 |
-
|
13 |
import gradio as gr
|
14 |
from ultralytics import YOLO
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
YOLO_CLASS_NAMES = json.loads(Path('yolo_classes.json').read_text())
|
|
|
18 |
|
19 |
|
20 |
def download_model(model_name: str, models_dir: Path, models: dict) -> str:
|
@@ -106,4 +113,74 @@ def get_csv_annotate(result_video_path: Path) -> str:
|
|
106 |
|
107 |
result_csv_path = f'{result_video_path.parent / result_video_path.stem}_annotations.csv'
|
108 |
df.to_csv(result_csv_path, index=False)
|
109 |
-
return result_csv_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import json
|
4 |
import urllib.request
|
5 |
from pathlib import Path
|
6 |
+
from typing import Tuple
|
7 |
|
|
|
|
|
8 |
import numpy as np
|
9 |
import cv2
|
10 |
+
import yt_dlp
|
11 |
import gradio as gr
|
12 |
from ultralytics import YOLO
|
13 |
|
14 |
+
import pandas as pd
|
15 |
+
import seaborn as sns
|
16 |
+
import matplotlib.pyplot as plt
|
17 |
+
|
18 |
+
plt.style.use('dark_background')
|
19 |
+
plt.rcParams.update({'figure.figsize': (12, 20)})
|
20 |
+
plt.rcParams.update({'font.size': 9})
|
21 |
+
|
22 |
|
23 |
YOLO_CLASS_NAMES = json.loads(Path('yolo_classes.json').read_text())
|
24 |
+
YOLO_CLASS_NAMES = {int(k): v for k, v in YOLO_CLASS_NAMES.items()}
|
25 |
|
26 |
|
27 |
def download_model(model_name: str, models_dir: Path, models: dict) -> str:
|
|
|
113 |
|
114 |
result_csv_path = f'{result_video_path.parent / result_video_path.stem}_annotations.csv'
|
115 |
df.to_csv(result_csv_path, index=False)
|
116 |
+
return result_csv_path
|
117 |
+
|
118 |
+
|
119 |
+
def get_matplotlib_fig(csv_annotations_path: str):
|
120 |
+
df = pd.read_csv(csv_annotations_path)
|
121 |
+
df_clean = df.dropna(subset=['class_name'])
|
122 |
+
|
123 |
+
fig, axes = plt.subplots(7, 1, figsize=(10, 20), constrained_layout=True)
|
124 |
+
|
125 |
+
sns.histplot(data=df_clean['conf'], kde=True, ax=axes[0])
|
126 |
+
axes[0].set_title('Распределение уверенности детекций')
|
127 |
+
axes[0].set_xlabel('Уверенность')
|
128 |
+
axes[0].set_ylabel('Количество обнаружений')
|
129 |
+
|
130 |
+
sns.boxplot(data=df_clean, x='class_name', y='conf', ax=axes[1])
|
131 |
+
axes[1].set_title('Распределение уверенности детекции по классам')
|
132 |
+
axes[1].set_xlabel('Класс объекта')
|
133 |
+
axes[1].set_ylabel('Уверенность')
|
134 |
+
axes[1].tick_params(axis='x', labelrotation=45)
|
135 |
+
|
136 |
+
sns.countplot(
|
137 |
+
data=df_clean,
|
138 |
+
x='class_name',
|
139 |
+
hue='class_name',
|
140 |
+
order=df_clean['class_name'].value_counts().index,
|
141 |
+
palette='viridis',
|
142 |
+
legend=False,
|
143 |
+
ax=axes[2],
|
144 |
+
)
|
145 |
+
axes[2].set_title('Количество обнаружений объектов по классам')
|
146 |
+
axes[2].set_xlabel('Класс объекта')
|
147 |
+
axes[2].set_ylabel('Количество')
|
148 |
+
|
149 |
+
face_count_per_frame = df.groupby('frame_number')['box_detected'].sum()
|
150 |
+
axes[3].plot(face_count_per_frame.index, face_count_per_frame.values, marker='o', linestyle='-')
|
151 |
+
axes[3].set_title('Частота обнаружения объектов по кадрам')
|
152 |
+
axes[3].set_xlabel('Номер кадра')
|
153 |
+
axes[3].set_ylabel('Количество обнаруженных объектов')
|
154 |
+
|
155 |
+
face_count_per_frame = df.groupby('frame_sec')['box_detected'].sum()
|
156 |
+
axes[4].plot(face_count_per_frame.index, face_count_per_frame.values, marker='o', linestyle='-')
|
157 |
+
axes[4].set_title('Частота обнаружения объектов по секундам')
|
158 |
+
axes[4].set_xlabel('Время (сек)')
|
159 |
+
axes[4].set_ylabel('Количество обнаруженных объектов')
|
160 |
+
|
161 |
+
sns.scatterplot(
|
162 |
+
data=df_clean,
|
163 |
+
x='frame_sec',
|
164 |
+
y='class_name',
|
165 |
+
hue='class_name',
|
166 |
+
palette='deep',
|
167 |
+
s=50,
|
168 |
+
alpha=0.6,
|
169 |
+
legend=True,
|
170 |
+
ax=axes[5],
|
171 |
+
)
|
172 |
+
axes[5].set_title('Временная шкала обнаружения объектов по классам')
|
173 |
+
axes[5].set_xlabel('Время видео (секунды)')
|
174 |
+
axes[5].set_ylabel('Эмоция')
|
175 |
+
axes[5].grid(True, linestyle='--', alpha=0.7)
|
176 |
+
axes[5].legend(title='Классы объектов', bbox_to_anchor=(1.05, 1), loc='upper left')
|
177 |
+
|
178 |
+
emotion_timeline = df.pivot_table(index='frame_sec', columns='class_name', aggfunc='size', fill_value=0)
|
179 |
+
emotion_timeline.plot(kind='area', stacked=True, ax=axes[6])
|
180 |
+
axes[6].set_title('Динамика обнаружения классов во времени')
|
181 |
+
axes[6].set_xlabel('Время видео (секунды)')
|
182 |
+
axes[6].set_ylabel('Количество детекций')
|
183 |
+
axes[6].grid(True, linestyle='--', alpha=0.7)
|
184 |
+
axes[6].legend(title='Классы объектов', bbox_to_anchor=(1.05, 1), loc='upper left')
|
185 |
+
|
186 |
+
return fig
|