Что нужно знать
- Когда появляется окно, чтобы показать мне рамку, она серого цвета.
- Индикатор веб-камеры гаснет
- Файл создан, он просто не воспроизводится (даже в vlc)
- Файл, кажется, имеет приличный размер файла (я думаю) 8 КБ для5 секунд
class Camera:
# Increase the scope of these variables to the class level
video = None
vid_codec = None
outputFilePath = None
resolutionX = None
resolutionY = None # Video Resolution
fps = 30.0
# Constructor
def __init__(self, _outputFilePath, _resolutionX, _resolutionY, _fps, _videoCaptureSetting = 0):
self.outputFilePath = os.path.dirname(_outputFilePath) # takes our string and converts it to a file path
if not os.path.exists(self.outputFilePath):
# If the directory already exists, we don't create it
# but we can still access it later on obviously
# stackoverflow.com/questions/273192/how-can-i-safely-create-a-nested-directory
os.makedirs(self.outputFilePath, exist_ok=True) # Creates the nested directories needed by the user
# Zero is external Camera
self.video = cv2.VideoCapture(_videoCaptureSetting) # Defaults to zero
self.resolutionX = _resolutionX
self.resolutionY = _resolutionY
self.fps = _fps
self.vid_codec = cv2.VideoWriter_fourcc(*"MPEG")
def record(self, secondsToRecord, _filename):
output = cv2.VideoWriter(self.outputFilePath + _filename + '.avi', self.vid_codec, self.fps, (self.resolutionX , self.resolutionY))
timeEnd = time.time() + (secondsToRecord) # NOTE: is not monotomic time, and will not adjust itself for machine lock
print(self.video.isOpened())
while time.time() <= timeEnd:
# Capture each frame of webcam video
ret, frame = self.video.read()
output.write(frame)
cv2.imshow("Capturing", frame)
output.release() # Closes the file
cv2.destroyAllWindows
def disconnectCamera(self):
self.video.release()
и это то, что я использую для его выполнения
cam = Camera('./swag/cool/', 1280, 720, 30)
cam.record(5, 'myVideo')
print("Done Recording")
cam.disconnectCamera()