如何正确使用“ urlparse”

仁南

我拥有的预先编写的代码应该可以访问我的USB网络摄像头。据我所知,它使用URL方案(我是编程新手,如果我在胡说八道,抱歉。)所以我现在有以下代码,我想知道如何使用opencv_capture而不是例如DummyCapture使其访问摄像机。如何确认条件“ if scheme =='opencv'”?

如果有人新来解决这个问题,那将是一个很大的帮助!

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()
虎222

要回答您的问题,URL sheme应该类似于opencv://index/N,其中N是整数(即USB cam的编号,通常在下方/dev/videoN)。完整的命令:

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

(离题) 但是您的代码似乎是一个更大项目的一部分。如果您不太了解编程,则应从最少的工作代码开始。文档使用OpenCV

#!/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()

这段代码将从USB网络摄像头拍摄照片,将其过滤为灰度并打印到窗口中。要退出,只需按一下键q

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章