Thursday, April 27, 2023

Object Detection with OpenCV-Python code

 import cv2


# Load the pre-trained face detection classifier

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


# Load the image

img = cv2.imread('test.jpg')


# Convert the image to grayscale

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


# Detect faces in the grayscale image

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))


# Draw rectangles around the detected faces

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

    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)


# Display the result

cv2.imshow('img', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

In this example, the cv2.CascadeClassifier function is used to load the pre-trained Haar Cascade classifier file for face detection. The detectMultiScale function is used to detect faces in the image. The scaleFactor parameter determines how much the image size is reduced at each image scale, the minNeighbors parameter controls the number of neighbors a detection candidate needs to retain, and the minSize parameter specifies the minimum size of the face to detect. Finally, the cv2.rectangle function is used to draw a rectangle around each detected face in the image, and the cv2.imshow function is used to display the result.

No comments:

Post a Comment

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...