在for循环中设置对象的属性

时间:2019-07-31 20:17:11

标签: python opencv cv2

我试图根据对象首先穿过哪条线来将名为“ start”的属性设置为1或2。

我得到的错误是“未定义名称'开始'”

我曾尝试在整个网络上进行搜索,但似乎找不到任何类似的问题。

代码如下:

我尝试将其设置为start [i] = 1并同时将start [arids [                         currentcarsindex [i]]] = 1

for i in range(currentcars):  # loops through all current car ids on screen

            # grabs centroid of certain carid for current frame
            curcent = df.iloc[int(framenumber)][str(carids[currentcarsindex[i]])]

            # grabs centroid of certain carid for previous frame
            oldcent = df.iloc[int(framenumber - 1)][str(carids[currentcarsindex[i]])]

            if curcent:  # if there is a current centroid

                # On-screen text for current centroid

                cv2.drawMarker(image, (int(curcent[0]), int(curcent[1])), (0, 0, 255), cv2.MARKER_STAR, markerSize=5,
                               thickness=1, line_type=cv2.LINE_AA)

                if oldcent:  # checks if old centroid exists
                    # adds radius box from previous centroid to current centroid for visualization
                    xstart = oldcent[0] - maxrad
                    ystart = oldcent[1] - maxrad
                    xwidth = oldcent[0] + maxrad
                    yheight = oldcent[1] + maxrad
                    cv2.rectangle(image, (int(xstart), int(ystart)), (int(xwidth), int(yheight)), (0, 125, 0), 1)

                    # checks if old centroid is on or below line and curcent is on or above line
                    # to count cars and that car hasn't been counted yet
                    if oldcent[1] >= lineypos2 and curcent[1] <= lineypos2 and carids[
                        currentcarsindex[i]] not in caridscrossed:

                        cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 255), 5)
                        start = 1


                    # checks if old centroid is on or above line and curcent is on or below line
                    # to count cars and that car hasn't been counted yet
                    elif oldcent[1] <= lineypos3 and curcent[1] >= lineypos3 and carids[
                        currentcarsindex[i]] not in caridscrossed:

                        cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 125), 5)
                        start = 2


                    if oldcent[1] >= lineypos3 and curcent[1] <= lineypos3 and start == 1 and carids[
                        currentcarsindex[i]] not in caridscrossed:
                        carscrossedup = carscrossedup + 1
                        caridscrossed.append(
                            currentcarsindex[i])  # adds car id to list of count cars to prevent double counting


                    # checks if old centroid is on or above line and curcent is on or below line
                    # to count cars and that car hasn't been counted yet
                    elif oldcent[1] <= lineypos2 and curcent[1] >= lineypos2 and start == 2 and carids[
                        currentcarsindex[i]] not in caridscrossed:
                        carscrosseddown = carscrosseddown + 1
                        caridscrossed.append(
                            currentcarsindex[i])

2 个答案:

答案 0 :(得分:0)

您应该在将要使用的作用域级别上声明变量。

if oldcent:  # checks if old centroid exists
                    # adds radius box from previous centroid to current centroid for visualization
    xstart = oldcent[0] - maxrad
    ystart = oldcent[1] - maxrad
    xwidth = oldcent[0] + maxrad
    yheight = oldcent[1] + maxrad
    cv2.rectangle(image, (int(xstart), int(ystart)), (int(xwidth), int(yheight)), (0, 125, 0), 1)
    start = None

    if oldcent[1] >= lineypos2 and curcent[1] <= lineypos2 and carids[
            currentcarsindex[i]] not in caridscrossed:
        ...

现在,start应该可以使用了。

答案 1 :(得分:0)

您的代码中包含以下流程

if (condition_1):
    start = 1
elif (condition_2):
    start = 2
# There is no else clause. If neither condition evaluates as True then "start"
# will not have been defined

if (condition_that_uses_start):
    # Because start is not defined this will cause your error
    pass
相关问题