predictor

This commit is contained in:
jha11aditya 2021-03-02 23:29:59 +05:30
parent 4787dc0960
commit b0bb218b2a
8 changed files with 72 additions and 9 deletions

57
predictor.py Normal file
View File

@ -0,0 +1,57 @@
import os
import sys
import pandas as pd
import numpy as np
from skimage.io import imread, imshow, imsave
from skimage.transform import resize
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn import metrics
import pickle
from skimage import img_as_ubyte
tempdir = "predictor_temp"
if len(sys.argv) < 3:
print("no input image")
exit(0)
if len(sys.argv) < 2:
print("no input model")
exit(0)
svm_model = pickle.load(open(sys.argv[1], 'rb'))
os.system('mkdir -p ' + tempdir)
os.system("python3 face_cutter.py " + sys.argv[2] + " " + tempdir )
img_files = [name for name in os.listdir(tempdir) if not os.path.isdir(os.path.join(tempdir, name)) ]
print(img_files)
cutf = tempdir +"/"+img_files[0]
os.system("python3 hogger.py " + cutf + " " + tempdir )
os.system("rm " + cutf)
img_files = [name for name in os.listdir(tempdir) if not os.path.isdir(os.path.join(tempdir, name)) ]
hogf = tempdir + "/" + img_files[0]
imgdat = imread(hogf, as_gray=True)
os.system("rm -rf " + tempdir)
imgdat = resize(imgdat, (64,64))
imgdat = img_as_ubyte(imgdat)
flat_imgdat = np.array( imgdat ).flatten()
print(flat_imgdat.shape)
X = np.array(flat_imgdat)
X = X.reshape(-1,1)
X = StandardScaler().fit_transform(X)
print(X.shape,X)
# pca = PCA(n_components=128)
# pcaofX = pca.fit_transform(X)
res = svm_model.predict(X.T)
print("Prediction:",res)

BIN
subject01.happy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
subject07.surprised.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
subject09.glasses.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
subject11.surprised.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
subject14.leftlight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
svm_model.sav Normal file

Binary file not shown.

View File

@ -6,6 +6,7 @@ from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn import svm from sklearn import svm
from sklearn import metrics from sklearn import metrics
import pickle
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("no input csv file") print("no input csv file")
@ -27,19 +28,24 @@ print(Y.shape)
X = StandardScaler().fit_transform(X) X = StandardScaler().fit_transform(X)
print(X) print(X)
pca = PCA(n_components=50) # pca = PCA(n_components=128)
pcaofX = pca.fit_transform(X) # pcaofX = pca.fit_transform(X)
print("shapeofX after pca",pcaofX.shape, ", cum Sum of variance ratio",pca.explained_variance_ratio_.cumsum()[-1]) # print("shapeofX after pca",pcaofX.shape, ", cum Sum of variance ratio",pca.explained_variance_ratio_.cumsum()[-1])
# pcaofX = X pcaofX = X
X_train, X_test, Y_train, Y_test = train_test_split(pcaofX, Y, test_size=0.3,random_state=109) X_train, X_test, Y_train, Y_test = train_test_split(pcaofX, Y, test_size=0.1,random_state=109)
print(X_train.shape) print(X_train.shape)
classifier = svm.SVC(kernel="linear") svm_model = svm.SVC(kernel="linear")
classifier.fit(X_train,Y_train) svm_model.fit(X_train,Y_train)
pred = classifier.predict(X_test) pred = svm_model.predict(X_test)
print(pred)
print("Accuracy:",metrics.accuracy_score(Y_test, pred)) print("Accuracy:",metrics.accuracy_score(Y_test, pred))
pickle.dump(svm_model, open("svm_model.sav", 'wb'))