File size: 3,544 Bytes
b410607
 
 
 
 
 
 
 
 
 
72bf9ac
b410607
b4d4f0c
b410607
 
 
 
 
 
 
 
 
 
6fb32c9
 
 
 
 
20eb8e9
b410607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fb32c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

import torch
from PIL import Image
from torchvision import transforms
import pathlib   
temp = pathlib.PosixPath   
pathlib.PosixPath = pathlib.WindowsPath



model = torch.hub.load(r"ultralytics/yolov5", 'custom', path=r'./models/best.pt')

img_path = r"ACNE.jpg"
image = Image.open(img_path)


transform = transforms.Compose([
    transforms.Resize((224, 224)),  # Resize to model's expected input size
    transforms.ToTensor(),         # Convert image to tensor
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),  # Normalize
])
im = transform(image).unsqueeze(0)  # Add batch dimension (BCHW)

try:   
    output = model(im)
    print(output)
except Exception as e:
        logger.error(f"Error in image prediction: {e}")
        

# Get predictions
with torch.no_grad():
    output = model(im)  # Raw model output (logits)

# Apply softmax to get confidence scores
softmax = torch.nn.Softmax(dim=1)
probs = softmax(output)

# Get the predicted class and its confidence score
predicted_class_id = torch.argmax(probs, dim=1).item()
confidence_score = probs[0, predicted_class_id].item()

# Print predicted class and confidence score
print(f"Predicted Class ID: {predicted_class_id}")
print(f"Confidence Score: {confidence_score:.4f}")

# Print predicted class name if available
if hasattr(model, 'names'):
    class_name = model.names[predicted_class_id]
    print(f"Predicted Class Name: {class_name}")

# import torch
# import cv2  # Import OpenCV
# from torchvision import transforms
# import pathlib

# # Pathlib adjustment for Windows compatibility
# temp = pathlib.PosixPath
# pathlib.PosixPath = pathlib.WindowsPath

# # Load pre-trained YOLOv5 model
# model = torch.hub.load(
#     r'C:\Users\RESHMA R B\OneDrive\Documents\Desktop\project_without_malayalam\chatbot2\yolov5',
#     'custom',
#     path=r"C:\Users\RESHMA R B\OneDrive\Documents\Desktop\project_without_malayalam\chatbot2\models\best.pt",
#     source="local"
# )

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

# # Define image transformations (for PyTorch)
# transform = transforms.Compose([
#     transforms.ToTensor(),  # Convert image to tensor
#     transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),  # Normalize
# ])

# # Load and preprocess the image using OpenCV
# img_path = r"C:\Users\RESHMA R B\OneDrive\Documents\Desktop\project_without_malayalam\chatbot2\ACNE.jpg"
# image = cv2.imread(img_path)  # Load image in BGR format
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Convert BGR to RGB
# image_resized = cv2.resize(image, (224, 224))  # Resize to match model's expected input size

# # Transform the image for the model
# im = transform(image_resized).unsqueeze(0)  # Add batch dimension (BCHW)

# # Get predictions
# with torch.no_grad():
#     output = model(im)  # Raw model output (logits)

# # Apply softmax to get confidence scores
# softmax = torch.nn.Softmax(dim=1)
# probs = softmax(output)

# # Get the predicted class and its confidence score
# predicted_class_id = torch.argmax(probs, dim=1).item()
# confidence_score = probs[0, predicted_class_id].item()

# # Print predicted class and confidence score
# print(f"Predicted Class ID: {predicted_class_id}")
# print(f"Confidence Score: {confidence_score:.4f}")

# # Print predicted class name if available
# if hasattr(model, 'names'):
#     class_name = model.names[predicted_class_id]
#     print(f"Predicted Class Name: {class_name}")


# cv2.imshow("Input Image", image)  
# cv2.waitKey(0)  
# cv2.destroyAllWindows()