在实时摄像机上运行代码时出错

时间:2019-04-29 06:28:30

标签: python opencv machine-learning

在实时摄像机上运行traffic_counter.py时,出现以下错误。我尝试使用其他摄像机,IP地址,代码无法显示视频。它仅显示2到3秒钟,并且视频在直播流中停止播放。这是错误:

-3074457345618259.0 30.0 1920 1080
Traceback (most recent call last):
  File "C:\Users\OM_PRAJWL_NAMAHA\Desktop\github\imp\traffic_counter\traffic_counter.py", line 231, in <module>
    curcent = df.iloc[int(framenumber)][str(carids[currentcarsindex[i]])]
  File "C:\Program Files\Python36\lib\site-packages\pandas\core\indexing.py", line 1500, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Program Files\Python36\lib\site-packages\pandas\core\indexing.py", line 2230, in _getitem_axis
    self._validate_integer(key, axis)
  File "C:\Program Files\Python36\lib\site-packages\pandas\core\indexing.py", line 2139, in _validate_integer
    raise IndexError("single positional indexer is out-of-bounds")

这是我的代码:

currentcars = 0  # current cars on screen
currentcarsindex = []  # current cars on screen carid index
for i in range(len(carids)):  # loops through all carids

    if df.at[int(framenumber), str(carids[i])] != '':
        # checks the current frame to see which car ids are active
        # by checking in centroid exists on current frame for certain car id

        currentcars = currentcars + 1  # adds another to current cars on screen
        currentcarsindex.append(i)  # adds car ids to current cars on screen

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.putText(image, "Centroid" + str(curcent[0]) + "," + str(curcent[1]),
                    (int(curcent[0]), int(curcent[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 2)

        cv2.putText(image, "ID:" + str(carids[currentcarsindex[i]]), (int(curcent[0]), int(curcent[1] - 15)),
                    cv2.FONT_HERSHEY_SIMPLEX, .5, (0, 255, 255), 2)

        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:

                carscrossedup = carscrossedup + 1
                cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 255), 5)
                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 carids[
                currentcarsindex[i]] not in caridscrossed:

                carscrosseddown = carscrosseddown + 1
                cv2.line(image, (0, lineypos2), (width, lineypos2), (0, 0, 125), 5)
                caridscrossed.append(currentcarsindex[i])

0 个答案:

没有答案