如何正确使用"urlparse"
How to use "urlparse" properly
我的预写代码应该可以访问我的 USB 网络摄像头。据我所知,它使用了一个 URL-scheme(我是编程新手,如果我在胡说八道,抱歉)。
所以我现在有以下代码,我想知道如何使用 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()
回答你的问题,URL sheme应该是opencv://index/N
,其中N
是一个整数(即你的USB cam的编号,一般在[=14以下=]).完整命令:
python -m bullseye.app --camera opencv://index/0
bullseye --camera opencv://index/0
(题外话) 但是您的代码似乎是一个更大项目的一部分。如果你不太了解编程,你应该从最小的工作代码开始。使用 OpenCV,从 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()
这段代码将从您的 USB 网络摄像头拍摄照片,将它们过滤成灰度并打印成 window。要退出,只需按 q
.
键
我的预写代码应该可以访问我的 USB 网络摄像头。据我所知,它使用了一个 URL-scheme(我是编程新手,如果我在胡说八道,抱歉)。 所以我现在有以下代码,我想知道如何使用 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()
回答你的问题,URL sheme应该是opencv://index/N
,其中N
是一个整数(即你的USB cam的编号,一般在[=14以下=]).完整命令:
python -m bullseye.app --camera opencv://index/0
bullseye --camera opencv://index/0
(题外话) 但是您的代码似乎是一个更大项目的一部分。如果你不太了解编程,你应该从最小的工作代码开始。使用 OpenCV,从 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()
这段代码将从您的 USB 网络摄像头拍摄照片,将它们过滤成灰度并打印成 window。要退出,只需按 q
.