我正在嘗試使用 OpenCV 將提取的幀從 Deepstream 管道保存到視頻中,但我最終得到的只是一個 9KB 的檔案。
這是我的代碼(在探測函式中執行):
batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
l_frame = batch_meta.frame_meta_list
frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)
frame = pyds.get_nvds_buf_surface(hash(gst_buffer), frame_meta.batch_id)
frame_copy = np.array(frame, copy=True, order='C')
frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_RGBA2BGRA)
每次呼叫探測函式時都會執行上述代碼。影像保存到佇列中:
frame_buffer.put(frame_copy)
在將所需數量的幀推入佇列后,我使用以下代碼將緩沖的幀保存到視頻檔案中:
codec = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('out.avi', codec, fps, (output_width, output_height))
out.write(frame_copy)
total_frames = FRAME_RECORDING_THRESH
while total_frames > 0:
frame = frame_buffer.get()
frame = cv2.resize(frame, (output_width, output_height), interpolation = cv2.INTER_LINEAR)
out.write(frame)
total_frames -= 1
out.release()
不幸的是,生成的檔案不是有效的視頻檔案。在上述程序中我做錯了嗎?任何幫助將不勝感激。
PS只是為了測驗幀是否已正確存盤在佇列中,如果我嘗試將幀保存為while回圈中的影像:
cv2.imwrite(dest_folder '/' f'tmp{total_frames}.png', frame)
我得到正確保存和有效的 png 影像。
output_width, output_heightPS 2 幀在緩沖時的解析度為。此外,在保存它們之前嘗試做 acv2.resize不會改變任何東西。
uj5u.com熱心網友回復:
我無法測驗它,但常見的錯誤是人們認為比代碼更重要
out = cv2.VideoWriter('out.avi', codec, fps, (output_width, output_height))
會自動調整幀大小(output_width, output_height),但事實并非如此。
您必須手動調整框架的大小。
如果你不這樣做,那么Writer會跳過幀,你會得到損壞的檔案 - 沒有幀。
while total_frames > 0:
frame = frame_buffer.get()
frame = cv2(frame, (output_width, output_height))
out.write(frame)
total_frames -= 1
編輯:
似乎問題可以使影像具有透明層A- RGBA因為視頻不使用A.
它需要洗掉它。
您可以使用cv2.COLOR_RGBA2BGR而不是轉換cv2.COLOR_RGBA2BGRA
frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_RGBA2BGR)
或者您可以從numpy.array
frame_copy = frame_copy[ : , : , :3 ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/486904.html
下一篇:使用C openCV處理顫振影像
