Reputation: 88
I want to close web cam i used the cap.released() but it does not close the web cam after it captures the image. Here is my code:
import cv2
import matplotlib.pyplot as plt
def main():
cap=cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
print(ret)
print(frame)
else:
ret=False
img1= cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imshow(img1)
plt.title('Color Image RGB')
plt.xticks([])
plt.yticks([])
plt.show()
cap.release()
if __name__=='__main__':
main()
Upvotes: 3
Views: 19207
Reputation: 339520
The cam will stay active until you close the figure, i.e. until the script finishes. This is because you only release the capture afterwards,
plt.show()
cap.release()
If you want to turn off the camera after taking the image, reverse this order
cap.release()
plt.show()
Upvotes: 3