~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to backends/kylin-assistant-daemon/src/camera/capture.py

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
### BEGIN LICENSE
 
5
# Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd
 
6
# Author: Kobe Lee
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify it
 
9
# under the terms of the GNU General Public License version 3, as published
 
10
# by the Free Software Foundation.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
14
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
15
# PURPOSE.  See the GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License along
 
18
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
### END LICENSE
 
20
#sudo apt-get install python-pygame
 
21
import os, sys
 
22
import time
 
23
import pygame
 
24
import pygame.camera
 
25
from pygame.locals import *
 
26
import threading
 
27
 
 
28
def get_local_format_time():
 
29
    '''
 
30
    year-month-day hour:minute:second
 
31
    2014-05-07 13:51:30
 
32
    '''
 
33
    local_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
 
34
    return local_date
 
35
 
 
36
#class Capture(object):
 
37
class Capture(threading.Thread):
 
38
    def __init__(self):
 
39
        threading.Thread.__init__(self)
 
40
        pygame.init()
 
41
        pygame.camera.init()
 
42
 
 
43
    def judge_camera(self):
 
44
        clist = pygame.camera.list_cameras()#['/dev/video0']
 
45
        if not clist:
 
46
            return False
 
47
#           raise ValueError("Sorry, no cameras detected.")
 
48
        else:
 
49
            return True
 
50
 
 
51
    def call_camera(self):
 
52
        threading.Thread(target=self.call_camera_real, name='Capture').start()
 
53
 
 
54
    def call_camera_real(self):
 
55
        self.size = (640,480)
 
56
        self.clist = pygame.camera.list_cameras()#['/dev/video0']
 
57
        self.display = pygame.display.set_mode(self.size, 0)
 
58
        self.snapshot = pygame.surface.Surface(self.size, 0, self.display)
 
59
        self.cam = pygame.camera.Camera(self.clist[0], self.size,"RGB")
 
60
        self.cam.start()
 
61
        going = True
 
62
        timevalue = "00-00-00"
 
63
        while going:
 
64
            events = pygame.event.get()
 
65
            for e in events:
 
66
                if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
 
67
                    self.cam.stop()
 
68
                    pic_name = get_local_format_time() + '.png'
 
69
                    print(pic_name)
 
70
                    going = False
 
71
            if self.cam.query_image():
 
72
                self.snapshot = self.cam.get_image(self.snapshot)
 
73
            self.display.blit(self.snapshot, (0,0))
 
74
            pygame.display.flip()
 
75
        pic_path = os.path.expanduser('~') + '/' + pic_name
 
76
        #pic_path = os.path.join('/home/trusty', pic_name)
 
77
        #os.path.expanduser('~')
 
78
        #pic_name = '/home/trusty/' + timevalue + '.png'
 
79
        pygame.image.save(self.snapshot, pic_path)
 
80
        pygame.quit()
 
81
        #exit(0)
 
82
 
 
83
#if __name__ == '__main__':
 
84
#    mycam=Capture()
 
85
#    if mycam.judge_camera():
 
86
#        mycam.call_camera()
 
87