language: zh
tags:
- 图像分类
- AI检测
- ViT
license: mit
AI图像检测器
模型描述
该模型用于检测图像是真实拍摄还是由AI生成,采用Vision Transformer(ViT)架构提供精准分类。
使用方法
from transformers import ViTImageProcessor, ViTForImageClassification
from PIL import Image
import torch
processor = ViTImageProcessor.from_pretrained("C:/Users/SUPREME TECH/Desktop/SAM3/ai-image-detector")
model = ViTForImageClassification.from_pretrained("C:/Users/SUPREME TECH/Desktop/SAM3/ai-image-detector")
def detect_image(image_path):
image = Image.open(image_path)
if image.mode != 'RGB':
image = image.convert('RGB')
inputs = processor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
predictions = outputs.logits.softmax(dim=-1)
scores = predictions[0].tolist()
results = [
{"label": "真实图像", "score": scores[0]},
{"label": "AI生成", "score": scores[1]}
]
results.sort(key=lambda x: x["score"], reverse=True)
return {
"prediction": results[0]["label"],
"confidence": f"{results[0]['score']*100:.2f}%",
"detailed_scores": [
f"{r['label']}: {r['score']*100:.2f}%"
for r in results
]
}
if __name__ == "__main__":
image_path = "path/to/your/image.jpg"
try:
result = detect_image(image_path)
print("\n图像分析结果:")
print(f"分类结果: {result['prediction']}")
print(f"置信度: {result['confidence']}")
print("\n详细分数:")
for score in result['detailed_scores']:
print(f"- {score}")
except Exception as e:
print(f"发生错误: {str(e)}")
分类类别
模型将图像分为两类:
- 真实图像 (0): 非AI生成的真实拍摄图像
- AI生成 (1): 由人工智能生成的图像
技术细节
- 模型架构: Vision Transformer (ViT)
- 输入格式: RGB图像
- 输出结果: 二分类置信度分数
- 最大图像尺寸: 224x224(自动调整)
依赖要求
transformers>=4.30.0
torch>=2.0.0
Pillow>=9.0.0
局限性
- 对清晰高质量图像效果最佳
- 对重度编辑的照片可能准确率下降
- 适用于通用图像检测场景
网页集成示例
async function detectImage(imageFile) {
const formData = new FormData();
formData.append('image', imageFile);
const response = await fetch('您的API端点', {
method: 'POST',
body: formData
});
return await response.json();
}
开发者信息