Spaces:
Sleeping
Sleeping
sergey21000
commited on
Commit
•
bf23476
1
Parent(s):
80e21da
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,109 +1,109 @@
|
|
1 |
-
import os
|
2 |
-
import glob
|
3 |
-
import json
|
4 |
-
import urllib.request
|
5 |
-
from pathlib import Path
|
6 |
-
from typing import List, Dict, Union, Tuple, Optional
|
7 |
-
|
8 |
-
import torch
|
9 |
-
import pandas as pd
|
10 |
-
import numpy as np
|
11 |
-
import cv2
|
12 |
-
import yt_dlp
|
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:
|
21 |
-
model_path = models_dir / model_name
|
22 |
-
if not model_path.exists():
|
23 |
-
urllib.request.urlretrieve(models[model_name], model_path)
|
24 |
-
return str(model_path)
|
25 |
-
|
26 |
-
|
27 |
-
def detect_image(image_path: str, model: YOLO, conf: float, iou: float) -> np.ndarray:
|
28 |
-
gr.Progress()(0.5, desc='Image detection...')
|
29 |
-
detections = model.predict(source=image_path, conf=conf, iou=iou)
|
30 |
-
np_image = detections[0].plot()
|
31 |
-
np_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
|
32 |
-
return np_image
|
33 |
-
|
34 |
-
|
35 |
-
def detect_video(video_path_or_url: str, model: YOLO, conf: float, iou: float) -> Tuple[Path, Path]:
|
36 |
-
progress = gr.Progress()
|
37 |
-
video_path = video_path_or_url
|
38 |
-
if 'youtube.com' in video_path_or_url or 'youtu.be' in video_path_or_url:
|
39 |
-
progress(0.001, desc='Downloading videos from YouTube...')
|
40 |
-
ydl_opts = {'format': 'bestvideo[height<=720]'}
|
41 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
42 |
-
video_info_dict = ydl.extract_info(video_path_or_url, download=True)
|
43 |
-
video_path = ydl.prepare_filename(video_info_dict)
|
44 |
-
|
45 |
-
cap = cv2.VideoCapture(video_path)
|
46 |
-
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
47 |
-
cap.release()
|
48 |
-
|
49 |
-
generator = model.predict(
|
50 |
-
source=video_path,
|
51 |
-
conf=0.5,
|
52 |
-
iou=0.5,
|
53 |
-
save=True,
|
54 |
-
save_txt=True,
|
55 |
-
save_conf=True,
|
56 |
-
stream=True,
|
57 |
-
verbose=False,
|
58 |
-
)
|
59 |
-
|
60 |
-
frames_count = 0
|
61 |
-
for result in generator:
|
62 |
-
frames_count += 1
|
63 |
-
progress((frames_count, num_frames), desc=f'Video detection, step {frames_count}/{num_frames}')
|
64 |
-
|
65 |
-
file_name = Path(result.path).with_suffix('.avi').name
|
66 |
-
result_video_path = Path(result.save_dir) / file_name
|
67 |
-
Path(video_path).unlink(missing_ok=True)
|
68 |
-
return result_video_path
|
69 |
-
|
70 |
-
|
71 |
-
def get_csv_annotate(result_video_path: Path) -> str:
|
72 |
-
if not isinstance(result_video_path, Path):
|
73 |
-
return None
|
74 |
-
|
75 |
-
txts_path = result_video_path.parent / 'labels'
|
76 |
-
escaped_pattern = glob.escape(result_video_path.stem)
|
77 |
-
matching_txts_path = sorted(txts_path.glob(f'{escaped_pattern}_*.txt'), key=os.path.getmtime)
|
78 |
-
|
79 |
-
df_list = []
|
80 |
-
for txt_path in matching_txts_path:
|
81 |
-
frame_number = int(txt_path.stem.rsplit('_')[-1])
|
82 |
-
with open(txt_path) as file:
|
83 |
-
df_rows = file.readlines()
|
84 |
-
for df_row in df_rows:
|
85 |
-
df_row = map(float, df_row.split())
|
86 |
-
df_list.append((frame_number, *df_row))
|
87 |
-
|
88 |
-
column_names = ['frame_number', 'class_label', 'x', 'y', 'w', 'h', 'conf']
|
89 |
-
df = pd.DataFrame(df_list, columns=column_names)
|
90 |
-
|
91 |
-
df.class_label = df.class_label.astype(int)
|
92 |
-
class_name_series = df.class_label.map(YOLO_CLASS_NAMES)
|
93 |
-
df.insert(loc=1, column='class_name', value=class_name_series)
|
94 |
-
|
95 |
-
cap = cv2.VideoCapture(str(result_video_path))
|
96 |
-
frames_fps = int(cap.get(cv2.CAP_PROP_FPS))
|
97 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
98 |
-
cap.release()
|
99 |
-
|
100 |
-
frame_sec_series = df.frame_number / frames_fps
|
101 |
-
df.insert(loc=1, column='frame_sec', value=frame_sec_series)
|
102 |
-
|
103 |
-
full_frames = pd.DataFrame({'frame_number': range(total_frames)})
|
104 |
-
df = pd.merge(full_frames, df, on='frame_number', how='outer')
|
105 |
-
df.frame_sec = df.frame_number / frames_fps
|
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
|
|
|
1 |
+
import os
|
2 |
+
import glob
|
3 |
+
import json
|
4 |
+
import urllib.request
|
5 |
+
from pathlib import Path
|
6 |
+
from typing import List, Dict, Union, Tuple, Optional
|
7 |
+
|
8 |
+
import torch
|
9 |
+
import pandas as pd
|
10 |
+
import numpy as np
|
11 |
+
import cv2
|
12 |
+
# import yt_dlp
|
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:
|
21 |
+
model_path = models_dir / model_name
|
22 |
+
if not model_path.exists():
|
23 |
+
urllib.request.urlretrieve(models[model_name], model_path)
|
24 |
+
return str(model_path)
|
25 |
+
|
26 |
+
|
27 |
+
def detect_image(image_path: str, model: YOLO, conf: float, iou: float) -> np.ndarray:
|
28 |
+
gr.Progress()(0.5, desc='Image detection...')
|
29 |
+
detections = model.predict(source=image_path, conf=conf, iou=iou)
|
30 |
+
np_image = detections[0].plot()
|
31 |
+
np_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
|
32 |
+
return np_image
|
33 |
+
|
34 |
+
|
35 |
+
def detect_video(video_path_or_url: str, model: YOLO, conf: float, iou: float) -> Tuple[Path, Path]:
|
36 |
+
progress = gr.Progress()
|
37 |
+
video_path = video_path_or_url
|
38 |
+
if 'youtube.com' in video_path_or_url or 'youtu.be' in video_path_or_url:
|
39 |
+
progress(0.001, desc='Downloading videos from YouTube...')
|
40 |
+
ydl_opts = {'format': 'bestvideo[height<=720]'}
|
41 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
42 |
+
video_info_dict = ydl.extract_info(video_path_or_url, download=True)
|
43 |
+
video_path = ydl.prepare_filename(video_info_dict)
|
44 |
+
|
45 |
+
cap = cv2.VideoCapture(video_path)
|
46 |
+
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
47 |
+
cap.release()
|
48 |
+
|
49 |
+
generator = model.predict(
|
50 |
+
source=video_path,
|
51 |
+
conf=0.5,
|
52 |
+
iou=0.5,
|
53 |
+
save=True,
|
54 |
+
save_txt=True,
|
55 |
+
save_conf=True,
|
56 |
+
stream=True,
|
57 |
+
verbose=False,
|
58 |
+
)
|
59 |
+
|
60 |
+
frames_count = 0
|
61 |
+
for result in generator:
|
62 |
+
frames_count += 1
|
63 |
+
progress((frames_count, num_frames), desc=f'Video detection, step {frames_count}/{num_frames}')
|
64 |
+
|
65 |
+
file_name = Path(result.path).with_suffix('.avi').name
|
66 |
+
result_video_path = Path(result.save_dir) / file_name
|
67 |
+
Path(video_path).unlink(missing_ok=True)
|
68 |
+
return result_video_path
|
69 |
+
|
70 |
+
|
71 |
+
def get_csv_annotate(result_video_path: Path) -> str:
|
72 |
+
if not isinstance(result_video_path, Path):
|
73 |
+
return None
|
74 |
+
|
75 |
+
txts_path = result_video_path.parent / 'labels'
|
76 |
+
escaped_pattern = glob.escape(result_video_path.stem)
|
77 |
+
matching_txts_path = sorted(txts_path.glob(f'{escaped_pattern}_*.txt'), key=os.path.getmtime)
|
78 |
+
|
79 |
+
df_list = []
|
80 |
+
for txt_path in matching_txts_path:
|
81 |
+
frame_number = int(txt_path.stem.rsplit('_')[-1])
|
82 |
+
with open(txt_path) as file:
|
83 |
+
df_rows = file.readlines()
|
84 |
+
for df_row in df_rows:
|
85 |
+
df_row = map(float, df_row.split())
|
86 |
+
df_list.append((frame_number, *df_row))
|
87 |
+
|
88 |
+
column_names = ['frame_number', 'class_label', 'x', 'y', 'w', 'h', 'conf']
|
89 |
+
df = pd.DataFrame(df_list, columns=column_names)
|
90 |
+
|
91 |
+
df.class_label = df.class_label.astype(int)
|
92 |
+
class_name_series = df.class_label.map(YOLO_CLASS_NAMES)
|
93 |
+
df.insert(loc=1, column='class_name', value=class_name_series)
|
94 |
+
|
95 |
+
cap = cv2.VideoCapture(str(result_video_path))
|
96 |
+
frames_fps = int(cap.get(cv2.CAP_PROP_FPS))
|
97 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
98 |
+
cap.release()
|
99 |
+
|
100 |
+
frame_sec_series = df.frame_number / frames_fps
|
101 |
+
df.insert(loc=1, column='frame_sec', value=frame_sec_series)
|
102 |
+
|
103 |
+
full_frames = pd.DataFrame({'frame_number': range(total_frames)})
|
104 |
+
df = pd.merge(full_frames, df, on='frame_number', how='outer')
|
105 |
+
df.frame_sec = df.frame_number / frames_fps
|
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
|