-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccuracy.py
More file actions
81 lines (60 loc) · 2.42 KB
/
Copy pathaccuracy.py
File metadata and controls
81 lines (60 loc) · 2.42 KB
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
import os
import json
from algorithm.object_detector import YOLOv7
import cv2
# Define the paths to the image and labels folders
image_folder = r'C:\Users\cools\Desktop\Dataset-2\images'
labels_folder = r'C:\Users\cools\Desktop\Dataset-2\labels'
# Load the list of image filenames
image_filenames = os.listdir(image_folder)
# Load the labels from the JSON files
labels = []
for filename in image_filenames:
label_path = os.path.join(labels_folder, f"{os.path.splitext(filename)[0]}.json")
with open(label_path, 'r') as file:
label = json.load(file)
labels.append(label)
# Load your trained model using the saved weights
yolov7 = YOLOv7()
yolov7.load('best.weights', classes='classes.yaml', device='cpu')
# Define variables for accuracy calculation
total_samples = len(image_filenames)
correct_predictions = 0
def load_and_preprocess_image(image_path):
# Load the image using OpenCV
image = cv2.imread(image_path)
# Preprocess the image (e.g., resize, normalize, etc.)
# Add your preprocessing steps here
return image
# Iterate through the dataset and make predictions
# Iterate through the dataset and make predictions
for i in range(total_samples):
image_filename = image_filenames[i]
image_path = os.path.join(image_folder, image_filename)
true_label = labels[i]
# Load and preprocess the image
image = load_and_preprocess_image(image_path)
# Perform inference using the model
predictions = yolov7.detect(image, track=False)
# Print predictions for debugging
#print(predictions)
# Compare the predicted labels with the true label
true_label_found = False
for prediction in predictions:
if isinstance(predictions, list) and len(predictions) > 0:
prediction = predictions[0] # Retrieve the first element from the list
if isinstance(prediction, dict):
if (
str(prediction.get('class')) == str(true_label['class_id'])
and prediction.get('x') == true_label['x']
and prediction.get('y') == true_label['y']
and prediction.get('width') == true_label['width']
and prediction.get('height') == true_label['height']
):
true_label_found = True
if true_label_found:
correct_predictions += 1
print(correct_predictions)
# Calculate accuracy
accuracy = correct_predictions / total_samples
print(f"Accuracy: {accuracy}")