0
votes

I try to run my bikes detection on opencv python. its work properly when I run it but when the program ends it comes to an error. I don't know how to fix it. Can somebody tell me where the problem is?

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()

And then the error was:

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
1
My guess would be that frame is None - Andrew Allaire
i dont get it. how do you put it in the code ? - Mahada Panji Anggadhita
Either frame or point1 or point2 is None. The first thing you need to do is to check which one it is. - mkrieger1
how do i check it ? - Mahada Panji Anggadhita
For example by printing the variables. - mkrieger1

1 Answers

0
votes

Refining @mkrieger1's comment, we can see in your code that you are checking that both point1 and point2 for casting to Boolean True, so the problem is not there. By default, then this means that frame is equal to None. Replace your line:

if point1 and point2:

with:

if frame and point1 and point2:

I mean, frame is the result of what looks like an input operation, and these can fail in oh so many creative ways.