from skimage.feature import hog
import joblib,glob,os,cv2
from sklearn.svm import LinearSVC
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn import svm, metrics
import numpy as np 
from sklearn.preprocessing import LabelEncoder

train_data = []
train_labels = []
pos_im_path = 'dataset/positive/'
neg_im_path = 'dataset/negative/'
model_path = 'models/models.dat'

for filename in glob.glob(os.path.join(pos_im_path,"*.png")):
    fd = cv2.imread(filename,0)
    fd = cv2.resize(fd,(64,128))
    fd = hog(fd,orientations=9,pixels_per_cell=(8,8),visualize=False,cells_per_block=(3,3))
    train_data.append(fd)
    train_labels.append(1)

for filename in glob.glob(os.path.join(neg_im_path,"*.png")):
    fd = cv2.imread(filename,0)
    fd = cv2.resize(fd,(64,128))
    fd = hog(fd,orientations=9,pixels_per_cell=(8,8),visualize=False,cells_per_block=(3,3))
    train_data.append(fd)
    train_labels.append(0)
train_data = np.float32(train_data)
train_labels = np.array(train_labels)
print('Preparando datos')
print('Train Data:',len(train_data))
print('Train Labels (1,0)',len(train_labels))
print("Clasificador SVM")

model = LinearSVC()
print('Entrenando...... SVM')
model.fit(train_data,train_labels)
joblib.dump(model, 'models/models.dat')
print('Modelo guardado : {}'.format('models/models.dat'))
