【问题标题】:TypeError: 'NoneType' object is not subscriptable ProblemTypeError:“NoneType”对象不可下标问题
【发布时间】:2020-05-04 20:14:58
【问题描述】:

我尝试在 opencv python 上运行我的自行车检测。当我运行它时它可以正常工作,但是当程序结束时它会出错。我不知道如何解决它。谁能告诉我问题出在哪里?

import cv2
import numpy as np
import pygame
import datetime as dt
from pygame import mixer
import time

#=============== Variable Mouse ==================#
drawing = False
point1 = ()
point2 = ()

drawingTwo = False
pointTwo_1 = ()
pointTwo_2 = ()
Mouse_count = False
#================================================#
def mouse_drawing(event, x, y, flags, params):
    global point1, point2, drawing
    global pointTwo_1, pointTwo_2, drawingTwo, Mouse_count

    #----------Mouse 1-------
    if Mouse_count == False:
        if event == cv2.EVENT_LBUTTONDOWN:
            if drawing is False:
                drawing = True
                point1 = (x, y)
            #else:
                #drawing = False

        elif event == cv2.EVENT_MOUSEMOVE:
            if drawing is True:
                point2 = (x, y)
        elif event == cv2.EVENT_LBUTTONUP:
            drawing = False
            Mouse_count = True



#================================================#
lastTime = dt.datetime.now()
currentTime = dt.datetime.now()

#Make Sound
pygame.mixer.init()


#create VideoCapture object and read from video file


cap = cv2.VideoCapture('test13.mp4')

cv2.namedWindow("Detecion motor")
cv2.setMouseCallback("Detecion motor", mouse_drawing)

while True:
    ret, frame = cap.read()
    car_cascade = cv2.CascadeClassifier('cascade11.xml')

    #============================== ROI One ============================#
    if point1 and point2:

        #Rectangle marker
        r = cv2.rectangle(frame, point1, point2, (100, 50, 200), 5)
        frame_ROI = frame[point1[1]:point2[1],point1[0]:point2[0]]

        #------------------Detect car ROI-------------------#
        if drawing is False:
            #convert video into gray scale of each frames
            ROI_grayscale = cv2.cvtColor(frame_ROI, cv2.COLOR_BGR2GRAY)
            #detect cars in the video
            cars_ROI = car_cascade.detectMultiScale(ROI_grayscale, 1.1, 3)
            if len(cars_ROI) > 0:
                if (currentTime - lastTime).seconds > 20:
                    lastTime = dt.datetime.now()
                    sound = mixer.Sound('sirine2.wav')
                    sound.play()

            for (x, y, w, h) in cars_ROI:
                cv2.rectangle(frame_ROI, (x, y), (x + w, y + h), (0, 255, 0), 2)
                currentTime = dt.datetime.now()
                # cv2.putText(frame_ROI, "Jumlah Motor : " + str(cars_ROI.shape[0]), (10,frame_ROI.shape[0] -25), cv2.FONT_HERSHEY_TRIPLEX, 0.5,(0,255,0), 1)
            # -------------------------------------------------#

    #============================== ROI Two ============================#

    #==================================================================#
    cv2.imshow("Detecion motor", frame)

    if cv2.waitKey(25) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

然后错误是:

Traceback (most recent call last):
  File "D:/Skripsi/CarDetection-master/DeteksiMotor.py", line 65, in <module>
    frame_ROI = frame[point1[1]:point2[1],point1[0]:point2[0]]
TypeError: 'NoneType' object is not subscriptable

【问题讨论】:

  • 我的猜测是那帧是无
  • 我不明白。你怎么把它放在代码中?
  • framepoint1point2None。您需要做的第一件事是检查它是哪个。
  • 如何检查?
  • 例如通过打印变量。

标签: python opencv


【解决方案1】:

优化@mkrieger1 的评论,我们可以在您的代码中看到您正在检查point1point2 是否都转换为布尔真,所以问题不存在。默认情况下,这意味着frame 等于无。替换你的行:

if point1 and point2:

与:

if frame and point1 and point2:

我的意思是,frame 是看起来像输入操作的结果,而这些操作可能会以多种创造性的方式失败。

【讨论】:

  • 它出来了。 Traceback(最近一次调用最后一次):文件“D:/Skripsi/CarDetection-master/DeteksiMotor.py”,第 61 行,在 中如果 frame 和 point1 和 point2:ValueError:一个以上数组的真值元素不明确。使用 a.any() 或 a.all()
  • 所以,很明显,frame 是一种不直接转换为 bool 的类型。您可能需要使用 any() 方法,但假设这是一个 numpy 数组,我不知道具体如何调用它。
  • 当我用键盘上的“q”按钮播放视频时,它的工作正常,没有错误出现。但是当我尝试完成视频时,它总是会出现我一开始提到的错误。为什么会这样?
猜你喜欢
  • 1970-01-01
  • 2021-12-14
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 2019-08-12
  • 2015-02-27
相关资源
最近更新 更多