Exemplo n.º 1
0
def lbp_classifier():
    img_gallery, y_gallery = load_gan_gallery()
    lra_train = np.array([calHistogram(lbp(img,8,2),16,16) for img in img_gallery])
    #lra_train = np.array([lbp(img,8,2).flatten() for img in img_gallery])
    
    # LRA Matrix
    #project_m = np.hstack((np.eye(149),np.zeros((149,number*140))))
    #W = np.dot(project_m,np.linalg.pinv(lra_train.T))
    W = np.dot(np.eye(149),np.linalg.pinv(lra_train.T))
    
    # Test Image
    probe = np.loadtxt('probe.txt',dtype='string',delimiter=' ')
    probe = pd.DataFrame(probe,columns=['path','ori_pose','ori_ill'])
    #label = np.repeat(range(149)*19,6)
    #label = np.repeat(range(149),6) 
    label = range(149)*19 #
    pose = ['041', '050', '080', '130','140', '190']
    light = ['00','01','02','03','04','05','06','08','09','10','11',
        '12','13','14','15','16','17','18','19']
    total = []
    for ipose in pose:
        count = 0
        #probe_pose = probe[probe.ori_ill==ipose].values
        probe_pose = probe[probe.ori_pose==ipose].values
        for i in range(probe_pose.shape[0]):
            name = '/home/pris/frontal_multipie_new2/'+probe_pose[i,0][3:]
            img = cv2.imread(name,0)
            feat = calHistogram(lbp(img,8,2),16,16)
            #feat = lbp(img,8,2).flatten()
            ilabel = np.dot(W,feat).argmax()
            if(ilabel == label[i]):
                count = count+1
        print('Current Acc: %d / %d , %f' % (count,i,count/float(i)))
        total.append(count/float(i))
Exemplo n.º 2
0
def binFaces_noex(image_name):
    faces = detectFaces(image_name)
    image = cv2.imread(image_name)
    if faces:
        the_face = image[faces[1]:faces[3], faces[0]:faces[2]]
        gray = cv2.cvtColor(the_face, cv2.COLOR_BGR2GRAY)
        return lbp(gray, 8, 1)
    else:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        return lbp(gray, 8, 1)
Exemplo n.º 3
0
def lbp_like(img, windows_num=4, P=8, R=1, lbp_type='uniform'):
    LBP_TYPE = ('default', 'ror', 'uniform', 'var')
    if lbp_type not in LBP_TYPE:
        return None
    H, W = img.shape
    h_step = H / windows_num
    w_step = W / windows_num
    out_lst = []
    for start_h in range(0, H - h_step + 1, h_step):
        for start_w in range(0, W - w_step + 1, w_step):
            end_h = start_h + h_step
            end_w = start_w + w_step
            rec_img = img[start_h:end_h, start_w:end_w]
            rec_lbp = lbp(rec_img, P, R, lbp_type)
            if lbp_type == 'uniform':
                lst = [0] * (P + 2)
            elif lbp_type in ['default', 'ror']:
                lst = [0] * (math.pow(2, P))
            for i in range(len(rec_lbp)):
                for j in range(len(rec_lbp[0])):
                    lst[int(rec_lbp[i][j])] = lst[int(rec_lbp[i][j])] + 1
            normal_lst = map(lambda x: round((float(x) / float(sum(lst))), 4),
                             lst)
            out_lst = out_lst + normal_lst
    return np.array(out_lst)
Exemplo n.º 4
0
def get_all_faces(img):
    a,faces = detect_faces(img)
    faces_list = []
    for face in faces:
        the_face = img[face[1]:face[3], face[0]:face[2]]
        faces_list.append(lbp(the_face,8,1))
    return a,faces_list
Exemplo n.º 5
0
def binFaces(image_name):
    face = detectFaces(image_name)
    image = cv2.imread(image_name)
    if face:
        the_face = image[face[1]:face[3], face[0]:face[2]]
        gray = cv2.cvtColor(the_face, cv2.COLOR_BGR2GRAY)
        return lbp(gray, 8, 1)
    else:
        return np.array([[None]])
Exemplo n.º 6
0
def pattern(img, radius=3, points=8):
    # Params
    n_points = points * radius
    img = normalize(img)
    img1 = lbp(img, radius, n_points)
    img1 = normalize(img1)
    original_image = resize(img)
    lbp_image = resize(img1)
    stack = np.hstack([original_image, lbp_image])
    return stack
