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
| # -*- coding:cp1252 -*-
import wx
import os
import sys
import time
import threading
from cv import *
from PIL import Image
from wx.lib.pubsub import Publisher
QUANTUM_GET = QUANTUM_SET = 3
class ThreadRecup(threading.Thread):
def __init__(self, image):
threading.Thread.__init__(self)
self.toDisplay = image
self.busyGet=0
print "thread init ok"
def run(self):
print "on run"
while True:
print "entree dans le while loopget"
self.busyGet=1
print "on passe get à 1"
self.image = self._getImage()
print "on est sorti de getimage"
self.busyGet=0
print "on passe get à 0"
#time.sleep(QUANTUM_GET)
def _getImage(self):
print "entree dans la fonction getimage"
monImage = QueryFrame(self.toDisplay)
print "on a récupéré une image de la video"
Publisher().sendMessage("pret", monImage)
#Publisher().sendMessage("test", 0)
print "on a dit qu'on faisait un callafter"
return monImage
class MaFenetre(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.alive = True
self.busyGet = 0
self.busySet = 0
#On initialise les webcam
self.camera=CreateCameraCapture(-1) #'0' si plusieurs cameras, '-1' si une seule camera
#Extraction d'une image depuis le flux video webcam
self.image = QueryFrame(self.camera)
print "type de self.image en tant qu'attribut après queryFrame :", type(self.image), "\n"
#Conversion d'une image PIL en image affichable par wxPython (bmp)
self.imBmp = wx.BitmapFromBuffer(self.image.width, self.image.height, self.image.tostring())
#print "en tant qu'attribut en bmp :", type(self.imBmp), "\n"
# Eléments graphiques
texte1 = wx.StaticText(self, -1, "Entrez votre nom :")
self.votreNom = wx.TextCtrl(self)
bouton_ok = wx.Button(self, wx.ID_OK)
bouton_quit = wx.Button(self, wx.ID_CANCEL)
self.bouton_video = wx.Button(self, -1, "start video")
self.bitmapWidget = wx.StaticBitmap(self, -1, self.imBmp)
#On rattache le bouton à l'évènement
self.bouton_video.Bind(wx.EVT_BUTTON, self.startVideo)
# Disposition
self.boitePrincipale = wx.BoxSizer(wx.VERTICAL)
boite = wx.BoxSizer(wx.HORIZONTAL)
boiteBoutons = wx.BoxSizer(wx.HORIZONTAL)
boiteVideos = wx.BoxSizer(wx.HORIZONTAL)
# Rangement
boiteVideos.Add(self.bitmapWidget)
boite.Add(texte1)
boite.Add(self.votreNom)
boiteBoutons.Add(bouton_ok)
boiteBoutons.AddSpacer(10)
boiteBoutons.Add(bouton_quit)
self.boitePrincipale.Add(boiteVideos, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
self.boitePrincipale.Add(self.bouton_video, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
self.boitePrincipale.Add(boite, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
self.boitePrincipale.Add(boiteBoutons, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
self.SetSizerAndFit(self.boitePrincipale)
self.Centre()
self.Show()
Publisher().subscribe(self.setImage, "pret")
Publisher().subscribe(self.fonctionTest, "test")
def startVideo(self, event):
thread2 = ThreadRecup(self.camera)
thread2.start()
print "thread2 lancé"
self.bouton_video.label = "stop video"
def setImage(self,msg):
print "entree dans la fonction setimage"
t = msg.data
print "type de t :", type(t)
self.imBmp = wx.BitmapFromBuffer(t.width, t.height, t.tostring())
print "type de t apres bmp :", type(self.imBmp)
time.sleep(QUANTUM_GET)
self.bitmapWidget.SetBitmap(self.imBmp)
def fonctionTest(self,msg):
print "entree fonction test"
a=msg.data
print "valeur de i :",a
class MonAppli(wx.App):
def OnInit(self):
print "entree programme"
fenetre=MaFenetre(None, -1, 'Demonstrateur')
print "fenetre initialisee, on va lancer 'start'"
print "on sort de 'start'"
return True
app = MonAppli()
app.MainLoop() |
Partager