File size: 1,746 Bytes
37b7c68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from torchvision import transforms
from PIL import Image
import numpy as np

# Load the trained model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = YourModel()  # Replace 'YourModel' with your actual model class
model.load_state_dict(torch.load('D:/Dataset/Cricket Bowl  Grip/final_model.pth'))
model.to(device)
model.eval()

# Define the transformation to be applied to the input image
transform = transforms.Compose([
    transforms.Resize((224, 224)),  # Resize image to fit your model's input size
    transforms.ToTensor(),  # Convert image to tensor
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),  # Example normalize values for ImageNet
])

# Define a function for making predictions
def predict(image):
    image = Image.fromarray(image)  # Convert numpy array to PIL Image
    image = transform(image).unsqueeze(0)  # Apply transformations and add batch dimension
    image = image.to(device)
    
    with torch.no_grad():
        outputs = model(image)
        _, predicted = torch.max(outputs, 1)
        
    # Map predicted label to class name
    class_names = ['OUTSWING', 'STRAIGHT', 'BACK_OF_HAND', 'CARROM', 'CROSSSEAM', 
                   'GOOGLY', 'INSWING', 'KNUCKLE', 'LEGSPIN', 'OFFSPIN']
    predicted_label = class_names[predicted.item()]
    
    return predicted_label

# Create the Gradio Interface
iface = gr.Interface(fn=predict, 
                     inputs=gr.Image(type="numpy"),  # Accepts image input
                     outputs=gr.Text(),  # Output the predicted class label
                     live=True)  # live=True enables prediction while image is being uploaded

# Launch the interface
iface.launch()