Пример #1
0
def rotate_bdd(X_train, deg):
    HOG_train = np.zeros((len(X_train), 324))
    for i in range(len(X_train)):
        image = X_train.ix[i].values.reshape(3, 32, 32).transpose(1, 2, 0)
        im = rotate(image, deg)
        HOG_train[i, :] = hog(im)
    return HOG_train
Пример #2
0
def flip_bdd(X_train):
    HOG_train = np.zeros((len(X_train), 324))
    for i in range(len(X_train)):
        image = X_train.ix[i].values.reshape(3, 32, 32).transpose(1, 2, 0)
        im = flip(image, 1)
        HOG_train[i, :] = hog(im)
    return HOG_train
Пример #3
0
    def fit(self, X, y, pix=False):
        '''
        Fit model to the observation (X,y) of detection
        '''
        y = np.array(y)
        X_feat = []
        AR = [[] for i in range(self.K)]
        for i, im in enumerate(X):

            ai = im.size[0] * 1. / im.size[1]
            if y[i] < self.K:
                AR[y[i]].append(ai)
            im_r = im.resize((22, 20))
            if not pix:
                X_feat.append(hog(im_r, 4).reshape((-1, )))
            else:
                im_r = im_r.convert('L')
                im_a = np.array(im_r.getdata()).reshape((-1, ))
                X_feat.append(im_a)

        for i in range(self.K):
            y_feat = (y == i)
            self.model[i].fit(X_feat, y_feat)
        self.AR = np.array([[np.mean(AR[i]), np.var(AR[i])]
                            for i in range(self.K)])
Пример #4
0
    def fit(self, X, y, pix=False):
        '''
        Fit model to the observation (X,y) of detection
        '''
        y = np.array(y)
        X_feat = []
        AR = [[] for i in range(self.K)]
        for i, im in enumerate(X):

            ai = im.size[0]*1./im.size[1]
            if y[i] < self.K:
                AR[y[i]].append(ai)
            im_r = im.resize((22,20))
            if  not pix:
                X_feat.append(hog(im_r,4).reshape((-1,)))
            else:
                im_r = im_r.convert('L')
                im_a = np.array(im_r.getdata()).reshape((-1,))
                X_feat.append(im_a)
        
        for i in range(self.K):
            y_feat = (y==i)
            self.model[i].fit(X_feat, y_feat)
        self.AR = np.array([[np.mean(AR[i]),np.var(AR[i])] 
                             for i in range(self.K)])
