OpenCV Python – Capture Video from Camera
Video capture is one of the most important features in OpenCV. It allows you to access a webcam or camera and process live video streams in real time.
In this tutorial, you will learn how to capture video from a camera using OpenCV Python.
1. What is Video Capture?
Video capture means reading frames continuously from a camera or video device.
It is used for:
- Face detection
- Object tracking
- Motion detection
- Real-time AI applications
2. Import OpenCV
import cv2
3. Access Webcam
Syntax:
cv2.VideoCapture(index)
-
0→ Default webcam -
1→ External camera
Example:
cap = cv2.VideoCapture(0)
4. Read Video Frames
while True:
ret, frame = cap.read()
cv2.imshow("Camera", frame)
if cv2.waitKey(1) & 0xFF == 27: # ESC key
break
cap.release()
cv2.destroyAllWindows()
5. Check Camera Status
if not cap.isOpened():
print("Cannot access camera")
6. Change Frame Properties
You can modify camera settings:
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
7. Convert Video to Grayscale
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("Grayscale Video", gray)
if cv2.waitKey(1) & 0xFF == 27:
break
8. Why Video Capture is Important
Video capture is used in:
- Face recognition systems
- Security surveillance
- Motion tracking
- AI-based camera apps
- Real-time object detection
9. Common Mistakes
❌ Camera not opening
✔ Solution:
-
Try different index:
0,1,2
❌ Blank video window
✔ Solution:
-
Check
cap.read()return value
10. Conclusion
Capturing video from a camera in OpenCV Python is the foundation of real-time computer vision applications. It allows you to process live frames for AI, detection, and tracking systems.
Once you master video capture, you can move to advanced topics like video recording and object tracking.


0 Comments