Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Forked from tedmiston/webcam-cv2.py
Created January 23, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/dc68c1de99a548d2eb28f8e4c55a77ca to your computer and use it in GitHub Desktop.
Save mtvbrianking/dc68c1de99a548d2eb28f8e4c55a77ca to your computer and use it in GitHub Desktop.
Display the webcam in Python using OpenCV (cv2)
'''
Simply display the contents of the webcam with optional mirroring using OpenCV
via the new Pythonic cv2 interface. Press <esc> to quit.
'''
import cv2
def show_webcam(mirror=False):
cam = cv2.VideoCapture(0)
while True:
ret_val, img = cam.read()
if mirror:
img = cv2.flip(img, 1)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
def main():
show_webcam(mirror=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment