|
|
import PIL.Image
|
|
|
import gradio as gr
|
|
|
import numpy as np
|
|
|
import random
|
|
|
import PIL
|
|
|
import subprocess
|
|
|
import sys
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from diffusers import DiffusionPipeline
|
|
|
import torch
|
|
|
|
|
|
import face_detection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_repo_id = "CSSE416-final-project/faceRecogModel"
|
|
|
weight_file_id = "matTemp.bin"
|
|
|
|
|
|
|
|
|
|
|
|
def load_model(repo_id):
|
|
|
|
|
|
weights_path = hf_hub_download(repo_id=model_repo_id, filename=weight_file_id)
|
|
|
|
|
|
|
|
|
model = models.resnet18(pretrained=True)
|
|
|
num_ftrs = model.fc.in_features
|
|
|
model.fc = nn.Linear(num_ftrs, 100)
|
|
|
|
|
|
|
|
|
|
|
|
state_dict = torch.load(weights_path, map_location=torch.device("cpu"))
|
|
|
model.load_state_dict(state_dict)
|
|
|
model.eval()
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
model = load_model(model_repo_id)
|
|
|
|
|
|
|
|
|
|
|
|
transforms = transforms.Compose(
|
|
|
[
|
|
|
transforms.ToTensor()
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
def process_image_str(groupImageFilePath: str):
|
|
|
groupImage = PIL.Image.open(groupImageFilePath)
|
|
|
locations, images = face_detection.getCroppedImages(groupImage)
|
|
|
|
|
|
outputLabels = []
|
|
|
for image in images:
|
|
|
|
|
|
intputTensor = transforms(image).unsqueeze(0)
|
|
|
|
|
|
|
|
|
with torch.no_grad():
|
|
|
outputs_t = model(intputTensor)
|
|
|
_, pred_t = torch.max(outputs_t, dim=1)
|
|
|
outputLabels.append(pred_t.item())
|
|
|
|
|
|
|
|
|
|
|
|
return outputLabels.pop(0)
|
|
|
|
|
|
|
|
|
|
|
|
interface = gr.Interface(
|
|
|
fn=process_image_str,
|
|
|
inputs=gr.Image(type='filepath'),
|
|
|
|
|
|
outputs='text',
|
|
|
allow_flagging='never',
|
|
|
title="Celebrity Face Detector",
|
|
|
description="Upload a picture of a celebrity or group of celebrities to identify them"
|
|
|
)
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
interface.launch() |