选择静态投资回报率

时间:2018-11-25 19:40:36

标签: python opencv

我正在关注本教程 https://www.pyimagesearch.com/2018/07/30/opencv-object-tracking/ 然后通过将视频与S暂停,然后在要跟踪的对象上创建一个窗口来选择ROI

我需要帮助的是::

  1. 我想选择对象而不暂停视频或选择窗口,我的意思是选择窗口是静态的,我只需要单击鼠标左键即可进行跟踪

  2. 每次我右键单击另一个对象时,它都会删除前一个对象并跟踪新对象

  3. 通过鼠标滚轮控制选择窗口的大小| 这是我现在正在使用的代码

# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import argparse
import imutils
import time
import cv2
import serial

arduino=serial.Serial('com51', 115200)
# Serial write section

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,
	help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf",
	help="OpenCV object tracker type")
args = vars(ap.parse_args())
# extract the OpenCV version info
(major, minor) = cv2.__version__.split(".")[:2]
 
# if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
# function to create our object tracker
if int(major) == 3 and int(minor) < 3:
	tracker = cv2.Tracker_create(args["tracker"].upper())
 
# otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the
# approrpiate object tracker constructor:
else:
	# initialize a dictionary that maps strings to their corresponding
	# OpenCV object tracker implementations
	OPENCV_OBJECT_TRACKERS = {
		"csrt": cv2.TrackerCSRT_create,
		"kcf": cv2.TrackerKCF_create,
		"boosting": cv2.TrackerBoosting_create,
		"mil": cv2.TrackerMIL_create,
		"tld": cv2.TrackerTLD_create,
		"medianflow": cv2.TrackerMedianFlow_create,
		"mosse": cv2.TrackerMOSSE_create
	}
 
	# grab the appropriate object tracker using our dictionary of
	# OpenCV object tracker objects
	tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
 
# initialize the bounding box coordinates of the object we are going
# to track
initBB = None
# if a video path was not supplied, grab the reference to the web cam
if not args.get("video", False):
	print("[INFO] starting video stream...")
	vs = VideoStream(src=0).start()
	time.sleep(1.0)
 
# otherwise, grab a reference to the video file
else:
	vs = cv2.VideoCapture(args["video"])
 
 #if vs.isOpened(): 
    # get vs property 
#width2 = vs.get(3)
#height2 = vs.get(4)


# initialize the FPS throughput estimator
fps = None
# loop over frames from the video stream
while True:
	# grab the current frame, then handle if we are using a
	# VideoStream or VideoCapture object
	frame = vs.read()
	frame = frame[1] if args.get("video", False) else frame
	
	# check to see if we have reached the end of the stream
	if frame is None:
		break
 
	# resize the frame (so we can process it faster) and grab the
	# frame dimensions
	#frame = imutils.resize(frame, width=1280)
	(H, W) = frame.shape[:2]
	# check to see if we are currently tracking an object
	if initBB is not None:
		# grab the new bounding box coordinates of the object
		(success, box) = tracker.update(frame)
 
		# check to see if the tracking was a success
		if success:
			(x, y, w, h) = [int(v) for v in box]
			cv2.rectangle(frame, (x, y), (x + w, y + h),
				(0, 255, 0), 2)                                
		# update the FPS counter
		fps.update()
		fps.stop()
		#fixing the x,y tracker box center
		x2=int(x+w/2)
		y2=int(y+h/2)
		#the offsets for the x,y tracking from the center
		sox = str(x2 - (W/2))
		soy = str((H/2) - y2)
		#sending the offsets to arduino
		arduino.write('x'.encode())
		arduino.write(sox.encode())
		#print ("offset X value sent: ")
		#print (sox)
		#time.sleep(0.01)
		arduino.write('y'.encode())
		arduino.write(soy.encode())
		#print ("offset Y value sent : ")
		#print (soy)
		#time.sleep(0.01)
		cv2.line(frame, (int(W/2), int(H/2)), (x2, y2), (0, 255, 0), 1)
		cv2.line(frame, (int(W), int(H/2)), (0, int(H/2)), (0, 0, 0), 2)
		cv2.line(frame, (int(W/2), int(H)), (int(W/2), 0), (0, 0, 0), 2)
		#cv2.line(frame, (320, 240), (x2, y2), (0, 255, 0), 1)
		
		# initialize the set of information we'll be displaying on
		# the frame
		info = [
		      
			#("Tracker", args["tracker"]),
			#("X = ",str(x)),
			#("Y = ",str(y)),
			("offset X = ",sox), 
			("offset y = ",soy),
			#("width = ",W),
			#("height = ",H),
			("FPS", "{:.2f}".format(fps.fps())),
			
		]
 
		# loop over the info tuples and draw them on our frame
		for (i, (k, v)) in enumerate(info):	
			text = "{}: {}".format(k, v)
			cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
				cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
				
				
	         
         

