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
Esempio n. 2
0
def hogDetection(frame, winStride, scale):

    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

    (rects, weights) = hog.detectMultiScale(frame, winStride=winStride, padding=(4,4)
                                            , scale=scale)

    # apply nms
    rects = nms(rects, probs=None, overlapThresh=0.65)

    return rects
Esempio n. 3
0
def detect_people(frame, winStride, scale, dnn):

    if dnn is False:
        hog = cv2.HOGDescriptor()
        hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

        (rects, weights) = hog.detectMultiScale(frame,
                                                winStride=winStride,
                                                padding=(4, 4),
                                                scale=scale,
                                                hitThreshold=0.4)
        rects = nms(rects, probs=None, overlapThresh=0.65)

    else:
        rects = []
        prototxt = "../model/MobileNetSSD_deploy.prototxt"
        caffeModel = "../model/MobileNetSSD_deploy.caffemodel"
        net = cv2.dnn.readNetFromCaffe(prototxt, caffeModel)
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        (h, w) = frame.shape[:2]
        blob = cv2.dnn.blobFromImage(rgb, 0.007843, (w, h), 127.5)

        net.setInput(blob)
        detections = net.forward()

        for i in np.arange(0, detections.shape[2]):

            confidence = detections[0, 0, i, 2]

            if confidence > 0.5:

                idx = int(detections[0, 0, i, 1])

                if CLASSES[idx] != "person":
                    continue

                box = detections[0, 0, i, 3:7] * np.array(
                    [w, h, w, h])  # rescale detection box size to frame size
                (startX, startY, endX, endY) = box.astype("int")

                x = startX
                y = startY
                w = endX - startX
                h = endY - startY

                rects.append((x, y, w, h))

    return rects
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)