Rdad commited on
Commit
4d23000
·
1 Parent(s): 3f70a77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -104
app.py CHANGED
@@ -1,108 +1,36 @@
1
- import gradio as gr
2
-
3
- import cv2
4
- from mtcnn.mtcnn import MTCNN
5
- import tensorflow as tf
6
- import tensorflow_addons
7
- import numpy as np
8
-
9
- import os
10
- #import zipfile
11
-
12
-
13
- #local_zip = "FINAL-EFFICIENTNETV2-B0.zip"
14
- #zip_ref = zipfile.ZipFile(local_zip, 'r')
15
- #zip_ref.extractall('FINAL-EFFICIENTNETV2-B0')
16
- #zip_ref.close()
17
-
18
- import h5py
19
-
20
- # افتح ملف النموذج H5
21
- h5_file = h5py.File('model_CNN.h5', 'r')
22
-
23
- # قراءة المحتوى
24
- # يمكنك استكشاف المحتوى باستخدام الأوامر التالية
25
- print(h5_file.keys()) # قائمة المفاتيح في الملف
26
- print(h5_file['model_weights'].keys()) # قائمة المفاتيح في مجموعة الأوزان
27
-
28
- # إنشاء ملف HDF5 جديد
29
- hdf5_file = h5py.File('model.hdf5', 'w')
30
-
31
- # نسخ المحتوى من ملف النموذج H5 إلى الملف الجديد
32
- h5_file.copy('model_weights', hdf5_file)
33
-
34
- # إغلاق الملفات
35
- h5_file.close()
36
- hdf5_file.close()
37
 
38
- model = tf.keras.models.load_model('model.hdf5')
39
 
40
- detector = MTCNN()
41
-
42
- def deepfakespredict(input_img ):
43
-
44
- labels = ['real', 'fake']
45
- pred = [0, 0]
46
- text =""
47
- text2 =""
48
-
49
- face = detector.detect_faces(input_img)
50
-
51
- if len(face) > 0:
52
- x, y, width, height = face[0]['box']
53
- x2, y2 = x + width, y + height
54
-
55
- cv2.rectangle(input_img, (x, y), (x2, y2), (0, 255, 0), 2)
56
-
57
- face_image = input_img[y:y2, x:x2]
58
- face_image2 = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
59
- face_image3 = cv2.resize(face_image2, (224, 224))
60
- face_image4 = face_image3/255
61
-
62
- pred = model.predict(np.expand_dims(face_image4, axis=0))[0]
63
 
64
- if pred[1] >= 0.6:
65
- text = "The image is FAKE."
66
- elif pred[0] >= 0.6:
67
- text = "The image is REAL."
68
- else:
69
- text = "The image may be REAL or FAKE."
70
-
 
 
 
 
 
 
 
 
 
71
  else:
72
- text = "Face is not detected in the image."
73
-
74
- text2 = "REAL: " + str(np.round(pred[0]*100, 2)) + "%, FAKE: " + str(np.round(pred[1]*100, 2)) + "%"
75
-
76
- return input_img, text, text2, {labels[i]: float(pred[i]) for i in range(2)}
77
-
78
-
79
- title="Deepfakes Image Detector"
80
- description=" project Deepfake detection images real and fake call me +967776215118 "
81
-
82
- examples = [
83
- ['Fake-1.png'],
84
- ['Fake-2.png'],
85
- ['Fake-3.png'],
86
- ['Fake-4.png'],
87
- ['Fake-5.png'],
88
-
89
- ['Real-1.png'],
90
- ['Real-2.png'],
91
- ['Real-3.png'],
92
- ['Real-4.png'],
93
- ['Real-5.png']
94
-
95
- ]
96
-
97
-
98
- gr.Interface(deepfakespredict,
99
- inputs = ["image"],
100
- outputs=[gr.outputs.Image(type="pil", label="Detected face"),
101
- "text",
102
- "text",
103
- gr.outputs.Label(num_top_classes=None, type="auto", label="Confidence")],
104
- title=title,
105
- description=description,
106
- examples = examples,
107
- examples_per_page = 5
108
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
2
 
3
+ import tensorflow_hub as hub
4
+ import gradio as gr
5
+ import tensorflow as tf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Load the model
8
+ model = tf.keras.models.load_model('model_CNN')
9
+
10
+ # Preprocess the image
11
+ def preprocess_image(image):
12
+ resized_image = tf.image.resize(image, [224, 224])
13
+ normalized_image = resized_image / 255.0
14
+ return normalized_image
15
+
16
+ # Function to detect fake images using the model
17
+ def detect_fake_image(image):
18
+ processed_image = preprocess_image(image)
19
+ prediction = model.predict(tf.expand_dims(processed_image, 0))
20
+ percentage = prediction[0][0] * 100
21
+ if percentage > 50:
22
+ result = f"Real with {percentage}% confidence"
23
  else:
24
+ result = f"Fake with {100 - percentage}% confidence"
25
+ return result
26
+
27
+ # Gradio Interface
28
+ iface = gr.Interface(
29
+ fn=detect_fake_image,
30
+ inputs="image",
31
+ outputs="text",
32
+ title="Fake Image Detector"
33
+ )
34
+
35
+ # Launch the interface
36
+ iface.launch()