Views
No views yet
1from transformers import ViTImageProcessor, ViTForImageClassification
2from PIL import Image
3import torch
4
5# تحميل النموذج والمعالج
6processor = ViTImageProcessor.from_pretrained("C:/Users/SUPREME TECH/Desktop/SAM3/ai-image-detector")
7model = ViTForImageClassification.from_pretrained("C:/Users/SUPREME TECH/Desktop/SAM3/ai-image-detector")
8
9def detect_image(image_path):
10 # فتح وتجهيز الصورة
11 image = Image.open(image_path)
12 if image.mode != 'RGB':
13 image = image.convert('RGB')
14
15 # معالجة الصورة
16 inputs = processor(images=image, return_tensors="pt")
17
18 # الحصول على التنبؤات
19 with torch.no_grad():
20 outputs = model(**inputs)
21 predictions = outputs.logits.softmax(dim=-1)
22
23 # تحليل النتائج
24 scores = predictions[0].tolist()
25 results = [
26 {"label": "REAL", "score": scores[0]},
27 {"label": "FAKE", "score": scores[1]}
28 ]
29
30 # ترتيب النتائج حسب درجة الثقة
31 results.sort(key=lambda x: x["score"], reverse=True)
32
33 return {
34 "prediction": results[0]["label"],
35 "confidence": f"{results[0]['score']*100:.2f}%",
36 "detailed_scores": [
37 f"{r['label']}: {r['score']*100:.2f}%"
38 for r in results
39 ]
40 }
41
42# كود للاختبار
43if __name__ == "__main__":
44 # يمكنك تغيير مسار الصورة هنا
45 image_path = "path/to/your/image.jpg"
46
47 try:
48 result = detect_image(image_path)
49 print("\nنتائج تحليل الصورة:")
50 print(f"التصنيف: {result['prediction']}")
51 print(f"درجة الثقة: {result['confidence']}")
52 print("\nالتفاصيل:")
53 for score in result['detailed_scores']:
54 print(f"- {score}")
55
56 except Exception as e:
57 print(f"حدث خطأ: {str(e)}")transformers>=4.30.0torch>=2.0.0Pillow>=9.0.01async function detectImage(imageFile) {
2 const formData = new FormData();
3 formData.append('image', imageFile);
4
5 const response = await fetch('YOUR_API_ENDPOINT', {
6 method: 'POST',
7 body: formData
8 });
9
10 return await response.json();
11}