Précédent   Forum du club des développeurs et IT Pro > Autres langages > Python & Zope > GUI
GUI Forum d'entraide sur les bibliothèques pour interfaces graphiques en Python
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 03/05/2012, 17h19   #21
afranck64
Membre Expert
 
Avatar de afranck64
 
Homme Franck Awounang N.
Étudiant
Inscription : janvier 2009
Messages : 587
Détails du profil
Informations personnelles :
Nom : Homme Franck Awounang N.
Âge : 21
Localisation : Autre

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : janvier 2009
Messages : 587
Points : 1 025
Points : 1 025
Envoyer un message via Yahoo à afranck64
salut,
Code :
1
2
def _setImage(self):
		bitmapWidget = wx.StaticBitmap(self, -1, self.imBmp)
ca devrait etre selt.bitmapWidget mais meme dans ce cas, redefinir le widget me semble etre terrible. Donc, ca devrait etre:
Code :
1
2
def _setImage(self):
    self.bitmapWidget.setBimap(self.imBmp)
Code :
1
2
3
4
def _loop_set(self):
		while self.alive:
			self._setImage()
            time.sleep(QUANTUM_SET)
Et pour finir, les threads devraient etre des wxThread.

@+
__________________
Win 7 HP 64 bits /Ubuntu 12.04, - AMD A6 Quad: Py32 / Py27
Citation:
CONTENU D'UNE QUESTION
Exemples:
- Configuration (système d'exploitation, version de Python et des bibliothèques utilisées)
- Code source du morceau de programme où il y a un bogue
- Ligne de code sur laquelle le bogue apparaît
- Erreur complète retournée pas l'interpréteur Python
- Recherche déjà effectuée (FAQ, Tuto, Web, ...)
- Tests déjà effectués
afranck64 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/05/2012, 09h23   #22
besail
Invité de passage
 
Homme Basile Sénéchal
Étudiant
Inscription : avril 2012
Messages : 13
Détails du profil
Informations personnelles :
Nom : Homme Basile Sénéchal
Localisation : France, Essonne (Île de France)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2012
Messages : 13
Points : 0
Points : 0
Bonjour,

Il me semble que j'ai un problème de "simultanéité"...

Je pense que loopget essaie d'accéder à l'image de la webcam au travers de mon Thread 1 alors que loopset l'utilise en même temps au travers du thread 2, et je pense que ça ne lui plait pas trop.

Code :
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.
De plus, j'ai lu ici qu'un thread ne pouvait pas modifier un UI directement par le thread, mais qu'il fallait plutot passer par un event (ou utiliser un CallAfter() mais j'avoue ne pas bien comprendre comment l'utiliser).

Je joins encore mon code en laissant cette fois les "print" de test pour voir où le programme plante.

Code :
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
# -*- coding:cp1252 -*-
 
import wx
import os
import sys
import time
import threading
from cv import *
from PIL import Image
 
QUANTUM_GET = QUANTUM_SET = 0.000025
 
class MaFenetre(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, id, title)
 
		self.alive = True
		#self.image = Image.new("RGB", (600,400), None)
 
		#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)
 
		#Conversion d'une image PIL en image affichable par wxPython (bmp)
		self.imBmp = wx.BitmapFromBuffer(self.image.width, self.image.height, self.image.tostring())
 
 
 
 
		# Eléments graphiques
		texte1 = wx.StaticText(self, -1, "Entrez votre nom :")
		self.votreNom = wx.TextCtrl(self)
 		bouton_ok = wx.Button(self, wx.ID_OK)
		self.bitmapWidget = wx.StaticBitmap(self, -1, self.imBmp)
 
		# Disposition
		self.boitePrincipale = wx.BoxSizer(wx.VERTICAL)
		boite = wx.BoxSizer(wx.HORIZONTAL)
		boiteVideos = wx.BoxSizer(wx.HORIZONTAL)
 
		# Rangement
		boiteVideos.Add(self.bitmapWidget)
		boite.Add(texte1)
		boite.Add(self.votreNom)
		self.boitePrincipale.Add(boiteVideos, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
		self.boitePrincipale.Add(boite, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
		self.boitePrincipale.Add(bouton_ok, flag=wx.ALIGN_CENTER | wx.ALL, border=10)
 
		self.SetSizerAndFit(self.boitePrincipale)
		self.Centre()
		self.Show()
 
	def _getImage(self):
		print "entree dans la fonction getimage"
		monImage = QueryFrame(self.camera)
		return monImage
 
	def _setImage(self):
		print "entree dans la fonction setimage"
		self.bitmapWidget.SetBitmap(self.imBmp)
 
	def _loop_get(self):
		print "entree dans la fonction loopget"
		while self.alive:
			print "entree dans le while loopget"
			self.image = self._getImage()
			self.imBmp = wx.BitmapFromBuffer(self.image.width, self.image.height, self.image.tostring())
			time.sleep(QUANTUM_GET)
			print "alive dans loop_get ? (on boucle si True) :", self.alive, "\n"
 
	def _loop_set(self):
		print "entree dans la fonction loopset"
		while self.alive:
			print "entree dans le while loopset"
			self._setImage()
			time.sleep(QUANTUM_SET)
			print "alive dans loop_set ? (on boucle si True) :", self.alive, "\n"
 
	def start(self):
		print "entree dans la fonction start"
		thread1 = threading.Thread(None, self._loop_get, "Thread-1")
        	thread2 = threading.Thread(None, self._loop_set, "Thread-2")
		print "les deux threads sont crees, on va les faire tourner"
		thread1.start()
		print "thread1 lancé"
		thread2.start()
		print "thread2 lancé"
 
	def stop(self):
		self.alive = False
 
class MonAppli(wx.App):
	def OnInit(self):
		print "entree programme"
		fenetre=MaFenetre(None, -1, 'Demonstrateur')
		print "fenetre initialisee, on va lancer 'start'"
		fenetre.start()
		print "on sort de 'start'"
		#fenetre.stop()
		return True
 
app = MonAppli()
app.MainLoop()
J'ai vraiment l'impression qu'il manque un petit rien mais je ne le trouve pas

Merci d'avance !

Besaïl
besail est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/05/2012, 12h00   #23
besail
Invité de passage
 
Homme Basile Sénéchal
Étudiant
Inscription : avril 2012
Messages : 13
Détails du profil
Informations personnelles :
Nom : Homme Basile Sénéchal
Localisation : France, Essonne (Île de France)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2012
Messages : 13
Points : 0
Points : 0
un CallAfter(...) pourrait-il me tirer d'affaire ?
besail est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/05/2012, 19h31   #24
afranck64
Membre Expert
 
Avatar de afranck64
 
Homme Franck Awounang N.
Étudiant
Inscription : janvier 2009
Messages : 587
Détails du profil
Informations personnelles :
Nom : Homme Franck Awounang N.
Âge : 21
Localisation : Autre

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : janvier 2009
Messages : 587
Points : 1 025
Points : 1 025
Envoyer un message via Yahoo à afranck64
du moins, un wxThread est preferable a un threading.Thread.

Ca ne coute rien d'essayer les callAfter
__________________
Win 7 HP 64 bits /Ubuntu 12.04, - AMD A6 Quad: Py32 / Py27
Citation:
CONTENU D'UNE QUESTION
Exemples:
- Configuration (système d'exploitation, version de Python et des bibliothèques utilisées)
- Code source du morceau de programme où il y a un bogue
- Ligne de code sur laquelle le bogue apparaît
- Erreur complète retournée pas l'interpréteur Python
- Recherche déjà effectuée (FAQ, Tuto, Web, ...)
- Tests déjà effectués
afranck64 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/05/2012, 09h54   #25
besail
Invité de passage
 
Homme Basile Sénéchal
Étudiant
Inscription : avril 2012
Messages : 13
Détails du profil
Informations personnelles :
Nom : Homme Basile Sénéchal
Localisation : France, Essonne (Île de France)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2012
Messages : 13
Points : 0
Points : 0
Citation:
Envoyé par afranck64 Voir le message
du moins, un wxThread est preferable a un threading.Thread.
J'aurais bien essayé mais visiblement en python, les wxThreads ne sont pas supportés.
Je vais trouver un itinéraire de secours !

Besaïl
besail est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/06/2012, 16h39   #26
besail
Invité de passage
 
Homme Basile Sénéchal
Étudiant
Inscription : avril 2012
Messages : 13
Détails du profil
Informations personnelles :
Nom : Homme Basile Sénéchal
Localisation : France, Essonne (Île de France)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : avril 2012
Messages : 13
Points : 0
Points : 0
Salutations, je reviens à la charge
J'ai modifié quelques trucs, j'ai plus qu'un seul thread que j'ai défini en tant que classe et j'utilise Publisher() pour faire le reste.
Ca tourne mais avec un lag monumental. En fait je pense avoir compris mon problème :
Je dois convertir l'image récupérée par la webcam (iplImage) en Bitmap pour pouvoir l'afficher dans mon BitmapWidget, mais le problème c'est que le temps qu'il convertisse l'image en bitmap, le thread qui récupère l'image depuis la webcam renvoie une nouvelle image et ça crée un conflit ou quelquechose du genre, et du coup je vois pas trop comment je peux faire du temps réel.

Je vais tenter PyQt je pense...
A bientot
Besail

Code :
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()
besail est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 01h07.


 
 
 
 
Partenaires

Hébergement Web