首页 > 其他分享 >opencv CascadeClassifier

opencv CascadeClassifier

时间:2024-12-18 18:31:26浏览次数:4  
标签:detection image cv2 face CascadeClassifier opencv time frame

opencv  CascadeClassifier

https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html

In this tutorial,

  • We will learn how the Haar cascade object detection works.
  • We will see the basics of face detection and eye detection using the Haar Feature-based Cascade Classifiers
  • We will use the cv::CascadeClassifier class to detect objects in a video stream. Particularly, we will use the functions:

 

 

Object Detection with OpenCV Cascade Classifiers

https://medium.com/@siromermer/object-detection-with-opencv-cascade-classifiers-f90d85b57403

When it comes to object detection, there are popular models like YOLO, SSD, RCNN, but most of the time these models require a GPU for training and inference, and they need a proper environment, which can be confusing to create especially for the first time.

If you don’t want to spend your time creating a complex environment, or if you don’t have a powerful GPU, Cascade classifiers might be a good choice for you.

  • In this article, I will discuss what are advantages and disadvantages of Cascade classifiers, how to make detections on images and videos, and how to use pretrained Cascade classifier models —>Python, OpenCV

Video by Kampus Production from Pexels

 

Advantages and Disadvantages of Cascade Classifiers

  • Advantages: Real-time Performance, Lightweight and Efficient, Pre-trained Models Available, Good for Simple Detection Tasks, Easy to Implement
  • Disadvantages: Perform poorly when the object has significant variations, High False Positives, Not good for complex objects

If you don’t need to detect complex objects, or if you dont have GPU for training and inferencing, Cascade Classifiers might be a good choice

Cascade Classifier ( image source )  

Pretrained Cascade Classifier Models

There are bunch of pretrained models that OpenCV shares with users, you can check models from image in the below. ( pretrained models )
Most of the pretrained models are for body, eye, and face detection.

You dont have to use this pretrained models, you can train your own model. Fortunately, OpenCV has step by step tutorial for it, you can follow it ( training custom model )

 

Detection→ Image

For loading pretrained model you need to create an object of the cv2.CascadeClassifier class and provide the pre-trained model file as a parameter.

For detection, you need to use the detectMultiScale method, and here is the parameters:

  • image→ input image in grayscale. Face detection works more efficiently in grayscale
  • scaleFactor → This parameter specifies how much the image size is reduced at each image scale.
  • minNeighbors → The detection algorithm finds multiple candidates, and minNeighbors controls how many detections around the same region are needed to declare if it is a object
  • minSize → It specifies the minimum size of the object to be detected
import cv2
import matplotlib.pyplot as plt

# Load the pre-trained Haar Cascade classifier for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_fullbody.xml')

# Read the image
image = cv2.imread(r"C:\Users\sirom\Downloads\pexels-chinmay-singh-251922-843563.jpg")

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(10, 10))

# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)

