def hog_det(frame):
	boundingboxes = []
	gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

	#Assuming we have a trained SVM model stroed in pickle file
	clf = joblib.load("/home/brij/python_opencv/real/pedestrians_det.pkl")
	win_size = (64, 128)
	m,n = gray.shape
	i,j,i1,j1=128,64,0,0
	iter_=0 
	while (i<=m):
		while(j<=n):
		 	mask = gray[i1:i,j1:j]

			hog0 = hog(mask, orientations=9, pixels_per_cell=(8,8), cells_per_block=(2,2), visualise=False)
			temp = clf.predict(hog0)
			print temp
			if temp == [1]:

				points = j1,i1,j1+64,i1+128
				boundingboxes.append(points)

			j=j+8
			j1 = j1+8
		j = 64
		j1=0
		i = i+8
		i1= i1+8
	boundingboxes = asarray(boundingboxes)
	pick = nms.non_max_suppression_slow(boundingboxes, 0.3)
	for (startX, startY, endX, endY) in pick:
		cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2)
	
	
	return frame
Пример #2
0
def testImg():
    svm = cv2.ml.SVM_load(".\svm.xml")
    for file in glob.glob(".\/test\pos\*.png"):
        img = cv2.imread(file)
        boundingList = None
        for (x, y, window) in helpers.sliding_window(img,
                                                     stepSize=32,
                                                     windowSize=(64, 128)):
            if window.shape[0] != 128 or window.shape[1] != 64:
                continue

            winDescriptor = hog.compute(window)
            rows, cols = winDescriptor.shape
            winDescriptor = winDescriptor.reshape((cols, rows))
            prediction = svm.predict(winDescriptor)
            if prediction[1] == 0:
                boundingBox = (x, y, x + 64, y + 128)
                if boundingList is None:
                    boundingList = np.array([boundingBox])
                else:
                    boundingList = np.vstack((boundingList, [boundingBox]))
            else:
                continue
            pick = nms.non_max_suppression_slow(boundingList, 0.3)
            x, y, w, h = pick[0]
            cv2.rectangle(img, (x, y), (w, h), (0, 255, 0), 2)
            cv2.imshow("image", img)
            k = cv2.waitKey(30) & 0xff
            if k == 27:
                break
def filter_bbs(bbs):
    """Filter a list of bounding boxes to reduce no. of overlapping windows.
    
    Returns a list of tuples

    `windows` is a list of tuples
    """
    if not bbs:
        return []

    overlap_thresh = 0.2  # threshold for non max suppression
    subset = nms.non_max_suppression_slow(
            np.vstack(bbs), overlap_thresh)
    return [tuple(x) for x in subset]
Пример #4
0
def extract(img):
    """
    # Check if image file exists.
    if os.path.exists(image) == False:
        logging.error("can not open %s: no such file.", image)
        sys.exit(0)
    # CV2 read image in BGR mode, need to transform it into RGB.
    img_BGR = cv2.imread(image)
    img = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)
    """
    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=1)

    # delete the region which contains whole image
    regions = sorted(regions, key=lambda x: x['size'], reverse=True)

    candidates = []

    for r in regions:
        # excluding biggest retangle which contains whole image
        if r['rect'][0] == 0 and r['rect'][1] == 0:
            continue
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding parts that are too small
        x, y, w, h = r['rect']

        if w * h < 9:
            continue

        # ecludeing parts too sharp
        if w > 100 * h or h > 100 * w:
            continue

        candidates.append(r['rect'])

    # remove rectangles opverlap each other with nms technique
    candidates = nms.non_max_suppression_slow(candidates)

    return candidates
