1 | initial version |
That code would never work. Indentation (the lines and spaces between code instructions) is key in Python, and, as you indented you code, the cv2.waitKey() instruction is out of the while loop, thus the image will no show. Change your code to:
i
mport numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
#capture frame by frame
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('test', img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()
cap.release()
Note: cv2.waitKey() is necessary so window has time to render the image, without it the image will not show. The input argument of that method is the time it should wait in milliseconds. If it is set to 0, it is waiting for you to press a key to move on to the next cycle.
2 | No.2 Revision |
That code would never work. Indentation (the lines and spaces between code instructions) is key in Python, and, as you indented you code, the cv2.waitKey() instruction is out of the while loop, thus the image will no not show. Change your code to:
i
mport numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
#capture frame by frame
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('test', img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()
cap.release()
Note: cv2.waitKey() is necessary so window has time to render the image, without it the image will not show. The input argument of that method is the time it should wait in milliseconds. If it is set to 0, it is waiting for you to press a key to move on to the next cycle.
3 | No.3 Revision |
That code would never work. Indentation (the lines and spaces between code instructions) is key in Python, and, as you indented you code, the cv2.waitKey() instruction is out of the while loop, thus the image will not show. Change your code to:
i
mport import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
#capture frame by frame
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('test', img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()
cap.release()
Note: cv2.waitKey() is necessary so window has time to render the image, without it the image will not show. The input argument of that method is the time it should wait in milliseconds. If it is set to 0, it is waiting for you to press a key to move on to the next cycle.