def detect(self, image, winDim, winStep=4, pyramidScale=1.5, minProb=0.7):
        boxes = []
        probs = []

        # loop over the image pyramid (scale down image by pyramidScale)
        for layer in helpers.pyramid(image, scale=pyramidScale,
                                     minSize=winDim):
            scale = image.shape[0] / float(layer.shape[0])

            # loop over the sliding widnows for hte current pyramid lyaer
            # 由左至右,以 sliding_window 的大小從目前的 pyramid layer 擷取影像內容
            for (x, y,
                 window) in helpers.sliding_window(layer, winStep, winDim):
                # Get window dimensions
                (winH, winW) = window.shape[:2]

                # ensure the window dimensions match the supplied sliding window dimensions
                if winH == winDim[1] and winW == winDim[0]:
                    # Get the HOG features and reshape it to one raw array (features vector)
                    features = self.desc.describe(window).reshape(1, -1)
                    # use our trained model to predict by using this feature vector
                    prob = self.model.predict_proba(features)[0][1]

                    # if probability over the min threshold, then add it into the result array
                    if prob > minProb:
                        (startX, startY) = (int(scale * x), int(scale * y))
                        endX = int(startX + (scale * winW))
                        endY = int(startY + (scale * winH))

                        boxes.append((startX, startY, endX, endY))
                        probs.append(prob)

        return (boxes, probs)
def slide(input_img, output):
    pixelate_count = 0

    for resized in helpers.pyramid(input_img, scale=2):
        # loop over the sliding window for each layer of the pyramid
        for (x, y, window) in helpers.sliding_window(resized, stepSize=STEP_SIZE, windowSize=(WIN_SIZE, WIN_SIZE)):
            # if the window does not meet our desired window size, ignore it
            if window.shape[0] != WIN_SIZE or window.shape[1] != WIN_SIZE:
                continue

            # This code below shows sliding window
            # clone = resized.copy()
            # cv2.rectangle(clone, (x, y), (x + WIN_SIZE, y + WIN_SIZE), (0, 255, 0), 2)

            # Ensure crop is on the original location after pyramid resize
            temp_y_start = y + (input_img.shape[1] - resized.shape[1])
            temp_x_start = x + (input_img.shape[0] - resized.shape[0])

            temp_y_end = temp_y_start + WIN_SIZE
            temp_x_end = temp_x_start + WIN_SIZE

            try:
                crop_img = cv2.resize(resized[temp_y_start:temp_y_end, temp_x_start:temp_x_end], (200, 200))
            except Exception as e:
                continue

            # Emoji confidence is below 0.9, above is nonsense
            if emoji_AI(crop_img) < 0.9:
                # If verified an emoji, pixelate
                pixelate_count += 1
                output[temp_y_start:temp_y_end, temp_x_start:temp_x_end] = \
                    pixelate(output[temp_y_start:temp_y_end, temp_x_start:temp_x_end])

    print("Number of pixelation:", pixelate_count)
    return output
Exemple #3
0
    def detect(self, image, winDim, winStep=4, pyramidScale=1.5, minProb=0.7):
        boxes = []
        probs = []

        # loop over the image pyramid
        for layer in helpers.pyramid(image, scale=pyramidScale,
                                     minSize=winDim):
            scale = image.shape[0] / float(layer.shape[0])

            # loop over the sliding window
            for (x, y,
                 window) in helpers.sliding_window(layer, winStep, winDim):
                (winH, winW) = window.shape[:2]

                # ensure the window is not truncated
                # since sliding_window does not check boundaries
                if winH == winDim[1] and winH == winDim[0]:
                    # extract HOG features from current window
                    # and classify if contains an object
                    features = self.desc.describe(window)

                    features = feature.hog(
                        window,
                        orientations=conf["orientations"],
                        pixels_per_cell=tuple(conf["pixels_per_cell"]),
                        cells_per_block=tuple(conf["cells_per_block"]),
                        transform_sqrt=conf["normalize"])
