|
|
import os |
|
|
from datetime import datetime |
|
|
import wave |
|
|
import contextlib |
|
|
|
|
|
""" |
|
|
Prints a summary of data in the dataset. Specifically, analyzes length of recordings for each recorder and prints recorder_data_summary.txt |
|
|
""" |
|
|
|
|
|
base_dir = os.path.dirname(os.path.abspath("__file__")) |
|
|
|
|
|
|
|
|
output_file = os.path.join(base_dir, "recorder_data_summary.txt") |
|
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
|
|
|
|
def get_wav_duration(file_path): |
|
|
try: |
|
|
with contextlib.closing(wave.open(file_path, 'r')) as wf: |
|
|
frames = wf.getnframes() |
|
|
rate = wf.getframerate() |
|
|
return frames / float(rate) |
|
|
except Exception as e: |
|
|
print(f"Error reading {file_path}: {e}") |
|
|
return 0 |
|
|
|
|
|
with open(output_file, "w") as f: |
|
|
f.write(f"Summary generated on: {current_time}\n") |
|
|
f.write("Folder, File Count, Total Size (MB), <59min Count, >=59min Count, Total Duration (hour)\n") |
|
|
|
|
|
|
|
|
for folder in os.listdir(base_dir): |
|
|
folder_path = os.path.join(base_dir, folder) |
|
|
|
|
|
|
|
|
if os.path.isdir(folder_path): |
|
|
data_folder = os.path.join(folder_path, "Data") |
|
|
|
|
|
if os.path.exists(data_folder) and os.path.isdir(data_folder): |
|
|
|
|
|
file_count = 0 |
|
|
total_size_bytes = 0 |
|
|
short_wavs = 0 |
|
|
long_wavs = 0 |
|
|
total_duration = 0 |
|
|
|
|
|
for file in os.listdir(data_folder): |
|
|
file_path = os.path.join(data_folder, file) |
|
|
|
|
|
|
|
|
if file.endswith(".wav"): |
|
|
file_count += 1 |
|
|
total_size_bytes += os.path.getsize(file_path) |
|
|
|
|
|
|
|
|
duration_sec = get_wav_duration(file_path) |
|
|
total_duration += duration_sec |
|
|
|
|
|
if duration_sec < 59 * 60: |
|
|
short_wavs += 1 |
|
|
else: |
|
|
long_wavs += 1 |
|
|
|
|
|
|
|
|
total_size_mb = total_size_bytes / (1024 * 1024) |
|
|
total_duration_hr = total_duration / 3600 |
|
|
|
|
|
|
|
|
f.write(f"{folder}, {file_count}, {total_size_mb:.4f}, {short_wavs}, {long_wavs}, {total_duration_hr:.2f}\n") |
|
|
|
|
|
print(f"Summary report saved to {output_file}") |
|
|
|