Winston de Jong commited on
Commit
0da216f
·
1 Parent(s): 1c98b00

test running model

Browse files
Files changed (1) hide show
  1. app.py +67 -18
app.py CHANGED
@@ -1,13 +1,18 @@
1
  import PIL.Image
2
  import gradio as gr
3
  import numpy as np
4
- import random
5
  import PIL
6
- import subprocess
7
- import sys
8
  import os
9
  import platform
10
  import time
 
 
 
 
 
 
 
11
 
12
  # by default, dlib will be compiled locally when installed via pip, which takes so long it
13
  # causes huggingface to time out during the build process.
@@ -20,9 +25,6 @@ start_time = time.time()
20
  import face_detection
21
  print(f"took {(time.time() - start_time) / 60} minutes to load face_recognition")
22
 
23
- # import spaces #[uncomment to use ZeroGPU]
24
- from diffusers import DiffusionPipeline
25
- import torch
26
 
27
  # # Function to display the uploaded image
28
  # def process_image(image : PIL.Image.Image):
@@ -30,23 +32,70 @@ import torch
30
  # # do AI stuff here
31
  # return gr.Image(outputs[0])
32
 
33
- def process_image_str(image : str):
34
- locations, paths = face_detection.getFaceLocationsAndFiles(image, "outputs", "imgs")
35
- # do AI stuff here
36
-
37
- return gr.Image(image)
38
-
39
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- # Create the Gradio interface
42
  interface = gr.Interface(
43
- fn=process_image_str, # Function to process the image
44
- inputs=gr.Image(type='filepath'), # Upload input
45
- outputs=gr.Image(), # Display output
 
46
  allow_flagging='never',
47
  title="Celebrity Face Detector",
48
  description="Upload a picture of a celebrity or group of celebrities to identify them"
49
  )
50
 
 
51
  if __name__ == "__main__":
52
- interface.launch()
 
1
  import PIL.Image
2
  import gradio as gr
3
  import numpy as np
4
+
5
  import PIL
 
 
6
  import os
7
  import platform
8
  import time
9
+ from huggingface_hub import hf_hub_download
10
+ import huggingface_hub
11
+ # import spaces #[uncomment to use ZeroGPU]
12
+ from diffusers import DiffusionPipeline
13
+ import torch
14
+ from torch import nn
15
+ import torchvision
16
 
17
  # by default, dlib will be compiled locally when installed via pip, which takes so long it
18
  # causes huggingface to time out during the build process.
 
25
  import face_detection
26
  print(f"took {(time.time() - start_time) / 60} minutes to load face_recognition")
27
 
 
 
 
28
 
29
  # # Function to display the uploaded image
30
  # def process_image(image : PIL.Image.Image):
 
32
  # # do AI stuff here
33
  # return gr.Image(outputs[0])
34
 
35
+ model_repo_id = "CSSE416-final-project/faceRecogModel"
36
+ weight_file_id = "matTemp.bin"
37
+
38
+
39
+ # 1. Load the model from Hugging Face Hub
40
+ def load_model(repo_id):
41
+ # Download the model weights from the repo
42
+ weights_path = hf_hub_download(repo_id=model_repo_id, filename=weight_file_id)
43
+
44
+ # Initialize the ResNet-18 architecture
45
+ model = torchvision.models.resnet18(pretrained=True) # TODO: does it matter if this is set to true or false?
46
+ num_ftrs = model.fc.in_features
47
+ model.fc = nn.Linear(num_ftrs, 100) # Adjust for your task (e.g., 128 classes)
48
+ # TODO: check if this number^^ corresponds to the number of classes
49
+
50
+ # Load the model weights
51
+ state_dict = torch.load(weights_path, map_location=torch.device("cpu"))
52
+ model.load_state_dict(state_dict)
53
+ model.eval() # Set the model to evaluation mode
54
+ return model
55
+
56
+
57
+ # 2. Load model
58
+ model = load_model(model_repo_id)
59
+
60
+
61
+ # 3. Define how to transform image
62
+ transforms = torchvision.transforms.Compose(
63
+ [
64
+ torchvision.transforms.ToTensor()
65
+ ])
66
+
67
+
68
+ # 4. Preprocess and display the image
69
+ def process_image_str(groupImageFilePath: str):
70
+ groupImage = PIL.Image.open(groupImageFilePath)
71
+ locations, images = face_detection.getCroppedImages(groupImage)
72
+
73
+ outputLabels = []
74
+ for image in images:
75
+ # Process the image
76
+ intputTensor = transforms(image).unsqueeze(0) # unsqueeze? add batch dimension??
77
+
78
+ # do AI stuff here
79
+ with torch.no_grad():
80
+ outputs_t = model(intputTensor)
81
+ _, pred_t = torch.max(outputs_t, dim=1)
82
+ outputLabels.append(pred_t.item())
83
+
84
+ #return gr.Image(image)
85
+ return outputLabels.pop(0)
86
+
87
 
88
+ # 5. Create the Gradio interface
89
  interface = gr.Interface(
90
+ fn=process_image_str, # Function to process the image
91
+ inputs=gr.Image(type='filepath'), # Upload input
92
+ # outputs=gr.Image(), # Display output
93
+ outputs='text',
94
  allow_flagging='never',
95
  title="Celebrity Face Detector",
96
  description="Upload a picture of a celebrity or group of celebrities to identify them"
97
  )
98
 
99
+ # 6. Launch the app
100
  if __name__ == "__main__":
101
+ interface.launch()