【问题标题】:syntax error:break is not inside the loop语法错误:break不在循环内
【发布时间】:2016-07-07 16:34:42
【问题描述】:
# import the necessary packages
import numpy as np
import argparse
import cv2

# initialize the current frame of the video, along with the list of
# ROI points along with whether or not this is input mode
frame = None
roiPts = []
inputMode = False
def selectROI(event, x, y, flags, param):
    # grab the reference to the current frame, list of ROI
    # points and whether or not it is ROI selection mode
    global frame, roiPts, inputMode

    # if we are in ROI selection mode, the mouse was clicked,
    # and we do not already have four points, then update the
    # list of ROI points with the (x, y) location of the click
    # and draw the circle
    if inputMode and event == cv2.EVENT_LBUTTONDOWN and len(roiPts) < 4:
        roiPts.append((x, y))
        cv2.circle(frame, (x, y), 4, (0, 255, 0), 2)
        cv2.imshow("frame", frame)
def main():
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video",
        help = "path to the (optional) video file")
    args = vars(ap.parse_args())

    # grab the reference to the current frame, list of ROI
    # points and whether or not it is ROI selection mode
    global frame, roiPts, inputMode

    # if the video path was not supplied, grab the reference to the
    # camera
    if not args.get("video", False):
        camera = cv2.VideoCapture(0)

    # otherwise, load the video
    else:
        camera = cv2.VideoCapture(args["video"])

    # setup the mouse callback
    cv2.namedWindow("frame")
    cv2.setMouseCallback("frame", selectROI)

    # initialize the termination criteria for cam shift, indicating
    # a maximum of ten iterations or movement by a least one pixel
    # along with the bounding box of the ROI
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    roiBox = None
# keep looping over the frames
while True:
    # grab the current frame
    (grabbed, frame) = camera.read()

    # check to see if we have reached the end of the
    # video
    if not grabbed:
        break

    # if the see if the ROI has been computed
    if roiBox is not None:
        # convert the current frame to the HSV color space
        # and perform mean shift
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)

        # apply cam shift to the back projection, convert the
        # points to a bounding box, and then draw them
        (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
        pts = np.int0(cv2.cv.BoxPoints(r))
        cv2.polylines(frame, [pts], True, (0, 255, 0), 2)
# show the frame and record if the user presses a key
cv2.imshow("frame", frame)
key = cv2.waitKey(1) & 0xFF

# handle if the 'i' key is pressed, then go into ROI
# selection mode
if key == ord("i") and len(roiPts) < 4:
    # indicate that we are in input mode and clone the
    # frame
    inputMode = True
    orig = frame.copy()

    # keep looping until 4 reference ROI points have
    # been selected; press any key to exit ROI selction
    # mode once 4 points have been selected
    while len(roiPts) < 4:
        cv2.imshow("frame", frame)
        cv2.waitKey(0)

    # determine the top-left and bottom-right points
    roiPts = np.array(roiPts)
    s = roiPts.sum(axis = 1)
    tl = roiPts[np.argmin(s)]
    br = roiPts[np.argmax(s)]

    # grab the ROI for the bounding box and convert it
    # to the HSV color space
    roi = orig[tl[1]:br[1], tl[0]:br[0]]
    roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    #roi = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)

    # compute a HSV histogram for the ROI and store the
    # bounding box
    roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
    roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
    roiBox = (tl[0], tl[1], br[0], br[1])

# if the 'q' key is pressed, stop the loop
elif key == ord("q"):
    break
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

运行此代码时,我在第 114 行出现错误,显示 'break is not inside the loop"...谁能帮我解决这个错误。

【问题讨论】:

  • 那么break 语句不在循环内。就那么简单。我相信您的 ifelif 语句应该缩进,以便它们位于 while True 循环内。
  • 您是否真的需要发布所有代码,因为错误消息清楚地指出了问题所在?
  • break 不在循环内...
  • 可能会重新审视你的循环概念。

标签: python


【解决方案1】:

因为整个 if 都在循环之外... 只需简单地将其放入 while 循环(通过选择行并按 Tab,或在空闲时按 ctrl+])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多