staging.inyokaproject.org

opencv 4.5 (cv2) - bearbeitete Frames als Video speichern (Python)

Status: Ungelöst | Ubuntu-Version: Ubuntu 22.04 (Jammy Jellyfish)
Antworten |

banause

Anmeldungsdatum:
17. November 2009

Beiträge: 302

Hallo Forum,

ich scheitere an der Erstellung eines Videos aus bearbeiteten Frames.

Allgemein wird die Speicherung eines Videos mit cv2 folgendermaßen beschrieben:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import cv2
import numpy as np

# Create a VideoCapture object
cap = cv2.VideoCapture(0)

# Check if camera opened successfully
if (cap.isOpened() == False): 
  print("Unable to read camera feed")

# Default resolutions of the frame are obtained.The default resolutions are system dependent.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))

while(True):
  ret, frame = cap.read()

  if ret == True: 
    
    # Write the frame into the file 'output.avi'
    out.write(frame)

    # Display the resulting frame    
    cv2.imshow('frame',frame)

    # Press Q on keyboard to stop recording
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

  # Break the loop
  else:
    break  

# When everything done, release the video capture and video write objects
cap.release()
out.release()

# Closes all the frames
cv2.destroyAllWindows()

Das funktioniert auch. Sogar, wenn ich statt der ersten zu findenden Kamera im Rechner

1
cap = cv2.VideoCapture(0)

eine WebCam über rtsp:// einbinde.

1
cap = cv2.VideoCapture("rtsp://1.2.3.4")

ABER: Sobald ich die Frames mit cv2-Tools bearbeite, wird das gewünschte Video als Datei zwar angelegt, aber nicht beschrieben.

    # Bearbeitung des mit read() gelesenen Frames
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Write the frame into the file 'output.avi'
    out.write(frame_gray)

Woran mag das liegen? Muss ich den bearbeiteten Frame (frame_gray) erst noch weiter bearbeiten, bevor ich ihn speichern kann?

Über Eure Tipps würde ich mich freuen.

Vielen Dank.

banause

Gloster

Anmeldungsdatum:
9. April 2020

Beiträge: 417

Evtl. kannst du die Frames in einen MKV-Container abspeichern ?!

Antworten |