import cv2
trackers = [cv2.TrackerBoosting_create,
cv2.TrackerMIL_create,
cv2.TrackerKCF_create,
cv2.TrackerTLD_create,
cv2.TrackerMedianFlow_create,
cv2.TrackerGOTURN_create,
cv2.TrackerCSRT_create,
cv2.TrackerMOSSE_create]
trackerIdx = 0
tracker = None
isFirst = True
video_src = 0
video_src = "./soccer.mp4"
cap = cv2.VideoCapture(video_src)
fps = cap.get(cv2.CAP_PROP_FPS)
delay = int(1000/fps)
win_name = 'Tracking APIs'
while cap.isOpened():
ret, frame = cap.read()
# (+추가) 동영상 크기 조정을 위한 코드
frame=cv2.resize(frame, dsize=None, fx=0.5, fy=0.5)
if not ret:
print('Cannot read video file')
break
img_draw = frame.copy()
if tracker is None:
cv2.putText(img_draw, "Press the Space to set ROI!!", \
(100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2,cv2.LINE_AA)
else:
ok, bbox = tracker.update(frame)
(x,y,w,h) = bbox
if ok: # 추적 성공
cv2.rectangle(img_draw, (int(x), int(y)), (int(x + w), int(y + h)), \
(0,255,0), 2, 1)
else : # 추적 실패
cv2.putText(img_draw, "Tracking fail.", (100,80), \
cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2,cv2.LINE_AA)
trackerName = tracker.__class__.__name__
cv2.putText(img_draw, str(trackerIdx) + ":"+trackerName , (100,20), \
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0,255,0),2,cv2.LINE_AA)
cv2.imshow(win_name, img_draw)
key = cv2.waitKey(delay) & 0xff
if key == ord(' ') or (video_src != 0 and isFirst):
isFirst = False
roi = cv2.selectROI(win_name, frame, False)
if roi[2] and roi[3]:
tracker = trackers[trackerIdx]()
isInit = tracker.init(frame, roi)
elif key in range(48, 56):
trackerIdx = key-48
if bbox is not None:
tracker = trackers[trackerIdx]()
isInit = tracker.init(frame, bbox)
elif key == 27 :
break
else:
print( "Could not open video")
cap.release()
cv2.destroyAllWindows()
시간이 지남에 따라 움직이는 물체를 찾는 과정
실시간 컴퓨터 비전을 목적으로 한 프로그래밍 라이브러리
기계의 시각에 해당하는 부분을 연구하는 컴퓨터 과학의 최신 연구 분야 중 하나
import cv2
trackers = [cv2.TrackerBoosting_create,
cv2.TrackerMIL_create,
cv2.TrackerKCF_create,
cv2.TrackerTLD_create,
cv2.TrackerMedianFlow_create,
cv2.TrackerGOTURN_create,
cv2.TrackerCSRT_create,
cv2.TrackerMOSSE_create]
cv.TrackerBoosing_create()
AdaBoost 알고리즘 기반
참고자료
cv2.TrackerMIL_create()
MIL(Multiple Instance Learning) 알고리즘 기반
참고자료
cv2.TrackerKCF_create()
KCF(Kernelized Correlation Filters) 알고리즘 기반
cv2.TrackerTLD_create()
TLD(Tracking, Learning and Detection) 알고리즘 기반
참고자료
cv2.TrackerMedianFlow_create()
객체의 전방향/역방향을 추적해서 불일치성을 측정
cv2.TrackerGOTURN_create()
CNN(Convolutional Neural Networks) 기반
참고자료
cv2.TrackerCSRT_create()
CSRT(Channel and Spatial Reliability)
cv2.TrackerMOSSE_create()
내부적으로 그레이 스케일 사용
video_src = 0 # 첫 번째로 연결된 캠과 연결됨
video_src = "./soccer.mp4" # 동영상과 연결됨
cap = cv2.VideoCapture(video_src)
fps = cap.get(cv2.CAP_PROP_FPS)
delay = int(1000/fps)
ret, frame = cap.read()
img_draw = frame.copy()
if tracker is None:
cv2.putText(img_draw, "Press the Space to set ROI!!", \
(100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2,cv2.LINE_AA)
else:
ok, bbox = tracker.update(frame)
(x,y,w,h) = bbox
if ok: # 추적 성공
cv2.rectangle(img_draw, (int(x), int(y)), (int(x + w), int(y + h)),
\(0,255,0), 2, 1)
else : # 추적 실패
cv2.putText(img_draw, "Tracking fail.", (100,80), \
cv2.FONT_HERSHEY_SIMPLEX, 0.75 (0,0,255),2,cv2.LINE_AA)
ok, bbox = tracker.update(frame)
cv2.rectangle(img_draw, (int(x), int(y)), (int(x + w), int(y + h)),
\(0,255,0), 2, 1)
trackerName = tracker.__class__.__name__
cv2.putText(img_draw, str(trackerIdx) + ":"+trackerName , (100,20), \
cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0,255,0),2,cv2.LINE_AA)
cv2.imshow(win_name, img_draw)
key = cv2.waitKey(delay) & 0xff
if key == ord(' ') or (video_src != 0 and isFirst):
isFirst = False
roi = cv2.selectROI(win_name, frame, False)
if roi[2] and roi[3]:
tracker = trackers[trackerIdx]()
isInit = tracker.init(frame, roi)
roi = cv2.selectROI(win_name, frame, False)
if roi[2] and roi[3]:
tracker = trackers[trackerIdx]()
isInit = tracker.init(frame, roi)
isInit = tracker.init(frame, roi)
elif key in range(48, 56):
trackerIdx = key-48
if bbox is not None:
tracker = trackers[trackerIdx]()
isInit = tracker.init(frame, bbox)
trackerIdx = key-48
tracker = trackers[trackerIdx]()
elif key == 27 :
break
cap.release()
cv2.destroyAllWindows()