ManishThota commited on
Commit
c8c4b3b
·
verified ·
1 Parent(s): 8c1f2bd

Update multi_video_app.py

Browse files
Files changed (1) hide show
  1. multi_video_app.py +24 -26
multi_video_app.py CHANGED
@@ -3,37 +3,30 @@ warnings.filterwarnings("ignore")
3
  import gradio as gr
4
  import re
5
  from typing import Dict, List
6
- import csv
7
  import os
8
  import torch
 
9
  from src.video_model import describe_video
10
  from src.utils import parse_string, parse_annotations
11
 
12
-
13
-
14
- # Function to save data to a CSV file
15
  def save_to_csv(observations: List[Dict], output_dir: str = "outputs") -> str:
16
  if not os.path.exists(output_dir):
17
  os.makedirs(output_dir)
18
 
 
 
 
 
19
  csv_file = os.path.join(output_dir, "video_observations.csv")
20
 
21
- with open(csv_file, mode='w', newline='') as file:
22
- writer = csv.writer(file)
23
- writer.writerow(["video_name", "standing", "hands.free", "indoors", "screen.interaction_yes"])
24
- for observation in observations:
25
- writer.writerow([
26
- observation['video_name'],
27
- observation['standing'],
28
- observation['hands.free'],
29
- observation['indoors'],
30
- observation['screen.interaction_yes']
31
- ])
32
 
33
  return csv_file
34
 
35
  # Function to process a single video and return the observation data
36
- def process_single_video(video_path: str, standing, hands, location, screen) -> Dict:
37
  video_name = os.path.basename(video_path) # Extract video name from the path
38
  query = "Describe this video in detail and answer the questions"
39
  additional_info = []
@@ -57,8 +50,8 @@ def process_single_video(video_path: str, standing, hands, location, screen) ->
57
  final_prompt = final_query + " " + end_query
58
 
59
  # Assuming your describe_video function handles the video processing
60
- final_response = describe_video(video_path, final_prompt)
61
-
62
 
63
  conditions = {
64
  'standing': (standing, 'standing: 1', 'standing: None'),
@@ -71,20 +64,24 @@ def process_single_video(video_path: str, standing, hands, location, screen) ->
71
  if not condition:
72
  final_response = final_response.replace(to_replace, replacement)
73
 
74
- return final_response
75
-
 
 
76
 
 
 
77
 
78
  # Function to process all videos in a folder
79
- def process_multiple_videos(video_files: List[str], sitting, hands, location, screen):
80
  all_observations = []
81
 
82
  for video_path in video_files:
83
- observation = process_single_video(video_path, sitting, hands, location, screen)
84
- if "error" not in observation:
85
  all_observations.append(observation)
86
  else:
87
- print(observation["error"]) # Log any errors
88
 
89
  # Clear GPU cache
90
  torch.cuda.empty_cache()
@@ -94,8 +91,9 @@ def process_multiple_videos(video_files: List[str], sitting, hands, location, sc
94
  return "Processing completed. Download the CSV file.", csv_file
95
 
96
  # Gradio interface
97
- def gradio_interface(video_files, sitting, hands, location, screen):
98
- return process_multiple_videos(video_files, sitting, hands, location, screen)
 
99
 
100
  # Inputs
101
  video_files = gr.File(file_count="multiple", file_types=["video"], label="Upload multiple videos")
 
3
  import gradio as gr
4
  import re
5
  from typing import Dict, List
 
6
  import os
7
  import torch
8
+ import pandas as pd
9
  from src.video_model import describe_video
10
  from src.utils import parse_string, parse_annotations
11
 
12
+ # Function to save data to a CSV file using pandas
 
 
13
  def save_to_csv(observations: List[Dict], output_dir: str = "outputs") -> str:
14
  if not os.path.exists(output_dir):
15
  os.makedirs(output_dir)
16
 
17
+ # Convert the list of dictionaries to a pandas DataFrame
18
+ df = pd.DataFrame(observations)
19
+
20
+ # Specify the CSV file path
21
  csv_file = os.path.join(output_dir, "video_observations.csv")
22
 
23
+ # Save the DataFrame to a CSV file
24
+ df.to_csv(csv_file, index=False)
 
 
 
 
 
 
 
 
 
25
 
26
  return csv_file
27
 
28
  # Function to process a single video and return the observation data
29
+ def process_single_video(video_path, standing, hands, location, screen) -> Dict:
30
  video_name = os.path.basename(video_path) # Extract video name from the path
31
  query = "Describe this video in detail and answer the questions"
32
  additional_info = []
 
50
  final_prompt = final_query + " " + end_query
51
 
52
  # Assuming your describe_video function handles the video processing
53
+ response = describe_video(video_path, final_prompt)
54
+ final_response = f"<video_name>{video_name}</video_name>" + " \n" + response
55
 
56
  conditions = {
57
  'standing': (standing, 'standing: 1', 'standing: None'),
 
64
  if not condition:
65
  final_response = final_response.replace(to_replace, replacement)
66
 
67
+ # Parse the response to extract video name and annotations
68
+ parsed_content = parse_string(final_response, ["video_name", "annotation"])
69
+ video_name = parsed_content['video_name'][0] if parsed_content['video_name'] else None
70
+ annotations_dict = parse_annotations(parsed_content['annotation']) if parsed_content['annotation'] else {}
71
 
72
+ # Return the observation as a dictionary
73
+ return {'video_name': video_name, **annotations_dict}
74
 
75
  # Function to process all videos in a folder
76
+ def process_multiple_videos(video_files: List[str], standing, hands, location, screen):
77
  all_observations = []
78
 
79
  for video_path in video_files:
80
+ observation = process_single_video(video_path, standing, hands, location, screen)
81
+ if observation['video_name']: # Only add valid observations
82
  all_observations.append(observation)
83
  else:
84
+ print("Error processing video:", video_path) # Log any errors
85
 
86
  # Clear GPU cache
87
  torch.cuda.empty_cache()
 
91
  return "Processing completed. Download the CSV file.", csv_file
92
 
93
  # Gradio interface
94
+ def gradio_interface(video_files, standing, hands, location, screen):
95
+ video_file_paths = [video.name for video in video_files] # Extract file paths from uploaded files
96
+ return process_multiple_videos(video_file_paths, standing, hands, location, screen)
97
 
98
  # Inputs
99
  video_files = gr.File(file_count="multiple", file_types=["video"], label="Upload multiple videos")