亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pi camera: 相機已經在使用端口 0

pi camera: 相機已經在使用端口 0

料青山看我應如是 2023-03-08 10:28:29
見下文,以改進對我的問題的解釋我在我的 Raspberry Pi 上使用 OpenCV 4.1.2 編寫了一個簡單的線跟隨器。一切正常,但現在我想調用一個也使用 camera.capture_continous 函數的函數。當我調用該函數時(它應該順便檢測相機框架中的圓圈)它只拍攝一張照片,檢測到一些圓圈然后凍結并且不拍攝任何其他照片。這是我的源代碼的一部分:rawCapture = PiRGBArray(camera, size=(320, 180))rawCaptureCircles = PiRGBArray(camera, size=(320, 180))for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):      counter = counter + 1    image = frame.array    line = cv2.inRange(image, (0, 0, 0), (255, 255, 75))     if counter > 10: #call function        function()    rawCapture.truncate(0)和功能:for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):          imageCirles = frame.array        gray = cv2.cvtColor(imageCirles, cv2.COLOR_BGR2GRAY)        # detect circles in the image        circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 2, 400)        # ensure at least some circles were found        if circles is not None:            # convert the (x, y) coordinates and radius of the circles to integers            circles = np.round(circles[0, :]).astype("int")            # loop over the (x, y) coordinates and radius of the circles            for (x, y, r) in circles:                # draw the circle in the output image, then draw a rectangle                cv2.circle(imageCirles, (x, y), r, (255, 255, 0), 4)                cv2.rectangle(imageCirles, (x - 5, y - 5), (x + 5, y + 5), (0, 0, 255), -1)                cv2.putText(imageCirles, str(((x - 160) / 10) - 24), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)我希望現在一切都或多或少地清楚了。編輯:希望對我的問題有更好的解釋:我很絕望!我已經嘗試了(至少在我看來)所有可能的方法,但該功能仍然不起作用。但是,如果我將函數的源代碼放入一個單獨的 python 文件中并執行它,一切正常!我知道理論上的錯誤是什么,但我真的不知道如何修復它。這是問題的另一種解釋:名為 function() 的函數基本上可以正常工作(如果我將它放入一個空的 python 中并執行它),但每次在我的正常程序中執行 function() 時,我都會收到以下錯誤消息 The camera is already using port 0.這是因為在程序的正常循環中for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True)用于創建無限循環但是如果現在調用 function(),第一行這導致我收到上述錯誤消息。是否必須關閉攝像頭端口才能再次使用?
查看完整描述

1 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

我不知道你到底想做什么,但你的錯誤是你使用了 second capture_continuous。您應該只使用一個capture_continuous并發送frame(甚至image)作為參數,function(frame)并且它應該只使用 this frame。在處理完這個單幀后,它將返回到主循環,主循環將function(frame)再次運行 next frame- 所以它會像在capture_continuous


# --- functions ---


def function(frame):

    

    imageCirles = frame.array

    gray = cv2.cvtColor(imageCirles, cv2.COLOR_BGR2GRAY)


    # detect circles in the image

    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 2, 400)


    # ensure at least some circles were found

    if circles is not None:


        # convert the (x, y) coordinates and radius of the circles to integers

        circles = np.round(circles[0, :]).astype("int")


        # loop over the (x, y) coordinates and radius of the circles

        for (x, y, r) in circles:

            # draw the circle in the output image, then draw a rectangle

            cv2.circle(imageCirles, (x, y), r, (255, 255, 0), 4)

            cv2.rectangle(imageCirles, (x - 5, y - 5), (x + 5, y + 5), (0, 0, 255), -1)


            cv2.putText(imageCirles, str(((x - 160) / 10) - 24), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)


# --- main ---


rawCapture = PiRGBArray(camera, size=(320, 180))


counter = 0


for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):  

    counter += 1


    image = frame.array

    line = cv2.inRange(image, (0, 0, 0), (255, 255, 75)) 


    if counter > 10: #call function

        function(frame)

        

    rawCapture.truncate(0)

編輯:


最終你可以使用break退出一個循環然后運行另一個循環。


counter = 0


for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):  

    counter += 1


    image = frame.array

    line = cv2.inRange(image, (0, 0, 0), (255, 255, 75)) 


    if counter > 10: #call function

        break

        

    rawCapture.truncate(0)

    

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):  


    function(frame)

        

    rawCapture.truncate(0)

我不確定,但它可能會造成很小的延遲。


如果第一個循環用于跟隨線,那么它將不再跟隨?,F在它將只檢查圓圈。如果你想同時跟隨線和檢查圓圈,那么你應該capture_continuous像第一個例子一樣運行所有的東西。


查看完整回答
反對 回復 2023-03-08
  • 1 回答
  • 0 關注
  • 82 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號