File size: 2,129 Bytes
b0cce5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from torchvision import transforms as tt
from PIL import Image
import torch
from torchvision import transforms as tt
from PIL import Image
import cv2


def predict_potato(image_path, model):

    # Define the pre-processing transform
    transforms = tt.Compose([
    tt.Resize((224, 224)),
    tt.ToTensor()
])
    image = Image.open(image_path).convert("RGB")
    # Pre-process the image
    image_tensor = transforms(image).unsqueeze(0)
    # Set the model to evaluation mode
    model.eval()

    # Make a prediction
    with torch.no_grad():
        output = model(image_tensor)

    # Convert the output to probabilities using softmax
    probabilities = torch.nn.functional.softmax(output[0], dim=0)
    # Get the predicted class
    predicted_class = torch.argmax(probabilities).item()
    # Get the probability for the predicted class
    predicted_probability = probabilities[predicted_class].item()
    # Define class labels
    class_labels = ['Potato Early Blight', 'Potato Late Blight', 'Potato Healthy']

    return class_labels[predicted_class], predicted_probability, image


def predict_tomato(image_file, model):
    # Define the pre-processing transform
    transforms = tt.Compose([
        tt.Resize((224, 224)),
        tt.ToTensor()
    ])

    # Load and preprocess the image
    image = Image.open(image_file).convert("RGB")
    image_tensor = transforms(image).unsqueeze(0)

    # Set the model to evaluation mode
    model.eval()

    # Make a prediction
    with torch.no_grad():
        output = model(image_tensor)

    # Convert the output to probabilities using softmax
    probabilities = torch.nn.functional.softmax(output[0], dim=0)
    # Get the predicted class
    predicted_class = torch.argmax(probabilities).item()
    # Get the probability for the predicted class
    predicted_probability = probabilities[predicted_class].item()
    # Define class labels for tomato
    class_labels = ['Tomato Early Blight', 'Tomato Late Blight', 'Tomato Healthy']

    return class_labels[predicted_class], predicted_probability, image