-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuntitled51.py
100 lines (65 loc) · 2.33 KB
/
untitled51.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 21 17:20:48 2023
@author: Hamza
"""
import cv2
import numpy as np
import os
from glob import glob
import matplotlib.pyplot as plt
#lib for lazy
from lazypredict.Supervised import LazyClassifier
#from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import time
import datetime
#-------
start=time.time()
folder_path = 'C:/Users/Hamza/Desktop/dataset50gs'
folder = 'C:/Users/Hamza/Desktop/dataset50'
images = [cv2.imread(file) for file in glob.glob(folder+ '/*/*.jpg')]
'''folder_images = glob(folder_path + '/*/*.jpg')
for i,image_path in enumerate(folder_images):
ir_=os.path.basename(os.path.dirname(image_path))
image = cv2.imread(image_path)
z=np.array(cv2.imread(image_path))
np.save('C:/Users/Hamza/Desktop/saved data/imgsarry.npy', z)
print('saved in array')
#hog = cv2.HOGDescriptor()
from skimage.feature import hog
image_features = []
label_features=[]
total_images=len(folder_images)
for i,image_path in enumerate(folder_images):
ir_=os.path.basename(os.path.dirname(image_path))
image = cv2.imread(image_path)
image1=image[...,2]
#imagea=np.expand_dims(image1,-1)
fd, hog_image = hog(image1, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 2), visualize=True, multichannel=False)
#plt.imshow(hog_image,cmap='gray')
#break
#features = hog.compute(image)
image_features.append(fd)
label_features.append(ir_)
print(i+1, '/' , total_images,'-->',round((i+1)/total_images*100,4),'%')
X=np.array(image_features)
y=np.array(label_features)
'''
#np.save('C:/Users/Hamza/Desktop/saved data/xt.npy', X)
#np.save('C:/Users/Hamza/Desktop/saved data/yt.npy', y)
end=time.time()
print("time taken in feature extraction= ",end-start,"seconds")
start=time.time()
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=.2,random_state =123)
model=LinearDiscriminantAnalysis()
model.fit(X_train,y_train)
accuracy=model.score(X_test,y_test)
print("Accuracy: {:.2f}%".format(accuracy * 100))
end=time.time()
print("time taken in classification= ",end-start,"seconds")