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

2024-10-03 06:32:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将名为“start”的属性设置为1或2,具体取决于对象首先穿过的线。你知道吗

我得到的错误是“name'start'未定义”

我试图在网上搜索,但似乎找不到任何类似的问题。你知道吗

代码如下:

我已经尝试将其设置为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])

Tags: andtoifisonlinecv2start
2条回答

您应该在将要使用的范围级别声明变量。你知道吗

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应该对您可用。你知道吗

您的代码中有以下流程

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

相关问题 更多 >