Exemplo n.º 7
0
def process_image(image, args=object):
    if args.DES_TYPE == "HOG":
        fd = hog(image, block_norm='L2', pixels_per_cell=args.PIXELS_PER_CELL)
    elif args.DES_TYPE == "LBP":
        fd = lbp(image, args.LBP_POINTS, args.LBP_RADIUS)
    elif args.DES_TYPE == "HAAR":
        fd = haar_like_feature(integral_image(image), 0, 0, 5, 5, 'type-3-x')
    else:
        raise KeyError("==> The Processing method does not exist!")
    return fd
Exemplo n.º 8
0
def extract_lbp_feature(file,  # the string location of the image
                        radius=1,  # the radius about which to look
                        npoints=8,  # the number of points around the radius.
                        nbins=128,  # for plotting the histogram
                        range_bins=(0, 255)):  # the range for plotting the histogram
    rgb = file
    gry = rgb2gray(rgb)
    feat = lbp(gry, R = radius, P = npoints)
    feats, edges = np.histogram( feat, bins = nbins, range = range_bins)
    return feat, edges
Exemplo n.º 9
0
    def classify(im, mask,cla="SVM"):
        """ Classification of blobs
        :im: Image with traffic signs
        :mask: Mask after segmentation
        :returns: List of bounding boxes
        """
        # Training

        bb_sol = []
        label_im = label(mask >200)
        props = regionprops(label_im)
        if cla=="SVM":
            for p in props:

                coordinates = Rect(xmin=p.bbox[0], ymin=p.bbox[1], xmax=p.bbox[2], ymax=p.bbox[3])
                signs = im[coordinates.xmin:coordinates.xmax, coordinates.ymin:coordinates.ymax, :]
                signs_found = resize(signs, (32, 32))
                io.imsave("/home/cesar/Desktop/datasetminin/gg.jpg", signs)
                datasetlbp = lbp(rgb2gray(signs_found), 8, 3, method='uniform')
                hist = np.histogram(datasetlbp, normed=True, bins=8, range=(0, 8))
                input_predict = hist[0]
                predict = clfSVM.predict([input_predict])
                if predict[0] == 1:
                    bb_sol.append(coordinates)


            return bb_sol
        if cla=="NN":
            for p in props:

                coordinates = Rect(xmin=p.bbox[0], ymin=p.bbox[1], xmax=p.bbox[2], ymax=p.bbox[3])
                signs = im[coordinates.xmin:coordinates.xmax, coordinates.ymin:coordinates.ymax, :]
                signs_found = resize(signs, (32, 32))
                datasetlbp = lbp(rgb2gray(signs_found), 8, 3, method='uniform')
                hist = np.histogram(datasetlbp, normed=True, bins=8, range=(0, 8))
                input_predict = hist[0]
                predict = clfNN.predict([input_predict])
                if predict[0] == 1:
                    bb_sol.append(coordinates)

            return bb_sol
Exemplo n.º 10
0
def lbpTexture (im, name, label, neighbors=2, radio=16):
    '''
    radio = 2
    neighbors = 8 * radio
    '''
    
    lbp_image = lbp(im,P = neighbors, R = radio)
    t = im.shape[0]
    hist,bin = np.histogram(lbp_image,density=False,bins = 256) #compute the histogram
    hist = np.float32(hist)
    hist = hist/t
    return hist,name,label
def LBP(numPoints, radius, image, eps=1e-7):
    m_lbp = lbp(image, numPoints, radius, method="uniform")
    (hist, _) = np.histogram(m_lbp.ravel(),
                             bins=np.arange(0, numPoints + 3),
                             range=(0, numPoints + 2))
    # normalize the histogram
    hist = hist.astype("float")
    hist /= (hist.sum() + eps)

    assert (hist.shape == (26, )), "LBP shape expected 26, got {}".format(
        hist.shape)
    return hist.ravel()
