Update README.md
Browse files
README.md
CHANGED
@@ -23,6 +23,9 @@ import torch
|
|
23 |
processor = ViTImageProcessor.from_pretrained("yaya36095/ai-image-detector")
|
24 |
model = ViTForImageClassification.from_pretrained("yaya36095/ai-image-detector")
|
25 |
|
|
|
|
|
|
|
26 |
def detect_image(image_path):
|
27 |
# Open image
|
28 |
image = Image.open(image_path)
|
@@ -35,16 +38,22 @@ def detect_image(image_path):
|
|
35 |
outputs = model(**inputs)
|
36 |
predictions = outputs.logits.softmax(dim=-1)
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
return result, confidence
|
44 |
|
45 |
# Example usage
|
46 |
-
# result
|
47 |
-
#
|
|
|
|
|
48 |
```
|
49 |
|
50 |
## Classes
|
|
|
23 |
processor = ViTImageProcessor.from_pretrained("yaya36095/ai-image-detector")
|
24 |
model = ViTForImageClassification.from_pretrained("yaya36095/ai-image-detector")
|
25 |
|
26 |
+
# Define class labels (Replace these with the labels used during training)
|
27 |
+
class_labels = ["Real Image", "AI Generated"]
|
28 |
+
|
29 |
def detect_image(image_path):
|
30 |
# Open image
|
31 |
image = Image.open(image_path)
|
|
|
38 |
outputs = model(**inputs)
|
39 |
predictions = outputs.logits.softmax(dim=-1)
|
40 |
|
41 |
+
# Map predictions to class labels
|
42 |
+
results = [
|
43 |
+
{"label": class_labels[i], "confidence": f"{(predictions[0][i].item() * 100):.2f}%"}
|
44 |
+
for i in range(len(class_labels))
|
45 |
+
]
|
46 |
+
|
47 |
+
# Sort results by confidence (optional)
|
48 |
+
results = sorted(results, key=lambda x: float(x["confidence"].strip('%')), reverse=True)
|
49 |
|
50 |
+
return results
|
|
|
51 |
|
52 |
# Example usage
|
53 |
+
# result = detect_image("path/to/image.jpg")
|
54 |
+
# for r in result:
|
55 |
+
# print(f"Class: {r['label']}, Confidence: {r['confidence']}")
|
56 |
+
|
57 |
```
|
58 |
|
59 |
## Classes
|