-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessor.py
More file actions
203 lines (121 loc) · 5.18 KB
/
processor.py
File metadata and controls
203 lines (121 loc) · 5.18 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import time
import threading
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from src.pipelines import pipeline_attendance
from src.pipelines.core import llm_integration
from pydub import AudioSegment
from src.pipelines.core.tools import logging_own
VIDEO_DIR = "./data/database/database_videos"
AUDIO_DIR = "./data/database/database_audios"
TRANSCRIPT_DIR = './data/raw/transcription'
NOTES_DIR = "./data/database/database_notes"
# Queue for processing
video_queue = []
lock = threading.Lock()
def convert_mp3_to_wav(mp3_path):
"""Converts an MP3 file to WAV format."""
try:
print(f"🎵 Converting {mp3_path} to WAV format...")
wav_path = mp3_path.replace('.mp3', '.wav')
audio = AudioSegment.from_mp3(mp3_path)
audio.export(wav_path, format="wav")
print(f"✅ Conversion complete: {wav_path}")
return wav_path
except Exception as e:
print(f"❌ Error converting MP3 to WAV: {e}")
return None
class VideoHandler(FileSystemEventHandler):
def on_created(self, event):
if event.is_directory:
return
if event.src_path.endswith(('.mp4', '.avi', '.mov')):
with lock:
print(f"New video detected: {event.src_path}")
video_queue.append(event.src_path)
def process_video_and_audio(video_path):
print(f"🚀 Processing video: {video_path}")
video_path = video_path.replace("\r","/r")
class_name = os.path.splitext(os.path.basename(video_path))[0] # Extract class name from filename
print(f"\n class name {class_name}\n")
output_video_path, csv_filepath, converted_output_video_path = pipeline_attendance.process_video(video_path, class_name)
print(f"✅ Completed video processing: {video_path}")
# 🔎 Check for a matching audio file
audio_path = os.path.join(AUDIO_DIR, f"{class_name}.wav")
if os.path.exists(audio_path):
pass
else:
audio_path = os.path.join(AUDIO_DIR, f"{class_name}.mp3")
audio_path = audio_path.replace("\r","/r")
print(f"audio path: {audio_path}")
if ".mp3" in audio_path :
audio_path = convert_mp3_to_wav(audio_path)
if os.path.exists(audio_path):
print(f"🎯 Found corresponding audio file: {audio_path}")
process_audio(audio_path,class_name)
else:
print(f"❌ No matching audio file found for: {video_path}")
logging_own.save_processed_video(video_path)
def process_audio(audio_path,class_name):
try:
print(f"🎧 Processing audio: {audio_path}")
# ✅ LLM Integration for transcription
output_transcript_path, transcription = llm_integration.transcribe_audio(
TRANSCRIPT_DIR=TRANSCRIPT_DIR,
AUDIO_FILENAME=audio_path ,
VOSK_MODEL_PATH="models/vosk-model-small-en-us-0.15"
)
print(f"✅ Transcription saved at: {output_transcript_path}")
print(f"📝 Transcription:\n{transcription}")
client = llm_integration.load_llm()
# class_name = os.path.splitext(os.path.basename(output_transcript_path))[0]
# Generate class notes
print("generating notes....")
notes, notes_path = llm_integration.generate_notes(transcription, client, NOTES_DIR, class_name)
print(f"✅ Notes saved at: {notes_path}")
except Exception as e:
print(f"❌ Error processing audio: {e}")
def worker():
while True:
with lock:
if video_queue:
video_path = video_queue.pop(0)
process_video_and_audio(video_path)
time.sleep(1)
def process_existing_videos():
"""Process all existing videos before starting live monitoring."""
print("🔍 Checking for existing videos...")
processed_videos = logging_own.load_processed_videos()
for filename in os.listdir(VIDEO_DIR):
if filename.endswith(('.mp4', '.avi', '.mov')):
video_path = os.path.join(VIDEO_DIR, filename)
if video_path not in processed_videos:
with lock:
video_queue.append(video_path)
else:
print(f"Skipping: {video_path} (Already processed)")
print(f"✅ Found {len(video_queue)} videos to process.")
# op = input("continue?yes[y]/no[n]")
op = "y"
if op.lower() == 'y':
pass
else:
exit()
def start_monitoring():
process_existing_videos() # Process existing videos first
event_handler = VideoHandler()
observer = Observer()
observer.schedule(event_handler, VIDEO_DIR, recursive=False)
observer.start()
print("🚀 Monitoring directory for new videos...")
# Start processing thread
threading.Thread(target=worker, daemon=True).start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
start_monitoring()