title: Displaying a webcam feed using OpenCV and Python
body: <p>I have been trying to create a simple program with <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29" rel="noreferrer">Python</a> which uses OpenCV to get a video feed from my webcam and display it on the screen.</p>
  
  <p>I know I am partly there because the window is created and the light on my webcam flicks on, but it just doesn't seem to show anything in the window. Hopefully someone can explain what I'm doing wrong.</p>
  
  <pre><code>import cv
  
  cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
  capture = cv.CaptureFromCAM(0)
  
  def repeat():
  
      frame = cv.QueryFrame(capture)
      cv.ShowImage("w1", frame)
  
  
  while True:
      repeat()
  </code></pre>
  
  <p>On an unrelated note, I have noticed that my webcam sometimes changes its index number in <code>cv.CaptureFromCAM</code>, and sometimes I need to put in 0, 1 or 2 even though I only have one camera connected and I haven't unplugged it (I know because the light doesn't come on unless I change the index). Is there a way to get Python to determine the correct index?</p>