import os
import pickle
def compare_results_by_filename(file1, file2, threshold):
# 載入兩個pickle檔案
with open(file1, 'rb') as f:
results1 = pickle.load(f)
with open(file2, 'rb') as f:
results2 = pickle.load(f)
# 提取每個結果集中圖片的基本名字
filenames1 = {os.path.splitext(os.path.basename(path))[0]: counts for path, counts in results1.items()}
filenames2 = {os.path.splitext(os.path.basename(path))[0]: counts for path, counts in results2.items()}
# 建立一個空列表來儲存差異大的圖片名稱
significant_differences = []
# 遍歷第一個結果集中的所有圖片名字
for name, counts1 in filenames1.items():
# 嘗試從第二個結果集中找到相同名字的圖片
if name in filenames2:
counts2 = filenames2[name]
# 比較兩張圖片每個扇區的黑色畫素數量
comparison = [abs(c1 - c2) for c1, c2 in zip(counts1, counts2)]
# 檢查是否有扇區的差異超過閾值
if any(d > threshold for d in comparison):
significant_differences.append(name)
return significant_differences
def batch_compare_directories(dir1, dir2, threshold):
# 確保兩個目錄都存在
assert os.path.exists(dir1), f"Directory {dir1} does not exist."
assert os.path.exists(dir2), f"Directory {dir2} does not exist."
# 獲取兩個目錄中的所有檔名
files1 = set([f for f in os.listdir(dir1) if f.endswith('.pickle')])
files2 = set([f for f in os.listdir(dir2) if f.endswith('.pickle')])
# 確保兩個目錄中都有pickle檔案
assert files1, f"No .pickle files found in {dir1}."
assert files2, f"No .pickle files found in {dir2}."
# 找出兩個目錄中都存在的pickle檔案
common_files = files1.intersection(files2)
# 建立一個空字典來儲存差異大的影像名稱
all_diff_images = {}
# 對於每一對pickle檔案,呼叫compare_results_by_filename函式
for filename in common_files:
file1 = os.path.join(dir1, filename)
file2 = os.path.join(dir2, filename)
# 呼叫函式比較兩個結果檔案
diff_images = compare_results_by_filename(file1, file2, threshold)
# 將差異大的影像名稱新增到字典中,鍵為pickle檔名
if diff_images:
all_diff_images[filename] = diff_images
return all_diff_images
# 設定差異閾值
threshold = 100
# 呼叫函式比較兩個資料夾的結果
diff_images_dict = batch_compare_directories('FOLDER1', 'FOLDER2', threshold)
# 輸出有顯著差異的圖片名稱,按pickle檔案分組
if diff_images_dict:
print("Images with significant differences in black pixel counts, grouped by pickle file:")
for pickle_file, images in diff_images_dict.items():
print(f"\nIn pickle file '{pickle_file}':")
for img in images:
print(img)
else:
print("No significant differences found.")