Пример #5
0
def detect(gray, frame, model):
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    
    for (x, y, w, h) in faces:
    	cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

    	#label = model.predict(lbp_hist(gray[y:y+h, x:x+w] ,25,8).reshape(1,-1))
    	label = model.predict(hog(cv2.resize(gray[y:y+h, x:x+w], (150,150))).reshape(1,-1))

    	cv2.putText(frame, label[0], (x+w,y-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 2, cv2.LINE_AA)
    	#print(label)

    return frame
Пример #6
0
    def detection(self, im, th, th1=0.1, pix=False):
        '''
        Perform the sliding window detection on im
        return a list with (x, y, class, proba)

        '''
        w = self.width
        h = self.height
        a = w * 1. / h
        w0 = im.size[0] - w
        h0 = im.size[1] - h
        res = []
        Ntot = w0 * h0

        for i in range(w0):
            for j in range(h0):
                sys.stdout.write('\rChar detect...{:6.2%}'.format(
                    (i * h0 + j) * 1. / Ntot))
                sys.stdout.flush()
                X = im.crop((i, j, i + w, j + h))
                X_r = X.resize((22, 20))
                X_feat = []
                if not pix:
                    X_feat.append(hog(X_r, 4).reshape((-1, )))
                else:
                    im_r = im_r.convert('L')
                    im_a = np.array(X_r.getdata()).reshape((-1, ))
                    X_feat.append(im_a)

                p = []
                for k in range(self.K):
                    p.append(self.model[k].predict_proba(X_feat)[0, 1])
                cj = np.argmax(p)
                muj, sigj = self.AR[cj]
                GS = p[cj] * np.exp(-(muj - a)**2 / (2 * sigj))
                if GS > th1:
                    res.append((i, j, p, cj, p[cj], GS))
        print '\rChar detect...done  '
        return self.NMS(res, th)
Пример #7
0
    def detection(self, im, th, th1=0.1, pix=False):
        '''
        Perform the sliding window detection on im
        return a list with (x, y, class, proba)

        '''
        w = self.width
        h = self.height
        a = w*1./h
        w0 = im.size[0] - w
        h0 = im.size[1] - h
        res = []
        Ntot = w0*h0
        
        for i in range(w0):
            for j in range(h0):
                sys.stdout.write('\rChar detect...{:6.2%}'.format(
                                    (i*h0+j)*1./Ntot))
                sys.stdout.flush()
                X = im.crop((i,j,i+w,j+h))
                X_r = X.resize((22,20))
                X_feat = []
                if not pix:
                    X_feat.append(hog(X_r,4).reshape((-1,)))
                else:
                    im_r = im_r.convert('L')
                    im_a = np.array(X_r.getdata()).reshape((-1,))
                    X_feat.append(im_a)
                
                p = []
                for k in range(self.K):
                    p.append(self.model[k].predict_proba(X_feat)[0,1])
                cj = np.argmax(p)
                muj,sigj = self.AR[cj]
                GS = p[cj]*np.exp(-(muj-a)**2/(2*sigj))
                if GS > th1:
                    res.append((i,j,p, cj, p[cj], GS))
        print '\rChar detect...done  '
        return self.NMS(res, th)
Пример #8
0
    def test(self, X, y, pix=False):
        '''
        Test model on the observation (X,y)
        '''
        y = np.array(y)
        X_feat = []
        AR = []
        for i, im in enumerate(X):
            ai = im.size[0] * 1. / im.size[1]
            AR.append(ai)
            im_r = im.resize((22, 20))
            if not pix:
                X_feat.append(hog(im_r, 4).reshape((-1, )))
            else:
                im_r = im_r.convert('L')
                im_a = np.array(im_r.getdata()).reshape((-1, ))
                X_feat.append(im_a)

        p = []
        for k in range(self.K):
            p.append(self.model[k].predict_proba(X_feat)[:, 1])
        p = np.array(p)
        i0 = p.argmax(axis=0)
        AR = np.array(AR)

        detect = np.exp(-(self.AR[i0, 0] - AR[i0])**2 / (2 * self.AR[i0, 1]))
        GS = np.multiply(p[i0, range(y.shape[0])], detect)

        err2 = ((y - i0).nonzero()[0].shape[0]) * 1. / y.shape[0]

        err = 1 - (GS > 0.1).mean()
        print('\nThis model miss {:6.2%} of the'
              ' character in the test db').format(err)

        print(
            'This model failed to recognize {:6.2%} character '
            'of the test db').format(err2)
Пример #9
0
    def test(self, X, y, pix=False):
        '''
        Test model on the observation (X,y)
        '''
        y = np.array(y)
        X_feat = []
        AR = []
        for i, im in enumerate(X):
            ai = im.size[0]*1./im.size[1]
            AR.append(ai)
            im_r = im.resize((22,20))
            if not pix:
                X_feat.append(hog(im_r,4).reshape((-1,)))
            else:
                im_r = im_r.convert('L')
                im_a = np.array(im_r.getdata()).reshape((-1,))
                X_feat.append(im_a)

        p = []
        for k in range(self.K):
            p.append(self.model[k].predict_proba(X_feat)[:,1])
        p = np.array(p)
        i0 = p.argmax(axis=0)
        AR = np.array(AR)

        detect = np.exp(-(self.AR[i0,0]-AR[i0])**2/(2*self.AR[i0,1]))
        GS = np.multiply(p[i0, range(y.shape[0])], detect)

        err2 = ((y-i0).nonzero()[0].shape[0])*1./y.shape[0]
        
        err = 1- (GS>0.1).mean()
        print ('\nThis model miss {:6.2%} of the'
               ' character in the test db').format(err)

        print ('This model failed to recognize {:6.2%} character '
               'of the test db').format(err2)
Пример #10
0
import features
import cv2
import numpy as np
import matplotlib.pyplot as plt

im1 = cv2.imread('output/000045.png')

template = cv2.imread('output/template.png')

template = cv2.cvtColor(template, cv2.COLOR_BGR2RGB)
im1 = im1[im1.shape[0] - 720:, im1.shape[1] - 720:, :]
template = template[50:350, 50:350, :]
print(template.shape, im1.shape)
tmp = features.hog(template)

img = features.hog(im1)

print(img.shape, tmp.shape)
r, c, _ = tmp.shape
sse = np.zeros(img.shape[:2])
# print
for i in range(img.shape[0] - r):
    for j in range(img.shape[1] - c):
        sse[i, j] = np.sqrt(
            np.square(img[i:i + r, j:j + c, :] - tmp).sum(axis=(0, 1))).sum()

tgt = np.argmin(sse[:img.shape[0] - r, :img.shape[1] - c])
match = np.unravel_index(tgt, (img.shape[0] - r, img.shape[1] - c))
print(match)
a = plt.figure()
ax = a.add_subplot(121)
Пример #11
0
        print(result_CV)
        print("Groundtruth: ")
        print(Yval)
        precision = np.sum(Yval == result_CV) / len(result_CV)
        print("Precision: %f" % precision)

        print("Time: ", time.time() - temps)
        print('____________________')

# Compute HOG vector on testing set
HOG_test = np.zeros((len(X_test), 324))
for i in range(len(X_test)):
    im = X_test.ix[i].values.reshape(3, 32, 32).transpose(1, 2, 0)
    # imGray = rgb2gray(im)
    # HOG_test[i, :] = hog(imGray, cells_per_block=(2,2))
    HOG_test[i, :] = hog(im)

Xtrain = HOG_train
Ytrain = Y_train['Prediction'].as_matrix()
Xtest = HOG_test

print("Fitting")
parameters = one_vs_all(Xtrain, Ytrain, C, kernel)
print('____________________')

print("Predicting")
temps = time.time()

number_of_classes = len(np.unique(Ytrain))
result = predict_multiclass(Xtest, number_of_classes, parameters)
Пример #12
0
backgrounds = []
for k, frame in enumerate(frames[0:100:10]):
    im = frame[:360].astype('double') / 255.0
    #im = skimage.color.rgb2hsv((im * 255).astype('uint8'))
    #plt.imshow(im[:, :, 0], interpolation = 'NONE', cmap = plt.cm.hsv);
    #plt.colorbar()
    #plt.show()
    #plt.imshow(im[:, :, 1], interpolation = 'NONE', cmap = plt.cm.hsv);
    #plt.colorbar()
    #plt.show()
    #plt.imshow(im[:, :, 2], interpolation = 'NONE', cmap = plt.cm.hsv);
    #plt.colorbar()
    #plt.show()

    tmp = time.time()
    hog = features.hogpad(features.hog(im, b))
    rgb = features.rgbhist(im, b)
    print time.time() - tmp

    #plt.imshow(im, interpolation = 'NONE')
    #plt.show()
    #plt.imshow(skimage.color.rgb2gray(im), interpolation = 'NONE', cmap = plt.cm.gray)
    #plt.imshow(rgb, interpolation = 'NONE')
    #plt.show()

    Y = rgb.shape[0]
    X = rgb.shape[1]

    rgbf = numpy.zeros((rgb.shape[0], rgb.shape[1], sy * sx * rgb.shape[2]))

    indices[k] = []
Пример #13
0
    def hog(self, mode='dense', algorithm='dalaltriggs', num_bins=9,
            cell_size=8, block_size=2, signed_gradient=True, l2_norm_clip=0.2,
            window_height=1, window_width=1, window_unit='blocks',
            window_step_vertical=1, window_step_horizontal=1,
            window_step_unit='pixels', padding=True, verbose=False,
            constrain_landmarks=True):
        r"""
        Represents a 2-dimensional HOG features image with C number of
        channels. The output object's class is either MaskedImage or Image
        depending on the input image.

        Parameters
        ----------
        mode : 'dense' or 'sparse'
            The 'sparse' case refers to the traditional usage of HOGs, so
            default parameters values are passed to the ImageWindowIterator.
            The sparse case of 'dalaltriggs' algorithm sets the window height
            and width equal to block size and the window step horizontal and
            vertical equal to cell size. Thse sparse case of 'zhuramanan'
            algorithm sets the window height and width equal to 3 times the
            cell size and the window step horizontal and vertical equal to cell
            size. In the 'dense' case, the user can change the
            ImageWindowIterator related parameters (window_height,
            window_width, window_unit, window_step_vertical,
            window_step_horizontal, window_step_unit, padding).

        Default: 'dense'
        window_height : float
            Defines the height of the window for the ImageWindowIterator
            object. The metric unit is defined by window_unit.

            Default: 1
        window_width : float
            Defines the width of the window for the ImageWindowIterator object.
            The metric unit is defined by window_unit.

            Default: 1
        window_unit : 'blocks' or 'pixels'
            Defines the metric unit of the window_height and window_width
            parameters for the ImageWindowIterator object.

            Default: 'blocks'
        window_step_vertical : float
            Defines the vertical step by which the window in the
            ImageWindowIterator is moved, thus it controls the features
            density. The metric unit is defined by window_step_unit.

            Default: 1
        window_step_horizontal : float
            Defines the horizontal step by which the window in the
            ImageWindowIterator is moved, thus it controls the features
            density. The metric unit is defined by window_step_unit.

            Default: 1
        window_step_unit : 'pixels' or 'cells'
            Defines the metric unit of the window_step_vertical and
            window_step_horizontal parameters for the ImageWindowIterator
            object.

            Default: 'pixels'
        padding : bool
            Enables/disables padding for the close-to-boundary windows in the
            ImageWindowIterator object. When padding is enabled, the
            out-of-boundary pixels are set to zero.

            Default: True
        algorithm : 'dalaltriggs' or 'zhuramanan'
            Specifies the algorithm used to compute HOGs.

            Default: 'dalaltriggs'
        cell_size : float
            Defines the cell size in pixels. This value is set to both the
            width and height of the cell. This option is valid for both
            algorithms.

            Default: 8
        block_size : float
            Defines the block size in cells. This value is set to both the
            width and height of the block. This option is valid only for the
            'dalaltriggs' algorithm.

            Default: 2
        num_bins : float
            Defines the number of orientation histogram bins. This option is
            valid only for the 'dalaltriggs' algorithm.

            Default: 9
        signed_gradient : bool
            Flag that defines whether we use signed or unsigned gradient
            angles. This option is valid only for the 'dalaltriggs' algorithm.

            Default: True
        l2_norm_clip : float
            Defines the clipping value of the gradients' L2-norm. This option
            is valid only for the 'dalaltriggs' algorithm.

            Default: 0.2
        constrain_landmarks : bool
            Flag that if enabled, it constrains landmarks that ended up outside
            of the features image bounds.

            Default: True
        verbose : bool
            Flag to print HOG related information.

            Default: False

        Raises
        -------
        ValueError
            HOG features mode must be either dense or sparse
        ValueError
            Algorithm must be either dalaltriggs or zhuramanan
        ValueError
            Number of orientation bins must be > 0
        ValueError
            Cell size (in pixels) must be > 0
        ValueError
            Block size (in cells) must be > 0
        ValueError
            Value for L2-norm clipping must be > 0.0
        ValueError
            Window height must be >= block size and <= image height
        ValueError
            Window width must be >= block size and <= image width
        ValueError
            Window unit must be either pixels or blocks
        ValueError
            Horizontal window step must be > 0
        ValueError
            Vertical window step must be > 0
        ValueError
            Window step unit must be either pixels or cells
        """
        # compute hog features and windows_centres
        hog, window_centres = fc.hog(self._image.pixels, mode=mode,
                                     algorithm=algorithm, cell_size=cell_size,
                                     block_size=block_size, num_bins=num_bins,
                                     signed_gradient=signed_gradient,
                                     l2_norm_clip=l2_norm_clip,
                                     window_height=window_height,
                                     window_width=window_width,
                                     window_unit=window_unit,
                                     window_step_vertical=window_step_vertical,
                                     window_step_horizontal=
                                     window_step_horizontal,
                                     window_step_unit=window_step_unit,
                                     padding=padding, verbose=verbose)
        # create hog image object
        hog_image = self._init_feature_image(hog,
                                             window_centres=window_centres,
                                             constrain_landmarks=
                                             constrain_landmarks)
        # store parameters
        hog_image.hog_parameters = {'mode': mode, 'algorithm': algorithm,
                                    'num_bins': num_bins,
                                    'cell_size': cell_size,
                                    'block_size': block_size,
                                    'signed_gradient': signed_gradient,
                                    'l2_norm_clip': l2_norm_clip,
                                    'window_height': window_height,
                                    'window_width': window_width,
                                    'window_unit': window_unit,
                                    'window_step_vertical':
                                    window_step_vertical,
                                    'window_step_horizontal':
                                    window_step_horizontal,
                                    'window_step_unit': window_step_unit,
                                    'padding': padding,
                                    'original_image_height':
                                    self._image.pixels.shape[0],
                                    'original_image_width':
                                    self._image.pixels.shape[1],
                                    'original_image_channels':
                                    self._image.pixels.shape[2]}
        return hog_image
Пример #14
0
    # loop over the images in each sub-folder
    for file in images:
        # get the image file name
        file_path = os.path.join(current_dir, file)

        # read the image and resize it to a fixed-size
        image = cv2.imread(file_path)
        image = cv2.resize(image, fixed_size)

        ####################################
        # Global Feature extraction
        ####################################

        fv_histogram = fd_haralick(image)
        fv_kaze = kaze_func(image)
        fv_hog = hog(image)
        fv_fast = fd_Fast(image)
        fv_lin = lin(image)
        fv_ORB = fd_ORB(image)
        #fv_hu_moments = fd_hu_moments(image)
        #        #fv_4 = fd_4(image)
        ###################################
        # Concatenate global features
        ###################################
        global_feature = np.hstack([fv_histogram, fv_kaze])

        # update the list of labels and feature vectors
        labels.append(current_label)
        global_features.append(global_feature)

    print("[STATUS] processed folder: {}".format(current_label))
Пример #15
0
    	#label = model.predict(lbp_hist(gray[y:y+h, x:x+w] ,25,8).reshape(1,-1))
    	label = model.predict(hog(cv2.resize(gray[y:y+h, x:x+w], (150,150))).reshape(1,-1))

    	cv2.putText(frame, label[0], (x+w,y-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 2, cv2.LINE_AA)
    	#print(label)

    return frame

if args['train'] is not None:
	path = args["train"]

	data_paths = [os.path.join(r, n) for r, _, f in os.walk(path) for n in f]

	#data = np.asarray([lbp_hist(i ,25,8) for i in tqdm(data_paths)])
	data = np.asarray([hog(i) for i in tqdm(data_paths)])
	labels = np.asarray([i.split('\\')[1] for i in tqdm(data_paths)])

	pkl.dump([data,labels], open('/data_array_hog.pkl', 'wb'))
	# data, labels = pkl.load(open('/data_array.pkl', 'rb'))
	
	arr = np.random.permutation(len(labels))

	data = data[arr]
	labels = labels[arr]

	scaler = StandardScaler()
	scaler.fit(data)
	#scaler.transform(data)

	x_train, x_test, y_train, y_test = train_test_split(data, labels, train_size = 0.7, random_state = 42)