def recognition_image(image): desc = LocalBinaryPatterns(24, 8) model = pickle.load(open("models/model.save", 'rb')) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) hist = desc.describe(gray) prediction = model.predict([hist])[0] return prediction
def get_dataset(image_path): dataset = [] labels = [] desc = LocalBinaryPatterns(24, 8) for img in image_path: image = cv.imread(img) gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) hist = desc.describe(gray) dataset.append(hist) label = img.split(os.path.sep)[-2] labels.append(label) dataset = np.array(dataset, dtype="float") dataset = normalize(dataset) labels = np.array(labels) return dataset, labels
def insert_lbp_features(path, table): name2vector = {} img_paths = load_images(path) desc = LocalBinaryPatterns(510, 8) for img_path in img_paths: hist = get_lbp_predictions(img_path, desc) name2vector[img_path.split('/')[-1]] = hist db = DBObject(db=DB_NAME, user=USER, password=PASSWORD) save_to_db(table, name2vector, db)
def recognition_images(): # initialize the local binary patterns descriptor along with # the data and label lists desc = LocalBinaryPatterns(24, 8) model = pickle.load(open("models/model.save", 'rb')) # loop over the testing images for imagePath in paths.list_images("faces/testing"): # load the image, convert it to grayscale, describe it, # and classify it image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) hist = desc.describe(gray) prediction = model.predict([hist])[0] # display the image and the prediction cv2.putText(image, prediction, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 3) cv2.imshow("Image", image) cv2.waitKey(0)
def run_iteration(iteration, hash_map): lbp = LocalBinaryPatterns(24, 8) data = [] labels = [] #Finding all images images = [os.path.join(root, name) for root, dirs, files in os.walk("../training_images") for name in files if name.endswith((".jpeg", ".jpg"))] #Spliting it into training and testing groups training, testing = train_test_split(images, test_size = 0.25) #Training Phase for imagePath in training: #Load the image, convert it to grayscale, and compute LBP image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if imagePath in hash_map: hist = hash_map[imagePath] else: hist = lbp.compute(gray) hash_map[imagePath] = hist print str(iteration) + " DEBUG(Training): Computed LBP Histogram for " + imagePath #Plotting histogram if needed #plt.bar(bin_edges[:-1], hist, width = 1) #plt.xlim(min(bin_edges), max(bin_edges)) #plt.show() #Extract the label from the image path, then update the label and data lists labels.append(imagePath.split("/")[-2]) data.append(hist) #Train classifier classifier = Classifier("SVM") print "\n\n" + str(iteration) + " DEBUG: Training Classifier" classifier.train(data, labels) print "\n\n" + str(iteration) + " DEBUG: Trained Classifier\n\n" #Testing Phase data = [] labels = [] for imagePath in testing: #Load the image, convert to grayscale, describe it and classify it image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if imagePath in hash_map: hist = hash_map[imagePath] else: hist = lbp.compute(gray) hash_map[imagePath] = hist print str(iteration) + " DEBUG(Testing): Computed LBP Histogram for " + imagePath data.append(hist) labels.append(imagePath.split("/")[-2]) print "\n\n" + str(iteration) + " DEBUG: Forming predictions" predictions = classifier.predict(data) counter = 0 print "\n\n" + str(iteration) + " DEBUG: Printing predictions\n\n" for index, prediction in enumerate(predictions): print "Name -> " + testing[index] + " Actual -> " + labels[index] + " Prediction -> " + prediction if labels[index] == prediction: counter = counter + 1 accuracy = (float(counter)/float(len(predictions))) * 100.0 print "\n\n" + str(iteration) + " The Classifier Accuracy was " + str(accuracy) + "%" return accuracy
from local_binary_patterns import LocalBinaryPatterns from classifier import Classifier import os import cv2 from sklearn.cross_validation import train_test_split import matplotlib.pyplot as plt #Caputuring the Image #---To be done--- lbp = LocalBinaryPatterns(24, 8) data = [] labels = [] #Finding all images training = [ os.path.join(root, name) for root, dirs, files in os.walk("../training_images") for name in files if name.endswith((".jpeg", ".jpg")) ] #Training Phase for imagePath in training: #Load the image, convert it to grayscale, and compute LBP image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) hist = lbp.compute(gray) #Plotting histogram if needed #plt.bar(bin_edges[:-1], hist, width = 1) #plt.xlim(min(bin_edges), max(bin_edges)) #plt.show()
from local_binary_patterns import LocalBinaryPatterns from classifier import Classifier import os import cv2 from sklearn.cross_validation import train_test_split #Caputuring the Image #---To be done--- lbp = LocalBinaryPatterns(24, 8) data = [] labels = [] #Finding all images training = [os.path.join(root, name) for root, dirs, files in os.walk("../training_images") for name in files if name.endswith((".jpeg", ".jpg"))] #Training Phase for imagePath in training: #Load the image, convert it to grayscale, and compute LBP image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) hist = lbp.compute(gray) #Extract the label from the image path, then update the label and data lists labels.append(imagePath.split("/")[-2]) data.append(hist) #Train classifier classifier = Classifier("Chi-Squared") classifier.train(data, labels)
from local_binary_patterns import LocalBinaryPatterns from sklearn.svm import LinearSVC from imutils import paths import pickle #opencv import cv2 # initialize the local binary patterns descriptor along with # the data and label lists desc = LocalBinaryPatterns(24, 8) data = [] labels = [] # loop over the training images for imagePath in paths.list_images("faces/training"): # load the image, convert it to grayscale, and describe it image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) hist = desc.describe(gray) # extract the label from the image path, then update the # label and data lists labels.append(imagePath.split("/")[-2]) data.append(hist) # train a Linear SVM on the data model = LinearSVC(C=100.0, random_state=42) model.fit(data, labels) pickle.dump(model, open("model.save", 'wb'))
from local_binary_patterns import LocalBinaryPatterns import os import cv2 import matplotlib.pyplot as plt from skimage import feature lbp = LocalBinaryPatterns(24, 8) data = [] #Finding all images images = [ os.path.join(root, name) for root, dirs, files in os.walk("../images") for name in files if name.endswith((".jpeg", ".jpg")) ] #Training Phase for imagePath in images: #Load the image, convert it to grayscale, and compute LBP image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) lbp = feature.local_binary_pattern(gray, 24, 8, method="uniform") cv2.imshow("LBP", lbp) cv2.waitKey(0) #Plotting histogram if needed #plt.bar(bin_edges[:-1], hist, width = 1) #plt.xlim(min(bin_edges), max(bin_edges)) #plt.show()