1 回答

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像第一個例子一樣運行所有的東西。
添加回答
舉報