|
import gradio as gr |
|
import torch |
|
from torchvision import transforms |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
model = YourModel() |
|
model.load_state_dict(torch.load('D:/Dataset/Cricket Bowl Grip/final_model.pth')) |
|
model.to(device) |
|
model.eval() |
|
|
|
|
|
transform = transforms.Compose([ |
|
transforms.Resize((224, 224)), |
|
transforms.ToTensor(), |
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), |
|
]) |
|
|
|
|
|
def predict(image): |
|
image = Image.fromarray(image) |
|
image = transform(image).unsqueeze(0) |
|
image = image.to(device) |
|
|
|
with torch.no_grad(): |
|
outputs = model(image) |
|
_, predicted = torch.max(outputs, 1) |
|
|
|
|
|
class_names = ['OUTSWING', 'STRAIGHT', 'BACK_OF_HAND', 'CARROM', 'CROSSSEAM', |
|
'GOOGLY', 'INSWING', 'KNUCKLE', 'LEGSPIN', 'OFFSPIN'] |
|
predicted_label = class_names[predicted.item()] |
|
|
|
return predicted_label |
|
|
|
|
|
iface = gr.Interface(fn=predict, |
|
inputs=gr.Image(type="numpy"), |
|
outputs=gr.Text(), |
|
live=True) |
|
|
|
|
|
iface.launch() |
|
|