def find_people_in_frame(inputFrame, detector=HOGDescriptor_getDefaultPeopleDetector):
    hog = HOGDescriptor()
    hog.setSVMDetector(detector())
    image = resize(inputFrame, width=min(400, inputFrame.shape[1]))
    orig = image.copy()
    (rects, _) = hog.detectMultiScale(
        image, winStride=(4, 4), padding=(8, 8), scale=1.05)
    for (x, y, w, h) in rects:
        rectangle(orig, (x, y), (x+w, y+h), (255, 0, 0), 2)
    rects = array([[x, y, x+w, y+h] for (x, y, w, h) in rects])
    merged_rects = nms(rects, probs=None, overlapThresh=0.65)
    for (xA, yA, xB, yB) in merged_rects:
        rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
        size_of_image = image.shape[0] * image.shape[1]
        size_of_window = abs(xA - xB) * abs(yA - yB)
        portion_occupied = (size_of_image / size_of_window) * 100
        color = None
        if portion_occupied >= 0.1:
            color = (255, 0, 0)
        elif portion_occupied >= 0.05:
            color = (0, 0, 255)
        else:
            color = (0, 255, 0)
        putText(
            image, f'Threat Level: {round(abs(xA - xB) * abs(yA - yB) * 1 / 150)}', (xA, yA-10), FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
    return image
Example #2
0
def get_hog_features(img,
                     orient,
                     pix_per_cell,
                     cell_per_block,
                     vis=False,
                     feature_vec=True,
                     fast=False):
    # Call with two outputs if vis==True
    if vis:
        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
    # Otherwise call with one output
    elif fast:
        w = img.shape[0]
        h = img.shape[1]
        cell_size = (pix_per_cell, pix_per_cell)
        block_size = (pix_per_cell * cell_per_block,
                      pix_per_cell * cell_per_block)
        block_stride = (pix_per_cell, pix_per_cell)
        opencv_hog = HOGDescriptor((w, h), block_size, block_stride, cell_size,
                                   orient)
        features = opencv_hog.compute(img)
        if not feature_vec:
            new_shape = ((w - block_size[0]) // block_stride[0] + 1,
                         (h - block_size[1]) // block_stride[1] + 1,
                         cell_per_block, cell_per_block, orient)
            features = features.reshape(new_shape)
        return features
    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
Example #3
0
 def cmptFeatures(self, image):
     winSize = (16, 48)
     blockSize = (16, 16)
     blockStride = (8, 8)
     cellSize = (8, 8)
     nbins = 9
     derivAperture = 1
     winSigma = 4.
     histogramNormType = 0
     L2HysThreshold = 2.0000000000000001e-01
     gammaCorrection = 100
     nlevels = 32
     hog = HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins,
                         derivAperture, winSigma, histogramNormType,
                         L2HysThreshold, gammaCorrection, nlevels)
     winStride = (8, 8)
     padding = (16, 16)
     hist = hog.compute(image, winStride, padding)
     return np.reshape(hist, 4500)
