<jupyter_start><jupyter_text>Object Tracking - Object detection vs Object tracking- How does object tracking work?   - The MIL method: https://faculty.ucmerced.edu/mhyang/papers/cvpr09a.pdf  - Multiple instance learning: boosting + multiple instances based on neighbourhood.  - Input frame + ROI --> Trained object tracking model --> ROI bbox.- Advantages: good and robust performance.- Disadvantages: cannot detect when tracking is lost.  Import Module<jupyter_code>import cv2<jupyter_output><empty_output><jupyter_text>Track Objects in a Video File<jupyter_code># create tracker object
tracker = cv2.TrackerMIL_create()

# capture video from prerecorded video
video_frames = cv2.VideoCapture('./notebook.MOV')  # 0: default camera

# read frames
ret, frame = video_frames.read()

# draw ROI from the first captured frame
roi = cv2.selectROI(img=frame, showCrosshair=False)

# initiate tracker
ret = tracker.init(frame, roi)

# get frame size from camera
frame_width = int(video_frames.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_frames.get(cv2.CAP_PROP_FRAME_HEIGHT))

# initialize writer
# fourcc: video codec. DIVX for windows, XVID for linux and macOS
# fps: 30
writer = cv2.VideoWriter('./recorded-obj-tracker.mp4', cv2.VideoWriter_fourcc(*'DIVX'), 30, (frame_width, frame_height))

# loop: grab and display frame
while True:
    # read frames
    ret, frame = video_frames.read()
    
    # update tracker
    success, roi = tracker.update(frame)
    
    # convert float values of roi to int and make them a tuple
    (x, y, w, h) = tuple(map(int, roi))
    
    # if tracking is a success, draw rectangle. else, display text
    if success:
        cv2.rectangle(img=frame, pt1=(x, y), pt2=(x+w, y+h), color=(255, 255, 255), thickness=3)
    else:
        cv2.putText(img=frame, text='Tracking is lost.', org=(100, 200), fontFace=cv2.FONT_HERSHEY_SIMPLEX, thickness=2)
    
    # save video
    writer.write(frame)
    
    # show frame
    cv2.imshow('frame', frame)
    
    # if frame is display for more than 1 ms and ESC key is pressed, close display
    if cv2.waitKey(1) & 0xFF == 27:
        break

video_frames.release()
writer.release()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Track Objects on Live Camera<jupyter_code># create tracker object
tracker = cv2.TrackerMIL_create()

# capture video from default camera
video_frames = cv2.VideoCapture(0)  # 0: default camera

# read frames
ret, frame = video_frames.read()

# draw object from the first captured frame
roi = cv2.selectROI(img=frame, showCrosshair=False)

# initiate tracker
ret = tracker.init(frame, roi)

# get frame size from camera
frame_width = int(video_frames.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_frames.get(cv2.CAP_PROP_FRAME_HEIGHT))

# initialize writer
# fourcc: video codec. DIVX for windows, XVID for linux and macOS
# fps: 30
writer = cv2.VideoWriter('./live-obj-tracker.mp4', cv2.VideoWriter_fourcc(*'DIVX'), 30, (frame_width, frame_height))

# loop: grab and display frame
while True:
    # read frames
    ret, frame = video_frames.read()
    
    # update tracker
    success, roi = tracker.update(frame)
    
    # convert float values of roi to int and make them a tuple
    (x, y, w, h) = tuple(map(int, roi))
    
    # if tracking is a success, draw rectangle. else, display text
    if success:
        cv2.rectangle(img=frame, pt1=(x, y), pt2=(x+w, y+h), color=(255, 255, 255), thickness=3)
    else:
        cv2.putText(img=frame, text='Tracking is lost.', org=(100, 200), fontFace=cv2.FONT_HERSHEY_SIMPLEX, thickness=2)
    
    # save video
    writer.write(frame)
    
    # show frame
    cv2.imshow('frame', frame)
    
    # if frame is display for more than 1 ms and ESC key is pressed, close display
    if cv2.waitKey(1) & 0xFF == 27:
        break

video_frames.release()
writer.release()
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10932311621966795, 'fraction_numerical': 0.022988505747126436, 'mean_word_length': 3.9885350318471335, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '638712', 'license': 'CC0-1.0', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1286, 'n_words': 381, 'path': '5_Object-Tracking-using-OpenCV.ipynb', 'repo_name': 'nghweigeok/opencv-course', 'size': '3915'}