人脸检测:一次只显示一张脸
Face Detection: show only one face at a time
我一直在开发一个与 ROS 集成的人脸检测应用程序,以与 DRONES(准确地说是 bebop 鹦鹉)一起使用。代码本身可以在 google 上找到,它的作用基本上是显示出现在屏幕上的每一张脸。我的问题是:我希望无人机跟随我的脸(而且只跟随我的脸),但正如我之前所说,代码可以同时检测多张脸。
这是代码:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# some people can just use the haarcascade_frontalface_default.xml without specifying the path
test = face_cascade.load('../../../../opencv/data/haarcascades/haarcascade_frontalface_default.xml')
# start the video capture
video_capture = cv2.VideoCapture(0)
# while-loop to detect face on webcam until you press 'q'.
while not rospy.is_shutdown():
# Capture frame-by-frame
ret, frame = video_capture.read()
frame = imutils.resize(frame, width=600)
#convert the frame (of the webcam) to gray)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
mask = cv2.erode(gray, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# detecting the faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
x_imagem = x + (w/2)
y_imagem = y + (h/2)
cv2.circle(frame, (x+(w/2), y+(h/2)), 5, (0,0,255), -1)
#600 x 450;
if(x_imagem > 200 and x_imagem < 400):
rospy.loginfo("CENTRO")
elif(x_imagem < 200): #ROSTO PRA ESQUERDA, ENTAO VAI PARA DIREITA
rospy.loginfo("ROSTO NA ESQUERDA")
pub_face.publish("esq")
elif(x_imagem > 400): #ROSTO PRA DIREITA, ENTAO VAI PARA ESQUERDA
rospy.loginfo("ROSTO NA DIREITA")
pub_face.publish("dir")
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
rospy.loginfo("FIM DO PROGRAMA DE DETECCAO DE ROSTOS")
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
如您所见,我们在以下位置有一组面孔:
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
代码会在人脸所在的位置绘制一个矩形,有没有办法让它只显示最大的矩形(在这种情况下,就是我的脸)?
希望我能说清楚!
Is there a way that I can make it show only the biggest rectangle (in that case, my face)?
迭代思想'faces'的列表并计算每个感兴趣区域(ROI)的面积,并抓住最大的面积。
我一直在开发一个与 ROS 集成的人脸检测应用程序,以与 DRONES(准确地说是 bebop 鹦鹉)一起使用。代码本身可以在 google 上找到,它的作用基本上是显示出现在屏幕上的每一张脸。我的问题是:我希望无人机跟随我的脸(而且只跟随我的脸),但正如我之前所说,代码可以同时检测多张脸。 这是代码:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# some people can just use the haarcascade_frontalface_default.xml without specifying the path
test = face_cascade.load('../../../../opencv/data/haarcascades/haarcascade_frontalface_default.xml')
# start the video capture
video_capture = cv2.VideoCapture(0)
# while-loop to detect face on webcam until you press 'q'.
while not rospy.is_shutdown():
# Capture frame-by-frame
ret, frame = video_capture.read()
frame = imutils.resize(frame, width=600)
#convert the frame (of the webcam) to gray)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
mask = cv2.erode(gray, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# detecting the faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
x_imagem = x + (w/2)
y_imagem = y + (h/2)
cv2.circle(frame, (x+(w/2), y+(h/2)), 5, (0,0,255), -1)
#600 x 450;
if(x_imagem > 200 and x_imagem < 400):
rospy.loginfo("CENTRO")
elif(x_imagem < 200): #ROSTO PRA ESQUERDA, ENTAO VAI PARA DIREITA
rospy.loginfo("ROSTO NA ESQUERDA")
pub_face.publish("esq")
elif(x_imagem > 400): #ROSTO PRA DIREITA, ENTAO VAI PARA ESQUERDA
rospy.loginfo("ROSTO NA DIREITA")
pub_face.publish("dir")
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
rospy.loginfo("FIM DO PROGRAMA DE DETECCAO DE ROSTOS")
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
如您所见,我们在以下位置有一组面孔:
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
代码会在人脸所在的位置绘制一个矩形,有没有办法让它只显示最大的矩形(在这种情况下,就是我的脸)? 希望我能说清楚!
Is there a way that I can make it show only the biggest rectangle (in that case, my face)?
迭代思想'faces'的列表并计算每个感兴趣区域(ROI)的面积,并抓住最大的面积。