| """ |
| 使用示例:展示如何使用不同模态配置进行人格分析 |
| """ |
| import os |
| from config import Config |
| from model_llm import PersonalityAnalyzer |
| from data_preprocessing import DataPreprocessor |
|
|
|
|
| def example_vision_only(): |
| """示例1: 仅使用视觉模态""" |
| print("=" * 50) |
| print("示例1: 仅视觉模态") |
| print("=" * 50) |
| |
| |
| Config.set_modalities(text=False, vision=True, audio=False) |
| |
| |
| analyzer = PersonalityAnalyzer() |
| preprocessor = DataPreprocessor() |
| |
| |
| video_path = "../First-Impression/data/example.mp4" |
| |
| |
| images = preprocessor.extract_images_from_video( |
| video_path, |
| "./temp_frames", |
| max_frames=10 |
| ) |
| |
| |
| result = analyzer.predict(images=images) |
| |
| print("\n预测结果:") |
| for trait, score in result["scores"].items(): |
| print(f" {trait}: {score:.3f}") |
|
|
|
|
| def example_text_only(): |
| """示例2: 仅使用文本模态""" |
| print("=" * 50) |
| print("示例2: 仅文本模态") |
| print("=" * 50) |
| |
| |
| Config.set_modalities(text=True, vision=False, audio=False) |
| |
| |
| analyzer = PersonalityAnalyzer() |
| |
| |
| text_description = """ |
| 这个人在视频中表现出以下特征: |
| - 说话声音较大,语速较快 |
| - 经常微笑,表情丰富 |
| - 手势较多,身体语言活跃 |
| - 与镜头有良好的眼神交流 |
| """ |
| |
| |
| result = analyzer.predict(text=text_description) |
| |
| print("\n预测结果:") |
| for trait, score in result["scores"].items(): |
| print(f" {trait}: {score:.3f}") |
|
|
|
|
| def example_multimodal(): |
| """示例3: 多模态组合""" |
| print("=" * 50) |
| print("示例3: 多模态组合(文本+视觉+音频)") |
| print("=" * 50) |
| |
| |
| Config.set_modalities(text=True, vision=True, audio=True) |
| |
| |
| analyzer = PersonalityAnalyzer() |
| preprocessor = DataPreprocessor() |
| |
| |
| video_path = "../First-Impression/data/example.mp4" |
| |
| |
| images = preprocessor.extract_images_from_video( |
| video_path, |
| "./temp_frames", |
| max_frames=10 |
| ) |
| |
| audio_path = preprocessor.extract_audio_from_video( |
| video_path, |
| "./temp_audio" |
| ) |
| |
| text_path = preprocessor.extract_text_from_video( |
| video_path, |
| "./temp_text" |
| ) |
| |
| |
| text = None |
| if text_path and os.path.exists(text_path): |
| with open(text_path, "r", encoding="utf-8") as f: |
| text = f.read() |
| |
| |
| result = analyzer.predict( |
| text=text, |
| images=images, |
| audio_path=audio_path |
| ) |
| |
| print("\n预测结果:") |
| for trait, score in result["scores"].items(): |
| print(f" {trait}: {score:.3f}") |
| |
| if result.get("reasoning"): |
| print(f"\n分析理由:\n{result['reasoning']}") |
|
|
|
|
| if __name__ == "__main__": |
| import os |
| |
| print("注意: 这些示例需要:") |
| print("1. 已安装所有依赖") |
| print("2. 已下载 QwenVL2.5-7B 模型") |
| print("3. 有可用的视频文件") |
| print("\n取消注释下面的函数调用来运行示例:\n") |
| |
| |
| |
| |
| |
|
|
|
|