【ROS】OpenCV+ROS 實現人臉識別(Ubantu16.04)

專注的阿熊發表於2022-07-21

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import rospy

import cv2

import numpy as np

from sensor_msgs.msg import Image, RegionOfInterest

from cv_bridge import CvBridge, CvBridgeError

class faceDetector:

     def __init__(self):

         rospy.on_shutdown(self.cleanup);

         # 建立 cv_bridge

         self.bridge = CvBridge()

         self.image_pub = rospy.Publisher("cv_bridge_image", Image, queue_size=1)

         # 獲取 haar 特徵的級聯表的 XML 檔案,檔案路徑在 launch 檔案中傳入

         cascade_1 = rospy.get_param("~cascade_1", "")

         cascade_2 = rospy.get_param("~cascade_2", "")

         # 使用級聯表初始化 haar 特徵檢測器

         self.cascade_1 = cv2.CascadeClassifier(cascade_1)

         self.cascade_2 = cv2.CascadeClassifier(cascade_2)

         # 設定級聯表的引數,最佳化人臉識別,可以在 launch 檔案中重新配置

         self.haar_scaleFactor  = rospy.get_param("~haar_scaleFactor", 1.2)

         self.haar_minNeighbors = rospy.get_param("~haar_minNeighbors", 2)

         self.haar_minSize      = rospy.get_param("~haar_minSize", 40)

         self.haar_maxSize      = rospy.get_param("~haar_maxSize", 60)

         self.color = (50, 255, 50)

         # 初始化訂閱 rgb 格式影像資料的訂閱者,此處影像 topic 的話題名可以在 launch 檔案中重對映

         self.image_sub = rospy.Subscriber("input_rgb_image", Image, self.image_callback, queue_size=1)

     def image_callback(self, data):

         # 使用 cv_bridge ROS 的影像資料轉換成 OpenCV 的影像格式

         try:

             cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")     

             frame = np.array(cv_image, dtype=np.uint8)

         except CvBridgeError, e:

             print e

         # 建立灰度影像

         grey_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

         # 建立平衡直方圖,減少光線影響

         grey_image = cv2.equalizeHist(grey_image)

         # 嘗試檢測人臉

         faces_result = 外匯跟單gendan5.comself.detect_face(grey_image)

         # opencv 的視窗中框出所有人臉區域

         if len(faces_result)>0:

             for face in faces_result:

                 x, y, w, h = face

                 cv2.rectangle(cv_image, (x, y), (x+w, y+h), self.color, 2)

         # 將識別後的影像轉換成 ROS 訊息併發布

         self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))

     def detect_face(self, input_image):

         # 首先匹配正面人臉的模型

         if self.cascade_1:

             faces = self.cascade_1.detectMultiScale(input_image,

                     self.haar_scaleFactor,

                     self.haar_minNeighbors,

                     cv2.CASCADE_SCALE_IMAGE,

                     (self.haar_minSize, self.haar_maxSize))

         # 如果正面人臉匹配失敗,那麼就嘗試匹配側面人臉的模型

         if len(faces) == 0 and self.cascade_2:

             faces = self.cascade_2.detectMultiScale(input_image,

                     self.haar_scaleFactor,

                     self.haar_minNeighbors,

                     cv2.CASCADE_SCALE_IMAGE,

                     (self.haar_minSize, self.haar_maxSize))

         return faces

     def cleanup(self):

         print "Shutting down vision node."

         cv2.destroyAllWindows()

if __name__ == '__main__':

     try:

         # 初始化 ros 節點

         rospy.init_node("face_detector")

         faceDetector()

         rospy.loginfo("Face detector is started..")

         rospy.loginfo("Please subscribe the ROS image.")

         rospy.spin()

     except KeyboardInterrupt:

         print "Shutting down face detector node."

         cv2.destroyAllWindows()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2907015/,如需轉載,請註明出處,否則將追究法律責任。

相關文章