File size: 2,889 Bytes
8dc9bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import PIL.Image
import gradio as gr
import numpy as np
import random
import PIL
import subprocess
import sys
import os

# os.system("ls -a")
# print("1")
# os.system("pip install ./dlib-19.24.99-cp36-cp36m-linux_x86_64.whl")
# print("2")
# os.system("pip install dlib-19.24.99-cp36-cp36m-linux_x86_64.whl")
# os.system("pip install face-recognition")
# import face_detection

# import spaces #[uncomment to use ZeroGPU]
from diffusers import DiffusionPipeline
import torch

import face_detection

# # Function to display the uploaded image
# def process_image(image : PIL.Image.Image):
#     outputs = face_detection.getCroppedImages(image)
#     # do AI stuff here
#     return gr.Image(outputs[0])

model_repo_id = "CSSE416-final-project/faceRecogModel"
weight_file_id = "matTemp.bin"


# 1. Load the model from Hugging Face Hub
def load_model(repo_id):
    # Download the model weights from the repo
    weights_path = hf_hub_download(repo_id=model_repo_id, filename=weight_file_id)

    # Initialize the ResNet-18 architecture
    model = models.resnet18(pretrained=True) # TODO: does it matter if this is set to true or false?
    num_ftrs = model.fc.in_features
    model.fc = nn.Linear(num_ftrs, 100)  # Adjust for your task (e.g., 128 classes)
    # TODO: check if this number^^ corresponds to the number of classes

    # Load the model weights
    state_dict = torch.load(weights_path, map_location=torch.device("cpu"))
    model.load_state_dict(state_dict)
    model.eval()  # Set the model to evaluation mode
    return model


# 2. Load model
model = load_model(model_repo_id)


# 3. Define how to transform image
transforms = transforms.Compose(
[
    transforms.ToTensor()
])


# 4. Preprocess and display the image
def process_image_str(groupImageFilePath: str):
    groupImage = PIL.Image.open(groupImageFilePath)
    locations, images = face_detection.getCroppedImages(groupImage)

    outputLabels = []
    for image in images:
        # Process the image
        intputTensor = transforms(image).unsqueeze(0) # unsqueeze? add batch dimension??

        # do AI stuff here
        with torch.no_grad():
            outputs_t = model(intputTensor)
            _, pred_t = torch.max(outputs_t, dim=1)
            outputLabels.append(pred_t.item())


    #return gr.Image(image)
    return outputLabels.pop(0)


# 5. Create the Gradio interface
interface = gr.Interface(
    fn=process_image_str,  # Function to process the image
    inputs=gr.Image(type='filepath'),  # Upload input
    # outputs=gr.Image(),  # Display output
    outputs='text',
    allow_flagging='never',
    title="Celebrity Face Detector",
    description="Upload a picture of a celebrity or group of celebrities to identify them"
)

# 6. Launch the app
if __name__ == "__main__":
    interface.launch()