Webcam-Bild mit OpenCV in Python aufnehmen
English
Deutsch
Dieser Code öffnet /dev/video0 und nimmt ein einzelnes Bild auf, wobei das Gerät danach geschlossen wird:
capture_webcam.py
import cv2
video_capture = cv2.VideoCapture(0)
# Erfolg prüfen
if not video_capture.isOpened():
raise Exception("Could not open video device")
# Bild lesen. ret === True bei Erfolg
ret, frame = video_capture.read()
# Gerät schließen
video_capture.release()Du kannst auch cv2.VideoCapture("/dev/video0") verwenden, aber dieser Ansatz ist plattformabhängig. cv2.VideoCapture(0) öffnet auch auf Nicht-Linux-Plattformen das erste Videogerät.
In Jupyter kannst du das Bild anzeigen mit
display_webcam_jupyter.py
import sys
from matplotlib import pyplot as plt
frameRGB = frame[:,:,::-1] # BGR => RGB
plt.imshow(frameRGB)If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow