【问题标题】:How can I add "a human face was found" if a face is detected?如果检测到人脸,如何添加“发现人脸”?
【发布时间】:2021-11-09 22:50:54
【问题描述】:

这是我写的代码,我希望能够在终端上显示找到了多少张面孔,我尝试了一些方法(if face_coordinates: cv2.imshow("a human was found", webcam) 等但是没有任何工作

import cv2

# load some pre-trained data on front faces (haarcascade algorithm)
trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# to capture video from webcam
webcam = cv2.VideoCapture(1)

# iterate forever over frames
while True:
    successful_frame_read, frame = webcam.read()
    #flip the video (mirror)
    flipped_frame = cv2.flip(frame, 1)
    # convert to grayscale
    grayscaled_img = cv2.cvtColor(flipped_frame, cv2.COLOR_BGR2GRAY)
    # detect faces
    face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
    # show rectangles around the face
    for (x, y, w, h) in face_coordinates:
        cv2.rectangle(flipped_frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    # show the webcam
    cv2.imshow("Fadi's face detector system", flipped_frame)
    key = cv2.waitKey(1)
    # exit app if Q or q are pressed
    if key==81 or key==113:
        break
    if face_coordinates:  # python types can be coerced to boolean
    cv2.imshow("Human was found!", webcam)
    continue
    else:
        cv2.imshow("no human was found...", webcam)
        continue

webcam.release()

【问题讨论】:

  • 如果face_coordinates 为空或无,则表示未检测到人脸 - 对吧?
  • 基本上,我发现如果一个列表为空,它将被设置为False,如果它包含值,它将被设置为True。是的,这就是它的意思!
  • 所以使用它并打印你想要的 - 在你这样做之后致电continue
  • 如何在终端打印?
  • if face_coordinates: # python 类型可以被强制转换为布尔值 cv2.imshow("Human was found!", webcam) continue else: cv2.imshow("no human was found...", webcam ) 继续

标签: python opencv machine-learning opencv3.0 face


【解决方案1】:

为了打印在终端中检测到的人脸数量,我尝试计算检测到的不同人脸,并在此基础上打印在终端中检测到的人的数量。我对您的代码做了一些小的改动,如下所示。

import cv2

# load some pre-trained data on front faces (haarcascade algorithm)
trained_face_data = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_frontalface_default.xml')

# to capture video from webcam
webcam = cv2.VideoCapture(0)

# iterate forever over frames
while True:
    successful_frame_read, frame = webcam.read()
    #flip the video (mirror)
    flipped_frame = cv2.flip(frame, 1)
    # convert to grayscale
    grayscaled_img = cv2.cvtColor(flipped_frame, cv2.COLOR_BGR2GRAY)
    # detect faces
    face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
    count=0
    # show rectangles around the face
    for (x, y, w, h) in face_coordinates:
        count=count+1
        cv2.rectangle(flipped_frame, (x, y), (x+w, y+h), (0, 255, 0), 3)

    # show the webcam
    cv2.imshow("Fadi's face detector system", flipped_frame)
    key = cv2.waitKey(1)
    # exit app if Q or q are pressed
    if key==81 or key==113:
        break
    # python types can be coerced to boolean
    if count==1:
        print(count," human found")
    elif count>0:
        print(count," humans found")
    else:
        print("No human was found")
webcam.release()

【讨论】:

  • 函数全局计数对我来说是新的,谢谢
  • @FadiEid 这里不需要声明变量count global。我的错。我将编辑代码。
猜你喜欢
  • 2013-09-24
  • 2019-11-18
  • 2019-02-27
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 2020-06-28
  • 2016-01-01
  • 2016-06-24
相关资源
最近更新 更多