首页 > 解决方案 > 在 OpenCV 中写入几帧视频

问题描述

我有一个将近 60 秒的视频,我想在不同的帧中应用不同的视频处理技术。我正在尝试选择不同的帧,但出现语法错误。这是我到目前为止所写的。

选择不同帧的函数:

def between(cap, lower: int, upper: int):
    return lower <= int(cap.get(cv.CAP_PROP_POS_FRAMES)) < upper

在此之后,我想选择不同的框架并应用不同的技术。例如,在下面的前 90 帧中,我转为灰度,从第 91 帧到第 150 帧,我想写一些文本,但是在第二次选择的帧中,我遇到了语法错误。

 while cap.isOpened():
    ret, frame = cap.read()
       if not ret:
       print("Can't receive frame (stream end?). Exiting ...")
       break
 if between(cap, 0, 90):
           frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
           frame = cv.cvtColor(frame, cv.COLOR_GRAY2BGR)  
        
 if between(cap, 90, 150)           
 # Our operations on the frame come here
           font = cv.FONT_HERSHEY_SIMPLEX 
           cv.putText(frame,  
             'TEXT ON VIDEO',  
             (50, 50),  
             font, 1,  
             (0, 255, 255),  
             2,  
             cv.LINE_4)
              pass
    # write the flipped frame
 out.write(frame)
 cv.imshow('frame', frame)
 if cv.waitKey(1) == ord('q'):
     break
 cap.release()
 out.release()
 cv.destroyAllWindows()

关于如何构造我的代码以连续选择下一帧的任何想法。

标签: pythonopencvvideovideo-processingframes

解决方案


推荐阅读