close webcam in openCV python

11,868

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()
Share:
11,868

Related videos on Youtube

Mohammad Arshad
Author by

Mohammad Arshad

Working at Jolta Technology Private Limited Bahria town Islamabad

Updated on June 04, 2022

Comments

  • Mohammad Arshad
    Mohammad Arshad almost 2 years

    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() 
    
    • Sembei Norimaki
      Sembei Norimaki over 5 years
      your main function is empty. You should fix this first
    • Dan Mašek
      Dan Mašek over 5 years
      Please, make a proper minimal reproducible example -- fix the indentation, and remove anything irrelevant (e.g. what does matplotlib have to do with the issue?) Also, how do you know it's not released, when cap.release() appears to be the last statement in your example?
    • Mohammad Arshad
      Mohammad Arshad over 5 years
      my webcam light is still on
    • Hayat
      Hayat over 5 years
      I can't see if statement. Also, check I code after else part is executing properly.
    • ImportanceOfBeingErnest
      ImportanceOfBeingErnest over 5 years
      Please fix the code such that one can copy and paste it from the question and see the same result as you. Also state how you are running this code.
    • Dan Mašek
      Dan Mašek over 5 years
      @MohammadArshad What version of OpenCV are you using? What platform?