Dateien hochladen nach „code“
This commit is contained in:
parent
a0d57e4cd8
commit
4b6f98fbec
173
code/recording.py
Normal file
173
code/recording.py
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
"""
|
||||||
|
Abhängigkeiten:
|
||||||
|
- cv2 (OpenCV-Paket)
|
||||||
|
- threading
|
||||||
|
- os
|
||||||
|
|
||||||
|
Autor: Roberto Gelsinger
|
||||||
|
Datum: 07.12.2023
|
||||||
|
Version: Modulversion
|
||||||
|
"""
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import threading
|
||||||
|
import os
|
||||||
|
from tkinter import simpledialog
|
||||||
|
|
||||||
|
|
||||||
|
recording_normal = False
|
||||||
|
recording = False # Globale Variable, um den Aufnahmestatus zu verfolgen
|
||||||
|
recording_finished_event = threading.Event()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def start_recording(video_name="aufgenommenes_video", length=5,testcase_resolution1=2560,testcase_resolution2=1440,testcase_fps=20):
|
||||||
|
"""
|
||||||
|
Startet die Videoaufnahme in einem separaten Thread.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
video_name (str): Der Basisname der Videodatei (Standard ist "aufgenommenes_video").
|
||||||
|
"""
|
||||||
|
global recording
|
||||||
|
recording = True
|
||||||
|
thread = threading.Thread(target=record_video, args=(video_name, length,testcase_resolution1,testcase_resolution2,testcase_fps))
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
def stop_recording():
|
||||||
|
"""
|
||||||
|
Beendet die Videoaufnahme, indem der globale 'recording'-Status auf False gesetzt wird.
|
||||||
|
"""
|
||||||
|
global recording
|
||||||
|
recording = False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def record_video(video_name="aufgenommenes_video", length=5,testcase_resolution1=2560,testcase_resolution2=1440,testcase_fps=20):
|
||||||
|
"""
|
||||||
|
Nimmt ein Video auf und speichert es im AVI-Format.
|
||||||
|
|
||||||
|
Die Funktion initialisiert eine Videoaufnahme über die Webcam und speichert das Video in einem vordefinierten Ordner.
|
||||||
|
Die Aufnahme läuft, solange die globale Variable 'recording' auf True gesetzt ist.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
video_name (str): Der Basisname der Videodatei (Standard ist "aufgenommenes_video").
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
output_folder = "code/videos"
|
||||||
|
output_file = os.path.join(output_folder, video_name + ".avi")
|
||||||
|
frame_rate = testcase_fps
|
||||||
|
|
||||||
|
cap = cv2.VideoCapture(0)
|
||||||
|
cap.set(cv2.CAP_PROP_FRAME_WIDTH, testcase_resolution1)
|
||||||
|
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, testcase_resolution2)
|
||||||
|
cap.set(cv2.CAP_PROP_FPS, testcase_fps)
|
||||||
|
if not cap.isOpened():
|
||||||
|
print("Fehler beim Öffnen der Kamera.")
|
||||||
|
return
|
||||||
|
|
||||||
|
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
||||||
|
out = cv2.VideoWriter(output_file, fourcc,testcase_fps,(testcase_resolution1, testcase_resolution2) )
|
||||||
|
|
||||||
|
|
||||||
|
total_frames = int(frame_rate * length) # Gesamtzahl der aufzunehmenden Frames
|
||||||
|
frame_count = 0 # Frame-Zähler
|
||||||
|
|
||||||
|
while frame_count < total_frames:
|
||||||
|
ret, frame = cap.read()
|
||||||
|
if not ret:
|
||||||
|
break
|
||||||
|
|
||||||
|
out.write(frame)
|
||||||
|
frame_count += 1
|
||||||
|
|
||||||
|
cv2.imshow('Recording', frame)
|
||||||
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||||
|
break
|
||||||
|
|
||||||
|
recording_finished_event.set()
|
||||||
|
|
||||||
|
recording = False
|
||||||
|
cap.release()
|
||||||
|
out.release()
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
def stop_normal_recording():
|
||||||
|
"""
|
||||||
|
Beendet die Videoaufnahme, indem der globale 'recording'-Status auf False gesetzt wird.
|
||||||
|
"""
|
||||||
|
global recording_normal
|
||||||
|
recording_normal = False
|
||||||
|
|
||||||
|
|
||||||
|
def start_normal_recording(video_name="aufgenommenes_video",video_resolution1=2560,video_resolution2=1440, fps=20):
|
||||||
|
"""
|
||||||
|
Startet die Videoaufnahme in einem separaten Thread.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
video_name (str): Der Basisname der Videodatei (Standard ist "aufgenommenes_video").
|
||||||
|
"""
|
||||||
|
global recording_normal
|
||||||
|
recording_normal = True
|
||||||
|
thread = threading.Thread(target=record_normal_video, args=(video_name,video_resolution1,video_resolution2,fps))
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
def record_normal_video(video_name="aufgenommenes_video",video_resolution1=2560,video_resolution2=1440, fps=20):
|
||||||
|
"""
|
||||||
|
Nimmt ein Video auf und speichert es im AVI-Format.
|
||||||
|
|
||||||
|
Die Funktion initialisiert eine Videoaufnahme über die Webcam und speichert das Video in einem vordefinierten Ordner.
|
||||||
|
Die Aufnahme läuft, solange die globale Variable 'recording' auf True gesetzt ist.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
video_name (str): Der Basisname der Videodatei (Standard ist "aufgenommenes_video").
|
||||||
|
"""
|
||||||
|
output_folder = "code/videos"
|
||||||
|
output_file = os.path.join(output_folder, video_name + ".avi")
|
||||||
|
|
||||||
|
cap = cv2.VideoCapture(0)
|
||||||
|
cap.set(cv2.CAP_PROP_FRAME_WIDTH, video_resolution1)
|
||||||
|
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, video_resolution2)
|
||||||
|
cap.set(cv2.CAP_PROP_FPS, fps)
|
||||||
|
|
||||||
|
if not cap.isOpened():
|
||||||
|
print("Fehler beim Öffnen der Kamera.")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
#usefull if you have problems with cam resolutions , for manual debugging
|
||||||
|
#print("video_resolution1:", video_resolution1, "type:", type(video_resolution1))
|
||||||
|
#print("video_resolution2:", video_resolution2, "type:", type(video_resolution2))
|
||||||
|
#print("fps:", fps, "type:", type(fps))
|
||||||
|
#actual_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
|
||||||
|
#actual_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
||||||
|
#actual_fps = cap.get(cv2.CAP_PROP_FPS)
|
||||||
|
#print("Actual width:", actual_width)
|
||||||
|
#print("Actual height:", actual_height)
|
||||||
|
#print("Actual FPS:", actual_fps)
|
||||||
|
|
||||||
|
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
||||||
|
out = cv2.VideoWriter(output_file, fourcc, fps, (video_resolution1, video_resolution2))
|
||||||
|
|
||||||
|
if not out.isOpened():
|
||||||
|
print("Fehler beim Öffnen der Videoausgabedatei.")
|
||||||
|
cap.release()
|
||||||
|
return
|
||||||
|
|
||||||
|
while recording_normal:
|
||||||
|
|
||||||
|
ret, frame = cap.read()
|
||||||
|
if not ret:
|
||||||
|
break
|
||||||
|
|
||||||
|
cv2.imshow('Recording', frame)
|
||||||
|
out.write(frame)
|
||||||
|
|
||||||
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||||
|
break
|
||||||
|
|
||||||
|
cap.release()
|
||||||
|
out.release()
|
||||||
|
cv2.destroyAllWindows()
|
Loading…
x
Reference in New Issue
Block a user