PSNbst commited on
Commit
d3eede8
·
verified ·
1 Parent(s): f916288

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -13,6 +13,9 @@ clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
13
  blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
14
  blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
15
 
 
 
 
16
  # 图像处理函数
17
  def compute_difference_images(img_a, img_b):
18
  def extract_sketch(image):
@@ -37,10 +40,10 @@ def compute_difference_images(img_a, img_b):
37
  }
38
 
39
  # 保存图像到文件
40
- def save_images(images):
41
  paths = []
42
  for key, img in images.items():
43
- path = f"{key}.png"
44
  img.save(path)
45
  paths.append((path, key.replace("_", " ").capitalize()))
46
  return paths
@@ -52,23 +55,29 @@ def generate_detailed_caption(image):
52
  return blip_processor.decode(caption[0], skip_special_tokens=True)
53
 
54
  # 特征差异可视化
55
- def plot_feature_differences(latent_diff):
56
  diff_magnitude = [abs(x) for x in latent_diff[0]]
57
  indices = range(len(diff_magnitude))
 
58
 
59
  plt.figure(figsize=(8, 4))
60
  plt.bar(indices, diff_magnitude, alpha=0.7)
61
  plt.xlabel("Feature Index (Latent Dimension)")
62
  plt.ylabel("Magnitude of Difference")
63
  plt.title("Feature Differences (Bar Chart)")
64
- bar_chart_path = "bar_chart.png"
65
  plt.savefig(bar_chart_path)
66
  plt.close()
67
 
68
  plt.figure(figsize=(6, 6))
69
- plt.pie(diff_magnitude[:10], labels=[f"Feature {i}" for i in range(10)], autopct="%1.1f%%", startangle=140)
 
 
 
 
 
70
  plt.title("Top 10 Feature Differences (Pie Chart)")
71
- pie_chart_path = "pie_chart.png"
72
  plt.savefig(pie_chart_path)
73
  plt.close()
74
 
@@ -88,13 +97,12 @@ def generate_text_analysis(api_key, api_type, caption_a, caption_b):
88
  {"role": "user", "content": f"图片A的描述为:{caption_a}。图片B的描述为:{caption_b}。\n请对两张图片的内容和潜在特征区别进行详细分析,并输出一个简洁但富有条理的总结。"}
89
  ]
90
  )
91
- # 修复: 正确访问返回值
92
  return response.choices[0].message.content.strip()
93
 
94
  # 分析函数
95
- def analyze_images(img_a, img_b, api_key, api_type):
96
  images_diff = compute_difference_images(img_a, img_b)
97
- saved_images = save_images(images_diff)
98
 
99
  caption_a = generate_detailed_caption(img_a)
100
  caption_b = generate_detailed_caption(img_b)
@@ -107,7 +115,7 @@ def analyze_images(img_a, img_b, api_key, api_type):
107
 
108
  latent_diff = np.abs(features_a - features_b).tolist()
109
 
110
- bar_chart, pie_chart = plot_feature_differences(latent_diff)
111
  text_analysis = generate_text_analysis(api_key, api_type, caption_a, caption_b)
112
 
113
  return {
@@ -125,7 +133,8 @@ def batch_analyze(images_a, images_b, api_key, api_type):
125
 
126
  results = []
127
  for i in range(num_pairs):
128
- result = analyze_images(images_a[i], images_b[i], api_key, api_type)
 
129
  results.append({
130
  "pair": (f"Image A-{i+1}", f"Image B-{i+1}"),
131
  **result
 
13
  blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
14
  blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
15
 
16
+ # 定义CLIP特征的名称(假设的特征名称,您可以根据需要调整)
17
+ CLIP_FEATURE_NAMES = [f"Dimension {i}" for i in range(512)]
18
+
19
  # 图像处理函数
20
  def compute_difference_images(img_a, img_b):
21
  def extract_sketch(image):
 
40
  }
41
 
42
  # 保存图像到文件
43
+ def save_images(images, prefix):
44
  paths = []
45
  for key, img in images.items():
46
+ path = f"{prefix}_{key}.png"
47
  img.save(path)
48
  paths.append((path, key.replace("_", " ").capitalize()))
49
  return paths
 
55
  return blip_processor.decode(caption[0], skip_special_tokens=True)
56
 
57
  # 特征差异可视化
58
+ def plot_feature_differences(latent_diff, prefix):
59
  diff_magnitude = [abs(x) for x in latent_diff[0]]
60
  indices = range(len(diff_magnitude))
61
+ top_indices = np.argsort(diff_magnitude)[-10:][::-1] # Top 10 differences
62
 
63
  plt.figure(figsize=(8, 4))
64
  plt.bar(indices, diff_magnitude, alpha=0.7)
65
  plt.xlabel("Feature Index (Latent Dimension)")
66
  plt.ylabel("Magnitude of Difference")
67
  plt.title("Feature Differences (Bar Chart)")
68
+ bar_chart_path = f"{prefix}_bar_chart.png"
69
  plt.savefig(bar_chart_path)
70
  plt.close()
71
 
72
  plt.figure(figsize=(6, 6))
73
+ plt.pie(
74
+ [diff_magnitude[i] for i in top_indices],
75
+ labels=[CLIP_FEATURE_NAMES[i] for i in top_indices],
76
+ autopct="%1.1f%%",
77
+ startangle=140
78
+ )
79
  plt.title("Top 10 Feature Differences (Pie Chart)")
80
+ pie_chart_path = f"{prefix}_pie_chart.png"
81
  plt.savefig(pie_chart_path)
82
  plt.close()
83
 
 
97
  {"role": "user", "content": f"图片A的描述为:{caption_a}。图片B的描述为:{caption_b}。\n请对两张图片的内容和潜在特征区别进行详细分析,并输出一个简洁但富有条理的总结。"}
98
  ]
99
  )
 
100
  return response.choices[0].message.content.strip()
101
 
102
  # 分析函数
103
+ def analyze_images(img_a, img_b, api_key, api_type, prefix):
104
  images_diff = compute_difference_images(img_a, img_b)
105
+ saved_images = save_images(images_diff, prefix)
106
 
107
  caption_a = generate_detailed_caption(img_a)
108
  caption_b = generate_detailed_caption(img_b)
 
115
 
116
  latent_diff = np.abs(features_a - features_b).tolist()
117
 
118
+ bar_chart, pie_chart = plot_feature_differences(latent_diff, prefix)
119
  text_analysis = generate_text_analysis(api_key, api_type, caption_a, caption_b)
120
 
121
  return {
 
133
 
134
  results = []
135
  for i in range(num_pairs):
136
+ prefix = f"comparison_{i+1}"
137
+ result = analyze_images(images_a[i], images_b[i], api_key, api_type, prefix)
138
  results.append({
139
  "pair": (f"Image A-{i+1}", f"Image B-{i+1}"),
140
  **result