【问题标题】:Extract a frame at a specific time of a video and insert a text OpenCV Python在视频的特定时间提取帧并插入文本 OpenCV Python
【发布时间】:2019-11-23 12:12:18
【问题描述】:

所以我有一个持续时间为 15 秒的视频,并且在特定时间我需要插入一个文本字段。 到目前为止,我的代码只是读取视频并显示它。之后我们提取帧并计算每一帧的持续时间。

import cv2

import numpy as np

# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('my_baby_dog.mp4')

# Check if camera opened successfully
if (cap.isOpened() == False):
    print("Error opening video stream or file")

# Read until video is completed
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    if ret == True:
        fps = cap.get(cv2.CAP_PROP_FPS)  # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        duration = frame_count / fps

        print('fps = ' + str(fps))
        print('number of frames = ' + str(frame_count))
        print('duration (S) = ' + str(duration))
        minutes = int(duration / 60)
        seconds = duration % 60
        print('duration (M:S) = ' + str(minutes) + ':' + str(seconds))
        # Display the resulting frame
        frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        cv2.putText(img=frame, text='EKO', org=(int(frameWidth / 2 - 20), int(frameHeight / 2)),
                    fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=3,
                    color=(0, 255, 0))
        cv2.imshow('Frame', frame)
        # Press Q on keyboard to  exit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

    # Break the loop
    else:
        break



# When everything done, release the video capture object
cap.release()

# Closes all the frames
cv2.destroyAllWindows()

【问题讨论】:

  • 欢迎来到 SO。显然,您遇到了编程问题,因此您的帖子在这里是合适的,但是,您实际上并没有提出问题,而且我个人无法确定您的问题到底出在哪里。你能编辑你的问题吗?
  • 由于我的视频是 15 秒,我想在视频时间达到 6 秒时插入文本(将文本设置为特定时间)

标签: python opencv image-processing video-processing


【解决方案1】:

听起来您想在 6 秒后添加文本叠加层。假设此覆盖将一直持续到视频结束,您将需要添加一个 if 语句来比较 duration 与您的文本覆盖的 start time。 然后显示它。

import cv2
import numpy as np

start_time = 6

# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('my_baby_dog.mp4')

........
while (cap.isOpened()):
    ........
        # ADD OVERLAY TEXT
        if start_time < duration:
            cv2.putText(img=frame, text='EKO', org=(int(frameWidth / 2 - 20), int(frameHeight / 2)),
                    fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=3,
                    color=(0, 255, 0))
        cv2.imshow('Frame', frame)

同样你可以像这样开始和停止文本覆盖

import cv2
import numpy as np

start_time = 6
stop_time = 10
    ........
        # ADD OVERLAY TEXT
        if start_time < duration < stop_time:
            cv2.putText(img=frame, text='EKO', org=(int(frameWidth / 2 - 20), int(frameHeight / 2)),
                    fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=3,
                    color=(0, 255, 0))
        .......

【讨论】:

  • 只要间隔 [start_time,stop_time] 始终为真,只要这些值在视频的整个持续时间范围内,那么文本将始终在整个视频持续时间上弹出。我需要一个可以在我设置特定时间时在视频中搜索的功能。谢谢你的回答:)
  • 这篇文章将向您展示如何在视频中寻找。 Seeking using openCV
猜你喜欢
  • 2019-03-29
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
相关资源
最近更新 更多