Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
-
from torchvision import transforms
|
| 5 |
-
from facenet_pytorch import MTCNN
|
| 6 |
-
from torchvision.transforms.functional import to_pil_image
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
-
import
|
| 10 |
|
| 11 |
-
# Function to load the ViT model
|
| 12 |
def load_model(model_path):
|
| 13 |
model = torch.load(model_path, map_location=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
|
| 14 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
@@ -22,7 +20,7 @@ mtcnn = MTCNN(keep_all=True, device=device)
|
|
| 22 |
# Function to preprocess the image using MTCNN for face detection
|
| 23 |
def preprocess_image(image, device):
|
| 24 |
# Convert PIL image to OpenCV format
|
| 25 |
-
open_cv_image = np.array(image)
|
| 26 |
# Convert RGB to BGR for OpenCV
|
| 27 |
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR)
|
| 28 |
|
|
@@ -55,8 +53,8 @@ def predict(image_tensor, model, device):
|
|
| 55 |
model.eval()
|
| 56 |
with torch.no_grad():
|
| 57 |
outputs = model(image_tensor)
|
| 58 |
-
# Adjust for your model's output
|
| 59 |
-
probabilities = torch.nn.functional.softmax(outputs
|
| 60 |
predicted_class = torch.argmax(probabilities, dim=1)
|
| 61 |
return predicted_class, probabilities
|
| 62 |
|
|
@@ -65,15 +63,17 @@ st.title("Face Detection and Classification with ViT")
|
|
| 65 |
st.write("Upload an image, and the model will detect faces and classify the image.")
|
| 66 |
|
| 67 |
model_path = "model_v1.0.pt" # Adjust this path as necessary
|
| 68 |
-
model, device
|
| 69 |
|
| 70 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 71 |
if uploaded_file is not None:
|
| 72 |
image = Image.open(uploaded_file).convert("RGB")
|
| 73 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
predicted_class, probabilities = predict(image_tensor, model, device)
|
| 76 |
|
| 77 |
st.write(f"Predicted class: {predicted_class.item()}")
|
| 78 |
# Display the final processed image
|
| 79 |
-
st.image(final_image, caption='Processed Image', use_column_width=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
+
from torchvision import transforms
|
|
|
|
|
|
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
+
from facenet_pytorch import MTCNN
|
| 8 |
|
| 9 |
+
# Function to load the ViT model
|
| 10 |
def load_model(model_path):
|
| 11 |
model = torch.load(model_path, map_location=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
|
| 12 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
| 20 |
# Function to preprocess the image using MTCNN for face detection
|
| 21 |
def preprocess_image(image, device):
|
| 22 |
# Convert PIL image to OpenCV format
|
| 23 |
+
open_cv_image = np.array(image.convert("RGB"))
|
| 24 |
# Convert RGB to BGR for OpenCV
|
| 25 |
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR)
|
| 26 |
|
|
|
|
| 53 |
model.eval()
|
| 54 |
with torch.no_grad():
|
| 55 |
outputs = model(image_tensor)
|
| 56 |
+
# Adjust for your model's output specifics
|
| 57 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
| 58 |
predicted_class = torch.argmax(probabilities, dim=1)
|
| 59 |
return predicted_class, probabilities
|
| 60 |
|
|
|
|
| 63 |
st.write("Upload an image, and the model will detect faces and classify the image.")
|
| 64 |
|
| 65 |
model_path = "model_v1.0.pt" # Adjust this path as necessary
|
| 66 |
+
model, device = load_model(model_path)
|
| 67 |
|
| 68 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 69 |
if uploaded_file is not None:
|
| 70 |
image = Image.open(uploaded_file).convert("RGB")
|
| 71 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 72 |
+
|
| 73 |
+
# Preprocess the image and perform inference
|
| 74 |
+
image_tensor, final_image = preprocess_image(image, device)
|
| 75 |
predicted_class, probabilities = predict(image_tensor, model, device)
|
| 76 |
|
| 77 |
st.write(f"Predicted class: {predicted_class.item()}")
|
| 78 |
# Display the final processed image
|
| 79 |
+
st.image(final_image, caption='Processed Image', use_column_width=True)
|