複数人の人物追跡をしたいと考えております。
以下はopencvを使い、動画の最初のフレームで追跡する対象を決め、その人物が動画からいなくなると終了するプログラムです。
python
1import cv2 2import sys 3 4(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') 5 6if __name__ == '__main__' : 7 8 # Set up tracker. 9 # Instead of MIL, you can also use 10 11 tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN'] 12 tracker_type = tracker_types[2] 13 14 if int(minor_ver) < 3: 15 tracker = cv2.Tracker_create(tracker_type) 16 else: 17 if tracker_type == 'BOOSTING': 18 tracker = cv2.TrackerBoosting_create() 19 if tracker_type == 'MIL': 20 tracker = cv2.TrackerMIL_create() 21 if tracker_type == 'KCF': 22 tracker = cv2.TrackerKCF_create() 23 if tracker_type == 'TLD': 24 tracker = cv2.TrackerTLD_create() 25 if tracker_type == 'MEDIANFLOW': 26 tracker = cv2.TrackerMedianFlow_create() 27 if tracker_type == 'GOTURN': 28 tracker = cv2.TrackerGOTURN_create() 29 30 # Read video 31 video = cv2.VideoCapture("CU01.avi") 32 33 # Exit if video not opened. 34 if not video.isOpened(): 35 print ("Could not open video") 36 sys.exit() 37 38 # Read first frame. 39 ok, frame = video.read() 40 if not ok: 41 print ('Cannot read video file') 42 sys.exit() 43 44 # Define an initial bounding box 45 bbox = (287, 23, 86, 320) 46 47 # Uncomment the line below to select a different bounding box 48 bbox = cv2.selectROI(frame, False) 49 50 # Initialize tracker with first frame and bounding box 51 ok = tracker.init(frame, bbox) 52 53 while True: 54 # Read a new frame 55 ok, frame = video.read() 56 if not ok: 57 break 58 59 # Start timer 60 timer = cv2.getTickCount() 61 62 # Update tracker 63 ok, bbox = tracker.update(frame) 64 65 # Calculate Frames per second (FPS) 66 fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer); 67 68 # Draw bounding box 69 if ok: 70 # Tracking success 71 p1 = (int(bbox[0]), int(bbox[1])) 72 p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) 73 cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1) 74 else : 75 # Tracking failure 76 cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2) 77 78 # Display tracker type on frame 79 cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2); 80 81 # Display FPS on frame 82 cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2); 83 84 # Display result 85 cv2.imshow("Tracking", frame) 86 87 # Exit if ESC pressed 88 k = cv2.waitKey(1) & 0xff 89 if k == 27 : break 90
このように最初に追跡する物体を決めるのではなく、動画内に映る人を自動で検出して追跡するにはどのようにすればよいでしょうか?
回答2件
あなたの回答
tips
プレビュー