Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

kivy video recorder

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.properties import StringProperty, NumericProperty

import cv2
import os

# Standard Video Dimensions Sizes
STD_DIMENSIONS =  {
    "480p": (640, 480),
    "720p": (1280, 720),
    "1080p": (1920, 1080),
    "4k": (3840, 2160),
}

# Video Encoding, might require additional installs
# Types of Codes: http://www.fourcc.org/codecs.php
VIDEO_TYPE = {
    'avi': cv2.VideoWriter_fourcc(*'XVID'),
    #'mp4': cv2.VideoWriter_fourcc(*'H264'),
    'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}

class KivyCamera(BoxLayout):
    filename = StringProperty('video.avi')
    frames_per_second = NumericProperty(30.0)
    video_resolution = StringProperty('720p')

    def __init__(self, **kwargs):
        super(KivyCamera, self).__init__(**kwargs)
        self.img1=Image()
        self.add_widget(self.img1)
        self.capture = cv2.VideoCapture(0)
        self.out = cv2.VideoWriter(self.filename, self.get_video_type(self.filename), self.frames_per_second, self.get_dims(self.capture, self.video_resolution))
        Clock.schedule_interval(self.update, 1 / self.frames_per_second)

    def update(self, *args):
        ret, frame = self.capture.read()
        self.out.write(frame)
        buf = cv2.flip(frame, 0).tostring()
        texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt="bgr")
        texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte")
        self.img1.texture = texture

    # Set resolution for the video capture
    # Function adapted from https://kirr.co/0l6qmh
    def change_resolution(self, cap, width, height):
        self.capture.set(3, width)
        self.capture.set(4, height)

    # grab resolution dimensions and set video capture to it.
    def get_dims(self, cap, video_resolution='1080p'):
        width, height = STD_DIMENSIONS["480p"]
        if self.video_resolution in STD_DIMENSIONS:
            width, height = STD_DIMENSIONS[self.video_resolution]
        ## change the current caputre device
        ## to the resulting resolution
        self.change_resolution(cap, width, height)
        return width, height

    def get_video_type(self, filename):
        filename, ext = os.path.splitext(filename)
        if ext in VIDEO_TYPE:
          return  VIDEO_TYPE[ext]
        return VIDEO_TYPE['avi']

class CamApp(App):
    def build(self):
        return KivyCamera()

if __name__ == '__main__':
    CamApp().run()
Comment

PREVIOUS NEXT
Code Example
Python :: find root of the path of file os package 
Python :: how travel a list invertida in python 
Python :: google video processor python nmp 
Python :: for c in range python 
Python :: python token stealer 
Python :: pandas exploring dataframe 
Python :: apa itu duck typing python 
Python :: ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’ meaning in resample 
Python :: insert string into middle of list python 
Python :: how to take input a matrix using map in python 
Python :: pandas iat 0 
Python :: python audio graph live stream 
Python :: deploy vue app to google cloud run 
Python :: number guessing game using tkinter python 
Python :: writer.append_data(image) means 
Python :: permutation test python 
Python :: How to Use Sets to Remove Duplicate Items in Other Data Structures 
Python :: Display tail of the DataFrame 
Python :: Python Tkinter Scale Widget Syntax 
Python :: Difference between the remove() method and discard() method of sets in python 
Python :: The float type in Python3 can represent decimal 0.1 without error. 
Python :: logged_passengers 
Python :: ssd 1306 esp32 python 
Python :: sklearn list parameters 
Python :: Creating column based on existing column 
Python :: como poner python 3 en la terminal mac 
Python :: python file operation 
Python :: how to reorder columns in pandas 
Python :: nums: List[int] in python function 
Python :: cv2 put font on center 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =