Coverage for src/botadi/mokadi/mokadi_picture.py: 64%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

22 statements  

1""" 

2@file 

3@brief Record a couple of seconds and returns the raw stream. 

4""" 

5 

6 

7def take_picture(filename=None, size=(1280, 720), module="cv2"): 

8 """ 

9 Take a picture with the camera. 

10 

11 @param filename picture to save (if not None) 

12 @param module module to use (look at the code). 

13 @return image object 

14 

15 We use the examples from 

16 `Capturing a single image from my webcam in Java or Python 

17 <http://stackoverflow.com/questions/11094481/capturing-a-single-image-from-my-webcam-in-java-or-python>`_. 

18 """ 

19 

20 if module == "pygame": 

21 import pygame 

22 import pygame.camera 

23 # pygame.camera.init() 

24 # pygame.camera.list_camera() 

25 try: 

26 cam = pygame.camera.Camera(0, size, "RGB") 

27 except Exception as e: 

28 raise Exception( 

29 "Unable to change resolution into {0}".format(size)) from e 

30 cam.start() 

31 img = cam.get_image() 

32 if filename is not None: 

33 pygame.image.save(img, filename) 

34 cam.stop() 

35 return img 

36 elif module == "cv2": 

37 from cv2 import VideoCapture, imwrite 

38 # initialize the camera 

39 cam = VideoCapture(0) # 0 -> index of camera 

40 _, img = cam.read() 

41 if filename is not None: 

42 imwrite(filename, img) 

43 return img 

44 else: 

45 raise ImportError("No module '{0}'".format(module))