Jabrave commited on
Commit
274ebbb
·
verified ·
1 Parent(s): ad9608e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -17
app.py CHANGED
@@ -2,6 +2,7 @@ from detect_face import detect_face
2
  from transformers import AutoModelForImageClassification
3
  from transformers import AutoImageProcessor
4
  from PIL import Image
 
5
  import torch
6
  import gradio as gr
7
  from extract_frames import extract_frames
@@ -26,31 +27,73 @@ print("Loaded labels:", id2label)
26
 
27
  def predict(image):
28
 
29
- # แปลงเป็น PIL Image
30
- image = Image.fromarray(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # preprocess
33
- inputs = processor(images=image, return_tensors="pt")
34
 
35
- # inference
36
- with torch.no_grad():
37
- outputs = model(**inputs)
 
 
 
 
 
 
38
 
39
- # softmax
40
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
 
41
 
42
- # class ที่มั่นใจสุด
43
- pred = probs.argmax().item()
 
 
 
 
 
 
44
 
45
- # confidence
46
- confidence = probs[0][pred].item()
 
47
 
48
- # label จริงจาก model
49
- label = id2label[pred]
50
 
51
  return {
52
- "label": label,
53
- "confidence": round(confidence * 100, 2)
 
 
 
54
  }
55
 
56
  def predict_video(video_path):
 
2
  from transformers import AutoModelForImageClassification
3
  from transformers import AutoImageProcessor
4
  from PIL import Image
5
+ import tempfile
6
  import torch
7
  import gradio as gr
8
  from extract_frames import extract_frames
 
27
 
28
  def predict(image):
29
 
30
+ # ----------------------
31
+ # save temp image
32
+ # ----------------------
33
+ temp_path = "temp_image.jpg"
34
+
35
+ Image.fromarray(image).save(temp_path)
36
+
37
+ # ----------------------
38
+ # วิเคราะห์ทั้งภาพ
39
+ # ----------------------
40
+ full_result = predict_image(temp_path)
41
+
42
+ # ----------------------
43
+ # detect face
44
+ # ----------------------
45
+ os.makedirs("faces", exist_ok=True)
46
+
47
+ faces = detect_face(temp_path)
48
+
49
+ face_scores = []
50
+ fake_face_found = False
51
+
52
+ for face_path in faces:
53
+
54
+ face_result = predict_image(face_path)
55
+
56
+ face_scores.append(face_result["confidence"])
57
 
58
+ if face_result["label"] != "real":
59
+ fake_face_found = True
60
 
61
+ # ----------------------
62
+ # combine score
63
+ # ----------------------
64
+ full_score = full_result["confidence"]
65
+
66
+ avg_face_score = (
67
+ sum(face_scores) / len(face_scores)
68
+ if face_scores else full_score
69
+ )
70
 
71
+ final_score = (
72
+ full_score + avg_face_score
73
+ ) / 2
74
 
75
+ final_label = (
76
+ "artificial"
77
+ if (
78
+ full_result["label"] != "real"
79
+ or fake_face_found
80
+ )
81
+ else "real"
82
+ )
83
 
84
+ # cleanup
85
+ if os.path.exists(temp_path):
86
+ os.remove(temp_path)
87
 
88
+ if os.path.exists("faces"):
89
+ shutil.rmtree("faces")
90
 
91
  return {
92
+ "label": final_label,
93
+ "final_score": round(final_score, 2),
94
+ "full_image_score": round(full_score, 2),
95
+ "face_score": round(avg_face_score, 2),
96
+ "faces_detected": len(faces)
97
  }
98
 
99
  def predict_video(video_path):