-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt
47 lines (38 loc) · 1.48 KB
/
gpt
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
from PIL import Image
import numpy as np
import os
def iou_folder(gt_folder, pred_folder):
# 获得groundtruth和predictedmask文件夹中的所有图像文件名
gt_files = os.listdir(gt_folder)
pred_files = os.listdir(pred_folder)
# 初始化IOU值和计数器
iou_sum = 0
count = 0
# 循环遍历groundtruth和predictedmask文件夹中的所有图像
for file in gt_files:
if file in pred_files:
# 加载groundtruth图像
gt_image = Image.open(os.path.join(gt_folder, file)).convert('1')
gt_array = np.array(gt_image).astype(np.bool)
# 加载predictedmask图像
pred_image = Image.open(os.path.join(pred_folder, file))
pred_array = np.array(pred_image).astype(np.bool)
# 计算IOU值
intersection = np.logical_and(gt_array, pred_array)
intersection_area = intersection.sum()
union = np.logical_or(gt_array, pred_array)
union_area = union.sum()
iou_score = intersection_area / union_area
# 更新IOU值和计数器
iou_sum += iou_score
count += 1
# 计算平均IOU值
avg_iou = iou_sum / count
# groundtruth和predictedmask文件夹的路径
gt_folder = 'groundtruth_folder'
pred_folder = 'predictedmask_folder'
# 计算平均IOU值
avg_iou = iou_folder(gt_folder, pred_folder)
# 输出结果
print(avg_iou)
return avg_iou