-
Notifications
You must be signed in to change notification settings - Fork 9
/
pano.py
executable file
·170 lines (154 loc) · 6.23 KB
/
pano.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Panorama
# Copyright (C) 2008, Nirav Patel
# Copyright (C) 2011, 2012, Alan Aguiar
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact information:
# Alan Aguiar <[email protected]>
# Nirav Patel <[email protected]>
import os
import sys
from gi.repository import Gtk, GObject
import time
from gettext import gettext as _
import logging
import stitcher
try:
import pygame
from pygame.locals import *
from pygame import camera
except ImportError:
print('Error in import Pygame. This activity requires Pygame 1.9')
class PanoCapture():
def __init__(self, parent):
self.parent = parent
self.auto_stich = False
self._has_camera = False
self._show_err_msg = False
#self.camera.set_controls(brightness = 129)
def auto_stiching(self, option):
self.auto_stich = option
def show_text(self, texto):
text = self.fuente.render(texto, 1, (255, 0, 0))
textrect = text.get_rect()
textrect.center = (820, 240)
self.display.blit(text, textrect)
pygame.display.flip()
def get_and_flip(self):
self.snapshot = self.camera.get_image(self.snapshot)
#box = self.display.blit(self.snapshot, (500,0))
# pygame.display.update(box)
self.display.blit(self.snapshot, (500, 0))
pygame.display.flip()
def add_capture(self):
N = len(self.imlist)
if not(N == self.max_cant):
self.imlist.append(self.snapshot.copy())
self.display.blit(self.snapshot, (20, 0))
self.tiny = pygame.transform.scale(
self.snapshot, (self.size[0]//self.thumbscale, self.size[1]//self.thumbscale), self.tiny)
self.display.blit(self.tiny, (self.offset, 480))
self.offset += 3*self.size[0]//(4*self.thumbscale)
pygame.display.flip()
else:
self.show_text('Max cant of captures')
def run(self):
self.size = (640, 480)
self.depth = 24
self.thumbscale = 4
pygame.init()
# camera.init()
self.fuente = pygame.font.Font(None, 60)
self.camlist = [] #camera.list_cameras()
if len(self.camlist):
self.camera = camera.Camera(self.camlist[0], self.size, "RGB")
self._has_camera = True
try:
self.camera.start()
except SystemError:
self._has_camera = False
try:
self.camera.set_controls(hflip=True)
except SystemError:
pass
self._show_err_msg = False
if not self._has_camera:
self._show_err_msg = True
self._has_camera = False
self.message = _('Camera not found')
self.parent.configure_buttons()
self.clock = pygame.time.Clock()
self.final = None
self.imlist = []
self.offset = 20
self.max_cant = 9
self.display = pygame.display.get_surface()
self.display.fill((82, 186, 74))
self.converted = pygame.surface.Surface(self.size, 0, self.display)
self.snapshot = pygame.surface.Surface(self.size, 0, self.display)
self.tiny = pygame.surface.Surface(
(self.size[0]//self.thumbscale, self.size[1]//self.thumbscale), 0, self.display)
pygame.display.flip()
going = True
while going:
# GTK events
while Gtk.events_pending():
Gtk.main_iteration()
events = pygame.event.get()
for e in events:
if e.type == pygame.USEREVENT and self._has_camera:
if hasattr(e, "action"):
if e.action == 'save_button':
if self._has_camera:
self.show_text("Saving")
if self.final:
self.parent.save_image(self.final)
else:
if not(self.imlist == []):
self.final = stitcher.build_panorama(
self, self.imlist, self.auto_stich)
self.parent.save_image(self.final)
else:
self.show_text(_("Cannot save"))
pygame.display.flip()
elif e.action == 'new_button':
self.imlist = []
self.final = None
self.display.fill((82, 186, 74))
self.offset = 20
pygame.display.flip()
elif e.action == 'capture_button':
self.add_capture()
elif e.action == 'stitch_button':
self.show_text("Processing")
if not(self.imlist == []):
self.final = stitcher.build_panorama(
self, self.imlist, self.auto_stich)
pygame.display.flip()
elif e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
going = False
elif e.type == KEYDOWN and e.key == K_SPACE and self._has_camera:
self.add_capture()
if self._has_camera:
self.get_and_flip()
self.clock.tick()
else:
self.show_text(self.message)
self.clock.tick(5)
if self._has_camera and self.camera:
self.camera.stop()