OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Saving a video from a webcam involves capturing frames from the webcam and then writing them to a video file. Here's a step-by-step tutorial on how to achieve this using OpenCV:
Make sure you've installed OpenCV:
pip install opencv-python
Then, import the necessary libraries:
import cv2
Use cv2.VideoCapture()
to capture video from the webcam:
cap = cv2.VideoCapture(0) # 0 is the default camera. Change the index if you have multiple cameras.
Before saving the video, you need to define the video writer and specify details like codec, frame rate, and resolution:
# Define the codec using VideoWriter_fourcc(*'XVID') and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
Here:
20.0
is the frames per second.(int(cap.get(3)), int(cap.get(4)))
gets the width and height of the frames, respectively.Now, you'll read frames from the webcam in a loop, possibly perform some operations on the frames, and then write them to the video file:
while cap.isOpened(): ret, frame = cap.read() # Read a frame if ret: # You can perform operations on 'frame' here if desired out.write(frame) # Save the frame to the video file cv2.imshow('Frame', frame) # Show the frame if cv2.waitKey(1) & 0xFF == ord('q'): # Exit when 'q' key is pressed break else: break
Always release the video capture and writer objects and close all OpenCV windows:
cap.release() out.release() cv2.destroyAllWindows()
Here's the full script to capture video from the webcam and save it:
import cv2 # Initialize the webcam cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)), int(cap.get(4)))) while cap.isOpened(): ret, frame = cap.read() if ret: # Perform operations on the frame if needed out.write(frame) cv2.imshow('Frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # Cleanup cap.release() out.release() cv2.destroyAllWindows()
Run the script and you'll capture video from your webcam. Press 'q' to stop recording. You should find the recorded video named 'output.avi' in the directory where the script was executed.
Saving processed video from a webcam with OpenCV in Python:
import cv2 # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) # Get the default width and height of the frames frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # Define the codec and create a VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output_video.avi', fourcc, 20.0, (frame_width, frame_height)) while True: # Read a frame from the webcam ret, frame = cap.read() # Perform processing on the frame (e.g., convert to grayscale) gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the processed frame cv2.imshow('Processed Frame', gray_frame) # Write the processed frame to the output video file out.write(gray_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the VideoCapture and VideoWriter objects cap.release() out.release() # Close all OpenCV windows cv2.destroyAllWindows()
Python code for saving video from a webcam in OpenCV:
import cv2 # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) # Get the default width and height of the frames frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # Define the codec and create a VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output_video.avi', fourcc, 20.0, (frame_width, frame_height)) while True: # Read a frame from the webcam ret, frame = cap.read() # Display the frame cv2.imshow('Webcam Frame', frame) # Write the frame to the output video file out.write(frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the VideoCapture and VideoWriter objects cap.release() out.release() # Close all OpenCV windows cv2.destroyAllWindows()
Video capture and processing with OpenCV:
import cv2 # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) while True: # Read a frame from the webcam ret, frame = cap.read() # Perform processing on the frame (e.g., convert to grayscale) gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the processed frame cv2.imshow('Processed Frame', gray_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the VideoCapture object cap.release() # Close all OpenCV windows cv2.destroyAllWindows()
Real-time video processing and saving in OpenCV:
import cv2 # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) # Get the default width and height of the frames frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # Define the codec and create a VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output_video.avi', fourcc, 20.0, (frame_width, frame_height)) while True: # Read a frame from the webcam ret, frame = cap.read() # Perform processing on the frame (e.g., convert to grayscale) gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the processed frame cv2.imshow('Processed Frame', gray_frame) # Write the processed frame to the output video file out.write(gray_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the VideoCapture and VideoWriter objects cap.release() out.release() # Close all OpenCV windows cv2.destroyAllWindows()
Saving video frames with specified file names in OpenCV:
import cv2 # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) frame_number = 0 while True: # Read a frame from the webcam ret, frame = cap.read() # Save each frame with a specified file name frame_file_name = f'frame_{frame_number}.jpg' cv2.imwrite(frame_file_name, frame) # Increment the frame number frame_number += 1 # Display the frame cv2.imshow('Webcam Frame', frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the VideoCapture object cap.release() # Close all OpenCV windows cv2.destroyAllWindows()
Combining video capture, processing, and saving with other OpenCV functions:
import cv2 # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) # Get the default width and height of the frames frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # Define the codec and create a VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output_video.avi', fourcc, 20.0, (frame_width, frame_height)) while True: # Read a frame from the webcam ret, frame = cap.read() # Perform processing on the frame (e.g., convert to grayscale) gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Apply additional processing as needed # ... # Display the processed frame cv2.imshow('Processed Frame', gray_frame) # Write the processed frame to the output video file out.write(gray_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the VideoCapture and VideoWriter objects cap.release() out.release() # Close all OpenCV windows cv2.destroyAllWindows()