Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -534,6 +534,7 @@ def analyze_clothing_details(image):
|
|
534 |
|
535 |
# 计算纹理变化的熵 - 真实衣物纹理变化的熵更高
|
536 |
features["clothing_texture_entropy"] = float(stats.entropy(texture_variations + 1e-10))
|
|
|
537 |
# 褶皱分析 - 使用形态学操作提取可能的褶皱
|
538 |
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
539 |
kernel = np.ones((3,3), np.uint8)
|
@@ -661,19 +662,19 @@ def check_ai_specific_features(image_features):
|
|
661 |
# 增加对微观纹理的分析权重
|
662 |
if "lbp_entropy" in image_features:
|
663 |
if image_features["lbp_entropy"] < 2.0:
|
664 |
-
ai_score += 0.
|
665 |
ai_signs.append("微观纹理熵极低,典型AI生成特征")
|
666 |
elif image_features["lbp_entropy"] < 3.0:
|
667 |
-
ai_score += 0.
|
668 |
ai_signs.append("微观纹理熵异常低")
|
669 |
|
670 |
# 增加对频率分布各向异性的分析权重
|
671 |
if "freq_anisotropy" in image_features:
|
672 |
if image_features["freq_anisotropy"] < 0.05:
|
673 |
-
ai_score += 0.
|
674 |
ai_signs.append("频率分布各向异性极低,典型AI生成特征")
|
675 |
elif image_features["freq_anisotropy"] < 0.5:
|
676 |
-
ai_score += 0.
|
677 |
ai_signs.append("频率分布各向异性异常低")
|
678 |
|
679 |
# 增加对细节一致性的分析
|
@@ -960,8 +961,8 @@ def detect_photoshop_signs(image_features):
|
|
960 |
|
961 |
return min(ps_score, 1.0), ps_signs
|
962 |
|
963 |
-
def get_detailed_analysis(ai_probability, ps_score, beauty_score, ps_signs, ai_signs, beauty_signs, valid_models_count):
|
964 |
-
"""
|
965 |
|
966 |
# 根据有效模型数量调整置信度描述
|
967 |
confidence_prefix = ""
|
@@ -972,6 +973,25 @@ def get_detailed_analysis(ai_probability, ps_score, beauty_score, ps_signs, ai_s
|
|
972 |
elif valid_models_count == 1:
|
973 |
confidence_prefix = "中等置信度:"
|
974 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
975 |
# 第一级分类:AI生成 vs 真人照片
|
976 |
if ai_probability > 0.6: # 降低AI判定阈值,提高AI检出率
|
977 |
category = confidence_prefix + "AI生成图像"
|
@@ -1026,7 +1046,7 @@ def detect_ai_image(image):
|
|
1026 |
valid_models = 0
|
1027 |
weighted_ai_probability = 0
|
1028 |
|
1029 |
-
|
1030 |
for key, model_info in models.items():
|
1031 |
if model_info["processor"] is not None and model_info["model"] is not None:
|
1032 |
try:
|
@@ -1064,7 +1084,8 @@ def detect_ai_image(image):
|
|
1064 |
final_ai_probability = weighted_ai_probability / sum(m["weight"] for k, m in models.items() if m["processor"] is not None and m["model"] is not None)
|
1065 |
else:
|
1066 |
return {"error": "所有模型加载失败"}
|
1067 |
-
|
|
|
1068 |
image_features = analyze_image_features(image)
|
1069 |
|
1070 |
# 检查AI特定特征
|
@@ -1125,19 +1146,11 @@ def detect_ai_image(image):
|
|
1125 |
# 确保概率在0-1范围内
|
1126 |
adjusted_probability = min(1.0, max(0.0, adjusted_probability))
|
1127 |
|
1128 |
-
# 如果umm-maybe/AI-image-detector模型的预测与其他模型不一致,增加其权重
|
1129 |
-
if "model1" in results and "ai_probability" in results["model1"]:
|
1130 |
-
ai_detector_prob = results["model1"]["ai_probability"]
|
1131 |
-
# 如果专用AI检测器给出的概率与调整后概率差异大,增加其权重
|
1132 |
-
if abs(ai_detector_prob - adjusted_probability) > 0.3:
|
1133 |
-
adjusted_probability = (adjusted_probability + ai_detector_prob * 2) / 3
|
1134 |
-
|
1135 |
# 获取详细分析
|
1136 |
category, description, ps_details, ai_details, beauty_details, main_category = get_detailed_analysis(
|
1137 |
-
adjusted_probability, ps_score, beauty_score, ps_signs, ai_signs, beauty_signs, valid_models
|
1138 |
)
|
1139 |
|
1140 |
-
# 构建最终结果
|
1141 |
# 构建最终结果
|
1142 |
final_result = {
|
1143 |
"ai_probability": adjusted_probability,
|
@@ -1155,10 +1168,8 @@ def detect_ai_image(image):
|
|
1155 |
"features": image_features
|
1156 |
}
|
1157 |
|
1158 |
-
# 为Label组件准备数据
|
1159 |
-
label_data = {main_category: 1.0}
|
1160 |
-
|
1161 |
# 返回两个值:JSON结果和Label数据
|
|
|
1162 |
return final_result, label_data
|
1163 |
|
1164 |
# 创建Gradio界面
|
|
|
534 |
|
535 |
# 计算纹理变化的熵 - 真实衣物纹理变化的熵更高
|
536 |
features["clothing_texture_entropy"] = float(stats.entropy(texture_variations + 1e-10))
|
537 |
+
|
538 |
# 褶皱分析 - 使用形态学操作提取可能的褶皱
|
539 |
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
540 |
kernel = np.ones((3,3), np.uint8)
|
|
|
662 |
# 增加对微观纹理的分析权重
|
663 |
if "lbp_entropy" in image_features:
|
664 |
if image_features["lbp_entropy"] < 2.0:
|
665 |
+
ai_score += 0.4 # 提高权重
|
666 |
ai_signs.append("微观纹理熵极低,典型AI生成特征")
|
667 |
elif image_features["lbp_entropy"] < 3.0:
|
668 |
+
ai_score += 0.3
|
669 |
ai_signs.append("微观纹理熵异常低")
|
670 |
|
671 |
# 增加对频率分布各向异性的分析权重
|
672 |
if "freq_anisotropy" in image_features:
|
673 |
if image_features["freq_anisotropy"] < 0.05:
|
674 |
+
ai_score += 0.4 # 提高权重
|
675 |
ai_signs.append("频率分布各向异性极低,典型AI生成特征")
|
676 |
elif image_features["freq_anisotropy"] < 0.5:
|
677 |
+
ai_score += 0.3
|
678 |
ai_signs.append("频率分布各向异性异常低")
|
679 |
|
680 |
# 增加对细节一致性的分析
|
|
|
961 |
|
962 |
return min(ps_score, 1.0), ps_signs
|
963 |
|
964 |
+
def get_detailed_analysis(ai_probability, ps_score, beauty_score, ps_signs, ai_signs, beauty_signs, valid_models_count, ai_feature_score):
|
965 |
+
"""提供更详细的分析结果,使用二级分类框架,优先考虑AI特征分析"""
|
966 |
|
967 |
# 根据有效模型数量调整置信度描述
|
968 |
confidence_prefix = ""
|
|
|
973 |
elif valid_models_count == 1:
|
974 |
confidence_prefix = "中等置信度:"
|
975 |
|
976 |
+
# 特征与模型判断严重不一致时的处理
|
977 |
+
if ai_feature_score > 0.8 and ai_probability < 0.6:
|
978 |
+
ai_probability = max(0.8, ai_probability) # 当AI特征分数非常高时,覆盖模型判断
|
979 |
+
category = confidence_prefix + "AI生成图像(基于特征分析)"
|
980 |
+
description = "基于多种典型AI特征分析,该图像很可能是AI生成的,尽管模型判断结果不确定。"
|
981 |
+
main_category = "AI生成"
|
982 |
+
elif ai_feature_score > 0.6 and ai_probability < 0.5:
|
983 |
+
ai_probability = max(0.7, ai_probability) # 当AI特征分数高时,提高AI概率
|
984 |
+
|
985 |
+
# 特定关键特征的硬性覆盖
|
986 |
+
if "lbp_entropy" in image_features and image_features["lbp_entropy"] < 2.0:
|
987 |
+
if "freq_anisotropy" in image_features and image_features["freq_anisotropy"] < 0.05:
|
988 |
+
# 当微观纹理熵极低且频率分布各向异性极低时,几乎可以确定是AI生成
|
989 |
+
ai_probability = 0.95
|
990 |
+
category = confidence_prefix + "AI生成图像(确定)"
|
991 |
+
description = "检测到多个决定性AI生成特征,该图像几乎可以确定是AI生成的。"
|
992 |
+
main_category = "AI生成"
|
993 |
+
return category, description, ps_details, ai_details, beauty_details, main_category
|
994 |
+
|
995 |
# 第一级分类:AI生成 vs 真人照片
|
996 |
if ai_probability > 0.6: # 降低AI判定阈值,提高AI检出率
|
997 |
category = confidence_prefix + "AI生成图像"
|
|
|
1046 |
valid_models = 0
|
1047 |
weighted_ai_probability = 0
|
1048 |
|
1049 |
+
# 使用每个模型进行预测
|
1050 |
for key, model_info in models.items():
|
1051 |
if model_info["processor"] is not None and model_info["model"] is not None:
|
1052 |
try:
|
|
|
1084 |
final_ai_probability = weighted_ai_probability / sum(m["weight"] for k, m in models.items() if m["processor"] is not None and m["model"] is not None)
|
1085 |
else:
|
1086 |
return {"error": "所有模型加载失败"}
|
1087 |
+
|
1088 |
+
# 分析图像特征
|
1089 |
image_features = analyze_image_features(image)
|
1090 |
|
1091 |
# 检查AI特定特征
|
|
|
1146 |
# 确保概率在0-1范围内
|
1147 |
adjusted_probability = min(1.0, max(0.0, adjusted_probability))
|
1148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1149 |
# 获取详细分析
|
1150 |
category, description, ps_details, ai_details, beauty_details, main_category = get_detailed_analysis(
|
1151 |
+
adjusted_probability, ps_score, beauty_score, ps_signs, ai_signs, beauty_signs, valid_models, ai_feature_score
|
1152 |
)
|
1153 |
|
|
|
1154 |
# 构建最终结果
|
1155 |
final_result = {
|
1156 |
"ai_probability": adjusted_probability,
|
|
|
1168 |
"features": image_features
|
1169 |
}
|
1170 |
|
|
|
|
|
|
|
1171 |
# 返回两个值:JSON结果和Label数据
|
1172 |
+
label_data = {main_category: 1.0}
|
1173 |
return final_result, label_data
|
1174 |
|
1175 |
# 创建Gradio界面
|