Exemplo n.º 12
0
def lbp_classifier():
    iset='ccbr'
    number = 10
    img_gallery, y_gallery = load_gallery(iset,1)
    lra_train1 = np.array([calHistogram(lbp(img,8,2),24,20) for img in img_gallery])
    lra_train2 = load_lra_train(iset, number, resize = 1)
    lra_train2 = np.array([calHistogram(lbp(img,8,2),24,20) for img in lra_train2])
    lra_train = np.vstack((lra_train1,lra_train2))
    
    # LRA Matrix
    project_m = np.hstack((np.eye(149),np.zeros((149,number*140))))
    W = np.dot(project_m,np.linalg.pinv(lra_train.T))
    #W = np.dot(np.eye(149),np.linalg.pinv(lra_train1.T))
    
    # Test Image
    probe = np.loadtxt('probe.txt',dtype='string',delimiter=' ')
    probe = pd.DataFrame(probe,columns=['path','ori_pose','ori_ill'])
    #label = np.repeat(range(149)*19,6)
    #label = np.repeat(range(149),6) 
    label = range(149)*19 #
    pose = ['041', '050', '080', '130','140', '190']
    light = ['00','01','02','03','04','05','06','08','09','10','11',
        '12','13','14','15','16','17','18','19']
    total = []
    for ipose in pose:
        count = 0
        #probe_pose = probe[probe.ori_pose==ipose].values
        probe_pose = probe[probe.ori_pose==ipose].values
        for i in range(probe_pose.shape[0]):
            if(iset == 'ccbr'):
                name = 'ccbr_probe/'+probe_pose[i,0][:-3]+'jpg'
            else:
                name = 'cpf_probe/'+probe_pose[i,0][:-3]+'bmp'
            img = cv2.imread(name,0)
            feat = calHistogram(lbp(img,8,2),24,20)
            ilabel = np.dot(W,feat).argmax()
            if(ilabel == label[i]):
                count = count+1
        print('Current Acc: %d / %d , %f' % (count,i,count/float(i)))
        total.append(count/float(i))
Exemplo n.º 13
0
def hdlbp(img, P=8, R=2, cell_size=4, fmt='default'):
    """
    Parameters
    ----------
    img : (N, M) array
        Graylevel image.
    P : int
        Number of circularly symmetric neighbour set points (quantization of
        the angular space).
    R : float
        Radius of circle (spatial resolution of the operator).
    cell_size : int
        block size of lbp cell
    fmt : {'default', 'ror', 'uniform', 'var'}
        Method to determine the pattern.
        * 'default': original local binary pattern which is gray scale but not
            rotation invariant.
        * 'ror': extension of default implementation which is gray scale and
            rotation invariant.
        * 'uniform': improved rotation invariance with uniform patterns and
            finer quantization of the angular space which is gray scale and
            rotation invariant.
        * 'nri_uniform': non rotation-invariant uniform patterns variant
            which is only gray scale invariant [2].
        * 'var': rotation invariant variance measures of the contrast of local
            image texture which is rotation but not gray scale invariant.
    
    Returns
    -------
    output : 1-D array
        HDLBP feature.
        
    References
    ----------
    .. [1] Multiresolution Gray-Scale and Rotation Invariant Texture
           Classification with Local Binary Patterns.
           Timo Ojala, Matti Pietikainen, Topi Maenpaa.
           http://www.rafbis.it/biplab15/images/stories/docenti/Danielriccio/\
           Articoliriferimento/LBP.pdf, 2002.
    .. [2] Face recognition with local binary patterns.
           Timo Ahonen, Abdenour Hadid, Matti Pietikainen,
           http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.214.6851,
           2004.
    """

    lbpTrain = calHistogram(lbp(img, P, R), cell_size)
    #lbpTrain_2 = np.array([calHistogram(lbp(img,8,2),19,16) for img in trainimg1])
    #lbpTrain_3 = np.array([calHistogram(lbp(img,8,2),14,12) for img in trainimg1])
    #lbpTrain = np.hstack((lbpTrain_1,lbpTrain_2,lbpTrain_3))

    return lbpTrain
