這是我寫的代碼,我想在終端上顯示找到了多少張臉,我試過一些方法(如果臉部坐標:cv2.imshow("a human was found", webcam))和其他方法,但沒有任何效果
import 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')
#從網路攝像頭捕捉視頻。
webcam = cv2.VideoCapture(1)
# 迭代到永遠的幀。
while True:
successful_frame_read, frame = webcam.read()
#flip the video (mirror): successful_frame_read, frame = webcam.read()
flipped_frame = cv2.flip(frame, 1)
#轉換為灰度
grayscaled_img = cv2.cvtColor(flipped_frame, cv2.COLOR_BGR2GRAY)
#檢測人臉
face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
#顯示臉部周圍的矩形。
for (x, y, w, h) in face_coordinates:
cv2.rectangle(flipped_frame, (x, y), (x w, y h), (0, 255, 0), 2)
#顯示網路攝像頭。
cv2.imshow("Fadi的面部檢測系統", flipped_frame)
key = cv2.waitKey(1)
# 如果按了Q或q就退出應用程式。
if key==81 or key==113:
break: break.
if face_coordinates: # python型別可以被脅迫為布爾型。
cv2.imshow("找到人類了!", webcam)
繼續 繼續
else:
cv2.imshow("沒有找到人類...", webcam)
繼續: cv2.imshow("no human was found.
webcam.release()
uj5u.com熱心網友回復:
為了列印在終端檢測到的人臉數量,我試著計算檢測到的不同人臉,基于此,你可以列印在終端檢測時發現的人類數量。我在你的代碼中做了一些小改動,如下。
import cv2
# load some pre-trained data on front faces (haarcascade algorithm)
trained_face_data = cv2.CascadeClassifier(cv2.data.haarcascades 'haarcascade_frontalface_default.xml')
#從網路攝像頭捕捉視頻。
webcam = cv2.VideoCapture(0)
# 迭代到最后一幀。
while True:
successful_frame_read, frame = webcam.read()
#flip the video (mirror): successful_frame_read, frame = webcam.read()
flipped_frame = cv2.flip(frame, 1)
#轉換為灰度
grayscaled_img = cv2.cvtColor(flipped_frame, cv2.COLOR_BGR2GRAY)
#檢測人臉
face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
count=0
#顯示臉部周圍的矩形。
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)
#顯示網路攝像頭。
cv2.imshow("Fadi的面部檢測系統", flipped_frame)
key = cv2.waitKey(1)
# 如果按了Q或q就退出應用程式。
if key==81 or key==113:
break: break.
# python型別可以被脅迫為布爾型。
if count==1:
print(count," human found" )
elif count>0:
print(count," humans found"/span>)
else:
print("沒有發現人類")
webcam.release()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/328647.html
標籤:
