element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Robotics
  • Technologies
  • More
Robotics
Blog ROS2 Learning Series - Blog 7 - Advanced Programming - Part 2
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Robotics to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: crisdeodates
  • Date Created: 13 Apr 2024 5:56 PM Date Created
  • Views 850 views
  • Likes 3 likes
  • Comments 2 comments
  • Robot operating System
  • robotics
  • ROS2
  • ROS
Related
Recommended

ROS2 Learning Series - Blog 7 - Advanced Programming - Part 2

crisdeodates
crisdeodates
13 Apr 2024
ROS2 Learning Series - Blog 7 - Advanced Programming - Part 2

ROS2 Face Detection Project

It is now time for us to go a bit more further.

We will modify and upgrade the camera stream publisher node from our camera stream project to capture a video stream from a camera, detect the faces inside the stream, overlay a rectangle over each detected faces and publish the overlayed image to the video stream topic.

We will also reuse the camera stream subscriber node to subscribe to the camera topic and display it in an OpenCV window. 

Create a new package with dependencies. 

$ cd ~/ros2_ws/src 
$ ros2 pkg create --build-type ament_python face_detector --dependencies rclpy image_transport cv_bridge sensor_msgs std_msgs opencv-python 

 

Create the face_detector_publisher.py file and populate it with the following code: 

import rclpy 
from rclpy.node import Node 
from sensor_msgs.msg import Image 
from cv_bridge import CvBridge 
import cv2 
import os 
 
class FaceDetectorPub(Node): 
    """ 
    Create a FaceDetectorPub class, which is a subclass of the Node class. 
    """ 
    def __init__(self): 
        """ 
        Class constructor to set up the node. 
        """ 
        # Initiate the Node class's constructor and give it a name. 
        super().__init__('face_detector_pub') 
         
        # Create the publisher. This publisher will publish an Image 
        # to the video_frame_data topic. The queue size is 10 messages. 
        self.publisher_ = self.create_publisher(Image, 'video_frame_data', 10) 
         
        # We will publish a message every 0.1 seconds. 
        timer_period = 0.1  # seconds 
         
        # Create the timer. 
        self.timer = self.create_timer(timer_period, self.timer_callback) 
             
        # Create a VideoCapture object. 
        # The argument '0' gets the default webcam. 
        self.cap = cv2.VideoCapture(0) 
             
        # Used to convert between ROS and OpenCV images. 
        self.br = CvBridge() 
 
        # Load the haar cascade classifier. 
        self.haar_path = os.path.expanduser('~') + '/ros2_ws/src/face_detector/resource/haar_classifier.xml' 
        self.face_cascade = cv2.CascadeClassifier(self.haar_path) 
         
    def timer_callback(self): 
        """ 
        Callback function. 
        This function gets called every 0.1 seconds. 
        """ 
        # Capture frame-by-frame. 
        # This method returns True/False as well 
        # as the video frame. 
        ret, frame = self.cap.read() 
 
        # Convert to gray scale image. 
        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
 
        ## Detect faces & returns positions of faces as Rect(x,y,w,h). 
        face_rects = self.face_cascade.detectMultiScale(frame_gray, 1.3, 5) 
 
        # Draw rectangles representing the detected faces. 
        for (x, y, w, h) in face_rects: 
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) 
 
        if ret == True: 
            # Publish the image. 
            # The 'cv2_to_imgmsg' method converts an OpenCV 
            # image to a ROS 2 image message. 
            self.publisher_.publish(self.br.cv2_to_imgmsg(frame)) 
 
    def release_camera(self): 
        """ 
        Release the camera when done. 
        """ 
        self.cap.release() 
     
def main(args=None): 
    """ 
    Main function to initialize the node and start the face detector publisher. 
    """ 
    # Initialize the rclpy library. 
    rclpy.init(args=args) 
     
    # Create the node. 
    face_detector_publisher_node = FaceDetectorPub() 
     
    # Spin the node so the callback function is called. 
    try: 
        rclpy.spin(face_detector_publisher_node) 
    finally: 
        # Release the camera. 
        face_detector_publisher_node.release_camera() 
     
    # Destroy the node explicitly. 
    face_detector_publisher_node.destroy_node() 
     
    # Shutdown the ROS client library for Python. 
    rclpy.shutdown() 
 
if __name__ == '__main__': 
    main() 

 

For the face detection, we will use a machine learning based approach called haar cascade.

Even though it is particularly popular for detecting faces, it can be trained to detect other objects as well.

This approach has the ability to rapidly evaluate features at multiple scales and efficiently reject background regions.

OpenCV includes support for haar cascades for object detection, including face detection.

This is carried out using pre-trained Haar cascade classifiers for various objects, including faces.

Trained face detection cascade values are provided in the form of an xml file. You can download the xml from here. 

Put this haar cascade xml file in the resource folder inside the package. 

 

Add the entry points to setup.py inside console_scripts section: 

'face_detector_pub = face_detector.face_detector_publisher:main', 

 

Since we already specified our dependencies during the package creation itself, we do not need to edit the package.xml file. 

 

Build the package. 

$ cd ~/ros2_ws 
$ colcon build --packages-select face_detector 

 

Source the new package. 

$ source install/setup.bash 

 

Run the face detector publisher node to start publishing the camera frames. 

$ ros2 run face_detector face_detector_pub 

 

Run the camera stream subscriber node in a separate terminal. 

$ cd ~/ros2_ws 
$ source install/setup.bash

$ ros2 run camera_stream camera_stream_sub 

  

The camera frames with the detected faces will be displayed in an OpenCV window. 

 image

 

Press ESC on the active OpenCV window to exit. 

  • Sign in to reply
  • crisdeodates
    crisdeodates over 1 year ago in reply to DAB

    Haar Cascade can only be trained to identify the matching shape and size or similar features. It can't be used for face recognition.
    But we could definitely use Deep Learning techniques to train a model to detect a specific person or multiple persons.
    As you noticed, a way is to extract the detected face and feed it into a trained model for recognition.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 1 year ago

    So at this point it will just highlight that you have a face or object that you have preset.

    The next step could be to take the face and compare it to a specific face for ID or tracking.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube