<jupyter_start><jupyter_text>Connect to A Webcam<jupyter_code>video_frames = cv2.VideoCapture(0)  # 0: default camera

# 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('./my_video.mp4', cv2.VideoWriter_fourcc(*'DIVX'), 30, (frame_width, frame_height))   
 
# loop: grab frame and display image
while True:
    # read frames
    ret, frame = video_frames.read()
    
    # 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>Load A Video File<jupyter_code>video_frames = cv2.VideoCapture('./my_video.mp4')

# check if video is loaded
if video_frames.isOpened() == False:
    print('Error: File not found or wrong codec used.')

# show video files with delay
while video_frames.isOpened():
    # read frames
    ret, frame = video_frames.read()
    
    # if the video is running
    if ret == True:
        # add delay, display at frame rate 30
        time.sleep(1/30)  
        
        # show frame
        cv2.imshow('frame', frame)  
        
        if cv2.waitKey(10) & 0xFF == 27:
            break
    
    # if the video is finished, break loop
    else:
        break

video_frames.release()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Draw Shapes on Video<jupyter_code>video_frames = cv2.VideoCapture(0)  

# 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))

# define top left corner
x = frame_width // 4
y = frame_height // 4

# define rectangle size
rect_width = 200
rect_height = 200
 
# loop: grab frame and display image
while True:
    # read frames
    ret, frame = video_frames.read()
    
    # rectangle
    cv2.rectangle(img=frame, pt1=(x,y), pt2=(x+rect_width, y+rect_height), color=(255, 0, 0), thickness=4)
    
    # show frame
    cv2.imshow('frame', frame)
    
    if cv2.waitKey(1) & 0xFF == 27:
        break

video_frames.release()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Interact with Video<jupyter_code>def check_clicks(event, x, y, flags, param):
    '''draw rectangles on screen by clicks'''
    
    global pt1, pt2, topLeft_clicked, botRight_clicked
    
    if event == cv2.EVENT_LBUTTONDOWN:
        # if there is existing rectangle, reset the variables
        if topLeft_clicked == True and botRight_clicked == True:
            pt1 = (0, 0)
            pt2 = (0, 0)
            topLeft_clicked = False
            botRight_clicked = False
        
        # if there is no pt1, register pt1
        if topLeft_clicked == False:
            pt1 = (x, y)
            topLeft_clicked = True
            
        # else if there is no pt2, register pt2
        elif botRight_clicked == False:
            pt2 = (x, y)
            botRight_clicked = True
# initialize variables           
pt1 = (0, 0)
pt2 = (0, 0)
topLeft_clicked = False
botRight_clicked = False

video_frames = cv2.VideoCapture(0)  # 0: default camera
cv2.namedWindow('frame')
cv2.setMouseCallback('frame', check_clicks)
 
# loop: grab frame and display image
while True:
    #read frames
    ret, frame = video_frames.read()
    
    # draw a circle marker at point 1
    if topLeft_clicked:
        cv2.circle(img=frame, center=pt1, radius=5, color=(0, 255, 0), thickness=-1)
    
    # draw a rectangle
    if topLeft_clicked and botRight_clicked:
        cv2.rectangle(img=frame, pt1=pt1, pt2=pt2, color=(0, 255, 0), thickness=3)
    
    # 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()
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10136592379583034, 'fraction_numerical': 0.02827701893122454, 'mean_word_length': 2.967680608365019, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '34118', 'license': 'CC0-1.0', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1394, 'n_words': 379, 'path': '2_Working-with-Videos-using-OpenCV.ipynb', 'repo_name': 'nghweigeok/opencv-course', 'size': '4173'}