# show the output frame
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
	
 
	# if the 's' key is selected, we are going to "select" a bounding
	# box to track
	
	
	    
	if key == ord("s"):
		# select the bounding box of the object we want to track (make
		# sure you press ENTER or SPACE after selecting the ROI)
		initBB = cv2.selectROI("Frame", frame, fromCenter=False,
			showCrosshair=True)
			
 
		# start OpenCV object tracker using the supplied bounding box
		# coordinates, then start the FPS throughput estimator as well
		tracker.init(frame, initBB)
		fps = FPS().start()
		
	# if the `q` key was pressed, break from the loop
	elif key == ord("q"):
		break
# if we are using a webcam, release the pointer
if not args.get("video", False):
	vs.stop()
 
# otherwise, release the file pointer
else:
	vs.release()
 

# close all windows
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

您将需要对代码进行一些更改。首先,在代码顶部添加此回调函数,以告知openCV单击该怎么办。

mouse_click = False
tracker_location = (0,0)
def click_track(event, x, y, flags, param):
    global mouse_click, tracker_location
    # if the left mouse button was clicked, change flag
    # Click is sensed as a button up
    if event == cv2.EVENT_LBUTTONUP:
        mouse_click = True
        tracker_location = (x,y)

然后在启动while循环之前,先添加它以启动具有鼠标单击功能的显示窗口。

# Create a window for click detection 
cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", click_track)

最后,在曾经选择ROI的位置,将其替换为仅在鼠标单击标记为true时起作用的静态ROI。

if mouse_click == True:
    # select the bounding box of the object we want to track (make
    # sure you press ENTER or SPACE after selecting the ROI)
    h , w, ch = frame.shape

    # initial ROI size
    ROI_size_x = 0.1 * w
    ROI_size_y = 0.1 * h

    # Adjust the size when object is near image border
    if( tracker_location[0] + ROI_size_x >= w):
        ROI_size_x = w - tracker_location[0]
    if( tracker_location[1] + ROI_size_y >= h):
        ROI_size_y = h - tracker_location[1]
    if( tracker_location[0] - ROI_size_x < 0):
        tracker_location[0] = 0
    if( tracker_location[1] - ROI_size_y < 0):
        tracker_location[1] = 0

    initBB = ( tracker_location[0] - ROI_size_x, tracker_location[1] - ROI_size_y, ROI_size_x * 2,ROI_size_y * 2)

您可以根据需要更改静态ROI的大小。目前将其设置为中心图像尺寸的20%。

这是完整的代码

# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import argparse
import imutils
import time
import cv2
import serial

mouse_click = False
tracker_location = (0,0)
def click_track(event, x, y, flags, param):
    global mouse_click, tracker_location

    tracker_location = (x,y)
    # if the left mouse button was clicked, change flag
    # Click is sensed as a button up
    if event == cv2.EVENT_LBUTTONUP:
        mouse_click = True



arduino=serial.Serial('com51', 115200)
# Serial write section



# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,
    help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf",
    help="OpenCV object tracker type")
args = vars(ap.parse_args())
# extract the OpenCV version info
(major, minor) = cv2.__version__.split(".")[:2]

# if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
# function to create our object tracker
if int(major) == 3 and int(minor) < 3:
    tracker = cv2.Tracker_create(args["tracker"].upper())

# otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the
# approrpiate object tracker constructor:
else:
    # initialize a dictionary that maps strings to their corresponding
    # OpenCV object tracker implementations
    OPENCV_OBJECT_TRACKERS = {
        "csrt": cv2.TrackerCSRT_create,
        "kcf": cv2.TrackerKCF_create,
        "boosting": cv2.TrackerBoosting_create,
        "mil": cv2.TrackerMIL_create,
        "tld": cv2.TrackerTLD_create,
        "medianflow": cv2.TrackerMedianFlow_create,
        "mosse": cv2.TrackerMOSSE_create
    }

    # grab the appropriate object tracker using our dictionary of
    # OpenCV object tracker objects
    tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()


# Create a window for click detection 
cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", click_track)