Exemple #4
0
def scan_image(path):
    # load the image and define the window width and height
    image = cv2.imread(path, cv2.IMREAD_COLOR)
    # loop over the image pyramid
    level = 0
    lst_img = list()
    lst_imgDetail = list()
    ori_clone = image.copy()
    overlapImg = image.copy()
    for resized_image in pyramid(image, scale):
        # loop over the sliding window for each layer of the pyramid
        for (x, y, window) in sliding_window(resized_image, stepSize=stepSize, windowSize=(winW, winH)):
            # if the window does not meet our desired window size, ignore it
            if window.shape[0] != winH or window.shape[1] != winW:
                continue

            # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW, SUCH AS APPLYING A
            # MACHINE LEARNING CLASSIFIER TO CLASSIFY THE CONTENTS OF THE
            # WINDOW

            curWindow = (x, y, x + winW, y + winH)
            subImage = utils.to_image(resized_image).crop(curWindow)
            normalized_img = pre_processing_data.process_single_file(subImage)

            lst_img.append(normalized_img)
            imgDetail = (x, y, level, resized_image)
            lst_imgDetail.append(imgDetail)

        level += 1

    # Predict all window
    lst_indexPositive, positive_scores = predict_multi(lst_img, model_path)
    time_now("fusing")
    for i in lst_indexPositive:
        subX, subY, subLevel, subImg = lst_imgDetail[i]
        ori_x, ori_y, new_winW, new_winH = reverse_window(subX, subY, subImg.shape[1], subImg.shape[0],
                                                          scale ** subLevel, image.shape[1], image.shape[0], winW, winH)
        # Get positive image and save it
        ori_window = (ori_x, ori_y, ori_x + new_winW, ori_y + new_winH)
        # Draw rectangle on output image
        cv2.rectangle(ori_clone, (ori_x, ori_y), (ori_x + new_winW, ori_y + new_winH), (0, 255, 0), 2)

        lstRect.append(ori_window)
    overlappedLst = run_NMS(lstRect, positive_scores, 0.05)
    time_now("draw rect")
    for i in overlappedLst:
        x1, y1, x2, y2 = lstRect[i]
        cv2.rectangle(overlapImg, (x1, y1), (x2, y2), (0, 255, 0), 2)

    result_path = os.path.join(stored_path, 'result.png')
    not_overlap = os.path.join(stored_path, 'be4.png')
    utils.to_image(ori_clone).save(not_overlap, 'png')
    utils.to_image(overlapImg).save(result_path, 'png')
    return result_path, len(overlappedLst)
Exemple #5
0
def find_ppl(img):
    for (counter,
         resized) in enumerate(pyramid(img, scale=1.2, minSize=(128, 128)), 1):
        ###################################################################### y , x (h , w)
        for (x, y, window) in sliding_window2(resized,
                                              stepSize_h=32,
                                              stepSize_v=16,
                                              windowSize=(128, 64)):
            ############## y, h           x, w
            if window.shape[0] != 128 or window.shape[1] != 64:
                continue
            clone = resized.copy()
            crop_img = clone[y:y + 128, x:x + 64]
            lbp_hist = lbp2(crop_img, method='uniform')
            #prediction = model.predict(lbp_hist.reshape(1, -1))
            pred = model.decision_function(lbp_hist.reshape(1, -1))
            #cv2.imshow('cropped', crop_img)
            weight = 1.2**(counter - 1)
            ##################################### PRZEDSTAWIENIE GRAFICZNE SKANOWANIA
            if pred > 0:
                xw = int(x * weight)
                yw = int(y * weight)
                dx = int(64 * weight)
                dy = int(128 * weight)
                cv2.rectangle(clone, (x, y), (x + 64, y + 128), (0, 255, 0), 2)
                #cv2.rectangle(img, (xw,yw), (xw+dx, yw+dy), (0,255,0), 2)   ################ ZAZNACZENIE NA ORAZIE WYKRYTYCH SYLWETEK
                rects.append((xw, yw, xw + dx, yw + dy, pred))
                #cv2.imwrite('neg_mine_128x64\\neg_mine' + str(i) + '.png', crop_img)
                #i += 1
            else:
                cv2.rectangle(clone, (x, y), (x + 64, y + 128), (0, 0, 255), 2)
            cv2.imshow("window", clone)
            if cv2.waitKey(1) == 0x1b:
                break
            #time.sleep(0.025)
            #print(counter)
        if cv2.waitKey(1) == 0x71:
            break
    #hm = heatmap(rects, img.shape[0], img.shape[1])
    #print(len(rects))
    #print(numpy.array(rects))
    pick = non_max_suppression_moje2(numpy.array(rects), 0.6)
    #print(pick)
    # draw the final bounding boxes
    for (xA, yA, xB, yB, c, vote) in pick:
        print(vote)
        print(0.15 * len(rects))
        # cv2.waitKey(0)
        if vote > 0.15 * len(rects):
            cv2.rectangle(img, (int(xA), int(yA)), (int(xB), int(yB)),
                          (0, 255, 0), 2)

    return img
    def detect(self, image, winDim, winStep=4, pyramidScale=1.5, minProb=0.7):
        # initialize the list of bounding boxes and associated probabilities
        boxes = []
        probs = []

        # loop over the image pyramid
        for layer in helpers.pyramid(image, scale=pyramidScale,
                                     minSize=winDim):
            # determine the current scale of the pyramid
            scale = image.shape[0] / float(layer.shape[0])

            # loop over the sliding windows for the current pyramid layer
            for (x, y,
                 window) in helpers.sliding_window(layer, winStep, winDim):
                # grab the dimensions of the window
                (winH, winW) = window.shape[:2]

                # ensure the window dimensions match the supplied sliding window dimensions
                if winH == winDim[1] and winW == winDim[0]:
                    # extract HOG features from the current window and classifiy whether or
                    # not this window contains an object we are interested in
                    features = self.desc.describe(window).reshape(1, -1)
                    prob = self.model.predict_proba(features)[0][1]

                    # check to see if the classifier has found an object with sufficient
                    # probability
                    if prob > minProb:
                        # compute the (x, y)-coordinates of the bounding box using the current
                        # scale of the image pyramid
                        (startX, startY) = (int(scale * x), int(scale * y))
                        endX = int(startX + (scale * winW))
                        endY = int(startY + (scale * winH))

                        # update the list of bounding boxes and probabilities
                        boxes.append((startX, startY, endX, endY))
                        probs.append(prob)

        # return a tuple of the bounding boxes and probabilities
        return (boxes, probs)
