| |
| |
|
|
| """ |
| 计算主观评分和CLIP得分的相关系数(SRCC、KRCC、PLCC) |
| """ |
|
|
| import os |
| import sys |
| import pandas as pd |
| import numpy as np |
| from scipy.stats import spearmanr, pearsonr, kendalltau |
| import argparse |
| import matplotlib.pyplot as plt |
|
|
| def load_txt_scores(txt_path): |
| """从TXT文件加载主观评分""" |
| mos_scores = [] |
| video_paths = [] |
| |
| with open(txt_path, 'r') as f: |
| lines = f.readlines() |
| |
| for line in lines: |
| parts = line.strip().split(',') |
| if len(parts) >= 2: |
| video_path = parts[0] |
| try: |
| score = float(parts[1]) |
| video_paths.append(video_path) |
| mos_scores.append(score) |
| except ValueError: |
| print(f"警告: 无法解析分数: {parts[1]}") |
| |
| return video_paths, mos_scores |
|
|
| def load_csv_scores(csv_path): |
| """从CSV文件加载CLIP得分""" |
| df = pd.read_csv(csv_path) |
| |
| |
| if 'clip_score' not in df.columns: |
| print(f"错误: CSV文件中没有找到'clip_score'列。可用列: {df.columns.tolist()}") |
| sys.exit(1) |
| |
| video_paths = df['video_path'].tolist() |
| clip_scores = df['clip_score'].tolist() |
| |
| return video_paths, clip_scores |
|
|
| def compute_correlations(mos_scores, clip_scores): |
| """计算相关系数""" |
| srcc, p_srcc = spearmanr(mos_scores, clip_scores) |
| plcc, p_plcc = pearsonr(mos_scores, clip_scores) |
| krcc, p_krcc = kendalltau(mos_scores, clip_scores) |
| |
| return { |
| 'SRCC': (srcc, p_srcc), |
| 'PLCC': (plcc, p_plcc), |
| 'KRCC': (krcc, p_krcc) |
| } |
|
|
| def draw_scatter_plot(mos_scores, clip_scores, output_path=None): |
| """绘制散点图并计算相关系数""" |
| plt.figure(figsize=(10, 6)) |
| plt.scatter(mos_scores, clip_scores, alpha=0.5) |
| |
| |
| z = np.polyfit(mos_scores, clip_scores, 1) |
| p = np.poly1d(z) |
| plt.plot(mos_scores, p(mos_scores), "r--", alpha=0.7) |
| |
| |
| corrs = compute_correlations(mos_scores, clip_scores) |
| |
| plt.xlabel('主观评分 (MOS)') |
| plt.ylabel('CLIP相似度得分') |
| plt.title(f'主观评分与CLIP得分相关性\nSRCC: {corrs["SRCC"][0]:.4f}, PLCC: {corrs["PLCC"][0]:.4f}, KRCC: {corrs["KRCC"][0]:.4f}') |
| plt.grid(alpha=0.3) |
| |
| if output_path: |
| plt.savefig(output_path, dpi=300, bbox_inches='tight') |
| print(f"散点图已保存至: {output_path}") |
| else: |
| plt.show() |
| |
| plt.close() |
|
|
| def main(): |
| parser = argparse.ArgumentParser(description='计算主观评分和CLIP得分的相关系数') |
| parser.add_argument('--txt_path', type=str, required=True, help='TXT文件路径,包含主观评分') |
| parser.add_argument('--csv_path', type=str, required=True, help='CSV文件路径,包含CLIP得分') |
| parser.add_argument('--output_path', type=str, default=None, help='输出结果的文件路径') |
| parser.add_argument('--plot', action='store_true', help='生成散点图') |
| args = parser.parse_args() |
| |
| |
| print(f"读取TXT文件: {args.txt_path}") |
| txt_videos, mos_scores = load_txt_scores(args.txt_path) |
| |
| print(f"读取CSV文件: {args.csv_path}") |
| csv_videos, clip_scores = load_csv_scores(args.csv_path) |
| |
| |
| print(f"TXT文件中的数据数量: {len(txt_videos)}") |
| print(f"CSV文件中的数据数量: {len(csv_videos)}") |
| |
| if len(txt_videos) != len(csv_videos): |
| print("警告: TXT和CSV文件中的数据数量不匹配。") |
| print("尝试基于视频路径匹配数据...") |
| |
| |
| matching_data = [] |
| for i, txt_video in enumerate(txt_videos): |
| for j, csv_video in enumerate(csv_videos): |
| if txt_video in csv_video or csv_video in txt_video: |
| matching_data.append((mos_scores[i], clip_scores[j])) |
| break |
| |
| if not matching_data: |
| print("错误: 无法匹配TXT和CSV文件中的数据。请确保视频路径兼容。") |
| sys.exit(1) |
| |
| |
| matched_mos, matched_clip = zip(*matching_data) |
| mos_scores = matched_mos |
| clip_scores = matched_clip |
| print(f"找到 {len(matching_data)} 对匹配的数据。") |
| |
| |
| corrs = compute_correlations(mos_scores, clip_scores) |
| |
| |
| print("\n相关系数分析结果:") |
| print(f"SRCC: {corrs['SRCC'][0]:.4f} (p-value: {corrs['SRCC'][1]:.4f})") |
| print(f"PLCC: {corrs['PLCC'][0]:.4f} (p-value: {corrs['PLCC'][1]:.4f})") |
| print(f"KRCC: {corrs['KRCC'][0]:.4f} (p-value: {corrs['KRCC'][1]:.4f})") |
| |
| |
| if args.output_path: |
| with open(args.output_path, 'w') as f: |
| f.write(f"总数据点: {len(mos_scores)}\n") |
| f.write(f"SRCC: {corrs['SRCC'][0]:.4f} (p-value: {corrs['SRCC'][1]:.4f})\n") |
| f.write(f"PLCC: {corrs['PLCC'][0]:.4f} (p-value: {corrs['PLCC'][1]:.4f})\n") |
| f.write(f"KRCC: {corrs['KRCC'][0]:.4f} (p-value: {corrs['KRCC'][1]:.4f})\n") |
| print(f"结果已保存至: {args.output_path}") |
| |
| |
| if args.plot: |
| plot_path = args.output_path.replace('.txt', '.png') if args.output_path else 'correlation_plot.png' |
| draw_scatter_plot(mos_scores, clip_scores, plot_path) |
|
|
| if __name__ == "__main__": |
| main() |