Пример #5
0
def detect_people(model, gray_image, peopleThresh, overlapThresh):
    im_gray = gray_image / 255
    h, w = gray_image.shape[0:2]
    jud = im_gray.reshape((1, h, w, 1))
    output = model.predict(jud)
    #out = output.copy()
    output = output.reshape((output.shape[1], output.shape[2]))
    people_loc = []
    for i in range(0, output.shape[0]):
        for j in range(0, output.shape[1]):
            if output[i, j] > peopleThresh:
                x = j * 8
                y = i * 8
                people_loc.append([x, y, x + 64, y + 128, output[i, j]])

    people_loc = np.asarray(people_loc)
    loc_result = nms.non_max_suppression_slow(people_loc, overlapThresh)

    return loc_result
Пример #6
0
def detect_people_mutiple_size(model, gray_image, peopleThresh, overlapThresh,
                               minsize, maxsize, rate):
    im_gray = gray_image
    h, w = gray_image.shape[0:2]
    hmin = round(h * minsize)
    wmin = round(w * minsize)
    hmax = round(h * maxsize)
    wmax = round(w * maxsize)

    dw = wmin
    dh = hmin

    all_loc = []

    while dh < hmax or dw < wmax:
        jug_im = cv2.resize(im_gray, (dw, dh))
        loc = detect_people(model, jug_im, peopleThresh, overlapThresh)
        #print (loc==[])
        #print (loc)
        if loc != []:
            loc[:, 0:4] = loc[:, 0:4] * (w / dw)
            all_loc.append(loc)
        dh = round(dh * rate)
        dw = round(dw * rate)
    all_loc = np.asarray(all_loc)

    size = 0
    for i in range(0, len(all_loc)):
        size = size + all_loc[i].shape[0]

    all_list = np.zeros((size, 5))

    index = 0

    for i in range(0, len(all_loc)):
        #print ('aa',all_loc[i].shape[0])
        all_list[index:index + all_loc[i].shape[0]] = all_loc[i].copy()
        index = index + all_loc[i].shape[0]

    loc_result = nms.non_max_suppression_slow(all_list, overlapThresh)

    return loc_result
Пример #7
0
def draw_yolo(im_, box_, class_co=25):
    bxy, bwh, to, classes = box_
    im = im_.copy()
    for i in range(bxy.shape[0]):
        list_box = []
        for x in range(13):
            for y in range(13):
                for anc in range(5):
                    confident = to[i, y, x, anc, 0]
                    class_score = classes[i, y, x, anc, :]
                    score = confident * class_score.max()
                    if score > 0.3:
                        xc = (x + bxy[i, y, x, anc, 0]) / 13
                        yc = (y + bxy[i, y, x, anc, 1]) / 13
                        #print (xc,yc)
                        xc = xc * 416
                        yc = yc * 416
                        w = bwh[i, y, x, anc, 0] * 416
                        h = bwh[i, y, x, anc, 1] * 416
                        #print (w/416,h/416)
                        x1 = int(xc - w / 2)
                        y1 = int(yc - h / 2)
                        x2 = int(xc + w / 2)
                        y2 = int(yc + h / 2)
                        obj = np.where(class_score == class_score.max())
                        obj_id = obj[0][0]
                        list_box.append([x1, y1, x2, y2, score, obj_id])
        list_box = np.asarray(list_box)
        new_list_box = nms.non_max_suppression_slow(list_box, 0.7)
        if len(new_list_box) > 1:
            for j in range(0, new_list_box.shape[0]):
                x1 = inrange(new_list_box[j, 0])
                y1 = inrange(new_list_box[j, 1])
                x2 = inrange(new_list_box[j, 2])
                y2 = inrange(new_list_box[j, 3])
                obj_id = int(new_list_box[j, 5])
                cv2.rectangle(im[i], (x1, y1), (x2, y2), (0, 0, 255), 2)
                cv2.putText(im[i], obj_name[obj_id], (x1, y1 + 30), 0, 1,
                            (0, 0, 255), 2)
    return im
