我OpenCV在 Python 中使用從網路攝像頭錄制視頻。我正在嘗試為視頻錄制添加時間戳。
問題: 我想要實作的是當視頻錄制開始時,當前系統時間戳應該放在視頻上。當錄制結束時,應該在視頻上添加另一個時間戳。這樣,視頻錄制將在錄制的視頻上有兩個時間戳(開始時間和結束時間)。
下面編輯的內容,以捍衛所提出的問題有重點,不得關閉。
我只能在視頻上添加一個連續運行的時間戳。我需要static視頻上的時間戳,即,一旦視頻開始,將放置一個開始時間戳,beginning video-frame當視頻結束時,添加另一個時間戳以反映視頻結束的時間。
import cv2
import time
import datetime
fps = 24
width = 864
height = 640
# Define the codec and create VideoWriter object
font = cv2.FONT_HERSHEY_SIMPLEX
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
ret = cap.set(3, 864)
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
video_file = "outputFile.avi"
# Create a video write before entering the loop
video_writer = cv2.VideoWriter(
video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
# Process until end.
while (cap.isOpened()):
ret, frame = cap.read()
if ret:
# describe the type of font you want to display
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
# Get date and time and save it inside a variable
dt = str(datetime.datetime.now())
# put the dt variable over the video frame
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
# write video to disk
video_writer.write(frame)
# show the video
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
# define the key to close the window
if key == 'q' or key == 27:
break
else:
break
# release the vid object
cap.release()
# close all the opened windows.
cv2.destroyAllWindows()
uj5u.com熱心網友回復:
假設我們不知道最后一幀何時到來,我們可能會一直將前一幀寫入檔案,而何時ret == False(最后一幀)將文本放在最后一幀并寫入最后一幀。
識別第一幀很簡單。
識別最后一幀的時間key == 'q' or key == 27也很簡單。
唯一的問題是在ret == False.
為了處理這種情況,我們可以使用一幀的緩沖區。
繼續寫上一幀...
時ret == False,我們知道最后一幀是最后一幀,所以我們將文本添加到最后一幀,并將其寫入視頻檔案。
注意:
我們需要video_writer.release()在最后添加。
代碼示例:
import cv2
import time
import datetime
fps = 24
width = 864
height = 640
# Define the codec and create VideoWriter object
font = cv2.FONT_HERSHEY_SIMPLEX
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
ret = cap.set(3, 864)
#cap = cv2.VideoCapture('input.mp4') # Read from file instead of Camera - simpler to debug.
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
video_file = "outputFile.avi"
# Create a video write before entering the loop
video_writer = cv2.VideoWriter(
video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
dt = None
frame = None
prev_frame = None
# Process until end.
while (cap.isOpened()):
if frame is not None:
prev_frame = frame.copy()
ret, frame = cap.read()
if ret:
# describe the type of font you want to display
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
# Get date and time and save it inside a variable
prev_dt = dt # Keep the previous dt
dt = str(datetime.datetime.now())
# put the dt variable over the video frame
if prev_frame is None:
# Firt frame:
# If prev_frame is None, this is the first frame.
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
else:
# Write the previous frame instead of the new frame.
video_writer.write(prev_frame)
# write video to disk
#video_writer.write(frame)
# show the video
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
# define the key to close the window
if key == 'q' or key == 27:
# Last frame:
# Add the time, and write the last last frame.
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
video_writer.write(frame)
break
else:
# Last frame:
# Add the time, and write the last last frame.
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
video_writer.write(frame)
break
# release the vid object
cap.release()
video_writer.release() # Important
# close all the opened windows.
cv2.destroyAllWindows()
uj5u.com熱心網友回復:
抱歉,我誤讀了您的 putText。我正在使用樹莓派 4,Bullseye。使用相同的代碼。按住“q”鍵 5 秒鐘然后松開。我在塊條件下添加了一些片段 if-else。
import cv2
import time
import datetime
fps = 24
width = 864
height = 640
# Define the codec and create VideoWriter object
font = cv2.FONT_HERSHEY_SIMPLEX
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
ret = cap.set(3, 864)
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
video_file = "outputFile.avi"
# Create a video write before entering the loop
video_writer = cv2.VideoWriter(
video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
#cv2.namedWindow('frame',cv2.WINDOW_AUTOSIZE)
# Process until end.
while (cap.isOpened()):
ret, frame = cap.read()
if ret:
# describe the type of font you want to display
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
# Get date and time and save it inside a variable
dt = str(datetime.datetime.now())
# put the dt variable over the video frame
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
# write video to disk
video_writer.write(frame)
# show the video
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
# define the key to close the window
if key == 'q' or key == 27:
cv2.imshow('frame', frame)
cv2.waitKey(1)
#break
else:
#break
cv2.imshow('frame', frame)
cv2.waitKey(1)
#break
# release the vid object
cap.release()
# close all the opened windows.
cv2.destroyAllWindows()
編輯:我找到了更好的解決方案。不要使用相同的重復和中斷。我正在嘗試減少編碼。我將嘗試使用threading。這會很有幫助。
while (cap.isOpened()):
ret, frame = cap.read()
if ret:
# describe the type of font you want to display
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
# Get date and time and save it inside a variable
dt = str(datetime.datetime.now())
# put the dt variable over the video frame
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
# write video to disk
video_writer.write(frame)
# show the video
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
# define the key to close the window
if key == 'q':
#video_writer.write(frame)
cv2.imshow('frame', frame)
cv2.waitKey(5)
if key == 27:
break
# release the vid object
cap.release()
# close all the opened windows.
cv2.destroyAllWindows()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/462001.html
上一篇:允許用戶輸入影像和變換矩陣并查看應用的變換的OpenCV程式
下一篇:Tesseract檢測質量極低
