import sys import cv2 import matplotlib import matplotlib.pyplot as plt import matplotlib.patches as patches print("Python Version: {}".format(sys.version.split('|')[0])) print("OpenCV Version: {}".format(cv2.__version__)) print("Matplotlib Version: {}".format(matplotlib.__version__)) VIDEO_PATH = "videos/ZuoYe.mp4" video = cv2.VideoCapture(VIDEO_PATH) if not video.isOpened(): print("Could not load video.") else: f_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) f_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) print("Video loaded!\n") print("Frame width: {} px".format(f_width)) print("Frame height: {} px".format(f_height)) # Uncomment the tracker to use for each run # tracker = cv2.TrackerTLD_create() tracker = cv2.TrackerBoosting_create() # tracker = cv2.TrackerMIL_create() # Read the first video frame ok, frame = video.read() # Define initial bounding box bbox = (824, 223, 91, 68) # Found using our bounding_box_finder.py tool #bbox = (x, y, X长度, Y长度) # Draw bounding box p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1) # cv2.circle(frame,824,10,(0,255,0)) # plt.imshow(frame) # plt.show() # # Save annotated frames to a list # frames = [frame] # Intialise tracker tracker.init(frame, bbox) while ok: # Read next frame ok, frame = video.read() # Start timer #timer = cv2.getTickCount() # Update tracker target_located, bbox = tracker.update(frame) # Calculate frames per second #fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) if target_located: # Draw bounding box p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) cv2.rectangle(frame, p1, p2, (255, 0, 0),1, 1) else: # Draw failure text cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # Display fps on frame # cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2); cv2.imshow('1',frame) cv2.waitKey(1) # frames.append(frame) # OUTPUT_VIDEO_PATH = "out/BB8_Boosting.mp4" # video_out = cv2.VideoWriter(OUTPUT_VIDEO_PATH, # cv2.VideoWriter_fourcc('h', '2', '6', '4'), # 30, # (f_width, f_height)) # # for frame in frames: # video_out.write(frame) # plt.imshow(frame) # plt.show() # # video_out.release()
python opencv 学习————tracker目标追踪(初试)
最新推荐文章于 2024-04-17 09:51:09 发布