Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions get_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ def run(im, multi=False):

def callback(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
if multi == False and len(pts_2) == 1:
print "WARN: Cannot select another object in SINGLE OBJECT TRACKING MODE."
print "Delete the previously selected object using key `d` to mark a new location."
return
if multi == False and len(pts_2) == 1:
print("WARN: Cannot select another object in SINGLE OBJECT TRACKING MODE.")
print("Delete the previously selected object using key `d` to mark a new location.")
return
run.mouse_down = True
pts_1.append((x, y))
elif event == cv2.EVENT_LBUTTONUP and run.mouse_down == True:
run.mouse_down = False
pts_2.append((x, y))
print "Object selected at [{}, {}]".format(pts_1[-1], pts_2[-1])
print("Object selected at [{}, {}]".format(pts_1[-1], pts_2[-1]))
elif event == cv2.EVENT_MOUSEMOVE and run.mouse_down == True:
im_draw = im.copy()
cv2.rectangle(im_draw, pts_1[-1], (x, y), (255,255,255), 3)
cv2.imshow(window_name, im_draw)

print "Press and release mouse around the object to be tracked. \n You can also select multiple objects."
print("Press and release mouse around the object to be tracked. \n You can also select multiple objects.")
cv2.setMouseCallback(window_name, callback)

print "Press key `p` to continue with the selected points."
print "Press key `d` to discard the last object selected."
print "Press key `q` to quit the program."
print("Press key `p` to continue with the selected points.")
print("Press key `d` to discard the last object selected.")
print("Press key `q` to quit the program.")

while True:
# Draw the rectangular boxes on the image
Expand All @@ -49,7 +49,7 @@ def callback(event, x, y, flags, param):
# Display the cropped images
cv2.namedWindow(window_name_2, cv2.WINDOW_NORMAL)
cv2.imshow(window_name_2, im_disp)
key = cv2.waitKey(30)
key = (0xFF & cv2.waitKey(30))
if key == ord('p'):
# Press key `s` to return the selected points
cv2.destroyAllWindows()
Expand All @@ -58,17 +58,17 @@ def callback(event, x, y, flags, param):
return corrected_point
elif key == ord('q'):
# Press key `q` to quit the program
print "Quitting without saving."
print("Quitting without saving.")
exit()
elif key == ord('d'):
# Press ket `d` to delete the last rectangular region
if run.mouse_down == False and pts_1:
print "Object deleted at [{}, {}]".format(pts_1[-1], pts_2[-1])
print("Object deleted at [{}, {}]".format(pts_1[-1], pts_2[-1]))
pts_1.pop()
pts_2.pop()
im_disp = im.copy()
else:
print "No object to delete."
print("No object to delete.")
cv2.destroyAllWindows()
point= [(tl + br) for tl, br in zip(pts_1, pts_2)]
corrected_point=check_point(point)
Expand Down Expand Up @@ -105,7 +105,7 @@ def check_point(points):
try:
im = cv2.imread(args["imagepath"])
except:
print "Cannot read image and exiting."
print("Cannot read image and exiting.")
exit()
points = run(im)
print "Rectangular Regions Selected are -> ", points
print("Rectangular Regions Selected are -> ", points)
26 changes: 13 additions & 13 deletions object-tracker-multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def run(source=0, dispLoc=False):

# If Camera Device is not opened, exit the program
if not cam.isOpened():
print "Video device or file couldn't be opened"
print("Video device or file couldn't be opened")
exit()

print "Press key `p` to pause the video to start tracking"
print("Press key `p` to pause the video to start tracking")
while True:
# Retrieve an image and Display it.
retval, img = cam.read()
if not retval:
print "Cannot capture frame device"
print("Cannot capture frame device")
exit()
if(cv2.waitKey(10)==ord('p')):
if(0xFF & cv2.waitKey(10))==ord('p'):
break
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.imshow("Image", img)
Expand All @@ -31,42 +31,42 @@ def run(source=0, dispLoc=False):
points = get_points.run(img, multi=True)

if not points:
print "ERROR: No object to be tracked."
print("ERROR: No object to be tracked.")
exit()

cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.imshow("Image", img)

# Initial co-ordinates of the object to be tracked
# Create the tracker object
tracker = [dlib.correlation_tracker() for _ in xrange(len(points))]
tracker = [dlib.correlation_tracker() for _ in range(len(points))]
# Provide the tracker the initial position of the object
[tracker[i].start_track(img, dlib.rectangle(*rect)) for i, rect in enumerate(points)]

while True:
# Read frame from device or file
retval, img = cam.read()
if not retval:
print "Cannot capture frame device | CODE TERMINATION :( "
print("Cannot capture frame device | CODE TERMINATION :( ")
exit()
# Update the tracker
for i in xrange(len(tracker)):
for i in range(len(tracker)):
tracker[i].update(img)
# Get the position of th object, draw a
# bounding box around it and display it.
rect = tracker[i].get_position()
pt1 = (int(rect.left()), int(rect.top()))
pt2 = (int(rect.right()), int(rect.bottom()))
cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
print "Object {} tracked at [{}, {}] \r".format(i, pt1, pt2),
print("Object {} tracked at [{}, {}] \r".format(i, pt1, pt2), end=' ')
if dispLoc:
loc = (int(rect.left()), int(rect.top()-20))
txt = "Object tracked at [{}, {}]".format(pt1, pt2)
cv2.putText(img, txt, loc , cv2.FONT_HERSHEY_SIMPLEX, .5, (255,255,255), 1)
txt = "Object tracked at [{}, {}]".format(pt1, pt2)
cv2.putText(img, txt, loc , cv2.FONT_HERSHEY_SIMPLEX, .5, (255,255,255), 1)
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.imshow("Image", img)
# Continue until the user presses ESC key
if cv2.waitKey(1) == 27:
if (0xFF & cv2.waitKey(1)) == 27:
break

# Relase the VideoCapture object
Expand All @@ -86,4 +86,4 @@ def run(source=0, dispLoc=False):
source = args["videoFile"]
else:
source = int(args["deviceID"])
run(source, args["dispLoc"])
run(source, args["dispLoc"])
18 changes: 9 additions & 9 deletions object-tracker-single.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def run(source=0, dispLoc=False):

# If Camera Device is not opened, exit the program
if not cam.isOpened():
print "Video device or file couldn't be opened"
print("Video device or file couldn't be opened")
exit()

print "Press key `p` to pause the video to start tracking"
print("Press key `p` to pause the video to start tracking")
while True:
# Retrieve an image and Display it.
retval, img = cam.read()
if not retval:
print "Cannot capture frame device"
print("Cannot capture frame device")
exit()
if(cv2.waitKey(10)==ord('p')):
if(0xFF & cv2.waitKey(10))==ord('p'):
break
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.imshow("Image", img)
Expand All @@ -31,7 +31,7 @@ def run(source=0, dispLoc=False):
points = get_points.run(img)

if not points:
print "ERROR: No object to be tracked."
print("ERROR: No object to be tracked.")
exit()

cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
Expand All @@ -47,7 +47,7 @@ def run(source=0, dispLoc=False):
# Read frame from device or file
retval, img = cam.read()
if not retval:
print "Cannot capture frame device | CODE TERMINATING :("
print("Cannot capture frame device | CODE TERMINATING :(")
exit()
# Update the tracker
tracker.update(img)
Expand All @@ -57,15 +57,15 @@ def run(source=0, dispLoc=False):
pt1 = (int(rect.left()), int(rect.top()))
pt2 = (int(rect.right()), int(rect.bottom()))
cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
print "Object tracked at [{}, {}] \r".format(pt1, pt2),
print("Object tracked at [{}, {}] \r".format(pt1, pt2), end=' ')
if dispLoc:
loc = (int(rect.left()), int(rect.top()-20))
txt = "Object tracked at [{}, {}]".format(pt1, pt2)
cv2.putText(img, txt, loc , cv2.FONT_HERSHEY_SIMPLEX, .5, (255,255,255), 1)
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
cv2.imshow("Image", img)
# Continue until the user presses ESC key
if cv2.waitKey(1) == 27:
if (0xFF & cv2.waitKey(1)) == 27:
break

# Relase the VideoCapture object
Expand All @@ -85,4 +85,4 @@ def run(source=0, dispLoc=False):
source = args["videoFile"]
else:
source = int(args["deviceID"])
run(source, args["dispLoc"])
run(source, args["dispLoc"])