Topic: Python OpenCV – Part 4: Video Processing, Webcam Capture, and Real-Time Operations
---
1. Reading Video from File
---
2. Capturing Video from Webcam
---
3. Saving Video to File
---
4. Real-Time Video Processing
• Example: Convert webcam feed to grayscale live.
---
Summary
• You can capture video from files or webcams easily with OpenCV.
• Real-time processing lets you modify frames on the fly (filters, detections).
• Saving video requires specifying codec, frame rate, and resolution.
---
Exercise
• Write a program that captures webcam video, applies Canny edge detection to each frame in real-time, and saves the processed video to disk. Quit when pressing 'q'.
---
#Python #OpenCV #VideoProcessing #Webcam #RealTime
https://t.iss.one/DataScience4
---
1. Reading Video from File
import cv2
cap = cv2.VideoCapture('video.mp4')
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
cv2.imshow('Video Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
---
2. Capturing Video from Webcam
cap = cv2.VideoCapture(0) # 0 is usually the built-in webcam
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow('Webcam Feed', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
---
3. Saving Video to File
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
out.write(frame) # write frame to output file
cv2.imshow('Recording', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
---
4. Real-Time Video Processing
• Example: Convert webcam feed to grayscale live.
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Webcam', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
---
Summary
• You can capture video from files or webcams easily with OpenCV.
• Real-time processing lets you modify frames on the fly (filters, detections).
• Saving video requires specifying codec, frame rate, and resolution.
---
Exercise
• Write a program that captures webcam video, applies Canny edge detection to each frame in real-time, and saves the processed video to disk. Quit when pressing 'q'.
---
#Python #OpenCV #VideoProcessing #Webcam #RealTime
https://t.iss.one/DataScience4
❤3