/
bioxakep
/
Kevin
Обзор
Документация
Войти
/
bioxakep
/
Kevin
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
utils.py
183 строки
6 KB
bioxakep
Remove main
30 мар 2026, 22:57
30 мар 2026, 22:57
94c1c25
Код
Авторство
О чём код?
import os import subprocess from pathlib import Path from typing import Dict import psutil import pydub from config import faces_config, voice_config import json def kill_process_by_name(name: str): for process in psutil.process_iter(): if process.name() == name: process.kill() def get_vosk_model_path(): if not os.path.exists(voice_config.vosk_models_path): raise FileNotFoundError( f"Директория с моделями VOSK не найдена: {voice_config.vosk_models_path}" ) models = list( filter( lambda s: "vosk" in s.lower() and ".zip" not in s.lower(), os.listdir(voice_config.vosk_models_path), ) ) models = { model: sum( file.stat().st_size for file in Path( str(os.path.join(voice_config.vosk_models_path, model)) ).rglob("*") if file.is_file() ) for model in models } c = 0 for model, size in models.items(): print(f"{c + 1}. {model} ({size // 1024 ** 2:.0f} МБ)") c += 1 index_str = input("Введите номер модели распознавания: ") if index_str.isdigit(): index = int(index_str) return list(models.keys())[index - 1] return "NO_MODEL" def humanize_time(time_seconds, clocks=True): if time_seconds == 0: return "мгновенно" human_time = "" human_time_clocks = "" # Дни days = time_seconds // 86400 if days > 0: human_time = str(days) if days % 10 == 1 and days not in range(11, 20): human_time += " день, " elif days % 10 in [2, 3, 4]: human_time += f" дня, " else: human_time += f" дней, " if clocks: human_time_clocks = f"{days}d " # Часы hours = (time_seconds - days * 86400) // 3600 if hours > 0: human_time += str(hours) if hours % 10 == 1 and hours not in range(11, 20): human_time += " час, " elif hours % 10 in [2, 3, 4] and hours not in range(11, 20): human_time += f" часа, " else: human_time += f" часов, " human_time_clocks += ("0" if hours < 10 else f"") + str(hours) + ":" else: human_time_clocks += "00:" # Минуты minutes = (time_seconds - days * 86400 - hours * 3600) // 60 if minutes > 0: human_time += str(minutes) if minutes % 10 == 1 and minutes not in range(11, 20): human_time += " минута, " elif minutes % 10 in [2, 3, 4] and minutes not in range(11, 20): human_time += f" минуты, " else: human_time += f" минут, " human_time_clocks += ("0" if minutes < 10 else f"") + str(minutes) + ":" else: human_time_clocks += "00:" # Секунды seconds = time_seconds - days * 86400 - hours * 3600 - minutes * 60 if seconds > 0: human_time += str(seconds) if seconds % 10 == 1 and seconds not in range(11, 20): human_time += " секунда" elif seconds % 10 in [2, 3, 4] and seconds not in range(11, 20): human_time += f" секунды" else: human_time += f" секунд" human_time_clocks += ("0" if seconds < 10 else f"") + str(seconds) else: human_time_clocks += "00" return human_time_clocks if clocks else human_time def check_audio_playing_status(): res = subprocess.check_output(["pmset", "-g"]).decode() res = res[res.find("\n sleep") :].replace("\n", "", 1) res = res[: res.find("\n")] res = res[res.find("(") + 1 : res.find(")")] print(res) result = res.count(",") print(result > 3) def read_labels() -> Dict: if not os.path.exists(faces_config.faces_labels_path): raise FileNotFoundError(f"Labels file not found at {faces_config.faces_labels_path}") data: Dict = json.load(open(faces_config.faces_labels_path)) return data def save_labels(data: Dict) -> None: with open(faces_config.faces_labels_path, "w") as f: json.dump(data, f) def split_by_silence( file_path: str, min_sound_len: int = 1000, min_db: float = -80.0, ) -> list: if not os.path.exists(file_path): raise FileNotFoundError(f"Файл {file_path} не найден!") audio_file = pydub.AudioSegment.from_file(file_path) print(f"Длительность записи: {audio_file.duration_seconds} секунд, ", end="") parts, offset, part_offset = 0, 0, 0 files_paths = list() chunk_size = 100 while True: end_index = ( (offset + chunk_size) if offset + chunk_size < len(audio_file) else len(audio_file) ) if end_index - offset < chunk_size: # print(f"Осталось {end_index - offset} мс, записываем последний отрезок и выходим из цикла") last_file_path: str = file_path.replace( "." + file_path.split(".")[-1], f"_{parts + 1}_end.wav" ) files_paths.append(last_file_path) last_part = audio_file[part_offset:] last_part.export(last_file_path, format="wav") parts += 1 break audio_part = audio_file[offset:end_index] if audio_part.dBFS < min_db: # print(f"Найдена тишина на моменте {offset} мс") if offset + int(chunk_size / 5) - part_offset < min_sound_len: # print(f"Но пропущена изза слишком короткого отрезка = {offset - part_offset} мс") offset += chunk_size continue out_part = audio_file[part_offset : offset + int(chunk_size / 5)] out_file_path: str = file_path.replace( "." + file_path.split(".")[-1], f"_{parts + 1}.wav" ) files_paths.append(out_file_path) out_part.export(out_file_path, format="wav") # print(f"Отрезок длиной {offset - part_offset} мс записан") offset += int(4 * chunk_size / 5) # print(f"Начинаем слушать с момента {offset} мс") # print("=" * 100) part_offset = offset parts += 1 else: offset += chunk_size print(f"файл разбит на {parts} отрезков.") return files_paths