Object Tracking

๋ฐ•๊ด‘์šฑยท2023๋…„ 1์›” 28์ผ
0

Computer Vision

๋ชฉ๋ก ๋ณด๊ธฐ
6/8

๐Ÿ“• Optional Flow

โœ ๋ฃจ์นด์Šค ์นด๋‚˜๋ฐ

import numpy as np
import cv2

corner_track_params = dict(maxCorners = 10, qualityLevel = 0.3, minDistance=7, blockSize=7)

lk_params = dict(winSize = (200,200), maxLevel = 2, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

cap = cv2.VideoCapture(0)

ret, prev_frame = cap.read()

prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)

# ์ถ”์ ํ•˜๊ณ  ์‹ถ์€ ์ 
prevPts = cv2.goodFeaturesToTrack(prev_gray, mask=None, **corner_track_params)

# mask๋Š” ์‹ค์ œ ์ ์„ ํ‘œ์‹œํ•˜๊ณ  ์„ ์„ ๊ทธ๋ฆด ์˜ˆ์ • -> ์ ์„ ์ถ”์ ํ•œ๋‹ค๊ธฐ ๋ณด๋‹ค ์„ ์„ ๊ทธ๋ฆฌ๋Š” ์—ญํ• 
mask = np.zeros_like(prev_frame)

while True:
    ret, frame = cap.read()
    
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    nextPts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray, frame_gray, prevPts, None, **lk_params) # ์ด ํ•จ์ˆ˜๊ฐ€ ๋ฃจ์นด์Šค ์นด๋‚˜๋ฐ ํ•จ์ˆ˜(LK)
    
    good_new = nextPts[status==1]
    good_prev = prevPts[status==1]
    
    for i, (new, prev) in enumerate(zip(good_new, good_prev)):
        x_new, y_new = new.ravel()
        x_prev, y_prev = prev.ravel()
        
        mask = cv2.line(mask, (x_new, y_new), (x_prev, y_prev), (0,255,0), 3)
        
        frame = cv2.circle(frame, (x_new, y_new), 8, (0,0,255), -1)
        
    img = cv2.add(frame, mask)
    cv2.imshow('tracking', img)
    
    k = cv2.waitKey(30) & 0xFF
    if k == 27:
        break
        
    prev_gray = frame_gray.copy()
    prevPts = good_new.reshape(-1,1,2)
    
cv2.destroyAllWindows()
cap.release()

โœ ๊ตฐ๋‚˜๋ฅด ํŒŒ๋„ˆ๋ฐฑ

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

ret, frame1 = cap.read()

prvsImg = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

hsv_mask = np.zeros_like(frame1)
hsv_mask[:,:, 1] = 255 # ์ฑ„๋„๋ฅผ ์ตœ๋Œ€์น˜์ธ 255๋กœ ์„ค์ •

while True:
    ret, frame2 = cap.read()
    
    nextImg = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
    
    flow = cv2.calcOpticalFlowFarneback(prvsImg, nextImg, None, 0.5, 3, 15, 3, 5, 1.2, 0) # ์ด๊ฒŒ ๋””ํดํŠธ ๊ฐ’
    
    mag, ang = cv2.cartToPolar(flow[:,:,0], flow[:,:,1], angleInDegrees=True)
    
    hsv_mask[:,:,0] = ang/2 # 360 ~ 180๋„ ์‚ฌ์ด๋กœ ๊ณ ์ •
    
    hsv_mask[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) # 0๋ถ€ํ„ฐ 255๊นŒ์ง€ ์ •๊ทœํ™”
    
    bgr = cv2.cvtColor(hsv_mask, cv2.COLOR_HSV2BGR)
    cv2.imshow('frame', bgr)
    
    k = cv2.waitKey(10) & 0xFF
    
    if k == 27:
        break
        
    prvsImg = nextImg
    
cap.release()
cv2.destroyAllWindows()

๐Ÿ“— MeanShift & CamShift

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

ret, frame = cap.read()

# ์ดˆ๊ธฐ ์ถ”์  ์œˆ๋„์šฐ ์„ค์ •
face_cascade = cv2.CascadeClassifier('../Computer-Vision-with-Python/DATA/haarcascades/haarcascade_frontalface_default.xml')
face_rects = face_cascade.detectMultiScale(frame)


