File size: 3,465 Bytes
3f86d6a | 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 | """
使用示例:展示如何使用不同模态配置进行人格分析
"""
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")
# 取消注释以运行示例
# example_vision_only()
# example_text_only()
# example_multimodal()
|