Bonsoir a tous, cela fait 2 heures que je galère sur mon projet d'info a rendre mardi..
Alors je viens vous demander de l'aide humblement
Mon projet consiste a créer une petite applications PC(Windows) Qui permette de prendre une photo en direct avec une webcam, puis de la modifier avec des filtres photos.
Pour les filtres photo c'est OK.
Voici le code pour l'instant :
Le but est d'afficher le flux webcam dans le plus grand cadre (Où il y a pour l'instant un cameleon).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 from Tkinter import * from PIL import Image, ImageTk import webbrowser def PrendrePhoto(): print "yeah" def OuvertureImage(): print"oh" def Enregistrer(): print"" def Tweet(): webbrowser.open('https://twitter.com/') def Fb(): webbrowser.open("https://fr-fr.facebook.com/login.php") #cr?ation des filtres def filtre_color(): color=var.get() im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = im.getpixel((x,y)) if (color==0): im2.putpixel((x,y),(p[0],0,0)) if (color==1): im2.putpixel((x,y),(0,p[1],0)) if (color==2): im2.putpixel((x,y),(0,0,p[2])) #endif #endfor #endfor im2.save("im_color.png") Cfiltre1.delete(ALL) im3 = ImageTk.PhotoImage(im2) Im_Color = Cfiltre1.create_image(74,51, image=im3) def filtre_B_W(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = im.getpixel((x,y)) gris = int(p[0]*0.299+p[1]*0.587+p[2]*0.114) im2.putpixel((x,y),(gris,gris,gris)) #endif #endfor #endfor im2.save("im_B-W.png") def filtre_negatif(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = im.getpixel((x,y)) im2.putpixel((x,y),(abs(p[0]-255),abs(p[1]-255),abs(p[2]-255))) #endif #endfor #endfor im2.save("im_negative.png") def filtre_contrast(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = list(im.getpixel((x,y))) for i in range (0,3): if p[i]<30: p[i]=0 if p[i]>225: p[i]=255 else : p[i] = int((255.0 / 195.0) * (p[i] - 30) + 0.5) im2.putpixel((x,y),(p[0],p[1],p[2])) #endif #endfor #endfor im2.save("im_contrast.png") def filtre_lumin(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = list(im.getpixel((x,y))) for i in range (0,3): p[i]= p[i]+30 im2.putpixel((x,y),(p[0],p[1],p[2])) #endif #endfor #endfor im2.save("im_lumin.png") fen1 = Tk() #fen1.wm_state(newstate="zoomed") fen1.configure(width=960, height=621) #cr?ation de la fenetre principale fen1.resizable(width=False, height=False) #ouverture des images background = Image.open('fond3.png') #background = background.resize((640,413)) Backg = ImageTk.PhotoImage(background) OuvertImage = Image.open('choisirunephoto3.png') #OuvertImage = OuvertImage.resize((172,34)) OuvImage = ImageTk.PhotoImage(OuvertImage) TakePhoto = Image.open('prendreunephoto3.png') #TakePhoto = TakePhoto.resize((172,34)) Photo = ImageTk.PhotoImage(TakePhoto) SvIm = Image.open('enregistrerlaphoto3.png') SavePhoto = ImageTk.PhotoImage(SvIm) cameleon = Image.open('filtre3.png') #cameleon = cameleon.resize((120,120)) Filtr1 = ImageTk.PhotoImage(cameleon) fb = Image.open('facebook3.png') #fb = fb.resize((120,120)) facebook = ImageTk.PhotoImage(fb) tw = Image.open('twitter3.png') #tw = tw.resize((120,120)) twitter = ImageTk.PhotoImage(tw) imColor = Image.open("im_color.png") ImColor = ImageTk.PhotoImage(imColor) imBW = Image.open("im_B-W.png") ImBW = ImageTk.PhotoImage(imBW) imContrast = Image.open("im_contrast.png") ImContrast = ImageTk.PhotoImage(imContrast) imLumin = Image.open("im_lumin.png") ImLumin = ImageTk.PhotoImage(imLumin) #cr?ation canvas fond ecran BG = Canvas(fen1, height=621, width=960) BG.place(x=0, y=0) Bg = BG.create_image(479,309, image=Backg) #cr?ation canvas filtre Cfiltre1 = Canvas (fen1, height=102, width=148) Cfiltre1.place(x=143, y=381) Cfiltre2 = Canvas (fen1, height=102, width=148) Cfiltre2.place(x=318, y=381) Cfiltre3 = Canvas (fen1, height=102, width=148) Cfiltre3.place(x=493, y=381) Cfiltre4 = Canvas (fen1, height=102, width=148) Cfiltre4.place(x=667, y=381) #affichage images filtres Im_Color = Cfiltre1.create_image(74,51, image=ImColor) Im_BW = Cfiltre2.create_image(74,51,image=ImBW) Im_Contrast = Cfiltre3.create_image(74,51,image=ImContrast) Im_Lumin = Cfiltre4.create_image(74,51,image=ImLumin) #cr?ation des boutons InserezImage = Button(fen1, height=48, width=278, image=OuvImage, relief=FLAT, command=OuvertureImage) InserezImage.place(x=506, y=111) CaptPhoto = Button(fen1, height=48, width=278, image=Photo, relief=FLAT, command=OuvertureImage) CaptPhoto.place(x=506, y=185) SaveImage = Button(fen1, height=60, width=286, image=SavePhoto, relief=FLAT, command=OuvertureImage) SaveImage.place(x=501, y=271) FaceBook = Button(fen1, height=86, width=87, image=facebook, command=Fb) FaceBook.place(x=851,y=385) Twitter = Button(fen1, height=86, width=87, image=twitter, command=Tweet) Twitter.place(x=851, y=485) #cr?ation radio bouton var = IntVar() filtre1 = Radiobutton(fen1, height=1, width=1, variable=var, value=0, command=filtre_color) filtre1.place(x=158, y=520) filtre2 = Radiobutton(fen1, height=1, width=1, variable=var, value=1, command=filtre_color) filtre2.place(x=198, y=520) filtre3 = Radiobutton(fen1, height=1, width=1, variable=var, value=2, command=filtre_color) filtre3.place(x=238, y=520) filtre4 = Radiobutton(fen1, height=1, width=1, variable=var, value=4, command=filtre_B_W) filtre4.place(x=375, y=520) filtre5 = Radiobutton(fen1, height=1, width=1, variable=var, value=5, command=filtre_contrast) filtre5.place(x=552, y=520) filtre6 = Radiobutton(fen1, height=1, width=1, variable=var, value=6, command=filtre_lumin) filtre6.place(x=729, y=520) fen1.mainloop()
On a opter pour le module videoCapture qui apparemment permet ce genre de choses.
Voici un code que j'ai récupérer sur un site et modifier légèrement:
J'ai donc réussi à afficher le flux de ma webcam dans un Frame contenant lui-même un canvas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 from VideoCapture import Device from PIL import Image, ImageTk import ImageFilter from Tkinter import * import time class Affichage(Frame): """ Affiche L'image Calculee """ def __init__(self, parent, temps="0:0:0"): Frame.__init__(self, parent) self.parent = parent self.CameraGauche = Device(devnum = 0, showVideoWindow =0) self.AffichageG = Canvas(self, width = 336, height = 254) self.AffichageG.grid(row=1, column=1) self.Rafraichis() def Rafraichis(self): self.PhotoGauche = self.CameraGauche.getImage() self.ImageGauche = ImageTk.PhotoImage(self.PhotoGauche) self.AffichageG.create_image(160,120, image = self.ImageGauche) self.parent.after(100, self.Rafraichis) root = Tk() root.title('Vision Stereo ....') Rendu = Affichage(root) Rendu.place(x=0, y=0) root.mainloop()
Mais je ne connais pas du tout la programmation orientée objet, alors avec un petit tuto, j'ai compris le code dans ses grandes lignes. (Définition de la classe Affichage semblable aux frame etc.)
Pour finir, j'ai essayer d'intégrer Le dernier code dans mon projet. ce qui donne :
-> Les lignes de code rajoutées sont précédées du commentaire "Nouvelles lignes de code". (soit tout en haut et tout en bas)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 from Tkinter import * from PIL import Image, ImageTk import webbrowser from VideoCapture import Device import time #--> Nouvelles lignes de code <--# class Affichage(Frame): """ Affiche L'image Calculee """ def __init__(self, parent, temps="0:0:0"): Frame.__init__(self, parent) self.parent = parent self.CameraGauche = Device(devnum = 0, showVideoWindow =0) self.AffichageG = Canvas(self, width = 336, height = 254) self.AffichageG.place(x=138, y=88) self.Rafraichis() def Rafraichis(self): self.PhotoGauche = self.CameraGauche.getImage() self.ImageGauche = ImageTk.PhotoImage(self.PhotoGauche) self.AffichageG.create_image(160,120, image = self.ImageGauche) self.parent.after(100, self.Rafraichis) #------------------------------# def WebCam(): a def OuvertureImage(): cam1 = Device(devnum=0, showVideoWindow=1) imagecam1 = cam1.getImage() imagecam1.save("Main_Im.png") Main_Im=Image.open("Main_Im.png") Main_Image = ImageTk.PhotoImage(Main_Im) Main_Photo = CanPrinc.create_image(168,127, Main_Image) def Enregistrer(): print"" def Tweet(): webbrowser.open('fr.twitter.com') def Fb(): webbrowser.open('fr-fr.facebook.com') #cr?ation des filtres def filtre_color(): color=var.get() im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = im.getpixel((x,y)) if (color==0): im2.putpixel((x,y),(p[0],0,0)) if (color==1): im2.putpixel((x,y),(0,p[1],0)) if (color==2): im2.putpixel((x,y),(0,0,p[2])) #endif #endfor #endfor im2.save("im_color.png") def filtre_B_W(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = im.getpixel((x,y)) gris = int(p[0]*0.299+p[1]*0.587+p[2]*0.114) im2.putpixel((x,y),(gris,gris,gris)) #endif #endfor #endfor im2.save("im_B-W.png") def filtre_negatif(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = im.getpixel((x,y)) im2.putpixel((x,y),(abs(p[0]-255),abs(p[1]-255),abs(p[2]-255))) #endif #endfor #endfor im2.save("im_negative.png") def filtre_contrast(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = list(im.getpixel((x,y))) for i in range (0,3): if p[i]<30: p[i]=0 if p[i]>225: p[i]=255 else : p[i] = int((255.0 / 195.0) * (p[i] - 30) + 0.5) im2.putpixel((x,y),(p[0],p[1],p[2])) #endif #endfor #endfor im2.save("im_contrast.png") def filtre_lumin(): im=Image.open("filtre3.png") #im.show() w,h=im.size im2 = Image.new("RGB", (w,h)) for y in range (h): for x in range(w): p = list(im.getpixel((x,y))) for i in range (0,3): p[i]= p[i]+30 im2.putpixel((x,y),(p[0],p[1],p[2])) #endif #endfor #endfor im2.save("im_lumin.png") fen1 = Tk() #fen1.wm_state(newstate="zoomed") fen1.configure(width=960, height=621) #cr?ation de la fenetre principale fen1.resizable(width=False, height=False) #ouverture des images background = Image.open('fond3.png') #background = background.resize((640,413)) Backg = ImageTk.PhotoImage(background) OuvertImage = Image.open('choisirunephoto3.png') #OuvertImage = OuvertImage.resize((172,34)) OuvImage = ImageTk.PhotoImage(OuvertImage) TakePhoto = Image.open('prendreunephoto3.png') #TakePhoto = TakePhoto.resize((172,34)) Photo = ImageTk.PhotoImage(TakePhoto) SvIm = Image.open('enregistrerlaphoto3.png') SavePhoto = ImageTk.PhotoImage(SvIm) cameleon = Image.open('filtre3.png') #cameleon = cameleon.resize((120,120)) Filtr1 = ImageTk.PhotoImage(cameleon) fb = Image.open('facebook3.png') #fb = fb.resize((120,120)) facebook = ImageTk.PhotoImage(fb) tw = Image.open('twitter3.png') #tw = tw.resize((120,120)) twitter = ImageTk.PhotoImage(tw) imColor = Image.open('im_color.png') Im_Color = ImageTk.PhotoImage(imColor) imBW = Image.open('im_B-W.png') Im_BW = ImageTk.PhotoImage(imBW) imLumin = Image.open('im_lumin.png') Im_Lumin = ImageTk.PhotoImage(imLumin) imContrast = Image.open('im_contrast.png') Im_Contrast = ImageTk.PhotoImage(imContrast) #cr?ation canvas fond ecran BG = Canvas(fen1, height=621, width=960) BG.place(x=0, y=0) Bg = BG.create_image(479,309, image=Backg) #cr?ation canvas filtre Cfiltre1 = Canvas (fen1, height=102, width=148) Cfiltre1.place(x=143, y=381) Cfiltre2 = Canvas (fen1, height=102, width=148) Cfiltre2.place(x=318, y=381) Cfiltre3 = Canvas (fen1, height=102, width=148) Cfiltre3.place(x=493, y=381) Cfiltre4 = Canvas (fen1, height=102, width=148) Cfiltre4.place(x=667, y=381) #affichage images filtres ImColor = Cfiltre1.create_image(74,51,image=Im_Color) ImBW = Cfiltre2.create_image(74,51,image=Im_BW) ImContrast = Cfiltre3.create_image(74,51,image=Im_Contrast) ImLumin = Cfiltre4.create_image(74,51,image=Im_Lumin) #--> Création canvas photo principale <-- #CanPrinc = Canvas (fen1, height=254, width=336) #CanPrinc.place(x=138, y=88) #cr?ation des boutons InserezImage = Button(fen1, height=48, width=278, image=OuvImage, relief=FLAT, command=OuvertureImage) InserezImage.place(x=506, y=111) CaptPhoto = Button(fen1, height=48, width=278, image=Photo, relief=FLAT, command=WebCam) CaptPhoto.place(x=506, y=185) SaveImage = Button(fen1, height=60, width=286, image=SavePhoto, relief=FLAT, command=OuvertureImage) SaveImage.place(x=501, y=271) Twitter = Button(fen1, height=87, width=87, image=twitter, command=Tweet) Twitter.place(x=850, y=480) FaceBook = Button(fen1, height=87, width=87, image=facebook, command=Fb) FaceBook.place(x=850, y=370) #cr?ation radio bouton var = IntVar() filtre1 = Radiobutton(fen1, height=1, width=1, bg='white', variable=var, value=0, command=filtre_color) filtre1.place(x=198, y=520) filtre2 = Radiobutton(fen1, height=1, width=1, variable=var, value=1, command=filtre_color) filtre2.place(x=158, y=520) filtre3 = Radiobutton(fen1, height=1, width=1, variable=var, value=2, command=filtre_color) filtre3.place(x=238, y=520) filtre4 = Radiobutton(fen1, height=1, width=1, variable=var, value=4, command=filtre_B_W) filtre4.place(x=375, y=520) filtre5 = Radiobutton(fen1, height=1, width=1, variable=var, value=5, command=filtre_contrast) filtre5.place(x=552, y=520) filtre6 = Radiobutton(fen1, height=1, width=1, variable=var, value=6, command=filtre_lumin) filtre6.place(x=729, y=520) #--> Nouvelles lignes de code <--# Rendu = Affichage(fen1) Rendu.place(x=0, y=0) #------------------------------# fen1.title('Cameleon') fen1.mainloop()
Mais comme vous pouvait le constater, le flux de la webcam ne s'affiche pas dans mon canvas.
Je ne comprend pas du tout pourquoi, alors si une âme charitable passe par là, je lui serait grandement reconnaissant de m'éclairer
Merci d'avance !
Partager