forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraTest.py
159 lines (130 loc) · 4.58 KB
/
CameraTest.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
import wx
import six
import time
import datetime
import threading
from six.moves.queue import Queue, Empty
import Utils
import Model
from PhotoFinish import SnapPhoto
now = datetime.datetime.now
def RescaleImage( image, width, height ):
wImage = image.GetWidth()
hImage = image.GetHeight()
ratio = min( float(width) / float(wImage), float(height) / float(hImage) )
return image.Rescale( int(wImage*ratio), int(hImage*ratio), wx.IMAGE_QUALITY_NORMAL ) if not (0.94 < ratio < 1.06) else image
class ScaledImage( wx.Panel ):
def __init__( self, parent, id=wx.ID_ANY, size=(640,400) ):
super(ScaledImage, self).__init__( parent, id, size=size )
self.SetBackgroundStyle( wx.BG_STYLE_CUSTOM )
self.image = None
self.Bind( wx.EVT_PAINT, self.OnPaint )
def OnPaint( self, event=None ):
dc = wx.AutoBufferedPaintDC( self )
dc.SetBackground( wx.WHITE_BRUSH )
dc.Clear()
width, height = self.GetSize()
try:
image = self.image.Copy()
image = RescaleImage( image, width, height )
except:
return
bitmap = image.ConvertToBitmap()
dc.DrawBitmap( bitmap, (width - bitmap.GetWidth())//2, (height - bitmap.GetHeight())//2 )
def SetImage( self, image ):
self.image = image
self.Refresh()
def GetImage( self ):
return self.image
class CameraTestDialog( wx.Dialog ):
def __init__( self, parent, id=wx.ID_ANY, title=_("Camera Test"), size=(690, 590) ):
super(CameraTestDialog, self).__init__(
parent, id, title=title, size=size,
style=wx.CAPTION
)
self.photo = ScaledImage( self )
self.status = wx.StaticText( self )
self.saveButton = wx.Button( self, label=_('Take Snapshot') )
self.saveButton.Bind( wx.EVT_BUTTON, self.onSave )
self.ok = wx.Button( self, wx.ID_OK )
self.ok.Bind( wx.EVT_BUTTON, self.onClose )
self.Bind( wx.EVT_CLOSE, self.onClose )
sizer = wx.BoxSizer( wx.VERTICAL )
sizer.Add( self.photo, 1, flag=wx.EXPAND|wx.ALL, border=4 )
hs = wx.BoxSizer( wx.HORIZONTAL )
hs.Add( self.status, 1, flag=wx.EXPAND|wx.ALIGN_CENTRE_VERTICAL|wx.RIGHT, border=4 )
hs.Add( self.saveButton, 0, flag=wx.ALL, border=4 )
hs.Add( self.ok, 0, flag=wx.ALL, border=4 )
sizer.Add( hs, 0, flag=wx.EXPAND|wx.ALL, border=4 )
self.SetSizer( sizer )
self.resolutionFormat = _("Camera Resolution") + u': {} x {} fps: {:.3f}'
self.tStart = now()
self.framesPerSecond = 25
self.frameDelay = 1.0/self.framesPerSecond
self.timer = wx.Timer( self )
self.frameCount = 0
self.tStart = now()
self.fps = 0.0
self.Bind( wx.EVT_TIMER, self.updatePhoto )
SetCameraState( True )
if Utils.cameraError:
self.status.SetLabel( Utils.cameraError )
else:
self.timer.Start( int(self.frameDelay*1000) )
def updatePhoto( self, event ):
tNow = now()
cameraImage = SnapPhoto()
if not cameraImage:
self.status.SetLabel( Utils.cameraError if Utils.cameraError else _("Camera Failed: Unknown Error.") )
return
try:
photo = AddPhotoHeader( 9999, (tNow - self.tStart).total_seconds(), cameraImage )
self.status.SetLabel( self.resolutionFormat.format(photo.GetWidth(), photo.GetHeight(), self.fps) )
self.photo.SetImage( photo )
except:
pass
self.frameCount += 1
if self.frameCount == self.framesPerSecond:
tNow = now()
self.fps = self.frameCount / (tNow - self.tStart).total_seconds()
self.frameCount = 0
self.tStart = tNow
def onSave( self, event ):
try:
tNow = now()
image = self.photo.GetImage()
except:
Utils.MessageOK( self, _('Cannot access photo image.'), title = _("Camera Error"), iconMask=wx.ICON_ERROR )
return
race = Model.race
fname = Utils.RemoveDisallowedFilenameChars(u'{} {}.png'.format( tNow.strftime('%Y-%m-%d %H-%M-%S'), Utils.toAscii(race.name if race else 'CameraTest')) )
fd = wx.FileDialog( self,
message=_("Save Photo as PNG File:"),
wildcard="PNG files (*.png)| *.png",
style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT,
defaultDir = Utils.getFileDir(),
)
fd.SetFilename( fname )
ret = fd.ShowModal()
if ret != wx.ID_OK:
fd.Destroy()
return
fname = fd.GetPath()
fd.Destroy()
try:
image.SaveFile( fname, wx.BITMAP_TYPE_PNG )
except Exception as e:
Utils.MessageOK( self, u'{}\n\n {}\n\n{}'.format(_('Cannot save photo:'), fname, e),
title = _("Save Error"), iconMask=wx.ICON_ERROR )
def onClose( self, event ):
self.timer.Stop()
SetCameraState( False )
self.EndModal( wx.ID_OK )
if __name__ == '__main__':
app = wx.App(False)
mainWin = wx.Frame(None, title="CrossMan", size=(100,100))
mainWin.Show()
cameraTestDialog = CameraTestDialog( mainWin )
cameraTestDialog.ShowModal()
cameraTestDialog.Destroy()
app.MainLoop()