Exemplo n.º 14
0
def lbp_histrogram( image ):
    '''Process LBP Histogram from a picture of a fish.'''

    lbpParams = (8, 2)

    # The goal is to subdivide the picture in different ways (3 here) and to
    # concatenate their histograms.
    subdivs = [(3, 12)]#, (4, 16)]

    # Here is the computation of one histogram (defined in Face Recognition with
    # Local Binary Patterns).
    # We shall first find n (the number of possible values of LBP) and m (the 
    # number of regions). Note that n += 1 because we want a value that represent
    # non-uniformity.
    subdiv = subdivs[0]
    n = n_for_2_uniform_lbp(lbpParams) + 1
    m = subdiv[0] * subdiv[1]

    # Then, we have an histogram that consists of the concatenation of m
    # histograms, each one of size n. It means, for one region we keep
    # the number of pixels that represent a label of LBP.
    # Jehan, note that I preallocated the table ! In python !
    lbpHist = zeros((m, n))
    lbpImage = lbp(image, lbpParams[0], lbpParams[1]) # …, method = 'uniform')
    labelMap = lut(lbpParams[0])

    # Effective creation of the histogram.
    region = 0
    for tileBounds in TiledIterator(lbpImage, subdiv):
        tile = image[tileBounds]
        size = shape(tile)
        area = size[0] * size[1]
    
        for row in range(size[0]):
            for col in range(size[1]):
                # Get the LBP at (row, col)
                label = tile[row, col]
                # And count it
                if (uniform_pattern(label, lbpParams[0], 2)):
                    lbpHist[region, labelMap[label]] += 1
                else:
                    lbpHist[region, n-1] += 1
    
        # Normalise
        lbpHist[region] = lbpHist[region] / area

        region += 1

    #lbpHist = normalise_lbphistogram(lbpHist)
    return lbpHist
