Jabrave commited on
Commit
7e5c609
·
verified ·
1 Parent(s): 4c69975

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -8
app.py CHANGED
@@ -1,12 +1,11 @@
1
  from detect_face import detect_face
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
9
- from predict_image import predict_image
10
 
11
  import os
12
  import shutil
@@ -16,10 +15,37 @@ model = AutoModelForImageClassification.from_pretrained(
16
  "Jabrave/deepfake-detector"
17
  )
18
 
19
- processor = AutoImageProcessor.from_pretrained(
20
  "Jabrave/deepfake-detector"
21
  )
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # โหลด labels จาก config อัตโนมัติ
24
  id2label = model.config.id2label
25
 
@@ -37,21 +63,39 @@ def predict(image):
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
 
@@ -126,14 +170,25 @@ def predict_video(video_path):
126
  # ----------------------
127
  # วิเคราะห์ทั้งภาพ
128
  # ----------------------
129
- full_result = predict_image(frame_path)
130
 
 
 
 
 
 
131
  full_scores.append(full_result["confidence"])
132
 
133
  # ----------------------
134
  # detect หน้า
135
  # ----------------------
136
- faces = detect_face(frame_path)
 
 
 
 
 
 
137
 
138
  face_fake_found = False
139
 
 
1
  from detect_face import detect_face
2
  from transformers import AutoModelForImageClassification
3
+ from transformers import AutoFeatureExtractor
4
  from PIL import Image
5
  import tempfile
6
  import torch
7
  import gradio as gr
8
  from extract_frames import extract_frames
 
9
 
10
  import os
11
  import shutil
 
15
  "Jabrave/deepfake-detector"
16
  )
17
 
18
+ processor = AutoFeatureExtractor.from_pretrained(
19
  "Jabrave/deepfake-detector"
20
  )
21
 
22
+ face_model = AutoModelForImageClassification.from_pretrained(
23
+ "Jabrave/face-detector"
24
+ )
25
+
26
+ face_processor = AutoFeatureExtractor.from_pretrained(
27
+ "Jabrave/face-detector"
28
+ )
29
+
30
+ def predict_with_model(image, model, processor):
31
+
32
+ inputs = processor(images=image, return_tensors="pt")
33
+
34
+ with torch.no_grad():
35
+ outputs = model(**inputs)
36
+
37
+ logits = outputs.logits
38
+ predicted_class = logits.argmax(-1).item()
39
+
40
+ confidence = torch.softmax(logits, dim=1)[0][predicted_class].item()
41
+
42
+ label = model.config.id2label[predicted_class]
43
+
44
+ return {
45
+ "label": label,
46
+ "confidence": round(confidence * 100, 2)
47
+ }
48
+
49
  # โหลด labels จาก config อัตโนมัติ
50
  id2label = model.config.id2label
51
 
 
63
  # ----------------------
64
  # วิเคราะห์ทั้งภาพ
65
  # ----------------------
66
+ full_image = Image.open(temp_path)
67
+
68
+ full_result = predict_with_model(
69
+ full_image,
70
+ model,
71
+ processor
72
+ )
73
 
74
  # ----------------------
75
  # detect face
76
  # ----------------------
77
  os.makedirs("faces", exist_ok=True)
78
 
79
+ face_image = Image.open(face_path)
80
+
81
+ face_result = predict_with_model(
82
+ face_image,
83
+ face_model,
84
+ face_processor
85
+ )
86
 
87
  face_scores = []
88
  fake_face_found = False
89
 
90
  for face_path in faces:
91
 
92
+ face_image = Image.open(face_path)
93
+
94
+ face_result = predict_with_model(
95
+ face_image,
96
+ face_model,
97
+ face_processor
98
+ )
99
 
100
  face_scores.append(face_result["confidence"])
101
 
 
170
  # ----------------------
171
  # วิเคราะห์ทั้งภาพ
172
  # ----------------------
173
+ frame_image = Image.open(frame_path)
174
 
175
+ full_result = predict_with_model(
176
+ frame_image,
177
+ model,
178
+ processor
179
+ )
180
  full_scores.append(full_result["confidence"])
181
 
182
  # ----------------------
183
  # detect หน้า
184
  # ----------------------
185
+ face_image = Image.open(face_path)
186
+
187
+ face_result = predict_with_model(
188
+ face_image,
189
+ face_model,
190
+ face_processor
191
+ )
192
 
193
  face_fake_found = False
194