def main(): print("PCA") pca.pca() print("RandomForest") rf.rf(2) print("KNN") knn.knn(2) print("SVC") svc.svc() print("GRID_SVC") svc.gridSearchScore() print("Logistic") logistic.Logistic().fit() print("DNN Classifier") classifier_model = classifier.classifier() classifier_model.fit()
# load smile data PATH = "../data/smile/" for idx, filename in enumerate(smilefiles): phi[idx] = vectorize(PATH + filename) labels.append(1) # load neutral data PATH = "../data/neutral/" offset = idx + 1 for idx, filename in enumerate(neutralfiles): phi[idx + offset] = vectorize(PATH + filename) labels.append(0) """ training the data with logistic regression """ lr = logistic.Logistic(dim) lr.train(phi, labels) """ open webcam and capture images """ cv2.namedWindow("preview") vc = cv2.VideoCapture(0) if vc.isOpened(): # try to get the first frame rval, frame = vc.read() else: rval = False print "\n\n\n\n\npress space to take picture; press ESC to exit" while True:
def getTrainingRes(): """ given a jpg image, vectorize the grayscale pixels to a (width * height, 1) np array it is used to preprocess the data and transform it to feature space """ def vectorize(filename): size = defaults.WIDTH, defaults.HEIGHT # (width, height) im = Image.open(filename) resized_im = im.resize(size, Image.ANTIALIAS) # resize image im_grey = resized_im.convert('L') # convert the image to *greyscale* im_array = np.array(im_grey) # convert to np array oned_array = im_array.reshape(1, size[0] * size[1]) return oned_array """ load training data """ # create a list for filenames of smiles pictures smilefiles = [] with open(defaults.smile_csv, 'rb') as csvfile: for rec in csv.reader(csvfile, delimiter=' '): smilefiles += rec # create a list for filenames of neutral pictures neutralfiles = [] with open(defaults.neutral_csv, 'rb') as csvfile: for rec in csv.reader(csvfile, delimiter=' '): neutralfiles += rec # create a list for filenames of disgust pictures disgustfiles = [] with open(defaults.disgust_csv, 'rb') as csvfile: for rec in csv.reader(csvfile, delimiter=' '): disgustfiles += rec # N x dim matrix to store the vectorized data (aka feature space) phi = np.zeros((len(smilefiles) + len(neutralfiles) + len(disgustfiles), defaults.dim)) # 1 x N vector to store binary labels of the data: 1 for smile and 0 for neutral labels = [] # load smile data for idx, filename in enumerate(smilefiles): phi[idx] = vectorize(defaults.smile_imgs + filename) labels.append(0) # load neutral data offset = idx + 1 for idx, filename in enumerate(neutralfiles): phi[idx + offset] = vectorize(defaults.neutral_imgs + filename) labels.append(1) # load disgust data offset = idx + 1 for idx, filename in enumerate(disgustfiles): phi[idx + offset] = vectorize(defaults.disgust_imgs + filename) labels.append(2) """ training the data with logistic regression """ lr = logistic.Logistic(defaults.dim) lr.train(phi, labels) return lr
#Convert the picture into a numpy array buff = np.fromstring(stream.getvalue(), dtype=np.uint8) #Now creates an OpenCV image image = cv2.imdecode(buff, 1) return image if __name__ == '__main__': ## Exists weigths if sys.argv[1]=="train": #Load training data lr=getTrainingRes() else: weigths = np.load(defaults.weigths_file) lr = logistic.Logistic(defaults.dim,weigths) """ open webcam and capture images """ if defaults.SHOW_PREVIEW: cv2.namedWindow("preview") print "\n--Starting to get images" if defaults.raspberry_plt: cam = picamera.PiCamera() cam.resolution = (320, 240) else: cam = cv.CaptureFromCAM(0) while True: if defaults.raspberry_plt: