最近嘗試做一個人臉檢測控制機器人來測體溫,記錄一下過程:
1、首先使用了opencv-ython試了一下人臉檢測,參考https://blog.csdn.net/qq_32892383/article/details/90732916
先跑了一下固定圖片的,效果堪憂啊
代碼:
# -*- coding: utf-8 -*-
import c2
import lgging
# 設置日誌
logging.basicConfig(level = logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
# 待檢測的圖片路徑
ImagePath = '1.jpg'
# 讀取圖片
logger.info('Reading image...')
image = cv2.imread(ImagePath)
# 把圖片轉換爲灰度模式
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 探測圖片中的人臉
logger.info('Detect faces...')
# 獲取訓練好的人臉的參數數據,進行人臉檢測
face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.15,minNeighbors=5,minSize=(3, 3))
search_info = "Find %d face."%len(faces) if len(faces) <= 1 else "Find %d faces."%len(faces)
logger.info(search_info)
# 繪製人臉的矩形區域(紅色邊框)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x,y), (x+w,y+h), (0,0,255), 2)
# 顯示圖片
cv2.imshow('Find faces!', image)
cv2.waitKey(0)
ros環境下接收電腦攝像頭的圖像話題的實時檢測代碼:
# -*- coding: utf-8 -*-
#opencv固定圖片檢測
#2020.2.13
#by躍動的風
import cv2
import logging
import rospy
import roslib
from sensor_msgs.msg import Image
from geometry_msgs.msg import Pose
from cv_bridge import CvBridge,CvBridgeError
def ImageCB(data):
bridge = CvBridge()
image = bridge.imgmsg_to_cv2(data,"bgr8")
# 把圖片轉換爲灰度模式
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 探測圖片中的人臉
#logger.info('Detect faces...')
# 獲取訓練好的人臉的參數數據,進行人臉檢測
face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.15,minNeighbors=5,minSize=(3, 3))
search_info = "Find %d face."%len(faces) if len(faces) <= 1 else "Find %d faces."%len(faces)
rospy.loginfo(search_info)
# 繪製人臉的矩形區域(紅色邊框)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x,y), (x+w,y+h), (0,0,255), 2)
cv2.imshow('Find faces!', image)
cv2.waitKey(1)
##ROS Node
if __name__ == '__main__':
#face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')
try:
rospy.init_node('face_detect', anonymous=True)
rospy.loginfo('face_detect start...')
pub = rospy.Publisher('/face_detection', Image, queue_size = 10)
pub_face_coo = rospy.Publisher('/face_coo', Pose, queue_size = 3)
rospy.Subscriber('/usb_cam/image_raw',Image, ImageCB)
rospy.spin()
except:
pass
2,使用dlib檢測
參考https://blog.csdn.net/Nirvana_6174/article/details/89599136
效果尚可,但是不能檢測戴口罩的人臉
靜態圖片檢測代碼
import dlib
import numpy as np
import cv2
def rect_to_bb(rect): # 獲得人臉矩形的座標信息
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
return (x, y, w, h)
def resize(image, width=1200): # 將待檢測的image進行resize
r = width * 1.0 / image.shape[1]
dim = (width, int(image.shape[0] * r))
resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
return resized
def detect():
image_file = "1.jpg"
detector = dlib.get_frontal_face_detector()
image = cv2.imread(image_file)
image = resize(image, width=1200)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
for (i, rect) in enumerate(rects):
(x, y, w, h) = rect_to_bb(rect)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, "Face: {}".format(i + 1), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow("Output", image)
cv2.waitKey(1)
if __name__ == "__main__":
while 1:
detect()
if cv2.waitKey(1) & 0xFF == ord("q"):
break
3,使用openpose檢測
效果極佳,就是環境配置很費勁。放一張效果圖,檢測戴口罩的毫無壓力:
具體安裝方法見另一篇文章https://blog.csdn.net/qq_23670601/article/details/104345718。