forked from norhanreda/neural-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry.py
110 lines (91 loc) · 3.74 KB
/
try.py
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
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
import cv2
import os
import numpy as np
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris
import joblib
import cv2
import time
# Load the saved classifier from the file
# clf = joblib.load('svm_classifier.joblib')
import os
from PIL import Image
# Set the directory path that contains the images
directory = "our_data\our"
features=[]
labels=[]
# Define the HOG parameters
win_size = (64, 64)
block_size = (16, 16)
block_stride = (8, 8)
cell_size = (8, 8)
nbins = 9
lower_skin = np.array([0, 135, 85])
upper_skin = np.array([255, 180, 135])
clf = joblib.load('svm_classifier.joblib')
# Loop through all the files in the directory
with open('results.txt', 'w') as r:
with open('time.txt', 'w') as t:
for sub_dir in os.listdir(directory):
print(sub_dir)
sub_dir_path = os.path.join(directory, sub_dir)
if not os.path.isdir(sub_dir_path):
continue
for filename in os.listdir(sub_dir_path):
# Check if the file is an image
# print(filename)
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
print('shit')
# Load the image file using Pillow
img_path = os.path.join(sub_dir_path, filename)
image = cv2.imread(img_path)
image= cv2.resize(image,(128,128))
# Record the start time
start_time = time.time()
ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
mask = cv2.inRange(ycrcb, lower_skin, upper_skin)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contour = max(contours, key = len)
min_x, min_y, w, h = cv2.boundingRect(contour)
new_img = np.zeros((h, w), dtype=np.uint8)
contour = contour - [min_x, min_y]
cv2.drawContours(new_img, [contour], 0, 255, -1)
new_img= cv2.resize(new_img,(128,128))
# Initialize HOG descriptor
hog = cv2.HOGDescriptor(win_size, block_size, block_stride, cell_size, nbins)
# Compute HOG features
hog_features = hog.compute(new_img)
predicted_labels = clf.predict(hog_features.reshape(1, -1))
# Record the end time
end_time = time.time()
# Calculate the execution time
execution_time = end_time - start_time
print(f"Execution time: {execution_time:.2f} seconds")
r.write(predicted_labels[0]+'\n')
t.write(f"{execution_time:.3f}"+'\n')
features.append(hog_features)
labels.append(sub_dir)
# print(img)
features = np.array(features)
labels = np.array(labels)
print(features.shape)
print(labels.shape)
# Load the saved classifier from the file
# Predict the labels of the test set using the trained SVM classifier
predicted_labels = clf.predict(features)
# Compute the accuracy of the SVM classifier
accuracy = accuracy_score(labels, predicted_labels)
# Print the predicted labels
# print(predicted_labels)
# with open('results.txt', 'w') as f:
# for i in range(0,len(predicted_labels)):
# Write a string to the file
# f.write(predicted_labels[i]+'\n')
# Compute the accuracy of the SVM classifier
# accuracy = accuracy_score(test_labels, predicted_labels)
print("Accuracy: {:.4f}%".format(accuracy * 100))