Exemplo n.º 15
0
def gethistHSV(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    if len(faces) > 0:
        histogram = np.array([])
        x, y, w, h = faces[0]
        crop_face = gray[y:y + w, x:x + h]
        crop_face = cv2.resize(crop_face, (64, 64))
        radius = 2
        n_points = 16
        # for i in range(crop_face.shape[2]):
        l = lbp(crop_face, n_points, radius, method='uniform')
        histogram = np.append(histogram, l.ravel())
        return histogram, faces
Exemplo n.º 16
0
 def process_image(self, image):
     '''
     This function is used to extract features from input image
     according to the config
     :param image: input image
     :return: The features extracted from input image
     '''
     if self.config.DES_TYPE == "HOG":
         fd = hog(image,
                  block_norm='L2',
                  pixels_per_cell=self.config.PIXELS_PER_CELL)
     elif self.config.DES_TYPE == "LBP":
         fd = lbp(image, self.config.LBP_POINTS, self.config.LBP_RADIUS)
     elif self.config.DES_TYPE == "HAAR":
         fd = haar_like_feature(integral_image(image), 0, 0, 5, 5,
                                'type-3-x')
     else:
         raise KeyError("==> The Processing method does not exist!")
     return fd
Exemplo n.º 17
0
    def _process(self, path):
        """
		Performing some operations, common for all the options
		"""
        if not os.path.isfile(path):
            #change that!
            sys.exit('File doesnt exist.')
        #grayscale image
        img = cv.imread(path, cv.IMREAD_GRAYSCALE)
        #lbp representation
        lbp_img = lbp(img, cfg.params['P'], cfg.params['R'],
                      cfg.params['LBP_METHOD'])
        lbp_img = lbp_img.astype(np.uint8)
        #histogram representation
        hist = histogram.spatial(lbp_img, cfg.params['NX'], cfg.params['NY'],
                                 cfg.params['NPATTERNS'],
                                 cfg.params['OVERLAPX'],
                                 cfg.params['OVERLAPY'])
        return (lbp_img, hist)
Exemplo n.º 18
0
def extract_lbp_feature(
    file,  # the string location of the image
    radius=1,  # the radius about which to look
    npoints=8,  # the number of points around the radius.
    nbins=128,  # for plotting the histogram
    range_bins=(0, 255)):  # the range for plotting the histogram
    # Read in the file. Because this is different to the template.py you will have to
    # Import all your libraries that you want to use again.

    rgb = imread(file)
    gry = rgb2gray(rgb)

    # Convert it to greyscale.

    # Now you need to import local_binary_pattern from skimage.features in the import area.
    # In much the same way that HOG does lbp has some basic parameters.
    # R is the radius around a central pixels that it will extend. In this case 1 means
    # that it will extend out from the center pixel 1 pixels (or there abouts), as the number
    # increases we scan a larger area.
    # P - number of points, to look at on the radius, for a radius of 1 it makes sense to look
    # at 8 pixels, why do you think?
    # Using this information we can use the local_binary_pattern in the following way:
    # features = local_binary_pattern( input image, R=radius, P=npoints )
    # When you are finished print the features shape.
    feat = lbp(gry, R=radius, P=npoints)
    # Let's go back to template.py and run this function.

    # So you have run it, what do you notice? The output is 140*140 which was the size of
    # our input image. So each pixel has a value associated with it! These values for the
    # standard lbp range from 0->255 based on a combination of pixels. For more information
    # on exactly how this is computed see: https://towardsdatascience.com/face-recognition-how-lbph-works-90ec258c3d6b
    # For here though, we are just going to assume that for each pixel in the image we have
    # a value on the range [0,255]. Next we need to create a feature vector based on these
    # values. We will use np.histogram to classify our values.
    # np.histogram takes as input the features you want to bin, the number of bins and the range
    # of the input.
    # By default we will classify the features into 128 bins on the range 0-255.
    feats, edges = np.histogram(feat, bins=nbins, range=range_bins)

    # now we need to return feats and edges and then go back to the template.py
    return feat, edges
Exemplo n.º 19
0
def LBP(image, points=8, radius=1):
    '''
    Uniform Local Binary Patterns algorithm
    Input image with shape (height, width, channels)
    Output features with length * number of channels
    '''
    # calculate pattern length
    length = points**2 - abs(points - 3)
    # lbp per channel in image
    histogram = np.empty(0, dtype=np.int)
    for i in range(image.shape[2]):
        channel = image[:, :, i]
        pattern = lbp(channel, points, radius, method='nri_uniform')
        pattern = pattern.astype(np.int).ravel()
        pattern = np.bincount(pattern)
        if len(pattern) < length:
            pattern = np.concatenate((pattern, np.zeros(59 - len(pattern))))
        histogram = np.concatenate((histogram, pattern))
    # normalize the histogram and return it
    features = (histogram - np.mean(histogram)) / np.std(histogram)
    return features
Exemplo n.º 20
0
def base_lbp(image, P, R, method, block,):
    check_nD(image, 2)
    image = np.ascontiguousarray(image, dtype=np.double)
    output = lbp(image, P, R, method) # original

    if method == DEFAULT:
        bins = 2**P
    
    elif method == UNIFORM:
        bins = P + 2

    elif method == NRI_UNIFORM:
        bins = P * (P - 1) + 3
    
    elif method == ROR:
        bins = bins_ROR[str(P)]
    
    else: # method == VAR
        bins = None

    return histogram(output, bins, block)
Exemplo n.º 21
0
image = cv2.imread(
    "/home/efforia/PycharmProjects/OpenCVProject/images/grant.jpg",
    cv2.IMREAD_GRAYSCALE)
cv2.imshow("image", image)

target_flag = False
target_lbp_hist = None
finish_sliding = False

radius = 2
n_points = radius * 8

roi = None
ROI_selector = RectSelector("image", onmouse)
lbp_image = lbp(image, n_points, radius, 'uniform')

while True:
    if not ROI_selector.dragging:
        cv2.imshow("image", image)
        if roi is not None:
            cv2.imshow("roi", roi)

            if target_flag:
                lbp_count = 0
                attempt = 0
                stepSize = 32
                clone = image.copy()
                target_flag = False
                finish_sliding = False
                cv2.imshow("target roi", roi)
    def cell_lbp_hist(self, img, trans=True):
        if trans == True:
            img = img.transpose()
        winSize = self.winSize
        blockSize = self.blockSize
        blockStride = self.blockStride
        cellSize = self.cellSize
        winStride = self.winStride
        radius = self.radius
        npoints = self.npoints
        from skimage.feature import local_binary_pattern as lbp
        import numpy as np
        METHOD = 'uniform'
        flag_1 = (winSize[0] % blockSize[0] == 0
                  and winSize[1] % blockSize[1] == 0
                  and blockSize[0] % cellSize[0] == 0
                  and blockSize[0] % cellSize[0] == 0)
        flag_2 = (img.shape[0] >= 64 and img.shape[1] >= 128)

        if flag_2 == False:
            print('Size error')
            print img.shape
            return
        lbpim = lbp(img, npoints, radius, METHOD)
        up_left = (0, 0)
        b_left = (0, winSize[1])
        up_right = (winSize[0], 0)
        b_right = winSize

        win_corners = (up_left, up_right, b_right, b_left)
        hist = np.array([])
        while win_corners[2][1] <= lbpim.shape[1]:
            while win_corners[2][0] <= lbpim.shape[0]:

                win = lbpim[win_corners[0][0]:win_corners[1][0],
                            win_corners[0][1]:win_corners[3][1]]
                bl_corners = ((0, 0), (blockSize[0], 0), blockSize,
                              (0, blockSize[1]))

                while bl_corners[2][1] <= win.shape[1]:
                    while bl_corners[2][0] <= win.shape[0]:
                        block = win[bl_corners[0][0]:bl_corners[1][0],
                                    bl_corners[0][1]:bl_corners[3][1]]
                        cell_corners = ((0, 0), (cellSize[0], 0), cellSize,
                                        (0, cellSize[1]))
                        while cell_corners[2][1] <= block.shape[1]:
                            while cell_corners[2][0] <= block.shape[0]:
                                cell = block[
                                    cell_corners[0][0]:cell_corners[1][0],
                                    cell_corners[0][1]:cell_corners[3][1]]
                                local_hist = np.zeros(npoints + 2)
                                for line in cell:
                                    for i in line:
                                        index = int(i)
                                        local_hist[index] += 1
                                hist = np.concatenate((hist, local_hist))
                                cell_corners = stride(cell_corners, cellSize,
                                                      'right')
                            cell_corners = stride(cell_corners, cellSize,
                                                  'down')
                            cell_corners = left_start(cell_corners, cellSize)
                        bl_corners = stride(bl_corners, blockStride, 'right')
                    bl_corners = stride(bl_corners, blockStride, 'down')
                    bl_corners = left_start(bl_corners, blockSize)

                win_corners = stride(win_corners, winStride, 'right')
            win_corners = stride(win_corners, winStride, 'down')
            win_corners = left_start(win_corners, winSize)
        return hist.reshape((hist.shape[0], 1))
Exemplo n.º 23
0
    plt.title('IMG' + str(index[i]))
plt.show()

#Using the Local Binary Pattern (LBP) recognizing features

b = [i for i in range(0, 55)]
b.append(255)

lbp_imgs = []
lbp_hists = []

for im in imgs:
    im = cv2.cvtColor(
        im, cv2.COLOR_BGR2GRAY
    )  #needed to be added because otherwise expected im should be 2d array with rgb
    aux = lbp(im, 8, 28, method='default')
    lbp_imgs.append(aux)

    aux2, _ = np.histogram(aux, bins=b)
    lbp_hists.append(aux2)

lbp_hists = np.asarray(lbp_hists)

#some of the histograms are to jsut view it
plt.figure(figsize=(12, 2))

for i, hist in enumerate(lbp_hists[index]):
    plt.subplot(1, 5, i + 1)
    plt.bar(b[:len(b) - 2], hist[:len(hist) - 1])
    plt.xticks(())
    plt.yticks(())
from time import time
from matplotlib import pyplot as plt
from skimage.feature import local_binary_pattern as lbp

folder_awal = 'KELAS_DATA/'
folder_tujuan = 'KELAS_DATA_lbp/'
ls = os.listdir(folder_awal)

if folder_tujuan not in ls:
    os.mkdir(folder_tujuan)

R = 3  #radius
P = 8 * R  #himpunan titik ketetanggaan

total = 0
for i in range(len(ls)):
    now = time()
    filename = ls[i]
    filepath = folder_awal + filename

    img = cv2.imread(filepath, 1)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_lbp = lbp(img_gray, P, R, method='default')

    plt.imsave(folder_tujuan + 'lbp_' + filename, img_lbp, cmap='gray')

    print("{}/{} done in {:.2f}s. ({}%)".format(i + 1, len(ls),\
        time() - now, 100 * float(i + 1) / (len(ls))))
    total += (time() - now)

print('Total {:.2f}s.'.format(total))
Exemplo n.º 25
0
def handle_LBP(gray_img, sample_coord, block_size=(20, 60), similar_condi=0.85):
    """
    function:
        handle_LBP(gray_img, sample_coord[, block_size=(20, 60)[, similar_condi=0.85]]):
        計算原始圖像和 sample 的 LBP 相似度

    parameter:
        gray_img: 調整大小後的灰階圖像
        sample_coord: 所有 sample 位於原始圖像的位置(左上角座標)
        block_size: 和 handle_sample 的 block_size 相同
        similar_condi: 相似度的門檻值, 默認 0.85, 範圍[0, 1), float

    method:
        1. 計算 sample LBP 值以及直方圖
        2. 相似度比較去除最不相似的 sample
        (採取逐一比較的方式, 去除相似度最低的, 其餘的都當成 markers)
        (相似度最低的判斷為 當前相似個數 - 最小相似個數 > 全距 // 2)
        3. 天空全部都標成 marker(最上面一行)
        4. 侵蝕一次

    return:
        markers: 二值化的圖像, 用來當成 watershed 的 markers
    """

    markers = np.zeros(gray_img.shape, np.uint8)
    width, height = block_size

    # 儲存 每張 LBP 值的 直方圖列表
    hist_list = list()

    for coord in sample_coord:
        y, x = coord
        target = gray_img[y:y + height, x:x+width]

        # 1. 計算 LBP 值(為了要使用 cv2.calcHist 函數, 要把圖像的資料類型轉成 np.uint8)
        lbp_img = lbp(target, 8, 1).astype(np.uint8)
        hist = cv2.calcHist([lbp_img], [0], None, [256], [0, 256])
        hist_list.append(hist)

        # 畫出所有 sample 的位置
        cv2.rectangle(gray_img, (x, y), (x + width, y + height), (255, 255, 255), 5)
        # cv2.imshow('sample region', gray_img)

    # print('hist 個數: ', len(hist_list))

    # 2.
    # 存放相似度結果的列表
    similar_list = list()
    for i in range(0, len(hist_list)):
        cnt = -1  # 計算相似個數(採逐一比較, 自己和自己比一定相似, 所以要減一)
        for j in range(0, len(hist_list)):
            sim = cv2.compareHist(hist_list[i], hist_list[j], cv2.HISTCMP_CORREL)
            if sim >= similar_condi:
                cnt += 1
        similar_list.append(cnt)
    # print("similar list: ", similar_list, "個數: ", len(similar_list))

    # 當前的個數 - 最小相似個數 <= sample 數量 // 2
    # markers_coord = list()  # 儲存最後被當成 markers 的座標
    for index, coord in enumerate(sample_coord):
        y, x = coord
        if similar_list[index] - min(similar_list) <= (max(similar_list) - min(similar_list)) // 2:
            # markers_coord.append((y, x))
            markers[y:y+height, x:x+width] = 255
            cv2.rectangle(gray_img, (x, y), (x + width, y + height), (0, 0, 0), 2)

    # 加上天空的座標
    for x in range(0, gray_img.shape[1], width):
        # markers_coord.append((0, x))
        markers[10:10+height, x+width:x-width] = 255

    # cv2.imshow('sample region', gray_img)
    # print(len(markers_coord))

    # 3.
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    markers = cv2.erode(markers, kernel, iterations=1)
    # cv2.imshow('markers', markers)
    return markers
Exemplo n.º 26
0
    parser.add_argument('-i', '--insert', action='store_true')
    parser.add_argument('path', type=str)
    args = parser.parse_args()

    path = args.path
    filename = path.strip().split('/')[-1]
    if not os.path.isfile(path):
        sys.exit('File doesnt exist.')
    """
	Performing some operations, common for all
	the options
	"""
    #greyscale image
    img = cv.imread(path, cv.IMREAD_GRAYSCALE)
    #lbp representation
    lbp_img = lbp(img, cfg.params['P'], cfg.params['R'],
                  cfg.params['LBP_METHOD'])
    lbp_img = lbp_image.astype(np.uint8)
    #histogram representation
    hist = histogram.spatial(lbp_img, cfg.params['NX'], cfg.params['NY'],
                             cfg.params['NPATTERNS'], cfg.params['OVERLAPX'],
                             cfg.params['OVERLAPY'])

    #Verify if the argument photo is or not
    #a valid one, i.e, corresponds to dog nose.
    if args.verify:
        #loading svm object
        tgt = open(cfg.params['VAULT'] + 'SVM', 'rb')
        clf = pickle.load(tgt)
        tgt.close()
        #predict
        result = clf.predict([hist])
Exemplo n.º 27
0
def main(folders):
    """Pretty much self-explanatory thanks to python: sets up servos and then reads images from picamera array and does stuff based on params"""
    servo = Servo(12,23,60/320, 45/240,320,240,90,90)
    global recognizer
    recognizer = cv2.createLBPHFaceRecognizer()
    trainAll(folders)
    if showImage:
        cv2.namedWindow("The Luca Bazooka", cv2.cv.CV_WINDOW_AUTOSIZE)
    camera=PiCamera()
    camera.resolution=resolution
    camera.framerate=framerate
    rawCapture=PiRGBArray(camera,size=size)
    frameNumber = 0
    if outputToFile:
        video_writer=imageio.get_writer('~/The-Luca-Bazooka/'+filename,fps=24)
    for nonprocessed in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
        frameNumber += 1
        if debug:
            print('VIDEO CAPTURE GOING')
        frame=nonprocessed.array
        if rotate:
            frame=ndimage.rotate(frame,270)
        if debug:
            #print(frame)
            pass
        faces = extractFace(frame)
        for i in faces:
            (x, y, w, h) = i
            predicted = predict(
                cv2.cvtColor(crop(frame, i), cv2.COLOR_RGB2GRAY))
            if showImage:
                cv2.imshow(
                'Detected Face', cv2.cvtColor(crop(frame, i), cv2.COLOR_RGB2GRAY))
                if visualizeLBP:
                    cv2.imshow('LBP Histogram',lbp(cv2.cvtColor(crop(frame,i),cv2.COLOR_RGB2GRAY)
                    ,1,15))
            if debugStuff:
                print(predicted)
            if predicted[1] <= confidenceLevel and (showImage or outputImage):
                print 'FOUND LUCA FACE'
                cv2.rectangle(frame, (x, y), (x+w, y+h), (227, 45, 45), 2)
                servo.update(x+w/2, y+h/2)
                if debug:
                    print('Updating servo with coords:')
                    print(x+w/2-160,y+h/2-120)
                charactersToCutOff=len('/home/pi')+len("/The-Luca-Bazooka/training/")
                cv2.putText(
                    frame, folders[predicted[0]][charactersToCutOff:-1], (x, y), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 255, 255))
            if predicted[1] <= confidenceLevel and not (showImage or outputImage):
                servo.update(x+w/2-160, y+h/2-120)
                if debug:
                    print 'UPDATING SERVO WITH COORDS:'
                    print (x+w/2,y+h/2)
                    print 'FOUND LUCA FACE'
                    print 'CONFIDENCE START'
                    print (predicted[1])
                    print 'CONFIDENCE END'
            else:
                if debug:
                    print 'FOUND NON-LUCA FACE'
                    print (x,y,x+w,y+h)
                if showImage or outputImage:
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 191, 255), 2)
        if outputImage:
            cv2.imwrite(outputImagePath+str(frameNumber)+'.jpg',frame)
        if showImage:
            cv2.imshow("The Luca Bazooka", frame)
        if outputToFile==True:
            video_writer.append_data(frame)
        rawCapture.truncate(0)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    if outputToFile==True:
        video_writer.close()
    vidcap.release()
    cv2.destroyAllWindows()
