2016-08-24 36 views
0

我預先寫好的代碼應該可以訪問我的USB網絡攝像頭。據我瞭解,它使用URL方案(我是編程新手,很抱歉,如果我說的是廢話)。 所以我現在有下面的代碼,我想知道如何使用opencv_capture而不是例如DummyCapture來訪問攝像頭。如何確認條件「if scheme =='opencv'」?如何正確使用「urlparse」

如果有人知道如何解決這個問題,這將是一個很大的幫助!

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

回答

0

要回答你的問題,URL sheme應該像opencv://index/N,其中N是一個整數(即您的USB凸輪的數量,一般在/dev/videoN)。完整的命令:

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攝像頭的圖片,過濾它們爲灰度並打印到一個窗口。要退出,只需點擊鍵q

+0

是的你是對的,這是一個Beamprofiler代碼的一部分。問題是,我並不是真的想要從我的相機獲取實時視頻,但我希望上面的代碼訪問相機,以便真正的Beamprofiler代碼可以評估從相機獲取的信息。 – Jennan

+0

你有鏈接到源代碼? –

+0

當然,你可以在這裏找到代碼:https://github.com/jordens/bullseye 我一直無法讓我的相機使用它。我使用的是隻有CCD芯片的Playstation相機,所以沒有鏡頭等。我可以用simplecv或opencv輕鬆訪問相機,但我無法弄清楚如何將它與bullseye代碼結合起來。 – Jennan