# Display the output
plt.imshow(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
Photo by Chinmay Singh from Pexels  

Detection→ Video

Main logic is similar, you just need to read the video and make prediction(detection) for each frame sequentially.

 
# Load the pre-trained Haar Cascade classifier for full body detection (you can use a face detector if needed)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_fullbody.xml')

# Capture video from a file or camera (use 0 for the default camera)
video_capture = cv2.VideoCapture(r"C:\Users\sirom\Downloads\7236898-hd_1280_720_25fps.mp4") # For a file
# video_capture = cv2.VideoCapture(0) # For camera input

# Start the time counter
prev_frame_time = time.time()
new_frame_time = 0

# Loop over frames from the video
while video_capture.isOpened():
# Capture frame-by-frame
ret, frame = video_capture.read()

if not ret:
print("Finished processing or cannot read the video.")
break

# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(10, 10))

# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 4)
cv2.putText(frame, 'Face', (x + 75, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)


# Calculate the FPS
new_frame_time = time.time()
fps = 1 / (new_frame_time - prev_frame_time)
prev_frame_time = new_frame_time

# Convert FPS to an integer for display
fps = int(fps)
fps_text = f'FPS: {fps}'

# Put the FPS text on the top-right corner of the frame
cv2.putText(frame, fps_text, (frame.shape[1] - 150, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)

# Display the resulting frame
cv2.imshow('Face Detection in Video with FPS', frame)

# Break the loop when 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture object and close all windows
video_capture.release()
cv2.destroyAllWindows()
Video by Pat Whelen from Pexels  

 

Face detection using Cascade Classifier using OpenCV-Python

https://www.linkedin.com/pulse/face-detection-using-cascade-classifier-opencv-python-shastrakar-sgpse/

Face detection using OpenCV with Haar Cascade Classifiers is a fundamental technique in computer vision that allows automatic detection of human faces within images or video streams. This approach relies on the use of pre-trained Haar cascade classifiers, a type of machine learning-based algorithm, to identify facial features and patterns.

Understanding Haar Cascade Classifiers:

Haar cascade classifiers are based on the Haar-like features concept, which involves comparing pixel intensities in various regions of an image to detect specific patterns or objects. These classifiers utilize a set of trained features and a cascade of classifiers to efficiently scan an image and determine whether a particular region contains the target object—in this case, a human face.

Working Principle:

It works in four stages:

· Haar-feature selection: A Haar-like feature consists of dark regions and light regions. It produces a single value by taking the difference of the sum of the intensities of the dark regions and the sum of the intensities of light regions. It is done to extract useful elements necessary for identifying an object. The features proposed by viola and jones are:


· Creation of Integral Images: A given pixel in the integral image is the sum of all the pixels on the left and all the pixels above it. Since the process of extracting Haar-like features involves calculating the difference of dark and light rectangular regions, the introduction of Integral Images reduces the time needed to complete this task significantly.

· AdaBoost Training: This algorithm selects the best features from all features. It combines multiple “weak classifiers” (best features) into one “strong classifier”. The generated “strong classifier” is basically the linear combination of all “weak classifiers”.

· Cascade Classifier: It is a method for combining increasingly more complex classifiers like AdaBoost in a cascade which allows negative input (non-face) to be quickly discarded while spending more computation on promising or positive face-like regions. It significantly reduces the computation time and makes the process more efficient.

OpenCV comes with lots of pre-trained classifiers. Those XML files can be loaded by cascadeClassifier method of the cv2 module. Here we are going to use haarcascade_frontalface_default.xml for detecting faces.


Code:

 import cv2

Importing OpenCV: The first step is importing the OpenCV library. It's commonly imported with the alias cv2.

image = cv2.imread('Images/image1.jpg')

Loading the Image: The code reads an image named 'image1.jpg' using the cv2.imread() function and assigns it to the variable image.

if image is None:
    print("Error: Unable to read the image.")
else:
    print("Image shape:", image.shape)

Error Handling for Image Loading: The code checks whether the image was loaded successfully or not. If the image is loaded successfully, it prints the shape of the image; otherwise, it prints an error message.

image = cv2.resize(image, (800, 600))
image.shape

Resizing the Image if reqired: The code resizes the loaded image to a width of 800 pixels and a height of 600 pixels using the cv2.resize() function.

#To Display image
if image is not None:
    # Display the image
    cv2.imshow('image', image)

    # Wait for a key press and close the window
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("Error: Unable to read the image.")

Displaying the Resized Image: The code displays the resized image using cv2.imshow(), waits for a key press using cv2.waitKey(), and then closes the window using cv2.destroyAllWindows().

image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Converting Image to Grayscale: The code converts the resized image to grayscale using the cv2.cvtColor() function.

#To Display image
if image_gray is not None:
    # Display the image
    cv2.imshow('image_gray', image_gray)

    # Wait for a key press and close the window
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("Error: Unable to read the image.")

Displaying the Grayscale Image: Similar to displaying the colored image, the code displays the grayscale image using cv2.imshow().

image.shape
image_gray.shape

Printing Shapes of Original and Grayscale Images: Finally, the code prints the shapes of the original and grayscale images.

face_detector = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')

Loading Haarcascade Classifier: The code loads a pre-trained Haar cascade classifier for detecting faces. This classifier is loaded using the CascadeClassifier function with the XML file path of the classifier.

detections = face_detector.detectMultiScale(image_gray)

Detecting Faces: The detectMultiScale() function is used to detect faces in the grayscale image (image_gray). It returns a list of rectangles where it detected faces.

numnber_of_faces=len(detections)
numnber_of_faces

Number of Faces Detected: The number of faces detected is calculated by finding the length of the detections list.

for (x, y, w, h) in detections:
  #print(x, y, w, h)
  cv2.rectangle(image, (x, y), (x + w, y + h), (0,255,255), 5)


#To Display image
if image is not None:
    # Display the image
    cv2.imshow('image', image)

    # Wait for a key press and close the window
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("Error: Unable to read the image.")

 

  • This loop iterates over each detection (each rectangle) and draws a rectangle around the detected face on the original image image. It uses cv2.rectangle to draw rectangles with the given coordinates and color (0,255,255) (which corresponds to yellow in BGR color space) and a thickness of 5 pixels and diaplay it.

 

Haarcascade parameters

##Haarcascade parameters
image = cv2.imread('Images/image1.jpg')
image = cv2.resize(image, (800, 600))
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
detections = face_detector.detectMultiScale(image_gray, scaleFactor = 1.09)
for (x, y, w, h) in detections:
  cv2.rectangle(image, (x, y), (x + w, y + h), (0,255,0), 5)


#To Display image
if image is not None:
    # Display the image
    cv2.imshow('image', image)

    # Wait for a key press and close the window
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("Error: Unable to read the image.")

 

  • This line detects faces in the grayscale image (image_gray) using the detectMultiScale function of the face detector.
  • The scaleFactor parameter adjusts the image scale to compensate for objects that may appear smaller due to perspective. In this case, it's set to 1.09, which means the algorithm will slightly increase the size of the image for more accurate detection.

 

image = cv2.imread('Images/image1.jpg')
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
detections = face_detector.detectMultiScale(image_gray, scaleFactor=1.2, minNeighbors=7,minSize=(20,20), maxSize=(100,100))

for (x, y, w, h) in detections:
  print(w, h)
  cv2.rectangle(image, (x, y), (x + w, y + h), (0,255,0), 2)


#To Display image
if image is not None:
    # Display the image
    cv2.imshow('image', image)

    # Wait for a key press and close the window
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("Error: Unable to read the image.")

 

  • This line detects faces in the grayscale image (image_gray) using the detectMultiScale function of the face detector.
  • The scaleFactor parameter adjusts the image scale to compensate for objects that may appear smaller due to perspective. In this case, it's set to 1.2.
  • The minNeighbors parameter specifies how many neighbors each candidate rectangle should have to retain it. Higher values result in fewer detections but with higher quality.
  • The minSize parameter specifies the minimum possible object size. Any detected object smaller than this size will be ignored.
  • The maxSize parameter specifies the maximum possible object size. Any detected object larger than this size will be ignored.

 


Full Code: https://github.com/TejasShastrakar/Computer_Vision.git

   

评论

                   

 

  我说两句…  
     

尚无评论。

抢先评论。

 

喜欢这篇文章?

订阅,就不会错过任何一期。

 

 

标签:detection,image,cv2,face,CascadeClassifier,opencv,time,frame
From: https://www.cnblogs.com/lightsong/p/18615660

相关文章

  • OpenCV零基础入门(3):ROI区域|颜色通道提取|边界填充|图像融合
    本文主要内容如下:ROI区域颜色通道提取边界填充图像融合1.截取部分图像数据(ROI区域)ROI(RegionofInterest),即感兴趣区域,是指在图像处理和计算机视觉中,从被处理的图像中以方框、圆、椭圆、不规则多边形等方式勾勒出的需要处理的区域。在机器视觉、图像处理等领域,ROI是一个重......
  • opencv Hough圆检测实现圆形表计在画面中位置的检测
    前言提醒:文章内容为方便作者自己后日复习与查阅而进行的书写与发布,其中引用内容都会使用链接表明出处(如有侵权问题,请及时联系)。其中内容多为一次书写,缺少检查与订正,如有问题或其他拓展及意见建议,欢迎评论区讨论交流。文章目录前言代码与运行结果Hough检测相关知识H......
  • Python+OpenCV系列:AI看图识人、识车、识万物
    在人工智能风靡全球的今天,用Python和OpenCV结合机器学习实现物体识别,不仅是酷炫技能,更是掌握未来的敲门砖。本篇博文手把手教你如何通过摄像头或图片输入,识别人、动物、车辆及其他物品,让你的程序瞬间具备AI能力。一、什么是物体识别?物体识别是计算机视觉中的关键任......
  • python opencv车牌图像校正
    车牌图片代码#-*-coding:UTF-8-*-importcv2importnumpyasnp#预处理defimgProcess(path):img=cv2.imread(path)#统一规定大小img=cv2.resize(img,(640,480))#高斯模糊img_Gas=cv2.GaussianBlur(img,(5,5),0)#RGB......
  • 学习笔记 | OpenCV的安装及其主要模块
    Open Source ComputerVision Library|开源的计算机视觉库官网:https://opencv.org/帮助文档:https://docs.opencv.org/4.x/index.htmlOpenCV是一个完整的计算机视觉处理框架。OpenCV的安装#方式一:cmd命令行安装pip3installopencv-python#方式二:从镜像源下载:pip......
  • OpenCV与AI深度学习 | 基于YOLO和EasyOCR从视频中识别车牌
    本文来源公众号“OpenCV与AI深度学习”,仅用于学术分享,侵权删,干货满满。原文链接:基于YOLO和EasyOCR从视频中识别车牌 在本文中,我们将探讨如何使用Python中的YOLO(YouOnlyLookOnce)和EasyOCR(OpticalCharacterRecognition)从视频文件中实现车牌检测。这种方法利用深度学......
  • opencv-python连接计算机摄像头,连接手机摄像头
    前言提醒:文章内容为方便作者自己后日复习与查阅而进行的书写与发布,其中引用内容都会使用链接表明出处(如有侵权问题,请及时联系)。其中内容多为一次书写,缺少检查与订正,如有问题或其他拓展及意见建议,欢迎评论区讨论交流。文章目录前言环境搭建读取文件夹中图片并显示打......
  • VS2022 配置openCV方法
    第一步下载opencv库解压出来这里不做过多讲解 第二步配置环境变量  %path%\build\x64\vc16\bin   %path%这个替换成自己的路径 然后打开项目属性设置点击VC++目录链接器、输入、附件依赖分别添加 前面的是我自己的目录换成你们自己目录即可第一步添加库目录D......
  • Python+OpenCV系列:膨胀和腐蚀——图像形态学操作深度解析
    文章目录什么是膨胀(Dilation)?什么是腐蚀(Erosion)?膨胀和腐蚀如何工作?如何在Python中使用OpenCV实现膨胀和腐蚀?**1.图像膨胀****2.图像腐蚀****3.膨胀与腐蚀的组合使用****调整结构元素****应用场景****总结**在计算机视觉中,图像形态学操作是一类非常重要的图像处理......
  • 基于米尔全志T527开发板的OpenCV进行手势识别方案
    本文将介绍基于米尔电子MYD-LT527开发板(米尔基于全志T527开发板)的OpenCV手势识别方案测试。摘自优秀创作者-小火苗米尔基于全志T527开发板 一、软件环境安装1.安装OpenCVsudo apt-get install libopencv-dev python3-opencv  2.安装pipsudo apt-get install......