-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_pipeline_config.py
More file actions
280 lines (239 loc) · 9.17 KB
/
custom_pipeline_config.py
File metadata and controls
280 lines (239 loc) · 9.17 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Example: Custom Pipeline Configuration.
This example shows how to create and use custom pipeline configurations
for specific use cases or experiments.
Usage:
python examples/custom_pipeline_config.py --video_path /path/to/video.mp4
"""
import argparse
import json
import logging
from pathlib import Path
from typing import Any
from src.pipelines.face_analysis import FacePipeline, FacePipelineConfig
from src.pipelines.person_tracking import PersonPipeline, PersonPipelineConfig
# Import the modernized pipelines
from src.pipelines.scene_detection import ScenePipeline, ScenePipelineConfig
def setup_logging(log_level: str = "INFO"):
"""Set up logging configuration."""
logging.basicConfig(
level=getattr(logging, log_level.upper()),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()],
)
def create_high_performance_config() -> dict[str, Any]:
"""Create a high-performance configuration for powerful systems."""
return {
"scene_detection": {
"threshold": 0.1, # More sensitive scene detection
"use_adaptive_threshold": True,
"min_scene_length": 0.5,
"clip_model": "ViT-B/32",
"batch_size": 8,
"skip_frames": 1, # Process every frame
},
"person_tracking": {
"model_name": "yolo11x", # Largest, most accurate model
"confidence_threshold": 0.3,
"iou_threshold": 0.5,
"max_det": 300,
"track_buffer": 60,
"match_thresh": 0.7,
"frame_rate": 30,
"enable_pose_estimation": True,
"enable_tracking": True,
},
"face_analysis": {
"backends": ["openface", "deepface"],
"detection_confidence": 0.7,
"recognition_model": "VGG-Face",
"emotion_model": "fer2013",
"enable_landmarks": True,
"enable_gaze_estimation": True,
"enable_action_units": True,
"batch_size": 16,
},
"audio_processing": {
"whisper_model": "large", # Best accuracy
"sample_rate": 16000,
"chunk_duration": 10.0,
"overlap_duration": 2.0,
"emotion_window_size": 2.0,
},
}
def create_lightweight_config() -> dict[str, Any]:
"""Create a lightweight configuration for limited resources."""
return {
"scene_detection": {
"threshold": 0.3,
"use_adaptive_threshold": False,
"min_scene_length": 2.0,
"clip_model": "ViT-B/32",
"batch_size": 2,
"skip_frames": 5, # Process every 5th frame
},
"person_tracking": {
"model_name": "yolo11n", # Smallest, fastest model
"confidence_threshold": 0.5,
"iou_threshold": 0.7,
"max_det": 100,
"track_buffer": 30,
"match_thresh": 0.8,
"frame_rate": 10,
"enable_pose_estimation": False,
"enable_tracking": True,
},
"face_analysis": {
"backends": ["deepface"], # Reliable backend only
"detection_confidence": 0.8,
"recognition_model": "Facenet",
"emotion_model": "fer2013",
"enable_landmarks": False,
"enable_gaze_estimation": False,
"enable_action_units": False,
"batch_size": 4,
},
"audio_processing": {
"whisper_model": "tiny", # Fastest model
"sample_rate": 16000,
"chunk_duration": 30.0,
"overlap_duration": 0.5,
"emotion_window_size": 5.0,
},
}
def create_research_config() -> dict[str, Any]:
"""Create a research-oriented configuration with all features enabled."""
return {
"scene_detection": {
"threshold": 0.15,
"use_adaptive_threshold": True,
"min_scene_length": 1.0,
"clip_model": "ViT-L/14", # Largest CLIP model
"batch_size": 4,
"skip_frames": 2,
"enable_scene_classification": True,
"classification_classes": ["indoor", "outdoor", "face", "object", "text"],
},
"person_tracking": {
"model_name": "yolo11x",
"confidence_threshold": 0.25,
"iou_threshold": 0.4,
"max_det": 500,
"track_buffer": 120,
"match_thresh": 0.6,
"frame_rate": 30,
"enable_pose_estimation": True,
"enable_tracking": True,
"pose_model": "yolo11x-pose",
},
"face_analysis": {
"backends": ["openface", "deepface"],
"detection_confidence": 0.5,
"recognition_model": "ArcFace",
"emotion_model": "fer2013",
"enable_landmarks": True,
"enable_gaze_estimation": True,
"enable_action_units": True,
"enable_head_pose": True,
"batch_size": 8,
},
"audio_processing": {
"whisper_model": "large",
"sample_rate": 16000,
"chunk_duration": 15.0,
"overlap_duration": 3.0,
"emotion_window_size": 2.0,
"enable_speaker_diarization": True,
"enable_music_detection": True,
"enable_audio_events": True,
},
}
def demonstrate_custom_config(video_path: Path, config_type: str):
"""Demonstrate using a custom configuration."""
logger = logging.getLogger(__name__)
# Create configuration based on type
if config_type == "high_performance":
config = create_high_performance_config()
logger.info("Using high-performance configuration")
elif config_type == "lightweight":
config = create_lightweight_config()
logger.info("Using lightweight configuration")
elif config_type == "research":
config = create_research_config()
logger.info("Using research configuration")
else:
raise ValueError(f"Unknown configuration type: {config_type}")
logger.info(f"Configuration: {json.dumps(config, indent=2)}")
# Create output directory
output_dir = Path(f"output_{config_type}")
output_dir.mkdir(exist_ok=True)
# Process with scene detection pipeline
logger.info("Processing with Scene Detection Pipeline")
scene_config = ScenePipelineConfig(**config["scene_detection"])
scene_pipeline = ScenePipeline(scene_config)
scene_results = scene_pipeline.process_video(video_path)
# Save results
with open(output_dir / "scene_results.json", "w") as f:
json.dump(scene_results, f, indent=2, default=str)
logger.info(f"Scene detection found {len(scene_results.get('scenes', []))} scenes")
# Process with person tracking pipeline
logger.info("Processing with Person Tracking Pipeline")
person_config = PersonPipelineConfig(**config["person_tracking"])
person_pipeline = PersonPipeline(person_config)
person_results = person_pipeline.process_video(video_path)
# Save results
with open(output_dir / "person_results.json", "w") as f:
json.dump(person_results, f, indent=2, default=str)
logger.info(f"Person tracking found {len(person_results.get('tracks', []))} tracks")
# Process with face analysis pipeline
logger.info("Processing with Face Analysis Pipeline")
face_config = FacePipelineConfig(**config["face_analysis"])
face_pipeline = FacePipeline(face_config)
face_results = face_pipeline.process_video(video_path)
# Save results
with open(output_dir / "face_results.json", "w") as f:
json.dump(face_results, f, indent=2, default=str)
logger.info(f"Face analysis found {len(face_results.get('faces', []))} faces")
logger.info(f"All results saved to {output_dir}")
def main():
parser = argparse.ArgumentParser(
description="Demonstrate custom pipeline configurations"
)
parser.add_argument(
"--video_path", type=str, required=True, help="Path to input video file"
)
parser.add_argument(
"--config_type",
type=str,
default="high_performance",
choices=["high_performance", "lightweight", "research"],
help="Type of configuration to use",
)
parser.add_argument(
"--log_level",
type=str,
default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
)
args = parser.parse_args()
# Set up logging
setup_logging(args.log_level)
logger = logging.getLogger(__name__)
# Validate inputs
video_path = Path(args.video_path)
if not video_path.exists():
logger.error(f"Video file not found: {video_path}")
return 1
logger.info(f"Processing video: {video_path}")
logger.info(f"Configuration type: {args.config_type}")
# Demonstrate custom configuration
try:
demonstrate_custom_config(video_path, args.config_type)
logger.info("Custom configuration demonstration completed successfully!")
return 0
except Exception as e:
logger.error(f"Custom configuration demonstration failed: {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
exit(main())