Exemplo n.º 28
0
while True:
    if not ROI_selector.dragging:
        cv2.imshow("image", image)
        if roi is not None:
            cv2.imshow("roi", roi)
            hsv_roi = cv2.cvtColor(roi.copy(), cv2.COLOR_BGR2HSV)
            gray_roi = cv2.cvtColor(roi.copy(), cv2.COLOR_BGR2GRAY)

            if target_flag:
                target_flag = False
                cv2.imshow("target roi", roi)
                target_hist = cv2.calcHist([hsv_roi], [0], None, [24],
                                           [0, 180])
                show_hist(target_hist, "target")

                target_lbp = lbp(gray_roi, n_points, radius, 'uniform')
                target_lbp = np.asarray(target_lbp, dtype=np.uint8)
                n_bins = int(target_lbp.max() + 1)
                target_lbp_hist = cv2.calcHist([target_lbp], [0], None,
                                               [n_bins], [0, n_bins])
                # cv2.normalize(target_lbp_hist, target_lbp_hist, 0, 1, cv2.NORM_MINMAX)
                # target_lbp_hist, _ = np.histogram(target_lbp, bins=n_bins, range=(0, n_bins), normed=True)
                # target_lbp_hist = np.asarray(target_lbp_hist, dtype=np.float32)
            elif candidate_flag:
                candidate_flag = False
                candidate_hist = cv2.calcHist([hsv_roi], [0], None, [24],
                                              [0, 180])
                show_hist(candidate_hist, "candidate")

                candidate_lbp = lbp(gray_roi, n_points, radius, 'uniform')
                candidate_lbp = np.asarray(candidate_lbp, dtype=np.uint8)