Member-only story
OpenCV on Oracle Linux — Recognize cars
In a recent article I described how we can deploy OpenCV on Oracle Linux 9 for Vision AI. In this post we quickly show how we can make use of it with a first example.
In this example, we have a very simple script that will take an image and detect if there is a car in the image. As stated, this is a first step in a series where we will build more and more complex things with OpenCV.
The below script takes nothing additional than the installed Python parts from the previous blogpost. This is, with the exception of the haar cascade XML file which will hold the data needed to recognize a car. This can be easily obtained from the OpenCV website and the OpenCV Github page.
import cv2
import sys
def detect_car(image_path):
# Load the Haar Cascade classifier for car detection
cascade_path = "/vagrant/haarcascade_car.xml" # Provide the full path to the classifier file
car_cascade = cv2.CascadeClassifier(cascade_path)
# Read the image file
image = cv2.imread(image_path)
# Convert the image to grayscale for better performance
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect cars in the image
cars =…