如何在检测到人脸时添加“检测到人脸”?

这是我写的代码,我想在终端上显示检测到的人脸数量,我尝试了一些方法(如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 webcamwebcam = cv2.VideoCapture(1)# iterate forever over frameswhile 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)        continuewebcam.release()

回答:

为了在终端打印检测到的人脸数量,我尝试了统计不同的检测到的人脸,并根据此在终端打印检测到的人数。我对你的代码做了一些小的修改如下。

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 webcamwebcam = cv2.VideoCapture(0)# iterate forever over frameswhile 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()

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注