Exemple #7
0
def hardNegativeMining():
    svm = cv2.ml.SVM_load(".\svm.xml")

    (winW, winH) = (64, 128)
    # for each scale of every image in the negative test dataset apply the sliding window technique
    # get HOG descriptor of every window and use SVM to predict results
    for image_file in glob.glob(".\/test\/neg\*.png"):
        image = cv2.imread(image_file)
        for resized in helpers.pyramid(image, scale=1.5):
            for (x, y, window) in helpers.sliding_window(resized,
                                                         stepSize=32,
                                                         windowSize=(winW,
                                                                     winH)):
                # if the window does not meet our desired window size, ignore it
                if window.shape[0] != winH or window.shape[1] != winW:
                    continue

                winDescriptor = hog.compute(window)
                rows, cols = winDescriptor.shape
                winDescriptor = winDescriptor.reshape((cols, rows))
                prediction = svm.predict(winDescriptor)
                print(prediction)
Exemple #8
0
@author: Allent_Computer
"""
# import the necessary packages
import helpers
import argparse
import time
import cv2

# load the image and define the window width and height
image = cv2.imread('E:/python/myInceptionProject/detected_image.jpg')
(winW, winH) = (60, 48)

i = 1
# loop over the image pyramid
for resized in helpers.pyramid(image, scale=1.5):
    # loop over the sliding window for each layer of the pyramid
    for (x, y, window) in helpers.sliding_window(resized,
                                                 stepSize=16,
                                                 windowSize=(winW, winH)):
        # if the window does not meet our desired window size, ignore it
        if window.shape[0] != winH or window.shape[1] != winW:
            continue

        # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW, SUCH AS APPLYING A
        # MACHINE LEARNING CLASSIFIER TO CLASSIFY THE CONTENTS OF THE
        # WINDOW

        # since we do not have a classifier, we'll just draw the window
        clone = resized.copy()
        #        cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
Exemple #9
0
# USAGE
# python pyramid.py --image imageApp/obama.jpg

import predict
import pre_processing_data
# import imutils
from helpers import pyramid
from helpers import sliding_window
import argparse
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
ap.add_argument("-s",
                "--scale",
                type=float,
                default=1.5,
                help="scale factor size")
args = vars(ap.parse_args())

# load the image and define the window width and height
image = cv2.imread(args["image"])
# (winW, winH) = (128, 128)

for (i, resized) in enumerate(pyramid(image, scale=args["scale"])):
    # show the resized image
    cv2.imshow("Layer {}".format(i + 1), resized)
    cv2.waitKey(0)
from helpers import pyramid
from helpers import sliding_window
import cv2
import time

if __name__ == "__main__":
    image = cv2.imread('images/pedestrian.jpg')
    print(image.shape)
    (win_height, win_width) = (128, 128)
    for layer in pyramid(image):
        for (x, y, window) in sliding_window(layer,
                                             step_size=32,
                                             window_size=(win_width,
                                                          win_height)):
            if window.shape[0] != win_height or window.shape[1] != win_width:
                continue
            clone = layer.copy()
            cv2.rectangle(clone, (x, y), (x + win_width, y + win_height),
                          (0, 255, 0), 2)
            cv2.imshow("Window", clone)
            cv2.waitKey(1)
            time.sleep(0.025)
Exemple #11
0
from helpers import pyramid
from helpers import sliding_window
import cv2
import time

image = cv2.imread('sliding_window.jpg')
(winW, winH) = (128, 128)

for resized in pyramid(image, scale=1.5):
    # loop over the sliding window for each layer of the pyramid
    for (x, y, window) in sliding_window(resized,
                                         stepSize=32,
                                         windowSize=(winW, winH)):
        # if the window does not meet our desired window size, ignore it
        if window.shape[0] != winH or window.shape[1] != winW:
            continue

        # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW, SUCH AS APPLYING A
        # MACHINE LEARNING CLASSIFIER TO CLASSIFY THE CONTENTS OF THE
        # WINDOW

        # since we do not have a classifier, we'll just draw the window
        clone = resized.copy()
        cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
        cv2.imshow("Window", clone)
        cv2.waitKey(1)
        time.sleep(0.025)
cv2.destroyAllWindows()
from helpers import pyramid
from helpers import sliding_window
import time
import cv2

eye = cv2.imread('..\\bazy\\Webcam\\eyes\\3.jpg', 0)
(winW, winH) = (103, 31)
orb = cv2.ORB_create()
kp_eye, eye_descriptor = orb.detectAndCompute(eye, None)  # find key points and computer descriptor

face = cv2.imread('..\\bazy\\Webcam\\6.jpg', 0)
matches = []
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# loop over the image pyramid
for resized in pyramid(face, scale=1.5):
    # loop over the sliding window for each layer of the pyramid
    for (x, y, window) in sliding_window(resized, stepSize=15, windowSize=(winW, winH)):
        # if the window does not meet our desired window size, ignore it
        if window.shape[0] != winH or window.shape[1] != winW:
            continue
        # clone = resized.copy()

        kp_window, windows_descriptor = orb.detectAndCompute(window, None)  #
        match = matcher.match(eye_descriptor, windows_descriptor)
        # matches.append(match)
        # match = sorted(match, key=lambda x: x.distance)

        print match
        #
        # cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
Exemple #13
0
from helpers import pyramid
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to the input image")
ap.add_argument("-s",
                "--scale",
                type=float,
                default=1.5,
                help="scale factor size")
args = vars(ap.parse_args())

# load the input image
image = cv2.imread(args["image"])

# loop over the layers of the image pyramid and display them
for (i, layer) in enumerate(pyramid(image, scale=args["scale"])):
    cv2.imshow(f"Layer {i+1}", layer)
    cv2.waitKey(0)
Exemple #14
0
                required=True,
                type=int,
                help="height of sliding window")
ap.add_argument("-s",
                "--scale",
                type=float,
                default=1.5,
                help="scale factor size")
args = vars(ap.parse_args())

# load the input image and unpack the command line arguments
image = cv2.imread(args["image"])
(winW, winH) = (args["width"], args["height"])

# loop over the image pyramid
for layer in pyramid(image, scale=args["scale"]):
    # loop over the sliding window for each layer of the pyramid
    for (x, y, window) in sliding_window(layer,
                                         stepSize=32,
                                         windowSize=(winW, winH)):
        # if the current window does not meet our desired window size, ignore it
        if window.shape[0] != winH or window.shape[1] != winW:
            continue

        # THIS IS WHERE WE WOULD PROCESS THE WINDOW, EXTRACT HOG FEATURES, AND
        # APPLY A MACHINE LEARNING CLASSIFIER TO PERFORM OBJECT DETECTION

        # since we do not have a classifier yet, let's just draw the window
        clone = layer.copy()
        cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
        cv2.imshow("Window", clone)
Exemple #15
0
from helpers import non_max_suppression_moje2
from helpers import non_max_suppression

path = 'E:\\MAGISTERSKA\\neg_test'
#rects = []
pickle_in = open('lbp_uni_2000_ahonen.pickle', 'rb')
model = pickle.load(pickle_in)

for i, file in enumerate(os.listdir(path)):
    current_path = str(path) + '\\' + str(file)
    print(current_path)
    img = cv2.imread(current_path)
    img = imutils.resize(img, width=min(400, img.shape[1]))
    rects = []
    for (counter,
         resized) in enumerate(pyramid(img, scale=1.2, minSize=(128, 128)), 1):
        ###################################################################### y , x (h , w)
        for (x, y, window) in sliding_window2(resized,
                                              stepSize_h=32,
                                              stepSize_v=16,
                                              windowSize=(128, 64)):
            ############## y, h           x, w
            if window.shape[0] != 128 or window.shape[1] != 64:
                continue
            clone = resized.copy()
            crop_img = clone[y:y + 128, x:x + 64]
            lbp_hist = lbp2(crop_img, method='uniform')
            #prediction = model.predict(lbp_hist.reshape(1, -1))
            pred = model.decision_function(lbp_hist.reshape(1, -1))
            #cv2.imshow('cropped', crop_img)
            weight = 1.2**(counter - 1)
if len(onlyfiles) > 0:
    images = numpy.empty(len(onlyfiles), dtype=object)
    for n in range(0, len(onlyfiles)):
        images[n] = cv2.imread(join(mypath, onlyfiles[n]))
elif (cv2.imread(args["image"]) != None):
    images = numpy.empty(1, dtype=object)
    images[1] = cv2.imread(args["image"])
else:
    raise ValueError(
        'Invalid arguments; image or directory path reference needed')

for image in images:
    win_size = 16
    (winW, winH) = (win_size, win_size)

    for resized in pyramid(image, scale=1.5, minSize=(20, 20)):
        for (x, y, window) in sliding_window(resized,
                                             stepSize=win_size,
                                             windowSize=(winW, winH)):
            # if the window does not meet our desired window size, ignore it
            if window.shape[0] != winH or window.shape[1] != winW:
                continue

            #CLASSIFY SHIT HERE

            # since we do not have a classifier... ahem... we'll just draw the window
            clone = resized.copy()
            cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
            cv2.imshow("Window", clone)
            cv2.waitKey(1)
            # time.sleep(0.0001)
Exemple #17
0
image = cv2.imread(args["image"], cv2.IMREAD_COLOR)
# Get image name for output
imageName = args["image"].split("/")[1]
(winW, winH) = (64, 64)
# (winW, winH) = (128, 128)

start_time = datetime.datetime.now()

# loop over the image pyramid
count = 0
level = 0
scale = 1.25
lst_img = list()
lst_imgDetail = list()
ori_clone = image.copy()
for resized_image in pyramid(image, scale):
    # loop over the sliding window for each layer of the pyramid
    # for (x, y, window) in sliding_window(resized_image, stepSize=8, windowSize=(winW, winH)):
    for (x, y, window) in sliding_window(resized_image,
                                         stepSize=16,
                                         windowSize=(winW, winH)):
        # if the window does not meet our desired window size, ignore it
        if window.shape[0] != winH or window.shape[1] != winW:
            continue

        # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW, SUCH AS APPLYING A
        # MACHINE LEARNING CLASSIFIER TO CLASSIFY THE CONTENTS OF THE
        # WINDOW

        curWindow = (x, y, x + winW, y + winH)
        subImage = utils.to_image(resized_image).crop(curWindow)
Exemple #18
0
    # DETECT PEDESTRIAN
    hog = HOGDescriptor.createHOGDescriptor((64, 128), (16, 16), (8, 8),
                                            (8, 8), 9, False)
    origin_image = cv2.imread('data/Train/pos/crop001048.png')
    origin_r, origin_c, _ = origin_image.shape
    ratio = origin_c / origin_r

    image = cv2.resize(origin_image,
                       None,
                       fx=250 / origin_c,
                       fy=250 / (ratio * origin_r))
    r, c, _ = image.shape
    print(r, c)
    bounding_box = []
    prob = []
    for (i, layer) in enumerate(pyramid(image, 1.05, (64, 128))):
        m, n, _ = layer.shape
        for (x, y, window) in sliding_window(layer, 8, (64, 128)):
            if window.shape[0] != 128 or window.shape[1] != 64:
                continue
            normalized = cv2.resize(window, (64, 128))
            data = np.array([hog.compute(normalized)]).squeeze()
            data = data.reshape(1, -1)
            resp = model.predict_proba(data)

            x_begin = int((x / n) * c)
            y_begin = int((y / m) * r)
            x_end = int(((x + 63) / n) * c)
            y_end = int(((y + 127) / m) * r)
            if resp[0][1] > resp[0][0]:
                bounding_box.append((x_begin, y_begin, x_end, y_end))