# initialize the bounding box coordinates of the object we are going
# to track
initBB = None
# if a video path was not supplied, grab the reference to the web cam
if not args.get("video", False):
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    time.sleep(1.0)

# otherwise, grab a reference to the video file
else:
    vs = cv2.VideoCapture(args["video"])

 #if vs.isOpened(): 
    # get vs property 
#width2 = vs.get(3)
#height2 = vs.get(4)


# initialize the FPS throughput estimator
fps = None
# loop over frames from the video stream
while True:
    # grab the current frame, then handle if we are using a
    # VideoStream or VideoCapture object
    frame = vs.read()
    frame = frame[1] if args.get("video", False) else frame

    # check to see if we have reached the end of the stream
    if frame is None:
        break

    # resize the frame (so we can process it faster) and grab the
    # frame dimensions
    #frame = imutils.resize(frame, width=1280)
    (H, W) = frame.shape[:2]
    # check to see if we are currently tracking an object
    if initBB is not None:
        # grab the new bounding box coordinates of the object
        (success, box) = tracker.update(frame)

        # check to see if the tracking was a success
        if success:
            (x, y, w, h) = [int(v) for v in box]
            cv2.rectangle(frame, (x, y), (x + w, y + h),
                (0, 255, 0), 2)                                
        # update the FPS counter
        fps.update()
        fps.stop()
        #fixing the x,y tracker box center
        x2=int(x+w/2)
        y2=int(y+h/2)
        #the offsets for the x,y tracking from the center
        sox = str(x2 - (W/2))
        soy = str((H/2) - y2)
        #sending the offsets to arduino
        arduino.write('x'.encode())
        arduino.write(sox.encode())
        #print ("offset X value sent: ")
        #print (sox)
        #time.sleep(0.01)
        arduino.write('y'.encode())
        arduino.write(soy.encode())
        #print ("offset Y value sent : ")
        #print (soy)
        #time.sleep(0.01)
        cv2.line(frame, (int(W/2), int(H/2)), (x2, y2), (0, 255, 0), 1)
        cv2.line(frame, (int(W), int(H/2)), (0, int(H/2)), (0, 0, 0), 2)
        cv2.line(frame, (int(W/2), int(H)), (int(W/2), 0), (0, 0, 0), 2)
        #cv2.line(frame, (320, 240), (x2, y2), (0, 255, 0), 1)

        # initialize the set of information we'll be displaying on
        # the frame
        info = [

            #("Tracker", args["tracker"]),
            #("X = ",str(x)),
            #("Y = ",str(y)),
            ("offset X = ",sox), 
            ("offset y = ",soy),
            #("width = ",W),
            #("height = ",H),
            ("FPS", "{:.2f}".format(fps.fps())),

        ]

        # loop over the info tuples and draw them on our frame
        for (i, (k, v)) in enumerate(info): 
            text = "{}: {}".format(k, v)
            cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)


    # select the bounding box of the object we want to track (make
    # sure you press ENTER or SPACE after selecting the ROI)
    h , w, ch = frame.shape

    # initial ROI size
    ROI_size_x = 0.1 * w
    ROI_size_y = 0.1 * h

    # Adjust the size when object is near image border
    if( tracker_location[0] + ROI_size_x >= w):
        ROI_size_x = w - tracker_location[0]
    if( tracker_location[1] + ROI_size_y >= h):
        ROI_size_y = h - tracker_location[1]
    if( tracker_location[0] - ROI_size_x < 0):
        tracker_location[0] = 0
    if( tracker_location[1] - ROI_size_y < 0):
        tracker_location[1] = 0

    initBB = ( tracker_location[0] - ROI_size_x, tracker_location[1] - ROI_size_y, ROI_size_x * 2,ROI_size_y * 2)
    #initBB = cv2.selectROI("Frame", frame, fromCenter=False,
    #   showCrosshair=True)

    if mouse_click == True:
        # start OpenCV object tracker using the supplied bounding box
        # coordinates, then start the FPS throughput estimator as well
        tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
        tracker.init(frame, initBB)
        fps = FPS().start()
        mouse_click = False

    #drawing the initial box
    cv2.rectangle(frame,int(initBB[0],initBB[1]),int(initBB[2],initBB[3]),(0,255,255),2)

    # show the output frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF



    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break
# if we are using a webcam, release the pointer
if not args.get("video", False):
    vs.stop()

# otherwise, release the file pointer
else:
    vs.release()


# close all windows
cv2.destroyAllWindows()