yaya36095 commited on
Commit
3555163
·
verified ·
1 Parent(s): c5ae317

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +16 -7
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
- # Get result
39
- prediction_id = torch.argmax(predictions).item()
40
- confidence = predictions[0][prediction_id].item() * 100
 
 
 
 
 
41
 
42
- result = "AI Generated" if prediction_id == 1 else "Real Image"
43
- return result, confidence
44
 
45
  # Example usage
46
- # result, confidence = detect_image("path/to/image.jpg")
47
- # print(f"Result: {result} (Confidence: {confidence:.2f}%)")
 
 
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