Car and Object Detection in Python 3 (Tensorflow – ImageAI)

What’s up guys? Been a minute!

In this post we’re gonna take a look at a basic approach to do object detection in Python 3 using ImageAI and TensorFlow.

Unfortunately this post will be kind of short since I’m in the middle of studying for a certification. So let’s get to it!

Python Environment

We’ll need a specific version of Python, which is: Python 3.6.8

Go ahead and download it for your operating system.

Then we’ll need the following libraries (install using pip):

* Make sure to use this specific version for compatibility.

Object Detection Code

Before we get into the code, you’ll need the object detection class, which you can download from the following link: choose “yolo.h5“.

Make sure to place that class in the same folder as the python script:

from imageai.Detection import VideoObjectDetection
import os
import cv2
# Get current working directory
curDir = os.getcwd()
# Initialize video capture (webcam)
camera = cv2.VideoCapture(0) 
# Initialize imageAI objects and files
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(curDir, "yolo.h5"))
detector.loadModel()
# Start object detection and print frames
video_path = detector.detectObjectsFromVideo(camera_input=camera,
                                output_file_path=os.path.join(curDir, "cam1")
                                , frames_per_second=4, log_progress=True)
print(video_path)

Note that the frames_per_second parameter will allow for more realistic recording of your webcam/camera. However, since we aren’t yet using a graphics card to process this, you might want to keep it low.

Anyway gotta get back to studying, any questions let me know and I’ll get to it later on in the week! 😉

Share: