def RecordCamera(self): #录像功能 tag = self.Record.text() if tag == '保存': try: image_name = self.RecordPath + 'image' + time.strftime( '%Y%m%d%H%M%S', time.localtime(time.time())) + '.jpg' self.label.setText(image_name) imwrite(image_name, self.Image) except Exception as e: self.label.setText(e) elif tag == '录像': self.Record.setText('停止') video_name = self.RecordPath + 'video' + time.strftime( '%Y%m%d%H%M%S', time.localtime(time.time())) + '.avi' fps = 24 / self.quick.value() size = (self.Image.shape[1], self.Image.shape[0]) fourcc = VideoWriter_fourcc('M', 'J', 'P', 'G') self.video_writer = VideoWriter(video_name, fourcc, self.camera.get(5), size) self.RecordFlag = 1 self.label.setAlignment(Qt.AlignCenter) self.label.setText('录像中...') self.Stop.setEnabled(False) self.Exit.setEnabled(False) elif tag == '停止': self.Record.setText('录像') self.video_writer.release() self.RecordFlag = 0 self.label.setText('录像已保存') self.Stop.setEnabled(True) self.Exit.setEnabled(True)
def _initialize_record(self, frame): self._frame_signal.disconnect_(self._initialize_record) import os from datetime import datetime from cv2 import VideoWriter_fourcc as CVCodec h, w = frame.shape[:2] self._filename = self._tag + datetime.now().strftime( "%Y%m%d_%H%M%S") + '.avi' self._output = VideoWriter('video-archive' + os.sep + self._filename, CVCodec(*'XVID'), self._speed if self._speed != 0 else 25.0, (w, h)) self._frame_signal.connect_(self._output.write)
def make_video(images, outimg=None, fps=5, size=None, is_color=True, format="XVID"): """ Create a video from a list of images. @param outvid output video @param images list of images to use in the video @param fps frame per second @param size size of each frame @param is_color color @param format see http://www.fourcc.org/codecs.php @return see http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html The function relies on http://opencv-python-tutroals.readthedocs.org/en/latest/. By default, the video will have the size of the first image. It will resize every image to this size before adding them to the video. """ from cv2.cv2 import VideoWriter, VideoWriter_fourcc, imread, resize fourcc = VideoWriter_fourcc(*format) vid = None for image in images: if not os.path.exists(image): raise FileNotFoundError(image) img = imread(image) if vid is None: if size is None: size = img.shape[1], img.shape[0] vid = VideoWriter(outvid, fourcc, float(fps), size, is_color) if size[0] != img.shape[1] and size[1] != img.shape[0]: img = resize(img, size) vid.write(img) vid.release() return vid
def __init__(self, input_filename: str, output_filename: str, codec: str, window_name: str) -> None: # OPTIONS self.input_filename = input_filename self.output_filename = output_filename self.window_name = window_name self.state = PlayerState() self.hud = HUD(self.state) self.writer = None if self.output_filename: codec = VideoWriter_fourcc(*(codec or 'XVID')) self.writer = VideoWriter(output_filename, codec, 30, (1280, 720)) self.capture = VideoCapture(input_filename) if not self.capture.isOpened(): raise Exception("The capture could not be opened") ok, self.frame = self.capture.read() if not ok: raise Exception("The capture could not be read") self.state.target_fps = self.state.original_fps = self.capture.get( CAP_PROP_FPS) imshow(self.window_name, self.frame) self.selector = Selector(self.window_name) self.selector.on_selecting = self.on_selecting self.selector.on_selected = self.on_selected self.label_uppercase = False self.meter = TickMeter()
fps = FPS().start() out = None # loop over frames from the video file stream while True: # grab the frame from the threaded video stream and resize it # to 500px (to speedup processing) frame = vs.read() frame = process_frame(frame, detector, data) if record: if out is None: (h, w) = frame.shape[:2] out = VideoWriter("outpy.avi", cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), frame_rate, (w, h)) out.write(frame) # display the image to our screen cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # update the FPS counter fps.update()
def __enter__(self): self._writer = VideoWriter(self._path, VideoWriter_fourcc(*self._codec), self._bitrate, self._dimension) return self