【问题标题】:lag while playing video while using opencv?使用opencv播放视频时延迟?
【发布时间】:2021-03-11 13:00:49
【问题描述】:

所以我使用 opencv 进行运动检测,当我使用网络摄像头进行运动检测时,程序运行完美,但是当我通过在 Videocapture() 函数中提供路径来使用其他视频时,输出滞后太多.我知道 waitkey() 在某种程度上对帧率负责,但这次似乎并非如此。我应该怎么做才能解决这个问题?

这是我的代码:

import cv2
import time
import  os
import time
video = cv2.VideoCapture("videofile.mp4")
_,frame0 = video.read()
frame0 = cv2.cvtColor(frame0,cv2.COLOR_BGR2GRAY)
frame0 = cv2.GaussianBlur(frame0,(3,3),1)
while video.isOpened():
  check, frame = video.read()
  if check == True:
    status = 0


    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray1 = cv2.GaussianBlur(gray,(3,3),1)
    diff = cv2.absdiff(frame0,gray1)
    

    _,thresh = cv2.threshold(diff,30,255,cv2.THRESH_BINARY)


    dilated = cv2.dilate(thresh, None, iterations=3)

    cascPath = os.path.dirname(cv2.__file__) + "/data/haarcascade_frontalface_default.xml"
    casc1Path = os.path.dirname(cv2.__file__) + "/data/haarcascade_fullbody.xml"
    casc2Path = os.path.dirname(cv2.__file__) + "/data/haarcascade_eye.xml"
    face_cascade = cv2.CascadeClassifier(cascPath)
    body_cascade = cv2.CascadeClassifier(casc1Path)
    face = face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
    body = body_cascade.detectMultiScale(gray,1.5,5)
    cnts, res = cv2.findContours(dilated,
                                 cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for (x,y,w,h) in body:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,255,255),2)


    for contour in cnts:
         if cv2.contourArea(contour) < 12000:
            continue
         
         (x, y, w, h) = cv2.boundingRect(contour)
         cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
         cv2.putText(frame, 'MOTION DETECTED', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 249, 0), 2)
    
    for (x ,y ,w ,h) in face:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
        cv2.putText(frame, 'HUMAN FACE', (x, y-10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
        eye_gray = gray[y:y+h,x:x+w]
        eye_color = frame[y:y+h,x:x+w]
        eyes_cascade = cv2.CascadeClassifier(casc2Path)
        eyes = eyes_cascade.detectMultiScale(eye_gray)
        for ex,ey,ew,eh in eyes:
            cv2.rectangle(eye_color,(ex, ey),(ex+ew,ey+eh),(0,0,255),1)
    seconds = time.ctime()
    cv2.putText(frame, 'Time:{}'.format(seconds), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 
    255), 2)
    width =  int(frame.shape[1]/3)
    height= int(frame.shape[0]/3)
    dimension = (width,height)
    frame = cv2.resize(frame, dimension, interpolation=cv2.INTER_AREA)

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




    video.release()

    cv2.destroyAllWindows()

请忽略错误的意图。 谢谢你。

【问题讨论】:

  • 设置输出视频的帧率

标签: python-3.x opencv opencv3.0


【解决方案1】:

主要问题:将级联分类器的所有初始化移到循环之外。为每一帧甚至每帧多次重新加载它们是不合理的(你的内部循环)。

其他问题:

  • 在读取任何帧之前,您应该只检查一次isOpened
  • 循环需要只是一个while True:,而不是重复检查isOpened
  • 如果check 是来自video.read() 的False,您应该立即中断循环,因为视频已经结束

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2014-01-08
    • 2013-06-08
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多