Python programming blog

Friday, May 21, 2021

Vehicle detection project (Open-CV)

Hello python programmers This blog contains the code for the Vehicle detection project using Open-cv library.



Step1: Import the Open-CV library and .xml file.

import cv2

vehiclexml=cv2.CascadeClassifier('vehicle.xml')


Step 2: Create a function for detecting the webcam frame.

def detection(frame):

    vehicle=vehiclexml.detectMultiScale(frame,1.15,4)

    for (x,y,w,h) in vehicle:

        cv2.rectangle(frame,(x,y),(x+w,y+h),color=(0,255,0),thickness=2)

        cv2.putText(frame,'vehicle detected.!',(x+w,y+h+20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),thickness=2)

   return frame


Step 3: Create a function for capturescreen

def capturescreen():

    realtimevideo=cv2.VideoCapture(0)

    while realtimevideo.isOpened():

        ret,frame=realtimevideo.read()

        controlkey=cv2.waitKey(1)

        if ret:

            vehicleframe=detection(frame)

            cv2.imshow('vehicle detection',vehicleframe)

        else:

            break

        if controlkey==ord('q'):

            break

Step 4: Call all the functions

    realtimevideo.release()

    cv2.destroyAllWindows()


capturescreen()


Full code: Vehicle detection with xml file link

Buy me a coffee...!💓


2 comments:

  1. there is no xml file uploaded

    ReplyDelete
  2. check this link he put it on his github
    https://github.com/akpythonyt/AKpythoncodes/blob/main/vehicle.xml

    ReplyDelete