Tutorial #6: Working with Camera in Kivy

Hey everyone, here is a new post that demonstrates the use of the Camera module in Kivy.

For those who are new to Kivy, I suggest you read the Getting started tutorial for Kivy before proceeding.

This tutorial describes the use of Camera, how to open a camera and take a screenshot of the live stream.

So let’s get started. Open your editor. I prefer Python IDLE. You can also choose any other editor.

Step 1: First you need to make sure that Kivy is up and running on your system. Go to the command prompt and type kivy and hit enter.

Step 2 : Create a new Python file named testcamera.py

Step 3: testcamera.py

import kivy

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.camera import Camera
from kivy.uix.button import Button
from kivy.core.window import Window

class MyApp(App):
          # Function to take a screenshot
          def doscreenshot(self,*largs):
	      def build(self):
                camwidget = Widget()  #Create a camera Widget
                cam = Camera()        #Get the camera
                cam=Camera(resolution=(640,480), size=(500,500))
                cam.play=True         #Start the camera

                camwidget.add_widget(button)    #Add button to Camera Widget
                return camwidget
if __name__ == '__main__':

Save your file. To run the program , go to the command prompt once again and type:

kivy testcamera.py

You should get a live stream. Click on the Screenshot button to take a snap.

For more information refer the Kivy docs for Camera, Window and Button

Karan Balkar About Karan Balkar
self proclaimed extraterrestrial fandroid, computer engineer, amateur gamer and die hard punk rock fan!

5 thoughts on “Tutorial #6: Working with Camera in Kivy

  1. Just for an extension of your program here, Is there a way to have any image processing algorithm run every time, the texture of Image updates; or whenever camera buffer is read.
    I basically want to have a event generated at every update of texture, so that i can bind a method to that event. Is it possible, if yes, could you tell how?

    • I am trying to get something using basic “vidcap” and kivy.uix.image,
      the code I wrote is
      from vidcap import new_Dev
      from kivy.uix.image import Image
      from kivy.uix.gridlayout import GridLayout
      from kivy.app import App
      from kivy.clock import Clock

      class myCam(Image):
      def build(self):
      self.dev = new_Dev(0,0)
      Clock.schedule_interval(self._update, 1/30)

      def _update(self,dt):
      data, w, h = self.dev.getbuffer()
      self.texture = data

      class myApp(App):
      def build(self):
      newCam = myCam()
      mainWidget = GridLayout()
      mainWidget.cols = 1
      mainWidget.rows = 1
      return mainWidget


    • Your question is really good. I don’t know exactly how to do it but I guess it should be possible. I have never tried integrating image processing using Kivy. As you know Kivy uses Python primarily. What I would suggest is that first u try writing a code in Python using any of the image processing libraries and get that part done. I think once u have that implemented…just create a simple camera UI in Kivy for starters…then you need to go through the Kivy docs to see if there any methods available for the camera API that could update the texture of the image…you need to go about this one step at a time.

      Do u have the image processing code ready and working? We can then discuss the kivy part later

  2. Hey! That’s pretty cool.
    Can you please direct me to some more examples on the use of “Window”. There are very few tutorials on internet on kivy. 🙁
    And also can you please tell me how to take a picture using the camera widget.
    Thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *