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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| #!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import wx
import threading
from PIL import Image
from VideoCapture import Device
CAMERA = None
DEFAULT_DEVICE_WIDTH = 0
DEFAULT_DEVICE_HEIGHT = 0
def InitCamera():
""" Initialisation de la webcam """
global CAMERA, DEFAULT_DEVICE_WIDTH, DEFAULT_DEVICE_HEIGHT
for x in range(0, 10) :
try :
CAMERA = Device(0)
width, height = 640, 480
CAMERA.setResolution(width, height)
DEFAULT_DEVICE_WIDTH = width
DEFAULT_DEVICE_HEIGHT = height
return True
except :
pass
return False
class VideoCaptureThread(threading.Thread):
def __init__(self, control):
self.width = DEFAULT_DEVICE_WIDTH
self.height = DEFAULT_DEVICE_HEIGHT
self.control = control
self.isRunning =True
self.buffer = wx.NullBitmap
threading.Thread.__init__(self)
def stop(self):
self.isRunning = False
def run(self):
while self.isRunning:
buffer, width, height = CAMERA.getBuffer()
im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
buff = im.tostring()
self.buffer = wx.BitmapFromBuffer(width, height, buff)
x, y = (0, 0)
try:
width, height = self.control.GetSize()
if width > self.width:
x = (width - self.width) / 2
if height > self.height:
y = (height - self.height) / 2
dc = wx.BufferedDC(wx.ClientDC(self.control), wx.NullBitmap, wx.BUFFER_VIRTUAL_AREA)
dc.SetBackground(wx.Brush(wx.Color(0, 0, 0)))
dc.Clear()
dc.DrawBitmap(self.buffer, x, y)
except TypeError:
pass
except wx.PyDeadObjectError:
pass
self.isRunning = False
# -----------------------------------------------------------------------------------------------------------------------------
class CTRL_Video(wx.Panel):
def __init__(self, parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.SUNKEN_BORDER):
wx.Panel.__init__(self, parent, id, pos, size, style)
self.SetBackgroundColour(wx.Color(0, 0, 0))
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, event):
try : self.Device.stop()
except : pass
def StopVideo(self):
self.Device.stop()
def StartVideo(self):
self.Device = VideoCaptureThread(self)
self.Device.start()
def IsRunning(self):
return self.Device.isRunning
# -----------------------------------------------------------------------------------------------------------------------------
class Dialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, -1, style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX|wx.THICK_FRAME)
self.parent = parent
# Vidéo
self.ctrl_video = CTRL_Video(self, -1)
self.bouton_capture = wx.Button(self, -1, u"Capturer")
# Boutons
self.ctrl_ligne = wx.StaticLine(self, -1)
self.bouton_aide = wx.Button(self, -1, "Aide")
self.bouton_options = wx.Button(self, -1, u"Options")
self.bouton_ok = wx.Button(self, -1, u"Ok")
self.bouton_annuler = wx.Button(self, -1, u"Annuler")
self.bouton_ok.Enable(False)
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.OnBoutonCapture, self.bouton_capture)
self.Bind(wx.EVT_BUTTON, self.OnBoutonAide, self.bouton_aide)
self.Bind(wx.EVT_BUTTON, self.OnBoutonOptions, self.bouton_options)
self.Bind(wx.EVT_BUTTON, self.OnBoutonOk, self.bouton_ok)
self.Bind(wx.EVT_BUTTON, self.OnBoutonAnnuler, self.bouton_annuler)
# Lancement de la capture vidéo
wx.CallLater(10, self.Initialisation)
def Initialisation(self):
etat = InitCamera()
if etat == True :
self.ctrl_video.StartVideo()
else:
self.bouton_capture.Enable(False)
self.bouton_options.Enable(False)
dlg = wx.MessageDialog(self, u"Le logiciel n'a pas réussi à se connecter à la caméra.\nVeuillez vérifier que celle-ci est bien installée...", u"Erreur", wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
def __set_properties(self):
self.bouton_capture.SetToolTipString(u"Cliquez ici pour prendre la photo")
self.bouton_aide.SetToolTipString(u"Cliquez ici pour obtenir de l'aide")
self.bouton_options.SetToolTipString(u"Cliquez ici pour définir les propriétés de la capture vidéo")
self.bouton_ok.SetToolTipString(u"Cliquez ici pour valider")
self.bouton_annuler.SetToolTipString(u"Cliquez ici pour annuler")
self.SetMinSize((700, 720))
def __do_layout(self):
grid_sizer_base = wx.FlexGridSizer(4, 1, 10, 10)
grid_sizer_boutons = wx.FlexGridSizer(1, 5, 10, 10)
grid_sizer_base.Add(self.ctrl_video, 0, wx.TOP|wx.LEFT|wx.RIGHT|wx.EXPAND, 10)
grid_sizer_base.Add(self.bouton_capture, 0, wx.LEFT|wx.RIGHT|wx.EXPAND, 10)
grid_sizer_base.Add(self.ctrl_ligne, 0, wx.LEFT|wx.RIGHT|wx.EXPAND, 10)
grid_sizer_boutons.Add(self.bouton_aide, 0, 0, 0)
grid_sizer_boutons.Add(self.bouton_options, 0, 0, 0)
grid_sizer_boutons.Add((20, 20), 0, wx.EXPAND, 0)
grid_sizer_boutons.Add(self.bouton_ok, 0, 0, 0)
grid_sizer_boutons.Add(self.bouton_annuler, 0, 0, 0)
grid_sizer_boutons.AddGrowableCol(2)
grid_sizer_base.Add(grid_sizer_boutons, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 10)
self.SetSizer(grid_sizer_base)
grid_sizer_base.Fit(self)
grid_sizer_base.AddGrowableRow(0)
grid_sizer_base.AddGrowableCol(0)
self.Layout()
self.CenterOnScreen()
def OnBoutonCapture(self, event):
""" Capture de la photo """
if self.ctrl_video.IsRunning() :
self.ctrl_video.StopVideo()
CAMERA.saveSnapshot("capture_video.jpg", quality=100)
self.bouton_capture.SetLabel(u"Re-capturer")
self.bouton_ok.Enable(True)
else:
self.ctrl_video.StartVideo()
self.bouton_capture.SetLabel(u"Capturer")
self.bouton_ok.Enable(False)
def OnBoutonAide(self, event):
print "Aide..."
def OnBoutonOptions(self, event):
# Création du menu contextuel
menuPop = wx.Menu()
menuPop.AppendItem(wx.MenuItem(menuPop, 10, u"Propriétés du flux vidéo"))
self.Bind(wx.EVT_MENU, self.Menu_proprietes_pin, id=10)
menuPop.AppendItem(wx.MenuItem(menuPop, 20, u"Propriétés de la capture vidéo"))
self.Bind(wx.EVT_MENU, self.Menu_proprietes_filter, id=20)
self.PopupMenu(menuPop)
menuPop.Destroy()
def Menu_proprietes_pin(self, event):
CAMERA.displayCapturePinProperties()
def Menu_proprietes_filter(self, event):
CAMERA.displayCaptureFilterProperties()
def OnBoutonOk(self, event):
self.EndModal(wx.ID_OK)
def OnBoutonAnnuler(self, event):
self.EndModal(wx.ID_CANCEL)
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
dialog_1 = Dialog(None)
app.SetTopWindow(dialog_1)
dialog_1.ShowModal()
app.MainLoop() |
Partager