Bonjour,
Je galère sur un nouveau projet où je souhaite intégrer une fonction dans laquelle j'allume la webcam du PC et je doit pouvoir prendre des photos.
Ci-dessous le code de mon application, pour démarrer vous devez d'abord vous créer un répertoire nommé "Affaires" sur votre PC. Ensuite, créez une affaire via l'IHM (peu importe le nombre de paramètres). Pour cela vous devez remplir le champ sous "Nouvelle affaire" et les champs en face des "Nom Paramètre" puis cliquez sur "Créer". Le bouton "Photo" doit apparaître.
Lorsqu'on clique dessus, ça ouvre une deuxième fenêtre contenant le flux vidéo de la caméra (normal puisqu'il y a l'instruction.
Code : Sélectionner tout - Visualiser dans une fenêtre à part cv2.imshow('my webcam', img)
Ce que j'aimerais comme résultat c'est quelque chose dans ce style là (code récupérer sur le net ci-dessous). J'aimerais que le flux vidéo soit intégrer dans le widget_2.
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 #!/usr/bin/python # -*- coding: utf-8 -*- from PyQt4 import QtGui, QtCore from PyQt4.QtGui import QFileDialog from PyQt4.QtGui import QImage import time import os.path import os from os import walk import shutil import win32com.client as win32 from openpyxl import Workbook import cv2 import sys #appel librairie import IHM_Tablette path_work_dir = "chemin vers votre répertoire" # initialisation def liste_affaires(a) : listerep = [] listerep=os.listdir(a) return listerep def fichier_ini (a) : if os.path.isdir(path_work_dir) == False : os.mkdir(path_work_dir) path_work_dir_affaire = path_work_dir +"\\" + a["affaire"] if os.path.isdir(path_work_dir_affaire) == False : os.mkdir(path_work_dir_affaire) path_work_dir_photo = path_work_dir +"\\" + a["affaire"] +"\\Photos" if os.path.isdir(path_work_dir_photo) == False : os.mkdir(path_work_dir_photo) path_logfile_affaire = path_work_dir +str("\\") +a["affaire"] +str("\\log_file.txt") ini_file = open(path_logfile_affaire,'a') for m in range (1,len(a)): ini_file.write( str(a[m]) + "\n") ini_file.close() def lecture_log_file (a) : file_txt = open(a, "r") data_txt = file_txt.readlines() file_txt.close() data_affaire = {} for h in range (len (data_txt)) : data_affaire[h + 1] = data_txt[h].strip() return data_affaire class ImageViewer(QtGui.QMainWindow, IHM_Tablette.Ui_MainWindow): def __init__(self, parent=None): super(ImageViewer, self).__init__(parent) self.setupUi(self) self.connectActions() self.spinBox.setValue(5) self.spinBox.setRange(1,10) def main(self): self.show() liste = liste_affaires (path_work_dir) self.affairebox.addItems(liste) self.affairebox.addItem("Nouvelle affaire") rank = self.affairebox.findText("Nouvelle affaire") self.affairebox.setCurrentIndex(rank) self.pushButton.hide() self.pushButton_5.hide() self.pushButton_6.hide() self.pushButton_7.hide() self.widget.hide() def gestion_affaire(self) : if self.affairebox.currentText() == "Nouvelle affaire" : self.effacer() self.pushButton_5.hide() self.pushButton_6.hide() self.pushButton_7.hide() self.pushButton_2.show() self.pushButton_3.show() self.pushButton.hide() for l in range(1,self.spinBox.value()+1) : eval("self.champ_" + str(l)+".show()") eval("self.label_" + str(l)+".show()") else : path_logfile_affaire = path_work_dir + "\\" + str(self.affairebox.currentText()) + "\\log_file.txt" data_affaire = lecture_log_file(path_logfile_affaire) for d in range (len(data_affaire.keys()) +1,11): eval("self.champ_" + str(d)+".hide()") eval("self.label_" + str(d)+".hide()") for e in range (1,len(data_affaire.keys())+1): eval("self.champ_" + str(e)+".show()") eval("self.label_" + str(e)+".show()") for key in data_affaire : eval("self.label_" + str(key) +".setText(\""+ data_affaire[key] +"\")") self.champ_11.hide() self.label_11.hide() self.spinBox.hide() self.pushButton_5.show() self.pushButton_6.show() self.pushButton_7.show() self.pushButton_2.hide() self.pushButton_3.hide() self.pushButton.show() def IHM(self): for l in range(1,self.spinBox.value()+1) : eval("self.champ_" + str(l)+".show()") eval("self.label_" + str(l)+".show()") for k in range (self.spinBox.value()+1,11): eval("self.champ_" + str(k)+".hide()") eval("self.label_" + str(k)+".hide()") def effacer(self): for j in range (1,12) : efface = "self.champ_" + str(j) +".clear()" eval(efface) self.spinBox.show() self.spinBox.setValue(5) self.textBrowser.clear() rank = self.affairebox.findText("Nouvelle affaire") self.affairebox.setCurrentIndex(rank) self.champ_11.show() self.label_11.show() self.label_1.setText("Nom Param\xe8tre 1") self.label_2.setText("Nom Param\xe8tre 2") self.label_3.setText("Nom Param\xe8tre 3") self.label_4.setText("Nom Param\xe8tre 4") self.label_6.setText("Nom Param\xe8tre 6") self.label_5.setText("Nom Param\xe8tre 5") self.label_7.setText("Nom Param\xe8tre 7") self.label_8.setText("Nom Param\xe8tre 8") self.label_9.setText("Nom Param\xe8tre 9") self.label_10.setText("Nom Param\xe8tre 10") self.pushButton_5.hide() self.pushButton_6.hide() self.pushButton_7.hide() self.pushButton.show() def effacer_2 (self) : for f in range (1,10) : if eval("self.champ_" + str(f) +".isVisible() ") == True : eval("self.champ_" + str(f)+".clear()") def cliquer(self): data ={} if self.champ_11.text() == "" : self.textBrowser.append("Veuiller saisir un num\xe9ro d'affaire") else : data["affaire"] = self.champ_11.text() for i in range (1,10) : if eval("self.champ_" + str(i) +".isVisible() ") == True : if eval("self.champ_" + str(i) +".text()") != "" : data[i] = eval("self.champ_" + str(i) +".text()") else : self.textBrowser.append("Compl\xe9ter le nom du param\xe8tre " + str(i)) fichier_ini(data) self.textBrowser.append("Affaire cr\xe9\xe9e") self.affairebox.addItem(str(data["affaire"])) rank = self.affairebox.findText(str(data["affaire"])) self.affairebox.setCurrentIndex(rank) self.champ_11.hide() self.label_11.hide() self.spinBox.hide() self.pushButton_5.show() self.pushButton_6.show() self.pushButton_7.show() self.pushButton_2.hide() self.pushButton_3.hide() self.pushButton.show() for f in range (1,10) : if eval("self.champ_" + str(f) +".isVisible() ") == True : eval("self.label_"+str(f)+".setText(data[" + str(f)+ "])") eval("self.champ_" + str(f)+".clear()") def supprimer(self) : path_xls_file = path_work_dir + "\\" + self.affairebox.currentText() + "\\" + self.affairebox.currentText()+".xlsx" path_affaire_dir = path_work_dir + "\\" + self.affairebox.currentText() path_rep_photo = path_work_dir + "\\" + self.affairebox.currentText() + "\\Photos" photos_files = os.listdir(path_rep_photo) if os.path.isfile(path_xls_file) == False and photos_files == []: shutil.rmtree(path_affaire_dir) self.affairebox.removeItem(self.affairebox.currentIndex()) rank = self.affairebox.findText("Nouvelle affaire") self.affairebox.setCurrentIndex(rank) self.spinBox.show() self.spinBox.setValue(5) self.textBrowser.clear() self.champ_11.show() self.champ_11.clear() self.label_11.show() self.label_1.setText("Nom Param\xe8tre 1") self.label_2.setText("Nom Param\xe8tre 2") self.label_3.setText("Nom Param\xe8tre 3") self.label_4.setText("Nom Param\xe8tre 4") self.label_6.setText("Nom Param\xe8tre 6") self.label_5.setText("Nom Param\xe8tre 5") self.label_7.setText("Nom Param\xe8tre 7") self.label_8.setText("Nom Param\xe8tre 8") self.label_9.setText("Nom Param\xe8tre 9") self.label_10.setText("Nom Param\xe8tre 10") self.pushButton_5.hide() self.pushButton_6.hide() self.pushButton_7.hide() self.pushButton.hide() self.pushButton_2.show() self.pushButton_3.show() for l in range(1,self.spinBox.value()+1) : eval("self.champ_" + str(l)+".show()") eval("self.label_" + str(l)+".show()") for k in range (self.spinBox.value()+1,11): eval("self.champ_" + str(k)+".hide()") eval("self.label_" + str(k)+".hide()") else : self.textBrowser.append("Impossible de supprimer cette affaire") def remplissage_xls(self) : path_xls_file = path_work_dir + "\\" + self.affairebox.currentText() + "\\" + self.affairebox.currentText()+".xlsx" if os.path.isfile(path_xls_file) == False : wb = Workbook() ws1 = wb.active for f in range (1,10) : if eval("self.champ_" + str(f) +".isVisible() ") == True : ws1.cell(row=1, column =f, value =eval("self.label_" + str(f) +".text() ")) wb.save(path_xls_file) excel = win32.gencache.EnsureDispatch('Excel.Application') wb = excel.Workbooks.Open(path_xls_file) ws = wb.Worksheets("Sheet") derniere_ligne = ws.UsedRange.Rows.Count + 1 for f in range (1,10) : if eval("self.champ_" + str(f) +".isVisible() ") == True : ws.Cells(derniere_ligne,f).Value= eval("self.champ_" + str(f)+".text()") eval("self.champ_" + str(f) +".clear()") excel.Application.DisplayAlerts=False wb.SaveAs(path_xls_file) excel.Application.Quit() def show_webcam(self): cam = cv2.VideoCapture(0) mirror=False while True: ret_val, img = cam.read() if mirror: img = cv2.flip(img, 1) cv2.imshow('my webcam', img) if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() def Ouverture_photo(self): self.widget.show() self.widget_2.show() self.show_webcam() #self.label.setPixmap(QtGui.QPixmap(logo)) def connectActions(self): self.pushButton_4.clicked.connect(self.close) self.spinBox.valueChanged.connect(self.IHM) self.pushButton_3.clicked.connect(self.effacer) self.pushButton_2.clicked.connect(self.cliquer) self.affairebox.activated.connect(self.gestion_affaire) self.pushButton_7.clicked.connect(self.effacer_2) self.pushButton.clicked.connect(self.supprimer) self.pushButton_6.clicked.connect(self.remplissage_xls) self.pushButton_5.clicked.connect(self.Ouverture_photo) self.pushButton_9.clicked.connect(self.widget.close) if __name__=='__main__': app = QtGui.QApplication(sys.argv) imageViewer = ImageViewer() imageViewer.main() app.exec_()
Je tourne un peu en rond, le code que j'ai trouvé sur le net me semble un peu "sur-dimensionné" par rapport à mon besoin. En plus, ça commence à dépasser mes compétences/connaissances en programmation.
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 from PyQt4 import QtCore, QtGui, uic import sys import cv2 import numpy as np import threading import time import Queue running = False capture_thread = None form_class = uic.loadUiType("simple.ui")[0] q = Queue.Queue() def grab(cam, queue, width, height, fps): global running capture = cv2.VideoCapture(cam) capture.set(cv2.CAP_PROP_FRAME_WIDTH, width) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height) capture.set(cv2.CAP_PROP_FPS, fps) while(running): frame = {} capture.grab() retval, img = capture.retrieve(0) frame["img"] = img if queue.qsize() < 10: queue.put(frame) else: print queue.qsize() class OwnImageWidget(QtGui.QWidget): def __init__(self, parent=None): super(OwnImageWidget, self).__init__(parent) self.image = None def setImage(self, image): self.image = image sz = image.size() self.setMinimumSize(sz) self.update() def paintEvent(self, event): qp = QtGui.QPainter() qp.begin(self) if self.image: qp.drawImage(QtCore.QPoint(0, 0), self.image) qp.end() class MyWindowClass(QtGui.QMainWindow, form_class): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) self.setupUi(self) self.startButton.clicked.connect(self.start_clicked) self.window_width = self.ImgWidget.frameSize().width() self.window_height = self.ImgWidget.frameSize().height() self.ImgWidget = OwnImageWidget(self.ImgWidget) self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.update_frame) self.timer.start(1) def start_clicked(self): global running running = True capture_thread.start() self.startButton.setEnabled(False) self.startButton.setText('Starting...') def update_frame(self): if not q.empty(): self.startButton.setText('Camera is live') frame = q.get() img = frame["img"] img_height, img_width, img_colors = img.shape scale_w = float(self.window_width) / float(img_width) scale_h = float(self.window_height) / float(img_height) scale = min([scale_w, scale_h]) if scale == 0: scale = 1 img = cv2.resize(img, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) height, width, bpc = img.shape bpl = bpc * width image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888) self.ImgWidget.setImage(image) def closeEvent(self, event): global running running = False capture_thread = threading.Thread(target=grab, args = (0, q, 1920, 1080, 30)) app = QtGui.QApplication(sys.argv) w = MyWindowClass(None) w.setWindowTitle('Kurokesu PyQT OpenCV USB camera test panel') w.show() app.exec_()
Voila si vous avez des idées je suis preneur, merci d'avance pour votre aide.
Partager