Пример #8
0
        #expansion
        print "[x] %d initial bounding boxes" % (len(detect_list))
        detect_list = np.array(detect_list)
        detect_list = expansion(detect_list)
        '''
		img = image.copy()
		for (x1,y1,x2,y2) in detect_list:
				cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
		cv2.imwrite('/home/cad/dataset/images/result/'+filename.split('.')[0]+'_expansion.jpg',img)
		'''
        #Apply non-maximum Suppression and save the final result
        boundingBoxes = np.array(detect_list)
        print "[x] after applying expansion, %d bounding boxes" % (
            len(boundingBoxes))
        pick = non_max_suppression_slow(boundingBoxes, 0.5)
        print "[x] after applying non-maximum, %d bounding boxes" % (len(pick))

        pick_src = readtxt(path='/home/cad/dataset/images/location.txt',
                           filename=filename)
        if len(pick_src) > 0:
            pick_right = judge(np.array(pick), np.array(pick_src), 0.4)

            for (startX, startY, endX, endY) in pick:
                cv2.rectangle(image, (startX, startY), (endX, endY),
                              (0, 0, 255), 2)
            for (startX, startY, endX, endY) in pick_right:
                cv2.rectangle(image, (startX, startY), (endX, endY),
                              (0, 255, 0), 2)
            cv2.imwrite(
                '/home/cad/dataset/images/result/' + filename.split('.')[0] +
Пример #9
0
import nms
import numpy as np
import cv2

images = [('images/audrey.jpg',
           np.array([(12, 84, 140, 212), (24, 84, 152, 212),
                     (36, 84, 164, 212), (12, 96, 152, 224),
                     (24, 108, 152, 236)])),
          ('images/bksomels.jpg',
           np.array([(114, 60, 178, 124), (120, 60, 184, 124),
                     (114, 66, 178, 130)])),
          ('images/gpripe.jpg',
           np.array([(12, 30, 76, 94), (12, 36, 76, 100), (72, 36, 200, 164),
                     (84, 48, 212, 176)]))]

for (imagePath, boundingBoxes) in images:
    image = cv2.imread(imagePath)
    orig = image.copy()

    for (startX, startY, endX, endY) in boundingBoxes:
        cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 0, 255), 2)

    pick = nms.non_max_suppression_slow(boundingBoxes)
    for (startX, startY, endX, endY) in pick:
        cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)

    cv2.imshow("Original", orig)
    cv2.imshow("After NMS", image)
    cv2.waitKey(0)
Пример #10
0
        (114, 66, 178, 130)])),
    ("../images/gpripe.jpg", np.array([
        (12, 30, 76, 94),
        (12, 36, 76, 100),
        (72, 36, 200, 164),
        (84, 48, 212, 176)]))]

# loop over the images
for (image_path, bounding_boxes) in images:
    # load the image and clone it
    print "[x] %d initial bounding boxes" % (len(bounding_boxes))
    image = cv2.imread(image_path)
    orig = image.copy()

    # loop over the bounding boxes for each image and draw them
    for (startX, startY, endX, endY) in bounding_boxes:
        cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 0, 255), 2)

    # perform non-maximum suppression on the bounding boxes
    pick = non_max_suppression_slow(bounding_boxes, 0.3)
    print "[x] after applying non-maximum, %d bounding boxes" % (len(pick))

    # loop over the picked bounding boxes and draw them
    for (startX, startY, endX, endY) in pick:
        cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)

    # display the images
    cv2.imshow("Original", orig)
    cv2.imshow("After NMS", image)
    cv2.waitKey(0)
Пример #11
0
    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))
                prob.append(resp[0][1])

    mod_im = image.copy()
    for rect in nms.non_max_suppression_slow(np.array(bounding_box),
                                             prob,
                                             overlap_thresh=0.27):
        cv2.rectangle(mod_im, (rect[0], rect[1]), (rect[2], rect[3]),
                      (0, 255, 0), 2)
    cv2.imshow('figure', mod_im)
    cv2.waitKey(0)