# ์–ผ๊ตด ํ•˜๋‚˜๋งŒ ์ถ”์ 
(face_x, face_y, w, h) = tuple(face_rects[0])
track_window = (face_x, face_y, w, h)

roi = frame[face_y:face_y+h, face_x:face_x+w] # ์–ผ๊ตด๋ถ€๋ถ„ ์„ ํƒ

hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0,180])

cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

while True:
    ret, frame = cap.read()
    
    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
        
        ############# MeanShift ##############
        # MeanShift๋Š” Box์˜ ํฌ๊ธฐ๋Š” ์•ˆ๋ณ€ํ•จ
        '''
        ret, track_window = cv2.meanShift(dst, track_window, term_crit)
        
        x, y, w, h = track_window
        img2 = cv2.rectangle(frame, (x, y), (x+w, y+h), (0,0,255), 5)
        '''
        ######################################
        
        ############# CamShift ###############
        # CamShift๋Š” ํฌ๊ธฐ๋„ ๋ณ€ํ•จ
        ret, track_window = cv2.CamShift(dst, track_window, term_crit)
        
        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        img2 = cv2.polylines(frame, [pts], True, (0,0,255), 5)
        
        ######################################
        
        cv2.imshow('img', img2)
        
        k = cv2.waitKey(1) & 0xFF
        
        if k == 27:
            break
            
    else:
        break
        
cv2.destroyAllWindows()
cap.release()

๐Ÿ“˜ Tracking APIs

import cv2

def ask_for_tracker():
    print("Welcome! What Tracker API would you like to use?")
    print("Enter 0 for BOOSTRING :")
    print("Enter 1 for MIL :")    
    print("Enter 2 for KCF :")    
    print("Enter 3 for TLD :")    
    print("Enter 4 for MEDIANFLOW :")
    choice = input("Please select your tracker : ")
    
    if choice == '0':
        tracker = cv2.TrackerBoosting_create()
    if choice == '1':
        tracker = cv2.TrackerMIL_create()
    if choice == '2':
        tracker = cv2.TrackerKCF_create()
    if choice == '3':
        tracker = cv2.TrackerTLD_create()
    if choice == '4':
        tracker = cv2.TrackerMedianFlow_create()
        
    
    return tracker
    
tracker = ask_for_tracker()
# <TrackerMedianFlow 000001A0T01523DI9> ๋ญ ์ด๋Ÿฐ ํ˜•์‹์—์„œ ์ด๋ฆ„๋งŒ ๊ฐ€์ ธ์˜ค๊ฒ ๋‹ค๋Š” ์˜๋ฏธ
tracker_name = str(tracker).split()[0][1:]

cap = cv2.VideoCapture(0)

ret, frame = cap.read()

# ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๋ฅผ ์ง์ ‘ ๊ทธ๋ฆด์ˆ˜ ์žˆ์–ด ๊ด€์‹ฌ์˜์—ญ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
roi = cv2.selectROI(frame, False)

# ํŠธ๋ž˜์ปค๋ฅผ ์ดˆ๊ธฐํ™”์‹œํ‚จ๋‹ค(init)
ret = tracker.init(frame, roi)

while True:
    ret, frame = cap.read()
    
    # ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ค€๋‹ค. 
    success, roi = tracker.update(frame)
    
    # roi๋Š” ์ง์‚ฌ๊ฐํ˜• ๊ผญ์ง“์ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” 4๊ฐœ์˜ ๋ถ€๋™ "์†Œ์ˆ˜์ "์„ ๋‚˜ํƒ€๋ƒ„.
    (x, y, w, h) = tuple(map(int, roi))
    
    if success:
        p1 = (x, y)
        p2 = (x+w, y+h)
        cv2.rectangle(frame, p1, p2, (0,255,0), 3)
    else:
        cv2.putText(frame, 'Failure to Detect Tracking!!', (100, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 3)
        
    cv2.putText(frame, tracker_name, (20, 400), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 3)
    
    cv2.imshow(tracker_name, frame)
    
    k = cv2.waitKey(1) & 0xFF
    
    if k == 27:
        break
        
cap.release()
cv2.destroyAllWindows()

0๊ฐœ์˜ ๋Œ“๊ธ€