首页 > 解决方案 > 使用闭路电视或 IP 摄像头的 OpenCV 慢速人脸检测

问题描述

当我尝试使用我的笔记本电脑或计算机网络摄像头检测面部时,它工作正常,但是当我尝试使用 IP 摄像头检测时,它看起来需要很长时间才能检测到一帧。有什么解决方案吗,因为我也尝试过 YOLO。它比 opencv haar cascade 花费更多时间

那里我有一个简单的代码来检测面部和裁剪而不是框架的一部分。

cap = cv2.VideoCapture("web_Cam_IP")

cropScal = 25


while(True):
    # Capture frame-by-frame
    for i in range(10): #this loop skip 10 frames if I don't skip frame it looks like it stack there
        ret, frame = cap.read()

    frame = cv2.resize(frame, (0, 0), fx=0.70, fy=0.70)

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    faces = faceCascade.detectMultiScale(gray, scaleFactor=1.02, minNeighbors=5, minSize=(30, 30))


    for (x, y, w, h) in faces:    
        if len(faces) > 0 :
            try:
                img = gray[y-cropScal:y+h+cropScal, x-cropScal:x+w+cropScal]
                img = cv2.resize(img,(200,200))
                img = Image.fromarray(img)
                img.save('images/'+datetime.now().strftime("%d_%m_%Y_%I_%M_%S_%p")+'.png')
            except Exception as e:
                pass
        cv2.rectangle(gray, (x-cropScal, y-cropScal), (x+w+cropScal, y+h+cropScal), (0, 255, 0), 2)



    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

标签: pythonopencvmachine-learningimage-processingyolo

解决方案


您只是将输入帧缩放 0.70 倍,而不是绝对分辨率。您的 IP 摄像头可能比网络摄像头具有更高的分辨率,因此检测需要更多时间来分析更大的帧。

尝试在人脸检测之前将帧重新缩放到确定的大小(例如 800x600)。


推荐阅读