Spaces:
Build error
Build error
Commit
·
6ed927a
1
Parent(s):
706638b
Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,53 @@
|
|
1 |
import streamlit as st
|
2 |
-
import torch
|
3 |
-
import torchvision.transforms as transforms
|
4 |
-
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
5 |
-
from PIL import Image
|
6 |
-
import requests
|
7 |
-
from PIL import Image
|
8 |
import cv2
|
9 |
import numpy as np
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
|
|
14 |
|
15 |
-
#
|
16 |
-
cnn_model = torch.hub.load('pytorch/vision:v0.9.0', 'resnet50', pretrained=True)
|
17 |
-
|
18 |
-
# Define the image transform
|
19 |
-
transform = transforms.Compose([
|
20 |
-
transforms.Resize((224, 224)),
|
21 |
-
transforms.ToTensor(),
|
22 |
-
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
23 |
-
])
|
24 |
-
|
25 |
-
# Define the function to predict whether an image is genuine or morphed
|
26 |
-
def predict(image):
|
27 |
-
# Convert the numpy array to PIL Image object
|
28 |
-
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
29 |
-
|
30 |
-
# Preprocess the image
|
31 |
-
image = transform(image)
|
32 |
-
image = image.unsqueeze(0)
|
33 |
-
|
34 |
-
# Predict the class using ViT
|
35 |
-
with torch.no_grad():
|
36 |
-
viT_output = vit_model(image)
|
37 |
-
viT_probs = torch.nn.functional.softmax(viT_output, dim=1)
|
38 |
-
viT_score, viT_pred = torch.max(viT_probs, 1)
|
39 |
-
|
40 |
-
# Predict the class using CNN
|
41 |
-
with torch.no_grad():
|
42 |
-
cnn_output = cnn_model(image)
|
43 |
-
cnn_probs = torch.nn.functional.softmax(cnn_output, dim=1)
|
44 |
-
cnn_score, cnn_pred = torch.max(cnn_probs, 1)
|
45 |
-
|
46 |
-
# Combine the predictions using a weighted average
|
47 |
-
combined_score = 0.7 * viT_score.item() + 0.3 * cnn_score.item()
|
48 |
-
combined_pred = viT_pred.item() if viT_score.item() > cnn_score.item() else cnn_pred.item()
|
49 |
-
return combined_pred, combined_score
|
50 |
-
|
51 |
-
# Define the function to restore an image
|
52 |
def restore(image):
|
|
|
|
|
53 |
# Apply a median blur to the image
|
54 |
image = cv2.medianBlur(image, 5)
|
55 |
return image
|
56 |
|
57 |
-
# Define the function
|
58 |
def enhance(image):
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
lab = cv2.merge((l, a, b))
|
65 |
-
image = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
|
66 |
-
return image
|
67 |
|
68 |
# Define the Streamlit app
|
69 |
def app():
|
70 |
-
|
71 |
-
|
72 |
-
# Upload an image
|
73 |
-
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
74 |
-
|
75 |
-
# Display the uploaded image and perform predictions, restoration, and enhancement
|
76 |
if uploaded_file is not None:
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
83 |
else:
|
84 |
-
st.write("The image
|
85 |
|
86 |
# Restore the image
|
87 |
-
|
88 |
-
st.image(
|
89 |
|
90 |
# Enhance the image
|
91 |
-
|
92 |
-
st.image(
|
93 |
|
94 |
-
# Run the
|
95 |
-
if __name__ ==
|
96 |
-
app()
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
+
from transformers import pipeline
|
5 |
|
6 |
+
# Set up the CLIP classifier
|
7 |
+
model_name = "openai/clip-vit-large-patch14-336"
|
8 |
+
classifier = pipeline("zero-shot-image-classification", model=model_name)
|
9 |
+
labels_for_classification = ["genuine face", "morphed face"]
|
10 |
|
11 |
+
# Define the restoration function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def restore(image):
|
13 |
+
# Convert the image to float32
|
14 |
+
image = np.float32(image)
|
15 |
# Apply a median blur to the image
|
16 |
image = cv2.medianBlur(image, 5)
|
17 |
return image
|
18 |
|
19 |
+
# Define the enhancement function
|
20 |
def enhance(image):
|
21 |
+
# Convert the image to grayscale
|
22 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
23 |
+
# Apply histogram equalization to the image
|
24 |
+
equalized = cv2.equalizeHist(gray)
|
25 |
+
return equalized
|
|
|
|
|
|
|
26 |
|
27 |
# Define the Streamlit app
|
28 |
def app():
|
29 |
+
# Create a file uploader
|
30 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
31 |
if uploaded_file is not None:
|
32 |
+
# Read the image file
|
33 |
+
image = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
34 |
+
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
|
35 |
+
|
36 |
+
# Classify the image using CLIP
|
37 |
+
scores = classifier(image, candidate_labels=labels_for_classification)
|
38 |
+
if scores[0]['label'] == "genuine face":
|
39 |
+
st.write("The image contains a genuine face")
|
40 |
else:
|
41 |
+
st.write("The image contains a morphed face")
|
42 |
|
43 |
# Restore the image
|
44 |
+
restored = restore(image)
|
45 |
+
st.image(restored, caption="Restored Image")
|
46 |
|
47 |
# Enhance the image
|
48 |
+
enhanced = enhance(restored)
|
49 |
+
st.image(enhanced, caption="Enhanced Image")
|
50 |
|
51 |
+
# Run the app
|
52 |
+
if __name__ == "__main__":
|
53 |
+
app()
|