4

I am using OpenCv to capture image from webcam.

It works fine I just don't know how to close the camera.

from cv2 import *
# initialize the camera
cam = VideoCapture(0)   # 0 -> index of camera
s, img = cam.read()
if s:    # frame captured without any errors
    namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
    imshow("cam-test",img)`
    waitKey(0)
    destroyWindow("cam-test")
    imwrite("testfilename.jpg",img) #save image
    cam.release
Akshay Kadidal
  • 515
  • 1
  • 7
  • 15
  • Your code is working fine. Camera is automatically closed once you press any key because you have used waitKey(0). can you elaborate your problem? – Jazz Oct 19 '17 at 03:29
  • Thank you, the camera light was blinking even after the file was written. it would only go off when the python session was closed. – Akshay Kadidal Oct 19 '17 at 04:05

3 Answers3

14

I think you're just missing () at the end of cam.release

trixr4kdz
  • 324
  • 2
  • 12
0

use below snippet

cap=cv2.VideoCapture(0) 
 
while True:
    ret,frame=cap.read()
    
    
    cv2.imshow("window",frame)
    if cv2.waitKey(0) & 0xFF==ord("q"):
        break

cv2.destroyAllWindows()

you can change the letter "q" to any other character and by pressing the "q" letter the video will stop

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-1

Change CV_WINDOW_AUTOSIZE to WINDOW_AUTOSIZE.

Karol Selak
  • 4,248
  • 6
  • 35
  • 65