Spaces:
Runtime error
Runtime error
Deepaksiwania12
commited on
Commit
Β·
e16290b
1
Parent(s):
6fe2a24
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import glob
|
4 |
+
import os
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import string
|
7 |
+
from mlxtend.plotting import plot_decision_regions
|
8 |
+
from mpl_toolkits.mplot3d import Axes3D
|
9 |
+
from sklearn.decomposition import PCA
|
10 |
+
from sklearn.preprocessing import StandardScaler
|
11 |
+
from sklearn.neighbors import KNeighborsClassifier
|
12 |
+
from sklearn.tree import DecisionTreeClassifier
|
13 |
+
from sklearn.model_selection import train_test_split, cross_val_score
|
14 |
+
from sklearn.utils.multiclass import unique_labels
|
15 |
+
from sklearn import metrics
|
16 |
+
from sklearn.svm import SVC
|
17 |
+
dim = 100
|
18 |
+
import torch
|
19 |
+
from torchvision import transforms
|
20 |
+
from PIL import Image
|
21 |
+
|
22 |
+
# Define your model class
|
23 |
+
class YourModelClass(torch.nn.Module):
|
24 |
+
# Define your model architecture here
|
25 |
+
|
26 |
+
# Create an instance of your model
|
27 |
+
model = YourModelClass()
|
28 |
+
|
29 |
+
# Load the pre-trained weights
|
30 |
+
model.load_state_dict(torch.load('model_weights.pth'))
|
31 |
+
model.eval()
|
32 |
+
def predict_leaf_health(image_path):
|
33 |
+
try:
|
34 |
+
# Open and preprocess the image
|
35 |
+
img = Image.open(image_path)
|
36 |
+
transform = transforms.Compose([
|
37 |
+
transforms.Resize((224, 224)),
|
38 |
+
transforms.ToTensor(),
|
39 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
40 |
+
])
|
41 |
+
img = transform(img)
|
42 |
+
img = img.unsqueeze(0) # Add batch dimension
|
43 |
+
|
44 |
+
# Make prediction
|
45 |
+
with torch.no_grad():
|
46 |
+
output = model(img)
|
47 |
+
prediction = torch.argmax(output).item()
|
48 |
+
|
49 |
+
# Map the prediction to class labels (modify as needed)
|
50 |
+
class_labels = {0: 'Unhealthy', 1: 'Healthy'}
|
51 |
+
result = class_labels.get(prediction, 'Unknown')
|
52 |
+
|
53 |
+
return result
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
return f"Error: {str(e)}"
|