Skip to content

Commit

Permalink
modified nms implementation using Adrians code
Browse files Browse the repository at this point in the history
  • Loading branch information
techfort committed Aug 27, 2015
1 parent 70ed55e commit 0125c91
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 51 deletions.
4 changes: 2 additions & 2 deletions chapter6/flann.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import cv2
from matplotlib import pyplot as plt

queryImage = cv2.imread('images/bathory_album.jpg',0)
trainingImage = cv2.imread('images/vinyls.jpg',0)
queryImage = cv2.imread('../images/bathory_album.jpg',0)
trainingImage = cv2.imread('../images/bathory_vinyls.jpg',0)

# create SIFT and detect/compute
sift = cv2.xfeatures2d.SIFT_create()
Expand Down
94 changes: 52 additions & 42 deletions chapter7/car_detector/non_maximum.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
# import the necessary packages
import numpy as np

def area(box):
return (abs(box[2] - box[0])) * (abs(box[3] - box[1]))

def overlaps(a, b, thresh=0.5):
print "checking overlap "
print a, b
x1 = np.maximum(a[0], b[0])
x2 = np.minimum(a[2], b[2])
y1 = np.maximum(a[1], b[1])
y2 = np.minimum(a[3], b[3])
intersect = float(area([x1, y1, x2, y2]))
return intersect / np.minimum(area(a), area(b)) >= thresh

def is_inside(rec1, rec2):
def inside(a,b):
if (a[0] >= b[0]) and (a[2] <= b[0]):
return (a[1] >= b[1]) and (a[3] <= b[3])
else:
return False

return (inside(rec1, rec2) or inside(rec2, rec1))

def non_max_suppression(boxes, overlap_thresh = 0.5):
# Malisiewicz et al.
# Python port by Adrian Rosebrock
def non_max_suppression_fast(boxes, overlapThresh):
# if there are no boxes, return an empty list
if len(boxes) == 0:
return []

# if the bounding boxes integers, convert them to floats --
# this is important since we'll be doing a bunch of divisions
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")

# initialize the list of picked indexes
pick = []

# grab the coordinates of the bounding boxes
x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]
scores = boxes[:,4]
score_idx = np.argsort(scores)

while len(score_idx) > 0:
box = scores[score_idx[0]]
print "checking box"
for s in score_idx:
to_delete = []
if s == 0:
continue
try:
if (overlaps(boxes[s], boxes[box], overlap_thresh)):
to_delete.append(box)
score_idx = np.delete(score_idx, [s], 0)
except:
pass
boxes = np.delete(boxes, to_delete, 0)
score_idx = np.delete(score_idx, 0, 0)

return boxes
# compute the area of the bounding boxes and sort the bounding
# boxes by the score/probability of the bounding box
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(scores)[::-1]

# keep looping while some indexes still remain in the indexes
# list
while len(idxs) > 0:
# grab the last index in the indexes list and add the
# index value to the list of picked indexes
last = len(idxs) - 1
i = idxs[last]
pick.append(i)

# find the largest (x, y) coordinates for the start of
# the bounding box and the smallest (x, y) coordinates
# for the end of the bounding box
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])

# compute the width and height of the bounding box
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)

# compute the ratio of overlap
overlap = (w * h) / area[idxs[:last]]

# delete all indexes from the index list that have
idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))

# return only the bounding boxes that were picked using the
# integer data type
return boxes[pick].astype("int")
6 changes: 3 additions & 3 deletions chapter7/car_sliding_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
def in_range(number, test, thresh=0.2):
return abs(number - test) < thresh

test_image = "/home/d3athmast3r/dev/python/study/images/cars.jpg"
img_path = "/home/d3athmast3r/dev/python/study/images/test.jpg"
# remote = "http://previews.123rf.com/images/aremac/aremac0903/aremac090300044/4545419-Lonely-car-on-an-empty-parking-lot-Stock-Photo.jpg"
test_image = "/home/d3athmast3r/dev/python/pycv/images/cars.jpg"
img_path = "/home/d3athmast3r/dev/python/pycv/images/test.jpg"

urllib.urlretrieve(test_image, img_path)

svm, extractor = car_detector()
Expand Down
11 changes: 7 additions & 4 deletions chapter8/surveillance_demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def update(self, frame):
cv2.LINE_AA)

def main():
camera = cv2.VideoCapture(path.join(path.dirname(__file__), "traffic.flv"))
# camera = cv2.VideoCapture(path.join(path.dirname(__file__), "768x576.avi"))
# camera = cv2.VideoCapture(path.join(path.dirname(__file__), "traffic.flv"))
camera = cv2.VideoCapture(path.join(path.dirname(__file__), "768x576.avi"))
# camera = cv2.VideoCapture(path.join(path.dirname(__file__), "..", "movie.mpg"))
# camera = cv2.VideoCapture(0)
history = 20
Expand All @@ -115,8 +115,8 @@ def main():
pedestrians = {}
firstFrame = True
frames = 0


fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while True:
print " -------------------- FRAME %d --------------------" % frames
grabbed, frane = camera.read()
Expand Down Expand Up @@ -156,8 +156,11 @@ def main():
frames += 1

cv2.imshow("surveillance", frame)
out.write(frame)
if cv2.waitKey(110) & 0xff == 27:
break
out.release()
camera.release()

if __name__ == "__main__":
main()
Binary file added images/bathory_vinyls.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0125c91

Please sign in to comment.