【问题标题】:OpenCV Human Body Detection through a webcam - No detections visible通过网络摄像头进行 OpenCV 人体检测 - 没有可见的检测
【发布时间】:2020-06-24 18:24:47
【问题描述】:

我正在尝试通过 OpenCV 检测人体。该代码不会引发错误。摄像头也会启动,但无法检测到任何东西。

import cv2

classifier = cv2.CascadeClassifier(r'C:\Users\dhruv\Desktop\DataScience\haarcascade_fullbody.xml')
 

video_captured = cv2.VideoCapture(0)
 
while (True):
   
    ret, frame = video_captured.read()
    frame = cv2.resize(frame,(640,360))
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
    # pass the frame to the classifier
    persons_detected = classifier.detectMultiScale(gray_frame)
 
    # check if people were detected on the frame

for (x, y, w, h) in persons_detected:
    cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 0, 0), 2)    
    cv2.imshow('Video footage', frame)
    
    if (cv2.waitKey(1) & 0xFF == ord('q')):
        break

#cv2.VideoCapture(0).release()

【问题讨论】:

    标签: python opencv object-detection


    【解决方案1】:

    我高度怀疑这是一个缩进错误。相机确实启动了,但您绘制人脸边界框的代码不在循环之外。只需缩进该代码。此外,您需要正确释放相机资源,否则下次运行代码时,相机将无法正确捕获帧。我已经更改了释放调用,以便它释放抓取的资源。

    import cv2
    
    classifier = cv2.CascadeClassifier(r'C:\Users\dhruv\Desktop\DataScience\haarcascade_fullbody.xml')
     
    
    video_captured = cv2.VideoCapture(0)
     
    while True:
       
        ret, frame = video_captured.read()
        frame = cv2.resize(frame,(640,360))
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     
        # pass the frame to the classifier
        persons_detected = classifier.detectMultiScale(gray_frame)
     
        # check if people were detected on the frame
    
        for (x, y, w, h) in persons_detected:
            cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 0, 0), 2)    
            cv2.imshow('Video footage', frame)
        
        if (cv2.waitKey(1) & 0xFF == ord('q')):
            break
    
    video_captured.release()  # Changed so that you're releasing the grabbed resource
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2016-01-18
      • 2011-08-23
      • 1970-01-01
      • 2014-11-22
      • 2013-02-14
      • 1970-01-01
      相关资源
      最近更新 更多