Advertisement
RexyBadDog

openCV_camTest.py

Dec 14th, 2021 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. import cv2
  2.  
  3. # The device number might be 0 or 1 depending on the device and the webcam.
  4. cap = cv2.VideoCapture()
  5. cap.open(0, cv2.CAP_DSHOW)
  6.  
  7. # this if is in case I want to open a video file that does not exists in a path for cv2.VideoCapture('path')
  8. if not cap.isOpened():
  9.     raise IOError("Cannot open file")
  10.  
  11. # this line here is only to give the window a name before the loop so I
  12. # can have a reference for the getWindowProperty window name.
  13. cv2.namedWindow("frame", cv2.WINDOW_AUTOSIZE)
  14.  
  15. # getWindowProperty will return -1 if if try to close it with my mouse, weird.... (but useful)
  16. while(cv2.getWindowProperty('frame', 0) >= 0):
  17.     ret, frame = cap.read()
  18.     cv2.imshow('frame', frame)
  19.     if cv2.waitKey(1) & 0xFF == ord('q'):
  20.         break
  21. cap.release()
  22. cv2.destroyAllWindows()
  23.  
  24. # TODO: I should try to wrap cv2 inside tkinter GUI to add more elements to the window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement