Search code examples
python-2.7opencvcameracaptureurlparse

How to use "urlparse" properly


The prewritten code I have is supposed to access my USB webcam. It uses an URL-scheme as far as I understand it (I am new to programming, so sorry if I am talking nonsense). So I am having the following code now, and I was wondering how I can make it access the camera using opencv_capture instead of for example DummyCapture. How do I confirm the condition "if scheme == 'opencv' "?

It would be a big help if somebody new how to solve that!

from traits.trait_base import ETSConfig
#ETSConfig.toolkit = "wx"
# fix window color on unity TODO: gets overriden by splitter
if ETSConfig.toolkit == "wx":
    from traitsui.wx import constants
    constants.WindowColor = constants.wx.NullColor

import optparse, logging, urlparse

from capture import BaseCapture, DummyCapture
from bullseye import Bullseye
from process import Process

def main():
   p = optparse.OptionParser(usage="%prog [options]")
   p.add_option("-c", "--camera", default="any:",
           help="camera uri (none:, any:, dc1394://guid/b09d01009981f9, "
                "fc2://index/1, replay://glob/beam*.npz) [%default]")
   p.add_option("-s", "--save", default=None,
           help="save images accordint to strftime() "
                "format string (e.g. 'beam_%Y%m%d%H%M%S.npz'), "
                "compressed npz format [%default]")
   p.add_option("-l", "--log",
           help="log output file [stderr]")
   p.add_option("-d", "--debug", default="info",
           help="log level (debug, info, warn, error, "
                "critical, fatal) [%default]")
   opts, args = p.parse_args()
   logging.basicConfig(filename=opts.log,
           level=getattr(logging, opts.debug.upper()),
           format='%(asctime)s %(levelname)s %(message)s')
   scheme, loc, path, query, frag = urlparse.urlsplit(opts.camera)
   if scheme == "opencv":
       from .opencv_capture import OpenCVCapture
       if loc == "index":
          cam = OpenCVCapture(int(path[1:]))
   elif scheme == "none":
       from capture import DummyCapture
          cam = DummyCapture()
   elif scheme == "any":
       try:
          from .opencv_capture import OpenCVCapture
          cam = OpenCVCapture()
       except Exception, e:
          logging.debug("opencv error: %s", e)
          from capture import DummyCapture
          cam = DummyCapture()
  logging.debug("running with capture device: %s", cam)
  if opts.save:
      cam.save_format = opts.save
  proc = Process(capture=cam)
  bull = Bullseye(process=proc)
  bull.configure_traits()
  bull.close()

if __name__ == "__main__":
    main()

Solution

  • To answer your question, URL sheme should be something like opencv://index/N, where N is an integer (ie. the number of your USB cam, generally under /dev/videoN). The full command:

    python -m bullseye.app --camera opencv://index/0
    bullseye --camera opencv://index/0
    

    (off-topic) But your code seems to be part of a bigger project. If you do not know much about programming, you should start with the minimum working code. Using OpenCV, from the docs:

    #!/usr/bin/env python
    # coding: utf-8
    
    import cv2
    
    cap = cv2.VideoCapture(0)
    
    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
    
        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
        # Display the resulting frame
        cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()
    

    This piece of code will take pictures from your USB webcam, filter them to grayscale and print into a window. To quit, just hit the key q.