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)
我不确定,但它可能会造成很小的延迟。
如果第一个循环用于跟随线,那么它将不再跟随。现在它将只检查圆圈。如果你想同时跟随线和检查圆圈,那么你应该capture_continuous像第一个例子一样运行所有的东西。
添加回答
举报