def getHOG3(imgs, ori=8, ppc=(4, 4), cpb=(4, 4)): # determine the shape of the output fd = hog(imgs[0, :, :, 0], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=False) # print fd.shape hogs = np.zeros((imgs.shape[0], fd.shape[0] * 3)) # HOG for i in range(imgs.shape[0]): # zimgs[i,:] = exposure.equalize_hist(imgs[i,:]) # imgs[i,:] = rank.equalize(imgs[i,:]/255,selem=disk(0)) # plt.imshow(imgs[i,:]),plt.show() hogs[i, 0 : fd.shape[0]] = hog( imgs[i, :, :, 0], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=False ) hogs[i, fd.shape[0] : (2 * fd.shape[0])] = hog( imgs[i, :, :, 1], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=False ) hogs[i, 2 * fd.shape[0] : (3 * fd.shape[0])] = hog( imgs[i, :, :, 2], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=False ) sys.stdout.write("\rIteration {0}/{1}".format((i + 1), imgs.shape[0])) sys.stdout.flush() mean = np.mean(hogs, axis=0) hogs -= mean return hogs
def extract_features(): des_type = 'HOG' # If feature directories don't exist, create them if not os.path.isdir(pos_feat_ph): os.makedirs(pos_feat_ph) # If feature directories don't exist, create them if not os.path.isdir(neg_feat_ph): os.makedirs(neg_feat_ph) print "Calculating the descriptors for the positive samples and saving them" for im_path in glob.glob(os.path.join(pos_im_path, "*")): #print im_path im = imread(im_path, as_grey=True) if des_type == "HOG": fd = hog(im, orientations, pixels_per_cell, cells_per_block, visualize, normalize) fd_name = os.path.split(im_path)[1].split(".")[0] + ".feat" fd_path = os.path.join(pos_feat_ph, fd_name) joblib.dump(fd, fd_path) print "Positive features saved in {}".format(pos_feat_ph) print "Calculating the descriptors for the negative samples and saving them" for im_path in glob.glob(os.path.join(neg_im_path, "*")): im = imread(im_path, as_grey=True) if des_type == "HOG": fd = hog(im, orientations, pixels_per_cell, cells_per_block, visualize, normalize) fd_name = os.path.split(im_path)[1].split(".")[0] + ".feat" fd_path = os.path.join(neg_feat_ph, fd_name) joblib.dump(fd, fd_path) print "Negative features saved in {}".format(neg_feat_ph) print "Completed calculating features from training images"
def SingleDecisionTreeClassifier(pix): print "\nCreating HOG Dataset from MNIST Data" start_time = time.time() training_image_data_hog = [hog(img, orientations=9, pixels_per_cell=(pix,pix), cells_per_block=(3, 3)) for img in training_image_data] testing_image_data_hog = [hog(img, orientations=9, pixels_per_cell=(pix, pix), cells_per_block=(3, 3)) for img in testing_image_data] end_time = time.time() - start_time print "It took "+ str(end_time) + " to make the HOG Images" print '\nTraining data' start_time = time.time() single_decision_tree_classifier = DecisionTreeClassifier() single_decision_tree_classifier.fit(training_image_data_hog, training_label_data) end_time = time.time() - start_time print "It took "+ str(end_time) + " to train the classifier" print 'Training Completed' print '\nTesting data ' start_time = time.time() single_decision_tree_classifier_accuracy = single_decision_tree_classifier.score(testing_image_data_hog, testing_label_data) end_time = time.time() - start_time print "It took "+ str(end_time) + " to test the data " # print '\n# printing Accuracy' print "\nTesting for Single Decision Tree Classifier with pixels per cell = ("+str(pix)+','+str(pix)+') :' print "-------------------------------------------------" print "\nSingleDecisionTreeClassifier accuracy for ("+str(pix)+','+str(pix)+") : "+ str(single_decision_tree_classifier_accuracy) return single_decision_tree_classifier_accuracy
def extract(self, image): features = np.array([]) vec = [] if 'raw' in self.features: vec = image.flatten() features = np.append(features, vec) vec = [] if 'textons' in self.features: import gen_histogram as tx vec = np.array(tx.histogram(image, self.centers)) features = np.append(features, vec) vec = [] if 'hog' in self.features: vec = hog(image, cells_per_block=(3, 3)) vec = np.append(vec, hog(image, cells_per_block=(4, 4))) vec = np.append(vec, hog(image, cells_per_block=(1, 1))) vec = np.append(vec, hog(image, cells_per_block=(2, 2))) features = np.append(features, vec) vec = [] if 'lbp' in self.features: vec = local_binary_pattern(image, 24, 3).flatten() features = np.append(features, vec) vec = [] if 'daisy' in self.features: vec = daisy(image).flatten() features = np.append(features, vec) return features
def classifier(): train_images,train_labels = processData('train') test_images,test_labels = processData('test') # sss = StratifiedShuffleSplit(train_labels, 3, test_size=0.5, random_state=0) # print sss # raw_input() # for train_index, test_index in sss: # print train_labels[train_index] # raw_input() train_images, train_labels = refineSets(train_images, train_labels, 1111) test_images, test_labels = refineSets(test_images, test_labels, 111) hog_train_images = [ hog(image) for image in train_images] hog_test_images = [ hog(image) for image in test_images] print 'Accuracy on test data : ' #DistanceMetric.get_metric(metric) forest_sizes = [100,200,300,400,500] for size in forest_sizes: clf = RandomForestClassifier(n_estimators=size,criterion='entropy',n_jobs=-1) clf.fit(hog_train_images,train_labels) print size,"->",clf.score(hog_test_images,test_labels)
def extractHOG(inputimg, showHOG=False): # convert image to single-channel, grayscale image = color.rgb2gray(inputimg) #extract HOG features if showHOG: fd, hog_image = feature.hog(image, orientations=36, pixels_per_cell=(16, 16), cells_per_block=(2, 2), visualise=showHOG) else: fd = feature.hog(image, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=showHOG) if(showHOG): fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True) ax1.axis('off') ax1.imshow(image, cmap=plt.cm.gray) ax1.set_title('Input image') ax1.set_adjustable('box-forced') # Rescale histogram for better display hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02)) ax2.axis('off') ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray) ax2.set_title('Histogram of Oriented Gradients') ax1.set_adjustable('box-forced') plt.show() return fd
def getHOG(imgs, ori=8, ppc=(4, 4), cpb=(4, 4), vis=True): # determine the shape of the output if vis: fd, im = hog(imgs[0, :], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=vis) imgs2 = imgs else: fd = hog(imgs[0, :], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=vis) hogs = np.zeros((imgs.shape[0], fd.shape[0])) # HOG for i in range(imgs.shape[0]): # zimgs[i,:] = exposure.equalize_hist(imgs[i,:]) # imgs[i,:] = rank.equalize(imgs[i,:]/255,selem=disk(0)) # plt.imshow(imgs[i,:]),plt.show() if vis: hogs[i, :], imgs2[i] = hog( imgs[i, :], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=vis ) else: hogs[i, :] = hog(imgs[i, :], orientations=ori, pixels_per_cell=ppc, cells_per_block=cpb, visualise=vis) sys.stdout.write("\rIteration {0}/{1}".format((i + 1), imgs.shape[0])) sys.stdout.flush() mean = np.mean(hogs, axis=0) hogs -= mean if vis: return hogs, imgs2 else: return hogs
def get_hog_features(img, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True): """ Extract the HOG features from the input image. Parameters: img: Input image. orient: Number of orientation bins. pix_per_cell: Size (in pixels) of a cell. cell_per_block: Number of cells in each block. vis: Visualization flag. feature_vec: Return the data as a feature vector. """ if vis == True: features, hog_image = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), transform_sqrt=True, visualise=vis, feature_vector=feature_vec) return features, hog_image else: features = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), transform_sqrt=True, visualise=vis, feature_vector=feature_vec) return features
def transfer_to_hog(file_name): if file_name =='train': csv_file_object = csv.reader(open('train.csv', 'rb')) hog_write = csv.writer(open('hog.csv', 'wb')) header = csv_file_object.next() imsize=(28,28) for row in csv_file_object: hogr=[] hogr.append(int(row[0])) image=map(int,row[1:]) image=np.reshape(image, imsize) fd= hog(image, orientations=8, pixels_per_cell=(4, 4), cells_per_block=(1, 1)) hogr.extend(fd) fd= hog(image[2:26,2:26], orientations=8, pixels_per_cell=(4, 4), cells_per_block=(1, 1)) hogr.extend(fd) hog_write.writerow(hogr) if file_name =='test': csv_file_object = csv.reader(open('test.csv', 'rb')) hog_write = csv.writer(open('hog_test.csv', 'wb')) header = csv_file_object.next() imsize=(28,28) for row in csv_file_object: hogr=[] image=map(int,row) image=np.reshape(image, imsize) fd= hog(image, orientations=8, pixels_per_cell=(4, 4), cells_per_block=(1, 1)) hogr.extend(fd) fd= hog(image[2:26,2:26], orientations=8, pixels_per_cell=(4, 4), cells_per_block=(1, 1)) hogr.extend(fd) hog_write.writerow(hogr)
def ComputeDescriptors(self,RGB,Depth,dep_mask,h): dep = np.float32(Depth) dep_mask =cv2.bitwise_not(dep_mask) ret, mask = cv2.threshold(dep, 1.7, 1, cv2.THRESH_BINARY_INV) mask = np.uint8(mask) ret, mask2 = cv2.threshold(dep, 0.01, 1, cv2.THRESH_BINARY) mask2 = np.uint8(mask2) mask = cv2.bitwise_and(mask,mask2) mask = cv2.bitwise_and(mask,dep_mask) if h: masked_data = cv2.bitwise_and(RGB, RGB, mask=mask) masked_data = cv2.bitwise_and(masked_data, masked_data, mask=mask2) sp = cv2.cvtColor(masked_data, cv2.COLOR_RGB2GRAY) sp = cv2.GaussianBlur(sp, (5, 5),10) fd, imn = hog(dep, self.orientations, self.pixels_per_cell, self.cells_per_block, self.visualize, self.normalize) if self.HogDepth: fdn,im = hog(sp, self.orientations, self.pixels_per_cell, self.cells_per_block, self.visualize, self.normalize) fd = np.concatenate((fd, fdn)) else: fd = [] fgrid = np.array([]) for i in xrange(4): for j in xrange(4): sub = RGB[25*i:25*(i+1),25*j:25*(j+1)] sub_mask = mask[25*i:25*(i+1),25*j:25*(j+1)] fsub = self.ComputeHC(sub,sub_mask) fgrid = np.concatenate((fgrid,fsub)) fd2 = fgrid.copy() return fd,fd2,masked_data
def test_img(svm, img_path, scales, subwindow=None): base_img = cv2.imread(img_path) prev_img_path = utils.get_prev_img(img_path) base_prev_img = cv2.imread(prev_img_path) windows = [] windows_features = [] sc = [] for scale in scales: img = cv2.resize(base_img, (0, 0), fx=scale, fy=scale) img_bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) prev_img = cv2.resize(base_prev_img, (0, 0), fx=scale, fy=scale) prev_img_bw = cv2.cvtColor(prev_img, cv2.COLOR_BGR2GRAY) height, width, _ = img.shape flow = cv2.calcOpticalFlowFarneback(prev_img_bw, img_bw, 0.5, 3, 15, 3, 5, 1.2, 0) hsv = np.zeros_like(img) hsv[..., 1] = 255 mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180/ np.pi / 2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) flowRGB = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) flow_bw = cv2.cvtColor(flowRGB, cv2.COLOR_BGR2GRAY) if subwindow == None: nsx, nsy, nw, nh = 0, 0, width, height else: nsx, nsy, nw, nh = utils.getDetectionWindow(subwindow, width, height, scale) for x in range(nsx, nsx + nw - 64, 16): for y in range(nsy, nsy + nh - 128, 16): img_crop = img_bw[y:y + 128, x:x + 64] hog_gray = hog(img_crop, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False) flow_crop = flow_bw[y:y + 128, x:x + 64] fd_flow = hog(flow_crop, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False) fd = hog_gray + fd_flow windows.append((x, y)) windows_features.append(fd) sc.append(scale) classes = svm.predict(windows_features) results = [] for i in range(0, len(windows)): if classes[i] == 1: scale = sc[i] results.append((int(windows[i][0] / scale), int(windows[i][1] / scale), int(64 / scale), int(128 / scale))) return results
def test_hog_output_equivariance_multichannel(): img = data.astronaut() img[:, :, (1, 2)] = 0 hog_ref = feature.hog(img, multichannel=True, block_norm='L1') for n in (1, 2): hog_fact = feature.hog(np.roll(img, n, axis=2), multichannel=True, block_norm='L1') assert_almost_equal(hog_ref, hog_fact)
def get_spat_arrng_ftrs(gray_img): # resize img to 600 * 600 resized_img = transform.resize(gray_img, (600,600)) left = resized_img.transpose()[:300].transpose() right = resized_img.transpose()[300:].transpose() I_anti = np.identity(600)[::-1] # anti - diagonal identity matrix inner = feature.hog(left) - feature.hog(I_anti.dot(right)) return dict(symmetry = np.linalg.norm(inner))
def get_set(metadataFile, classType): set = [] with open(metadataFile, "r") as f: entries = f.readlines() for entry in entries: entry = entry.split() filePath = entry[0] x, y, scale = int(entry[1]), int(entry[2]), float(entry[3]) img = cv2.imread(filePath) img = cv2.resize(img, (0, 0), fx=scale, fy=scale) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_gray_crop = img_gray[y:y+128, x:x+64] hog_gray = hog(img_gray_crop, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False) prevFilePath = utils.get_prev_img(filePath) prev_img = cv2.imread(prevFilePath) prev_img = cv2.resize(prev_img, (0, 0), fx=scale, fy=scale) prev_img_gray = cv2.cvtColor(prev_img, cv2.COLOR_BGR2GRAY) flow = cv2.calcOpticalFlowFarneback(prev_img_gray, img_gray, 0.5, 3, 15, 3, 5, 1.2, 0) # flowx, flowy = flow[..., 0], flow[..., 1] # flowx_crop, flowy_crop = flowx[y:y+128, x:x+64], flowy[y:y+128, x:x+64] # # hog_flow_x = hog(flowx_crop, orientations=9, pixels_per_cell=(8, 8), # cells_per_block=(2, 2), visualise=False) # hog_flow_y = hog(flowy_crop, orientations=9, pixels_per_cell=(8, 8), # cells_per_block=(2, 2), visualise=False) hsv = numpy.zeros_like(img) hsv[..., 1] = 255 mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180/ numpy.pi / 2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) flowRGB = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) flow_gray = cv2.cvtColor(flowRGB, cv2.COLOR_BGR2GRAY) flow_gray_crop = flow_gray[y:y+128, x:x+64] hog_flow = hog(flow_gray_crop, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False) desc = hog_gray + hog_flow set.append(desc) return set, [classType] * len(entries)
def test_img_new(svm, img_path, scales, subwindow=None): base_img = cv2.imread(img_path) prev_img_path = utils.get_prev_img(img_path) base_prev_img = cv2.imread(prev_img_path) windows = [] windows_features = [] sc = [] for scale in scales: img = cv2.resize(base_img, (0, 0), fx=scale, fy=scale) img_bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) prev_img = cv2.resize(base_prev_img, (0, 0), fx=scale, fy=scale) prev_img_bw = cv2.cvtColor(prev_img, cv2.COLOR_BGR2GRAY) height, width, _ = img.shape flow = cv2.calcOpticalFlowFarneback(prev_img_bw, img_bw, 0.5, 3, 15, 3, 5, 1.2, 0) flowx, flowy = flow[..., 0], flow[..., 1] if subwindow == None: nsx, nsy, nw, nh = 0, 0, width, height else: nsx, nsy, nw, nh = utils.getDetectionWindow(subwindow, width, height, scale) for x in range(nsx, nsx + nw - 64, 16): for y in range(nsy, nsy + nh - 128, 16): img_crop = img_bw[y:y + 128, x:x + 64] hog_gray = hog(img_crop, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False) flowx_crop, flowy_crop = flowx[y:y+128, x:x+64], flowy[y:y+128, x:x+64] hog_flow_x = hog(flowx_crop, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False) hog_flow_y = hog(flowy_crop, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=False) fd = numpy.concatenate((hog_gray, hog_flow_x, hog_flow_y)) windows.append((x, y)) windows_features.append(fd) sc.append(scale) classes = svm.predict(windows_features) results = [] for i in range(0, len(windows)): if classes[i] == 1: scale = sc[i] results.append((int(windows[i][0] / scale), int(windows[i][1] / scale), int(64 / scale), int(128 / scale))) return results
def get_hog_features(img, params): if params['vis'] == True: features, hog_image = hog(img, orientations=params['n_orient_bins'], pixels_per_cell=params['pixels_per_cell'], cells_per_block=params['cell_per_block'], transform_sqrt=False, visualise=params['vis'], feature_vector=params['collapse_feat']) return features, hog_image else: features = hog(img, orientations=params['n_orient_bins'], pixels_per_cell=params['pixels_per_cell'], cells_per_block=params['cell_per_block'], transform_sqrt=False, visualise=params['vis'], feature_vector=params['collapse_feat']) return features
def extract_features(self, sample): kwargs = dict(normalize=self.normalize, orientations=self.orientations, pixels_per_cell=self.pixels_per_cell, cells_per_block=self.cells_per_block) features = hog(sample[:, :, 0], **kwargs) features += hog(sample[:, :, 1], **kwargs) features += hog(sample[:, :, 2], **kwargs) return features
def calculate_hog_features(raw_training_data): num_train = len(raw_training_data) test = raw_training_data[0] test = test.reshape((28,28)) fd = hog(test, orientations=12, pixels_per_cell=(7,7), cells_per_block=(1,1), visualise=False) hf_length = len(fd) hog_features = np.zeros((num_train,hf_length)) for i in range(num_train): temp = raw_training_data[i] temp = temp.reshape((28,28)) fd = hog(temp, orientations=12, pixels_per_cell=(7,7), cells_per_block=(1,1), visualise=False) hog_features[i] = fd return hog_features
def get_hog_features(img, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True): # Call with two outputs if vis==True if vis == True: features, hog_image = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), transform_sqrt=False, visualise=vis, feature_vector=feature_vec) return features, hog_image # Otherwise call with one output else: features = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), transform_sqrt=False, visualise=vis, feature_vector=feature_vec) return features
def test_histogram_of_oriented_gradients(): img = img_as_float(data.astronaut()[:256, :].mean(axis=2)) fd = feature.hog(img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(1, 1)) assert len(fd) == 9 * (256 // 8) * (512 // 8)
def getFeature(image, blocks=5): ''' Given an cv2 Image object it returns its feature vector. Args: image (ndarray): image to process. blocks (int, optional): number of block to subdivide the RGB space into. Returns: numpy array. ''' image_resized = cv2.resize(image, (64, 64)) imgray = cv2.cvtColor(image_resized, cv2.COLOR_RGB2GRAY) feature = np.zeros(blocks * blocks * blocks + 144) # 144 for hog width, height, channel = image_resized.shape pixel_count = width * height r = ((image[:, :, 2]) / (256 / blocks)).astype("int") g = ((image[:, :, 1]) / (256 / blocks)).astype("int") b = ((image[:, :, 0]) / (256 / blocks)).astype("int") result = r + g * blocks + b * blocks * blocks unique, counts = np.unique(result, return_counts=True) feature[unique] = counts feature = feature / (pixel_count) feature_hog = hog( imgray, orientations=9, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=False) feature[blocks**3:] = feature_hog return feature
def hogKmeansClustering(param, orientNum, DATA_FOLDER): ts = ListaSet(param) print "{} images in total".format(ts.get_num_images()) clustNum = 5 # d1 = {} # Patches = np.empty([ts.get_num_images(), 13, 13 ], dtype=np.float32) PatchArr = np.empty([ ts.get_num_images(), orientNum]) for i in range( ts.get_num_images()): # Patches[i,:,:] = ts.get_input(i) PatchArr[i, :] = hog( ts.get_input(i), orientations=orientNum, pixels_per_cell=(13, 13), cells_per_block=(1, 1), visualise=False) # for 5x5 centered at 13x13 input # PatchArr[i, :] = hog( ts.get_input(i)[4:9, 4:9], orientations=orientNum, pixels_per_cell=(5, 5), cells_per_block=(1, 1), visualise=False) # for 17x17 input # PatchArr[i, :] = hog( ts.get_input(i)[6:11, 6:11], orientations=orientNum, pixels_per_cell=(5, 5), cells_per_block=(1, 1), visualise=False) if i % 10000 == 0: print i print "load existing codebook..." codebook = np.load( DATA_FOLDER+'/4bins_5cls/codebookArtificial2.npy') print codebook print "assign codes for patches..." code, dist = scipy.cluster.vq.vq( PatchArr, codebook) d1 = {} for i in range( clustNum): d1['label'+str(i+1)+'_idx'] = np.where( code == i)[0].astype( np.uint32) d1['labelArr'] = code.astype( np.uint32) # d1['PatchArr'+str(i+1)] = Patches[ np.where( code == i)[0], :, :].astype( np.uint32) # d1['PatchArr'] = Patches scipy.io.savemat( DATA_FOLDER+'/4bins_5cls/trainlabelsArtificial13code2_sps100_r8.mat', d1)
def cal_hog(image,orientations=9, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=True): # fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16), # cells_per_block=(1, 1), visualise=True) fd, hog_image = hog(image, orientations, pixels_per_cell, cells_per_block, visualise) return fd,hog_image
def print_hog_image(image): """ image is expected to be in it's original format function prints hog image """ print image.shape image = color.rgb2gray(image) fd, hog_image = hog(image, orientations=8, pixels_per_cell=(4, 4), cells_per_block=(1, 1), visualise=True, normalise=True) print "finished hog..." fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True) ax1.axis('off') ax1.imshow(image, cmap=plt.cm.gray) ax1.set_title('Input image') ax1.set_adjustable('box-forced') # Rescale histogram for better display hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02)) ax2.axis('off') ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray) ax2.set_title('Histogram of Oriented Gradients') ax1.set_adjustable('box-forced') plt.show()
def learn_hog(img): img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) global n_bin global b_size global c_size global hog_list global labels w, l = np.shape(img) img_list = list() img_list.append(img) img_list.append(img[5:w - 2, 7:l - 6]) img_list.append(img[4:, :]) img_list.append(img[0:w-4, :]) img_list.append(img[:, 4:]) img_list.append(img[:, :l-4]) img_list.append(img[7:, :]) img_list.append(img[0:w-7, :]) img_list.append(img[:, 7:]) img_list.append(img[:, :l-7]) img_list.append(img[12:, :]) img_list.append(img[0:w-12, :]) img_list.append(img[:, 12:]) img_list.append(img[:, :l-12]) index = 0 global show for imgs in img_list: imgs = cv2.resize(imgs, (115, 120), interpolation=cv2.INTER_AREA) # resize image if SHOW == 1: cv2.imshow('img' + str(index), imgs) index += 1 HOG_LIST.append(hog(imgs, orientations=n_bin, pixels_per_cell=(c_size, c_size), cells_per_block=(b_size / c_size, b_size / c_size), visualise=False)) labels.append(label)
def plothog(k1): infile=os.path.join('C:\meenuneenu\project\libsvm-3.17\python\data',"IMG-%s.png" % k1) #infile="C:/Python27/Lib/site-packages/temp/svm/window/window4.jpg" #img = Image.open(infile) img = Image.open(infile) #image = color.rgb2gray(data.lena()) """ width = 50 height = 50 # Resize it. img = img.resize((width, height), Image.BILINEAR) # Save it back to disk. img.save(os.path.join("C:/Python27/Lib/site-packages/temp/svm/", 'resized.png')) """ img = img.convert('1') img.save("C:/Python27/Lib/site-packages/temp/IMG-4.png") fd = list(hog(img, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=False)) #filepath=os.path.join('C:/Python27/Lib/site-packages/temp',"data%s.txt" % k1) filepath=os.path.join('C:\meenuneenu\project\libsvm-3.17\python\data',"data%s.txt" % k1) file = open(filepath, "w") for item in fd: file.write("%s " % item) file.close() #delpath=os.path.join('rm C:/Python27/Lib/site-packages/temp/',"IMG-%s.png" % k) os.system('rm C:/Python27/Lib/site-packages/temp/IMG-4.png')
def do_hog(im): img = color.rgb2gray(im) fd, hog_image = hog(img, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=True) hog_image_rescaled = hog_image # exposure.rescale_intensity(hog_image, in_range=(0, 0.02)) return hog_image_rescaled
def neg_hog_rand(path, num_samples, window_size, num_window_per_image): rows = window_size[0] cols = window_size[1] features = [] cnt = 0 for dirpath, dirnames, filenames in walk(path): for my_file in filenames: if cnt < num_samples: print cnt,my_file cnt = cnt + 1 im = cv2.imread(path + my_file) image = color.rgb2gray(im) image_rows = image.shape[0] image_cols = image.shape[1] for i in range(0,num_window_per_image): x_min = random.randrange(0,image_rows - rows) y_min = random.randrange(0,image_cols - cols) x_max = x_min + rows y_max = y_min + cols image_hog = image[x_min:x_max , y_min:y_max] my_feature, _ = hog(image_hog, orientations=9, pixels_per_cell=(8, 8),cells_per_block=(2, 2), visualise=True) features.append(my_feature) return features
def extractPHOGFeature(image, level1, level2): ''' Extract PHOG feature from image ''' height, width = image.shape orientationNum = 9 pHOGVec = np.array([]) widthBase = 4 heightBase = 3 for l in xrange(level1, level2 + 1): multi = 2 ** (l - 1) widthGridNum = widthBase * multi heightGridNum = heightBase * multi featureVector, hogImage = hog(image, orientations = orientationNum, pixels_per_cell = (width/widthGridNum, height/heightGridNum), cells_per_block=(1, 1), visualise = True) featureVector = featureVector[np.newaxis].transpose() # transpose to column vector if pHOGVec.size == 0: pHOGVec = featureVector.copy() else: pHOGVec = np.vstack((pHOGVec, featureVector)) return pHOGVec
def getFeat(Data, mode, fileNames, positive): num = 0 for image in Data: gray = rgb2gray(image) fd, hog_image = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize, feature_vector) if(visualize): visualize(data, hog_image) fd_name = str(fileNames[num]) + '.feat' # set file name if mode == 'train': if positive == True: fd_path = os.path.join('./features/train/positive/', fd_name) else: fd_path = os.path.join( './features/train/negative/', fd_name) else: if positive == True: fd_path = os.path.join('./features/test/positive/', fd_name) else: fd_path = os.path.join('./features/test/negative/', fd_name) joblib.dump(fd, fd_path, compress=3) # save data to local num += 1 print("%d saving: ." % (num))
ax1.axis('off') ax2.imshow(image_gb5, vmin=image.min(), vmax=image.max(), cmap='gray') ax2.set_title('gaussian blur 5') ax2.axis('off') fig.tight_layout() fig.savefig('image-gaussian_blur.png') # In[35]: #hog feature (hog, hog_image) = feature.hog(image_gb3, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(4, 4), block_norm='L2-Hys', visualize=True, transform_sqrt=True) plt.imshow(image_raw) plt.imshow(hog_image, alpha=0.7, cmap='hsv') plt.show() #plt.imwrite('hog_gb.jpg', hog_image*255) # In[162]: img_rgb = image_gb5 # In[163]:
def extract_hog_feature(self, image): hog_feature = hog(image, orientations=9, pixels_per_cell=(16, 16), cells_per_block=(2, 2)) return hog_feature
data = [] labels = [] #Read all images in training directory print("Computing HOG...") for file in paths.list_images(args["training"]): file = file.replace('\\', '') #This treat if has some space in image name img_original = cv2.imread(file) img = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY) #Image in grayscale #Hog method (H, hogImage) = hog(img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), transform_sqrt=True, visualise=True, block_norm='L2-Hys') #Call this function to print the HOG image #print_hog(hogImage, file.split('/')[-1], img_original) labels.append(file.split('/')[-2]) data.append(H) mode = args["machineLearning"] #Training a model if (int(mode) == 2): print("Training a Model with SVC...")
# find contours in the edge map, keeping only the largest one which # is presmumed to be the car logo cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key=cv2.contourArea) # extract the logo of the car and resize it to a canonical width # and height (x, y, w, h) = cv2.boundingRect(c) logo = gray[y:y + h, x:x + w] logo = cv2.resize(logo, (224, 224)) # extract Histogram of Oriented Gradients from the logo H = feature.hog(logo, orientations=9, pixels_per_cell=(10, 10), cells_per_block=(2, 2), feature_vector=True,transform_sqrt=True, block_norm="L1") # update the data and labels data.append(H) labels.append(make) # "train" the nearest neighbors classifier print("[INFO] training classifier...") model = KNeighborsClassifier(n_neighbors=1) model.fit(data, labels) print("[INFO] evaluating...") # loop over the test dataset for (i, imagePath) in enumerate(paths.list_images(args["test"])): # load the test image, convert it to grayscale, and resize it to
print("brightness features") brightness = util.loading_map(extraction.calculateDarktoBrightRatio, resized) print("hsv 11 + std") hsv_11_std = util.loading_map( lambda x: extraction.split_image_features( lambda y: extraction.color_features(y, mean=True, std=True), 11, x), hsv) print("luv features") luv_features = util.loading_map(lambda x: extraction.pixel_features(x, 11), luv) print("hog features") hog = util.loading_map( lambda x: feature.hog(x, orientations=6, pixels_per_cell=(12, 12), cells_per_block=(8, 8), normalise=True), grayscaled) print("corner features") corners = util.loading_map( lambda x: extraction.pixel_features( numpy.sqrt(feature.corner_shi_tomasi(x, sigma=8)), 8), grayscaled) n_folds = 10 model = Pipeline([("standard scaler", StandardScaler()), ("logistic regression", LogisticRegression(solver='lbfgs', multi_class='multinomial'))]) print("Evaluating brightness features")
def HOG_SVM_detector(image_path, model_path): ori_img = cv2.imread(image_path) if type(model_path) == str: model = joblib.load(model_path) else: model = model_path # print(type(model_path)==str) # 将图像裁剪到最大宽度为400个像素,之所以降低我们的图像维度(其实就是之所以对我们的图像尺寸进行裁剪)主要有两个原因: # 1. 减小图像的尺寸可以减少在图像金字塔中滑窗的数目,如此可以降低检测的时间,从而提高整体检测的吞吐量。 # 2. 调整图像的尺寸能够整体提高行人检测的精度。 minAxis_Image = 400 img = imutils.resize(ori_img, width=min(minAxis_Image, ori_img.shape[1])) # 修改的图像是以最小的那个数字对齐 # 用于之后复原图片 HO, WO, _ = ori_img.shape HR, WR, _ = img.shape # 改 程序运行时间过长,将参数改大,注释中是 论文参数 # 这个设置是按照开创性的 Dalal 和 Triggs 论文来设置的 Histograms of Oriented Gradients for Human Detection win_size = (64, 128) # 窗口的大小固定在64*128像素大小 step_size = (5, 5) # 一个分别在x方向和y方向步长为(4,4)像素大小的滑窗 downscale = 1.2 # 尺度 scale=1.05 的图像金字塔 threshold = 0.6 # 设定 SVM 预测的阈值,即只有当预测的概率大于 0.6 时,才会确定支持向量机的预测 overlapThresh = 0.3 # nms rectangles = [] scores = [] scale = 0 for img_scale in pyramid_gaussian(img, downscale=downscale): if img_scale.shape[0] < win_size[1] or img_scale.shape[1] < win_size[0]: break for (x, y, img_window) in sliding_window(img_scale, win_size, step_size): if img_window.shape[0] != win_size[1] or img_window.shape[ 1] != win_size[ 0]: # ensure the sliding window has met the minimum size requirement continue fd = hog(rgb2gray(img_window), orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2)) fd = fd.reshape(1, -1) predict = model.predict(fd) score = model.decision_function(fd)[0] if predict == 1 and score > threshold: x = int(x * (downscale**scale)) y = int(y * (downscale**scale)) w = int(win_size[0] * (downscale**scale)) h = int(win_size[1] * (downscale**scale)) rectangles.append((x, y, x + w, y + h)) scores.append(score) scale += 1 rectangles = np.array(rectangles) scores = np.array(scores) bounding_boxes = non_max_suppression(rectangles, probs=scores, overlapThresh=overlapThresh) # print("rectangles.shape : ", rectangles.shape) # print("bounding_boxes.shape : ", bounding_boxes.shape) # 如果检测到了内容 if rectangles.shape[0] > 0: # 复原 rectangles = rectangles.astype(float) rectangles[:, [0, 2]] *= (WO / WR) rectangles[:, [1, 3]] *= (HO / HR) rectangles = rectangles.astype(int) bounding_boxes = bounding_boxes.astype(float) bounding_boxes[:, [0, 2]] *= (WO / WR) bounding_boxes[:, [1, 3]] *= (HO / HR) bounding_boxes = bounding_boxes.astype(int) # print("rectangles.shape : ", rectangles.shape) # print("bounding_boxes.shape : ", bounding_boxes.shape) # print(bounding_boxes) # return rectangles, bounding_boxes return bounding_boxes
labels = [] #fill the training dataset # the flow is # 1) load sample # 2) resize it to (200,200) so that we have same size for all the images # 3) get the HOG features of the resized image # 4) save them in the data list that holds all the hog features # 5) also save the label (target) of that sample in the labels list for filename in brian_filenames: #read the images image = imread(filename, 1) #flatten it image = imresize(image, (200, 200)) hog_features = hog(image, orientations=12, pixels_per_cell=(16, 16), cells_per_block=(1, 1)) data.append(hog_features) labels.append(0) print 'Finished adding brians samples to dataset' for filename in alfin_filenames: image = imread(filename, 1) image = imresize(image, (200, 200)) hog_features = hog(image, orientations=12, pixels_per_cell=(16, 16), cells_per_block=(1, 1)) data.append(hog_features) labels.append(1) print 'Finished adding alfins samples to dataset'
def get_hog(img): hog_image = hog(img, orientations=10, pixels_per_cell=(5, 5), cells_per_block=(3, 3)) return exposure.rescale_intensity(hog_image, in_range=(0, 0.9))
def calculate_hog(img): return feature.hog(img, orientations=8, pixels_per_cell=(32, 32), cells_per_block=(8, 8), block_norm='L2-Hys')
# read the image img = cv2.imread("dataset/digits.png") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # break up the image into individual digits cells = [np.hsplit(row, 100) for row in np.vsplit(gray, 50)] # compute the HOG features for each digit and make a dataset dataset = [] for i in range(50): for j in range(100): feature = hog(cells[i][j], pixels_per_cell=(10, 10), cells_per_block=(1, 1)) dataset.append(feature) dataset = np.array(dataset, 'float64') # make labels for all 5000 digits labels = [] for i in range(10): labels = labels + [i] * 500 # use the KNN algorithm for training clf = KNeighborsClassifier() x_train, x_test, y_train, y_test = tts(dataset, labels, test_size=0.3)
im_gray1) # Extract out the object and place into output imag out[mask == 255] = im_gray1[mask == 255] lower_value = np.array([60]) upper_value = np.array([200]) # Threshold the HSV image to get only blue colors mask = cv2.inRange(out, lower_value, upper_value) # Bitwise-AND mask and original image out = cv2.bitwise_and(out, out, mask=mask) v = out[out[:, :] > 0] # print(v.mean()) out[out[:, :] < (u.mean())] = 255 out[out[:, :] > (u.mean() + 10)] = 0 fd = hog(out.reshape((512, 512)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False) features.append(fd) hog_features = np.array(features, 'float64') labels.append("true") # cv2.imshow("TestImageGry", im_gray) # cv2.imshow("TestImage", imq) i = i + 1 # cv2.waitKey() # inp=input() print(i) if i is 32: test = False
# curImageCropped.save("processed_alpha_test/" + imagename) # Crop images to 900 x 900 # img2 = curImage.crop((0, 0, 900, 900)) # img2.save("processed_alpha_test/img_a.png") # Save image > matrix # Save label list_hog_fd = [] for feature in letter_image_features: print "feature", feature fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False) list_hog_fd.append(fd) hog_features = np.array(list_hog_fd, 'float64') print "Count of digits in dataset", Counter(labels) # Create an linear SVM object clf = LinearSVC() # Perform the training clf.fit(hog_features, labels) # Save the classifier joblib.dump(clf, "digits_cls_alpha1.pkl", compress=3)
if testEdgeDetection: edge = cv2.Canny(cv2.cvtColor(im, cv2.COLOR_RGB2GRAY),100,300) plot.figure(plotIndex) plotIndex += 1 plot.clf() plot.imshow(edge) # ************************************************* if trainClassifier: l = [] for i in range(len(trainingImages)): if trainFromHSVImage: l.append(labels[i]) hsv = cv2.cvtColor(trainingImages[i], cv2.COLOR_RGB2HSV) feat = hog(hsv[:,:,1], orientations=5, pixels_per_cell=(7, 7), cells_per_block=(2, 2), block_norm='L1-sqrt', visualise=False) normalizedFeatures = StandardScaler().fit(feat.reshape(-1, 1)).transform(feat.reshape(-1, 1)) if (i == 0): features = normalizedFeatures else: features = np.column_stack((features, normalizedFeatures)) if trainFromEdgeImage: l.append(labels[i]) edge = cv2.Canny(cv2.cvtColor(trainingImages[i], cv2.COLOR_RGB2GRAY),50,100) feat = hog(edge, orientations=5, pixels_per_cell=(7, 7), cells_per_block=(2, 2), block_norm='L1-sqrt', visualise=False) normalizedFeatures = StandardScaler().fit(feat.reshape(-1, 1)).transform(feat.reshape(-1, 1)) if (i == 0) and not (trainFromHSVImage): features = normalizedFeatures else: features = np.column_stack((features, normalizedFeatures)) svm = SVC(kernel='rbf').fit(features.T,np.array(l))
f = os.popen("ls /dev/ttyACM*") usbPort = f.read() usbPort = usbPort[0:len(usbPort) - 1] ser = serial.Serial(usbPort, 9600) print("connect succesfully") while True: img = cv2.imread('image.jpg', 0) mi = 1.0 xx = 0 yy = 0 img = cv2.flip(img, 1) img = cv2.resize(img, (160, 120)) for ii in range(16): for jj in range(9): frame = img[jj * 8:jj * 8 + 56, ii * 8:ii * 8 + 40] out = feature.hog(frame, visualise=False, feature_vector=True) compare = arr - out x = 0.0 for i in compare: x += i**2 x /= 1215.0 x = math.sqrt(x) if x < mi: mi = x xx = ii yy = jj print('min:', mi, ' at ', xx, '-', yy) if mi <= maxCorrect: if yy > high: cv2.rectangle(img, (xx * 8 - 1, yy * 8 - 1), (xx * 8 + 40, yy * 8 + 56), (255, 255, 255))
from sklearn.model_selection import train_test_split from sklearn import svm, datasets from skimage import color,transform import matplotlib.image as mpimg from skimage.feature import hog X = [] Y = [] dataset_size = 12500 datasetpath = "dataset/train/" for i in range(dataset_size): img_name = datasetpath+"cat." + str(i) + '.jpg' img_cat = color.rgb2gray(mpimg.imread(img_name)) img = transform.resize(img_cat, (128,64) , mode='constant') fcat, hog_img = hog(img, orientations = 9, pixels_per_cell = (8,8),cells_per_block = (2,2), visualise = True , block_norm='L2-Hys') X.append(fcat) Y.append(0) for i in range(dataset_size): img_name = datasetpath +"dog." + str(i) + '.jpg' img_dog = color.rgb2gray(mpimg.imread(img_name)) img = transform.resize(img_dog, (128, 64) , mode='constant') fdog, hog_img = hog(img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualise=True , block_norm='L2-Hys') X.append(fdog) Y.append(1) X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.30) svc = svm.SVC(kernel='poly', C=1 , degree=1).fit(X_train, y_train)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Mar 25 21:56:30 2021 @author: batuhan """ import matplotlib.pyplot as plt from skimage.feature import hog from skimage import data, exposure image = data.astronaut() fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualize=True, multichannel=True) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True) ax1.axis('off') ax1.imshow(image, cmap=plt.cm.gray) ax1.set_title('Input image') # Rescale histogram for better display hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10)) ax2.axis('off') ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray) ax2.set_title('Histogram of Oriented Gradients') plt.show()
def testImage(path, args): # Read the image #im = imread(args["image"], as_grey=False) im = cv2.imread(path, cv2.IMREAD_GRAYSCALE) # read img from file min_wdw_sz = (24, 48) step_size = (5, 5) downscale = args['downscale'] visualize_det = args['visualize'] # Load the classifier clf = joblib.load("../myData/traffic.model6") # List to store the detections detections = [] # The current scale of the image scale = 0 # Downscale the image and iterate for im_scaled in pyramid_gaussian(im, downscale=downscale): # This list contains detections at the current scale cd = [] # If the width or height of the scaled image is less than # the width or height of the window, then end the iterations. if im_scaled.shape[0] < min_wdw_sz[1] or im_scaled.shape[ 1] < min_wdw_sz[0]: break for (x, y, im_window) in sliding_window(im_scaled, min_wdw_sz, step_size): if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[ 1] != min_wdw_sz[0]: continue # Calculate the HOG features fd = hog(im_window, orientations, pixels_per_cell, cells_per_block, block_norm="L1", visualise=False, transform_sqrt=True, feature_vector=True) fd = [fd] # clf wants a list of samples #pred = clf.predict(fd) probs = list(clf.predict_proba(fd)[0]) #print(pred) pred = probs.index(max(probs)) # index of max # TODO: use all my labels if pred != 0 and probs[pred] > 0.80: print("prediction is " + str(pred)) print(probs) #plt.imshow(im_window) #plt.show() print("Detection:: Location -> ({}, {})".format(x, y)) print("Scale -> {} | Confidence Score {} \n".format( scale, probs[pred])) detections.append((x, y, probs[pred], int(min_wdw_sz[0] * (downscale**scale)), int(min_wdw_sz[1] * (downscale**scale)))) cd.append(detections[-1]) # If visualize is set to true, display the working # of the sliding window if visualize_det: clone = im_scaled.copy() for x1, y1, _, _, _ in cd: # Draw the detections at this scale cv2.rectangle( clone, (x1, y1), (x1 + im_window.shape[1], y1 + im_window.shape[0]), (0, 0, 0), thickness=2) cv2.rectangle(clone, (x, y), (x + im_window.shape[1], y + im_window.shape[0]), (255, 255, 255), thickness=2) cv2.imshow("Sliding Window in Progress", clone) cv2.waitKey(30) # Move the the next scale scale += 1 # Display the results before performing NMS clone = im.copy() for (x_tl, y_tl, _, w, h) in detections: # Draw the detections cv2.rectangle(im, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 0, 0), thickness=2) cv2.imshow("Raw Detections before NMS", im) cv2.waitKey() # Perform Non Maxima Suppression detections = nms(detections, threshold) # Display the results after performing NMS for (x_tl, y_tl, _, w, h) in detections: # Draw the detections cv2.rectangle(clone, (x_tl, y_tl), (x_tl + w, y_tl + h), (0, 0, 0), thickness=2) # save image to file fname = path[::-1] # reverse the string index = fname.find("/") fname = fname[:index][::-1] # isolate just the file name and reverse back cv2.imwrite(fname, clone) print("saved to " + str(fname)) cv2.imshow("Final Detections after applying NMS", clone) cv2.waitKey() cv2.destroyAllWindows()
imgs = [] labels = [] label_counter = 0 file_counter = 0 for c in c_list: curr_dir = os.path.join(args.data, c) f_list = os.listdir(curr_dir) f_list.sort() print('Reading class:', c) for f in f_list: f_path = os.path.join(curr_dir, f) img = imread(f_path) feats = hog(img, orientations=9, pixels_per_cell=(16, 16), cells_per_block=(3, 3), block_norm='L2', visualize=False, transform_sqrt=False, feature_vector=True, multichannel=True) if args.subsample: if file_counter % 10 == 0: imgs.append(feats) labels.append(label_counter) else: imgs.append(feats) labels.append(label_counter) file_counter += 1 label_counter += 1 imgs = np.vstack(imgs) labels = np.array(labels)
mndata = MNIST('data') tr_imgs, tr_lbs = mndata.load_training() test_imgs, test_lbs = mndata.load_testing() new_tr_lbs = [item for item in tr_lbs] new_test_lbs = [item for item in test_lbs] print(purity_score([2, 1, 2, 3, 2, 2, 3, 1], [1, 1, 2, 3, 2, 2, 3, 1])) new_train = [] for img in tr_imgs: pixels = np.array(img, dtype='uint8') pixels = pixels.reshape((28, 28)) pixels = deskew(pixels) pixels = np.pad(pixels, ((4, 4), (4, 4)), mode='constant') img_hog = hog(pixels, block_norm='L2-Hys') new_train.append(img_hog) print('here') new_test = [] for img in test_imgs: pixels = np.array(img, dtype='uint8') pixels = pixels.reshape((28, 28)) pixels = deskew(pixels) # print(np.shape(pixels)) pixels = np.pad(pixels, ((4, 4), (4, 4)), mode='constant') img_hog = hog(pixels, block_norm='L2-Hys') # print(np.shape(img_hog)) new_test.append(img_hog) print(np.shape(new_train))
def quantify_images(image): features = feature.hog(image, orientations=9, pixels_per_cell=(10,10), cells_per_block=(2,2), transform_sqrt=True, block_norm='L1') return features
def hogProcess(image): fd, hog_image = hog(image, orientations=8, pixels_per_cell=(8,8), cells_per_block=(1, 1), visualize=True, multichannel=False) return hog_image
# cambio a binario ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV) # busco la figura en la imagen ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # cuento figuras encontradas rects = [cv2.boundingRect(ctr) for ctr in ctrs] for rect in rects: cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) leng = int(rect[3] * 1.4) pt1 = int(rect[1] + rect[3] // 2 - leng // 2) pt2 = int(rect[0] + rect[2] // 2.5 - leng // 2) roi = im_th[pt1:pt1 + leng, pt2:pt2 + leng] roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA) roi = cv2.dilate(roi, (3, 3)) roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1)) nbr = clf.predict(np.array([roi_hog_fd], 'float64')) cv2.putText(im, str(int(nbr[0])), (rect[0], rect[1]), cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 3) cv2.imshow("Resulting Image with Rectangular ROIs", im) cv2.waitKey()
print('removing', file_path) os.unlink(file_path) print("Calculating the descriptors for the positive samples and saving them") for im_path in glob.glob(os.path.join(pos_im_path, "*")): print("Working on file: ", im_path) # Read image and convert to grayscale im = cv2.imread(im_path) gray_im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) resize_im = cv2.resize(gray_im, scale_size) # Resize the image fd = hog(resize_im, orientations=orientations, pixels_per_cell=pixels_per_cell, cells_per_block=cells_per_block, visualise=False) # Save features fd_name = os.path.split(im_path)[1].split(".")[0] + ".feat" fd_path = os.path.join(pos_feat_dir, fd_name) joblib.dump(fd, fd_path) print("Positive features saved in {}".format(pos_feat_dir)) print("Calculating the descriptors for the negative samples and saving them") for im_path in glob.glob(os.path.join(neg_im_path, "*")): print("Working on file: ", im_path) im = cv2.imread(im_path) gray_im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
img[..., 1] = np.reshape(data[1024:2048], (32, 32)) img[..., 2] = np.reshape(data[2048:3072], (32, 32)) return img batches_meta = unpack(DATA_PATH + "batches.meta") data_batches = [ unpack(DATA_PATH + "data_batch_" + str(i+1)) for i in range(5) ] test_batch = unpack(DATA_PATH + "test_batch") from skimage.color import rgb2gray from skimage.feature import hog for i in range(5): data_batches[i][b"data"] = [hog(rgb2gray(reshape(img))) for img in data_batches[i][b"data"]] test_batch[b"data"] = [hog(rgb2gray(reshape(img))) for img in test_batch[b"data"]] hyperparam_train_data = data_batches[0][b"data"][:900] hyperparam_train_labels = data_batches[0][b"labels"][:900] hyperparam_test_data = data_batches[0][b"data"][900:1000] hyperparam_test_labels = data_batches[0][b"labels"][900:1000] import datetime begin = datetime.datetime.now() ks = [2, 3, 4, 5, 6, 7, 8, 9, 10] correct_sums = [] for k in ks: clf = neighbors.KNeighborsClassifier(k, weights="distance")
import numpy as np import os import scipy.ndimage import imageio from skimage.feature import hog from skimage import data, color, exposure from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.externals import joblib knn = joblib.load('model/knn_model.pkl') image = imageio.imread('dataSet/9/IMG_49421.png') image = color.rgb2gray(image) df = hog(image, orientations=8, pixels_per_cell=(10, 10), cells_per_block=(5, 5)) predict = knn.predict(df.reshape(1, -1))[0] print(predict)
for i in vals: if i[1] <= (int(q) + 50) and i[1] >= (int(q) - 50): vds.append(i) nums = [] for rect in vds: cv2.rectangle(im, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) leng = int(rect[3] * 1.6) pt1 = int(rect[1] + rect[3] // 2 - leng // 2) pt2 = int(rect[0] + rect[2] // 2 - leng // 2) roi = im_th[pt1:pt1 + leng, pt2:pt2 + leng] roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA) roi = cv2.dilate(roi, (3, 3)) roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False) nbr = clf.predict(np.array([roi_hog_fd], 'float64')) nums.append(nbr[0]) cv2.putText(im, str(int(nbr[0])), (rect[0], rect[1]), cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 3) print('vals', vals) print(nums) print(vds) cv2.imshow("Resulting Image with Rectangular ROIs", im) cv2.waitKey()
# Import the modules from sklearn.externals import joblib from sklearn import datasets from skimage.feature import hog from sklearn.svm import LinearSVC import numpy as np X,y= datasets.fetch_openml('mnist_784', version=1, return_X_y=True) #importing datset and labels straight from sklearn Images = np.array(X, 'int16') #converting list of Images into a numpy array. labels = np.array(y, 'int') list_hog_fd = [] for Image in Images: fd = hog(Image.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualize=False,block_norm='L2') # using hog algorithm to extract features from each image list_hog_fd.append(fd) #adding the hog features extracted from the image into a list hog_features = np.array(list_hog_fd, 'float64') #converting the HOG feature list into a numpy array ''' Two line code to train entire dataset load the LinearSVC() model into clf and then fit hog_features and list into clf ''' clf = LinearSVC() clf.fit(hog_features,labels) print('Accuracy is:',clf.score(hog_features,labels)*100,'%') joblib.dump(clf, "digits_cls.pkl", compress=3)
# In[17]: #load image and convert into gray image gray_sample = cv2.imread(file_list[0], 0) # In[18]: print(gray_sample.shape) print(gray_sample) # In[19]: fd, hog_image = hog(gray_sample, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=True) # In[20]: print(hog_image.shape) # In[21]: print(hog_image) # In[22]: print(fd)
x_train = x_train_full[0:73500] y_train = y_train_full[0:73500] x_test = x_test_full[0:18000] y_test = y_test_full[0:18000] print(x_test.shape) print(y_test.shape) y_train = y_train.ravel() y_test = y_test.ravel() df_1 = [] for i in range(0, x_train.shape[0]): df1 = hog(np.reshape(x_train[i], (32, 32)), orientations=8, pixels_per_cell=(8, 8), cells_per_block=(4, 4)) df_1.append(df1) x_train = np.array(df_1, 'float64') df_2 = [] for i in range(0, x_test.shape[0]): df2 = hog(np.reshape(x_test[i], (32, 32)), orientations=8, pixels_per_cell=(8, 8), cells_per_block=(4, 4)) df_2.append(df2) x_test = np.array(df_2, 'float64') print('Shape of Training data after HOG is: ' + str(x_train.shape))
def compute_descriptor(im, sp, spPatch=15, colBins=20, hogCells=9, hogBins=6, redirect=False): """ Compute region descriptors for NLC Input: im: (h,w,c) or (n,h,w,c): 0-255: np.uint8: RGB sp: (h,w) or (n,h,w): 0-indexed regions, #regions <= numsp spPatch: patchsize around superpixel for feature computation Output: regions: (k,d) where k < numsp*n frameEnd: (n,): indices of regions where frame ends: 0-indexed, included """ sTime = time.time() if im.ndim < 4: im = im[None, ...] sp = sp[None, ...] hogCellSize = int(spPatch / np.sqrt(hogCells)) n, h, w, c = im.shape d = 6 * colBins + hogCells * hogBins + 2 numsp = np.max(sp) + 1 # because sp are 0-indexed regions = np.ones((numsp * n, d), dtype=np.float) * -1e6 frameEnd = np.zeros((n,), dtype=np.int) count = 0 for i in range(n): boxes = get_region_boxes(sp[i]) # get patchsize around center; corner cases handled inside loop boxes[:, :2] = ((boxes[:, :2] + boxes[:, 2:] - spPatch) / 2) boxes = boxes.astype(np.int) boxes[:, 2:] = boxes[:, :2] + spPatch for j in range(boxes.shape[0]): # fix corner cases xmin, xmax = np.maximum(0, np.minimum(boxes[j, [0, 2]], w - 1)) ymin, ymax = np.maximum(0, np.minimum(boxes[j, [1, 3]], h - 1)) xmax = spPatch if xmin == 0 else xmax xmin = xmax - spPatch if xmax == w - 1 else xmin ymax = spPatch if ymin == 0 else ymax ymin = ymax - spPatch if ymax == h - 1 else ymin imPatch = im[i, ymin:ymax, xmin:xmax] hogF = hog( color.rgb2gray(imPatch), orientations=hogBins, pixels_per_cell=(hogCellSize, hogCellSize), cells_per_block=(int(np.sqrt(hogCells)), int(np.sqrt(hogCells))), visualise=False) colHist = color_hist(imPatch, colBins) regions[count, :] = np.hstack(( hogF, colHist, [boxes[j, 1] * 1. / h, boxes[j, 0] * 1. / w])) count += 1 frameEnd[i] = count - 1 if not redirect: sys.stdout.write('Descriptor computation: [% 5.1f%%]\r' % (100.0 * float((i + 1) / n))) sys.stdout.flush() regions = regions[:count] eTime = time.time() print('Descriptor computation finished: %.2f s' % (eTime - sTime)) return regions, frameEnd