Prathmesh Patil commited on
Commit
3dad336
·
verified ·
1 Parent(s): 925a79f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -42
app.py CHANGED
@@ -6,10 +6,10 @@ from keras.models import load_model
6
  import cv2 as cv
7
 
8
  # Load the trained model
9
- model = load_model('FaceAuthenticator.keras')
10
 
11
  # Load the pre-trained face detection model with error handling
12
- face_cascade = cv.CascadeClassifier('img_for_deepfake_detection\\hass_face.xml')
13
 
14
  # Define a function to preprocess the input image
15
  def preprocess_image(image_path):
@@ -21,59 +21,46 @@ def preprocess_image(image_path):
21
  return img_array
22
 
23
  # Define a function to classify the input image
24
- def classify_image(image_path):
25
- # Preprocess the image
26
- img_array = preprocess_image(image_path)
 
 
27
 
28
- # Convert the image to grayscale
29
- gray_image = cv.cvtColor(cv.imread(image_path), cv.COLOR_BGR2GRAY)
30
 
31
- # Detect faces in the image
32
- faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
33
 
34
- # Check if any faces were detected
35
- if len(faces) == 0:
36
- return "No faces detected in the input image."
37
- else:
38
- # Make predictions
39
- prediction = model.predict(img_array)
40
 
41
- # Return the prediction
42
- if prediction[0][0] > 0.5:
43
- return "The image is classified as real."
44
  else:
45
- return "The image is classified as fake."
 
 
 
 
 
 
 
 
 
46
 
47
  # Create the Gradio interface
48
  demo = gr.Interface(
49
  fn=classify_image,
50
- inputs=gr.Image(type="file", label="Upload Image"),
51
  outputs=gr.Textbox(label="Prediction"),
52
  title="DeepFake Image Detection",
53
  description="Upload an image and the model will classify it as real or fake.",
54
  theme="default",
55
- layout="vertical",
56
- css="""
57
- .gradio-container {
58
- font-family: 'Roboto', sans-serif;
59
- }
60
- .gradio-input, .gradio-output {
61
- border: 1px solid #ccc;
62
- border-radius: 4px;
63
- padding: 10px;
64
- font-size: 16px;
65
- }
66
- .gradio-button {
67
- background-color: #4CAF50;
68
- color: white;
69
- border: none;
70
- border-radius: 4px;
71
- padding: 10px 20px;
72
- font-size: 16px;
73
- cursor: pointer;
74
- }
75
- """
76
  )
77
 
78
  # Launch the Gradio app
79
- demo.launch()
 
6
  import cv2 as cv
7
 
8
  # Load the trained model
9
+ model = load_model('fake_real_face_classification_model.h5')
10
 
11
  # Load the pre-trained face detection model with error handling
12
+ face_cascade = cv.CascadeClassifier('hass_face.xml')
13
 
14
  # Define a function to preprocess the input image
15
  def preprocess_image(image_path):
 
21
  return img_array
22
 
23
  # Define a function to classify the input image
24
+ def classify_image(image_data):
25
+ try:
26
+ # Save the uploaded image temporarily
27
+ temp_image_path = "temp_image.jpg"
28
+ image_data.save(temp_image_path)
29
 
30
+ # Preprocess the image
31
+ img_array = preprocess_image(temp_image_path)
32
 
33
+ # Convert the image to grayscale
34
+ gray_image = cv.cvtColor(cv.imread(temp_image_path), cv.COLOR_BGR2GRAY)
35
 
36
+ # Detect faces in the image
37
+ faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
 
 
 
 
38
 
39
+ # Check if any faces were detected
40
+ if len(faces) == 0:
41
+ return "No faces detected in the input image."
42
  else:
43
+ # Make predictions
44
+ prediction = model.predict(img_array)
45
+
46
+ # Return the prediction
47
+ if prediction[0][0] > 0.5:
48
+ return "The image is classified as real."
49
+ else:
50
+ return "The image is classified as fake."
51
+ except Exception as e:
52
+ return f"An error occurred: {str(e)}"
53
 
54
  # Create the Gradio interface
55
  demo = gr.Interface(
56
  fn=classify_image,
57
+ inputs=gr.Image(type="pil", label="Upload Image"),
58
  outputs=gr.Textbox(label="Prediction"),
59
  title="DeepFake Image Detection",
60
  description="Upload an image and the model will classify it as real or fake.",
61
  theme="default",
62
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  )
64
 
65
  # Launch the Gradio app
66
+ demo.launch(share=True)