Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在获取检测到的面数_Python_Opencv_Haar Classifier - Fatal编程技术网

Python 正在获取检测到的面数

Python 正在获取检测到的面数,python,opencv,haar-classifier,Python,Opencv,Haar Classifier,我写了一个人脸检测代码,但我的目标是获得haar cascades检测到的人脸数量,并在每个人脸上打印人脸数量。我使用face.shape来获得人脸的形状,它给出了人脸的数量和坐标。打印face.shape的第一个参数将给出总的面数。关键是要得到每个面的数量。比如1,2,3写在不同的脸上。我需要一些算法。谢谢 与其给我的帖子发帖子,不如来谈谈哪里出了问题,这样我就可以改进它了 cap = cv2.VideoCapture(0) # Set the camera to use while Tru

我写了一个人脸检测代码,但我的目标是获得haar cascades检测到的人脸数量,并在每个人脸上打印人脸数量。我使用face.shape来获得人脸的形状,它给出了人脸的数量和坐标。打印face.shape的第一个参数将给出总的面数。关键是要得到每个面的数量。比如1,2,3写在不同的脸上。我需要一些算法。谢谢

与其给我的帖子发帖子,不如来谈谈哪里出了问题,这样我就可以改进它了

cap = cv2.VideoCapture(0) # Set the camera to use

while True:
    ret, frame = cap.read()    # For image as an input -> frame = cv2.imread(r'C:\Users\ASUS\Desktop\Novumare Technologies\crowded.mp4')
    if ret is True:
        #start_time = time.time()
        frame = cv2.resize(frame, (640, 360))    # Downscale to improve frame rate
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)    # Haar-cascade classifier needs a grayscale image
    else:
        break

    # Capturing and writing on detected bodies
    bodies = body_cascade.detectMultiScale(gray, 1.2, 5)
    for(x, y, w, h) in bodies:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        cv2.putText(frame, 'Human', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,255), 2)
    # Capturing and writing on detected faces
    faces = face_cascade.detectMultiScale(gray)
    for(ex, ey, ew, eh) in faces:
        cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 0, 255), 2)
        cv2.putText(frame, 'Face #' + str(faces.shape[0]), (ex, ey), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 2)
        print(faces)
        print('------------')
        print(faces.shape)

    # Open here when you save the output -> out.write(frame)
    #end_time = time.time()
    #print("Elapsed time: ", end_time-start_time)
    cv2.putText(frame, "Number of faces detected: " + str(faces.shape[0]), 
    (0,frame.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,0), 1)
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):   # Exit condition
        break

# Release everything if job is finished
cap.release()
# Open here when you save the output -> out.release()
cv2.destroyAllWindows()

由于面总数为len(面),因此您可以自己计算每个面数。您可以使用
面部计数来完成此操作

face_count = 0 # edit it to 1 if you want the number to start with 1
for(ex, ey, ew, eh) in faces:
    cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 0, 255), 2)
    cv2.putText(frame, 'Face #' + str(face_count), (ex, ey), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,0), 2)
    print(faces)
    print('------------')
    print(faces.shape)
    face_count += 1

欢迎来到stackoverflow!请拿起,仔细阅读,并提供一个复制您的问题。我使用的是完全相同的东西。但它给出了人脸的总数。但我需要像第一张脸、第二张脸、第三张脸等一样数一数。我现在看到你的代码了。我刚刚编辑了我的答案。你是最棒的!因此,每次haar_cascade检测到一张脸时,循环都会被激活并增加计数器。迅速的谢谢你的帮助。