def run_detection_baseline(inputFolder, outputFolder):
    hog = HOGDescriptor()
    hog.setSVMDetector(HOGDescriptor_getDefaultPeopleDetector())
    if not exists(outputFolder):
        mkdir(outputFolder)
    for imagePath in list_images(inputFolder):
        image = imread(imagePath)
        image = resize(image, width=min(400, image.shape[1]))
        orig = image.copy()
        (rects, _) = hog.detectMultiScale(
            image, winStride=(4, 4), padding=(8, 8), scale=1.05)
        for (x, y, w, h) in rects:
            rectangle(orig, (x, y), (x+w, y+h), (0, 0, 255), 2)
        rects = array([[x, y, x+w, y+h] for (x, y, w, h) in rects])
        merged_rects = nms(rects, probs=None, overlapThresh=0.65)
        for (xA, yA, xB, yB) in merged_rects:
            rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
        filename = imagePath[imagePath.rfind('/') + 1:]
        imwrite(join(outputFolder, filename), image)
 def ComputeHOG(self, trainData, testData):
     print(
         'ComputeHog Function is called to calculate Histogram of Oriented Gradient'
     )
     trainSize = trainData.shape[0]
     testSize = testData.shape[0]
     if self.imgNormalize is True:
         trainData = trainData * 255
         testData = testData * 255
         trainData = trainData.astype('uint8')
         testData = testData.astype('uint8')
     else:
         print('Images already Normalized')
     hog = HOGDescriptor(_winSize=(self.winsize, self.winsize),
                         _blockSize=(self.blocksize, self.blocksize),
                         _blockStride=(self.blockstride, self.blockstride),
                         _cellSize=(self.cellsize, self.cellsize),
                         _nbins=self.nbin)
     hogTrain = np.zeros((trainSize, hog.getDescriptorSize()),
                         dtype="float32")
     for i in range(0, trainSize):
         trainImage = trainData[i].reshape(self.winsize, self.winsize)
         h = hog.compute(trainImage)
         hogTrain[i, :] = h[:, 0]
     hogTest = np.zeros((testSize, hog.getDescriptorSize()),
                        dtype="float32")
     for i in range(0, testSize):
         testImage = testData[i].reshape(self.winsize, self.winsize)
         h = hog.compute(testImage)
         hogTest[i, :] = h[:, 0]
         print("Extracted HoG features (", h.size, " per image)")
         return hogTrain, hogTest
Example #6
0
def traffic_sign_hog_descriptor():
    # hog params for traffic signs
    winSize = (24, 24)
    cellSize = (4, 4)
    blockSize = (4, 4)  # (8, 8)
    blockStride = (2, 2)  # (4, 4)
    bins = 24  # 18/36
    derivAperture = 1
    winSigma = -1.
    histogramNormType = 0
    L2HysThreshold = 0.2
    gammaCorrection = 1
    levels = 64
    signedGradients = True  # False

    # init hog descriptor
    return HOGDescriptor(winSize, blockSize, blockStride, cellSize, bins,
                         derivAperture, winSigma, histogramNormType,
                         L2HysThreshold, gammaCorrection, levels,
                         signedGradients)
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),
                                  block_norm='L2-Hys',
                                  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:
        USE_SKIMAGE = True
        if USE_SKIMAGE:
            features = hog(img,
                           orientations=orient,
                           pixels_per_cell=(pix_per_cell, pix_per_cell),
                           cells_per_block=(cell_per_block, cell_per_block),
                           block_norm='L2-Hys',
                           transform_sqrt=False,
                           visualise=vis,
                           feature_vector=feature_vec)
        else:
            features = HOGDescriptor(
                (64, 64), (16, 16), (pix_per_cell, pix_per_cell),
                (pix_per_cell, pix_per_cell), orient).compute(img)
        #print('Feature shape = {}'.format(features.shape))
        return features
Example #8
0
#    cv2.destroyAllWindows()

# show a histogram
#hist = cv2.calcHist([image], [0], None, [256], [0, 256])
#plt.figure()
#plt.title("Grayscale Histogram")
#plt.xlabel("Bins")
#plt.ylabel("# of Pixels")
#plt.plot(hist)
#plt.xlim([0, 256])

# -------------------------------- OpenCV: HoG --------------------------------

# Compute the histogram of oriented gradients
# http://docs.opencv.org/modules/gpu/doc/object_detection.html
hog = HOGDescriptor(_winSize=(w,w), _blockSize=(4,4), _blockStride=(2,2), _cellSize=(2,2), _nbins=9)

hogTrain = np.zeros((trainSize, hog.getDescriptorSize()), dtype="float32")
for i in xrange(0,trainSize):
    image = dataTrain[i].reshape(w,w)
    h = hog.compute(image)
    hogTrain[i,:] = h[:,0]

hogTest = np.zeros((testSize, hog.getDescriptorSize()), dtype="float32")
for i in xrange(0,testSize):
    image = dataTest[i].reshape(w,w)
    h = hog.compute(image)
    hogTest[i,:] = h[:,0]

print "Extracted HoG features (", h.size, " per image)"