tomerk commited on
Commit
7437080
·
verified ·
1 Parent(s): 82240dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -12,10 +12,7 @@ import json
12
  import os
13
  from moviepy.editor import ImageSequenceClip
14
  from gradio_client import Client, file
15
- # https://16d3-2a0d-6fc2-61b1-8500-5d45-b385-9a4d-5522.ngrok-free.app/video_feed
16
- # rtsp://admin:[email protected]:5678/Streaming/Channels/101
17
-
18
- import os
19
 
20
  api_key = os.getenv("OPEN_AI_KEY")
21
  user_name = os.getenv("USER_NAME")
@@ -41,6 +38,17 @@ client = openai.OpenAI(api_key=api_key)
41
  stop_capture = False
42
  alerts_mode = True
43
 
 
 
 
 
 
 
 
 
 
 
 
44
  def encode_to_video_fast(frames, fps):
45
 
46
  os.makedirs('videos', exist_ok=True)
@@ -148,21 +156,21 @@ def process_clip(prompt, frames, chatbot):
148
  f.write(frame_data)
149
  frame_paths.append(frame_path)
150
 
151
- def process_clip_from_file(prompt, frames, chatbot, fps):
152
  global stop_capture
153
  if not stop_capture:
154
  israel_tz = pytz.timezone('Asia/Jerusalem')
155
  start_time = datetime.now(israel_tz).strftime('%H:%M:%S')
156
  print("[Start]:", start_time, len(frames))
157
 
158
- fps = 20
159
- frames_to_skip = int(fps * 1)
160
  base64Frames = process_frames(frames, frames_to_skip)
161
  frames_count, processing_time, api_response = check_condition(prompt, base64Frames)
162
 
163
  result = None
164
  if api_response and api_response.get("condition_met", False):
165
- video_clip_path = encode_to_video_fast(frames, fps)
 
166
  chatbot.append(((video_clip_path,), None))
167
  chatbot.append((f"Time: {start_time}\nDetails: {api_response.get('details', '')}", None))
168
 
@@ -206,6 +214,7 @@ def analyze_video_file(prompt, video_path, chatbot):
206
  frames_per_chunk = fps * LENGTH # Number of frames per 5-second chunk
207
 
208
  frames = []
 
209
 
210
  # Create a thread pool for concurrent processing
211
  with ThreadPoolExecutor(max_workers=4) as executor:
@@ -219,12 +228,14 @@ def analyze_video_file(prompt, video_path, chatbot):
219
 
220
  # Split the video into chunks of frames corresponding to 5 seconds
221
  if len(frames) >= frames_per_chunk:
222
- futures.append(executor.submit(process_clip_from_file, prompt, frames.copy(), chatbot, fps))
223
  frames = []
 
224
 
225
  # If any remaining frames that are less than 5 seconds, process them as a final chunk
226
  if len(frames) > 0:
227
- futures.append(executor.submit(process_clip_from_file, prompt, frames.copy(), chatbot, fps))
 
228
 
229
  cap.release()
230
  # Yield results as soon as each thread completes
 
12
  import os
13
  from moviepy.editor import ImageSequenceClip
14
  from gradio_client import Client, file
15
+ import subprocess
 
 
 
16
 
17
  api_key = os.getenv("OPEN_AI_KEY")
18
  user_name = os.getenv("USER_NAME")
 
38
  stop_capture = False
39
  alerts_mode = True
40
 
41
+ def clip_video_segment(input_video_path, start_time, duration):
42
+ os.makedirs('videos', exist_ok=True)
43
+ output_video_path = f"videos/{uuid.uuid4()}.mp4"
44
+
45
+ subprocess.call([
46
+ 'ffmpeg', '-y', '-ss', str(start_time), '-i', input_video_path,
47
+ '-t', str(duration), '-c', 'copy', output_video_path
48
+ ])
49
+
50
+ return output_video_path
51
+
52
  def encode_to_video_fast(frames, fps):
53
 
54
  os.makedirs('videos', exist_ok=True)
 
156
  f.write(frame_data)
157
  frame_paths.append(frame_path)
158
 
159
+ def process_clip_from_file(prompt, frames, chatbot, fps, video_path, id):
160
  global stop_capture
161
  if not stop_capture:
162
  israel_tz = pytz.timezone('Asia/Jerusalem')
163
  start_time = datetime.now(israel_tz).strftime('%H:%M:%S')
164
  print("[Start]:", start_time, len(frames))
165
 
166
+ frames_to_skip = int(fps)
 
167
  base64Frames = process_frames(frames, frames_to_skip)
168
  frames_count, processing_time, api_response = check_condition(prompt, base64Frames)
169
 
170
  result = None
171
  if api_response and api_response.get("condition_met", False):
172
+ # video_clip_path = encode_to_video_fast(frames, fps)
173
+ video_clip_path = clip_video_segment(video_path, id*LENGTH, (id+1)*LENGTH)
174
  chatbot.append(((video_clip_path,), None))
175
  chatbot.append((f"Time: {start_time}\nDetails: {api_response.get('details', '')}", None))
176
 
 
214
  frames_per_chunk = fps * LENGTH # Number of frames per 5-second chunk
215
 
216
  frames = []
217
+ chunk = 0
218
 
219
  # Create a thread pool for concurrent processing
220
  with ThreadPoolExecutor(max_workers=4) as executor:
 
228
 
229
  # Split the video into chunks of frames corresponding to 5 seconds
230
  if len(frames) >= frames_per_chunk:
231
+ futures.append(executor.submit(process_clip_from_file, prompt, frames.copy(), chatbot, fps, video_path, chunk))
232
  frames = []
233
+ chunk++
234
 
235
  # If any remaining frames that are less than 5 seconds, process them as a final chunk
236
  if len(frames) > 0:
237
+ futures.append(executor.submit(process_clip_from_file, prompt, frames.copy(), chatbot, fps, video_path, chunk))
238
+ chunk++
239
 
240
  cap.release()
241
  # Yield results as soon as each thread completes