THIS PROJECT MUST WITHOUT EXCEPTION USE OPENCV & PYTHON...I am trying to track a player and object in the same frame/video/camera...ultimately camera only ... using KCF for now to track the player, but will not track ball
import cv2
import sys
import imutils
if __name__ == '__main__' :
tracker_types = ['KSF ']
tracker_type = tracker_types[0]
tracker = cv2.TrackerKCF_create()
# Read video
video = [login to view URL]("[login to view URL]")
# Exit if video not opened.
if not [login to view URL]():
print ("Could not open video")
[login to view URL]()
# Read first frame.
ok, frame = [login to view URL]()
frame = [login to view URL](frame, width= 720)
#frame = [login to view URL](frame, height= 720)
if not ok:
print ('Cannot read video file')
[login to view URL]()
# Define an initial bounding box
bbox = (320, 230, 230, 320)
# Uncomment the line below to select a different bounding box
#bbox = [login to view URL](frame, False)
# Initialize tracker with first frame and bounding box
ok = [login to view URL](frame, bbox)
while True:
# Read a new frame
ok, frame = [login to view URL]()
if not ok:
break
# Start timer
timer = [login to view URL]()
# Update tracker
ok, bbox = [login to view URL](frame)
# Calculate Frames per second (FPS)
fps = [login to view URL]() / ([login to view URL]() - timer);
# Draw bounding box
if ok:
# Tracking success
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
[login to view URL](frame, p1, p2, (255,0,0), 2, 1)
else :
# Tracking failure
[login to view URL](frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
# Display tracker type on frame
[login to view URL](frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
# Display FPS on frame
[login to view URL](frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
# Display result
[login to view URL]("Tracking", frame)
colorLower = (91,45,58)
colorUpper = (90,33,93)
# Start with LED off
# loop over the frames from the video stream
while True:
# grab the next frame from the video stream, Invert 180o, resize the
# frame, and convert it to the HSV color space
frame = [login to view URL]()
frame = [login to view URL](frame, width=500)
frame = [login to view URL](frame, height=700)
hsv = [login to view URL](frame, cv2.COLOR_BGR2HSV)
# construct a mask for the object color, then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = [login to view URL](hsv, colorLower, colorUpper)
mask = [login to view URL](mask, None, iterations=2)
mask = [login to view URL](mask, None, iterations=2)
# find contours in the mask and initialize the current
# (x, y) center of the object
cnts = [login to view URL]([login to view URL](), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[-2]
#cnts = cnts[0] if imutils.is_cv2() else cnts[1]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=[login to view URL])
((x, y), radius) = [login to view URL](c)
M = [login to view URL](c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# only proceed if the radius meets a minimum size
if radius > 10:
# draw the circle and centroid on the frame,
# then update the list of tracked points
[login to view URL](frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
[login to view URL](frame, center, 5, (0, 0, 255), -1)
# show the frame to our screen
[login to view URL]("Frame", frame)
print("\n [INFO] Exiting [embed=file 1415770][embed=file 1415771]Program and cleanup stuff \n")
[login to view URL]()
[login to view URL]()