AI-Manith commited on
Commit
376d76a
1 Parent(s): 6bd4420

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -1,8 +1,9 @@
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
 
7
  # Function to load the ViT model and MTCNN
8
  def load_model_and_mtcnn(model_path):
@@ -15,12 +16,13 @@ def load_model_and_mtcnn(model_path):
15
  # Function to preprocess the image and return both the tensor and the final PIL image for display
16
  def preprocess_image(image, mtcnn, device):
17
  processed_image = image # Initialize with the original image
 
18
  try:
19
  # Directly call mtcnn with the image to get cropped faces
20
  cropped_faces = mtcnn(image)
21
  if cropped_faces is not None and len(cropped_faces) > 0:
22
  # Convert the first detected face tensor back to PIL Image for further processing
23
- processed_image = cropped_faces[0].cpu()
24
  except Exception as e:
25
  st.write(f"Exception in face detection: {e}")
26
  processed_image = image
@@ -30,17 +32,21 @@ def preprocess_image(image, mtcnn, device):
30
  transforms.ToTensor(),
31
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
32
  ])
33
- image_tensor = transform(processed_image).to(device)
34
- image_tensor = image_tensor.unsqueeze(0) # Add a batch dimension
35
- return image_tensor, processed_image
 
 
 
 
36
 
37
  # Function for inference
38
  def predict(image_tensor, model, device):
39
  model.eval()
40
  with torch.no_grad():
41
  outputs = model(image_tensor)
42
- # Adjust for your model's output structure
43
- probabilities = torch.nn.functional.softmax(outputs, dim=1)
44
  predicted_class = torch.argmax(probabilities, dim=1)
45
  return predicted_class, probabilities
46
 
 
1
  import streamlit as st
2
  from PIL import Image
3
  import torch
4
+ from torchvision import transforms, utils
5
  from facenet_pytorch import MTCNN
6
+ from torchvision.transforms.functional import to_pil_image
7
 
8
  # Function to load the ViT model and MTCNN
9
  def load_model_and_mtcnn(model_path):
 
16
  # Function to preprocess the image and return both the tensor and the final PIL image for display
17
  def preprocess_image(image, mtcnn, device):
18
  processed_image = image # Initialize with the original image
19
+ cropped_image = None
20
  try:
21
  # Directly call mtcnn with the image to get cropped faces
22
  cropped_faces = mtcnn(image)
23
  if cropped_faces is not None and len(cropped_faces) > 0:
24
  # Convert the first detected face tensor back to PIL Image for further processing
25
+ cropped_image = to_pil_image(cropped_faces[0].cpu())
26
  except Exception as e:
27
  st.write(f"Exception in face detection: {e}")
28
  processed_image = image
 
32
  transforms.ToTensor(),
33
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
34
  ])
35
+ # Apply the transformation to the cropped image if available
36
+ if cropped_image is not None:
37
+ processed_image = transform(cropped_image).to(device)
38
+ # Add a batch dimension
39
+ processed_image = processed_image.unsqueeze(0)
40
+
41
+ return processed_image, cropped_image
42
 
43
  # Function for inference
44
  def predict(image_tensor, model, device):
45
  model.eval()
46
  with torch.no_grad():
47
  outputs = model(image_tensor)
48
+ # Adjust for your model's output if it does not have a 'logits' attribute
49
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
50
  predicted_class = torch.argmax(probabilities, dim=1)
51
  return predicted_class, probabilities
52