質問編集履歴
1
質問の訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -50,469 +50,7 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
-
import sys
|
54
53
|
|
55
|
-
from PyQt5.QtWidgets import *
|
56
|
-
|
57
|
-
from PyQt5.QtGui import *
|
58
|
-
|
59
|
-
from PyQt5.QtCore import *
|
60
|
-
|
61
|
-
import subprocess
|
62
|
-
|
63
|
-
import time
|
64
|
-
|
65
|
-
import numpy as np
|
66
|
-
|
67
|
-
import cv2
|
68
|
-
|
69
|
-
import math
|
70
|
-
|
71
|
-
import RPi.GPIO as GPIO
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
import image_processing
|
76
|
-
|
77
|
-
import HSV
|
78
|
-
|
79
|
-
import motor
|
80
|
-
|
81
|
-
import Gyro
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
class Tab1Widget(QWidget):
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
def __init__(self):
|
90
|
-
|
91
|
-
super().__init__()
|
92
|
-
|
93
|
-
self.title = "自律走行車"
|
94
|
-
|
95
|
-
self.left = 50
|
96
|
-
|
97
|
-
self.top = 50
|
98
|
-
|
99
|
-
self.width = 1300
|
100
|
-
|
101
|
-
self.height = 1200
|
102
|
-
|
103
|
-
self.initUI()
|
104
|
-
|
105
|
-
self.counter = 0
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
def initUI(self):
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
super(Tab1Widget, self).__init__()
|
114
|
-
|
115
|
-
self.cap = cv2.VideoCapture(0)
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH,320)
|
120
|
-
|
121
|
-
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT,240)
|
122
|
-
|
123
|
-
self.cap.set(cv2.CAP_PROP_FPS,10)
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
self.camera_run = 0
|
130
|
-
|
131
|
-
self.auto_run = 0
|
132
|
-
|
133
|
-
self.countM = 0
|
134
|
-
|
135
|
-
self.M = 1
|
136
|
-
|
137
|
-
self.Duty_R = self.Duty_L = 0
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
self.Pitch=0
|
142
|
-
|
143
|
-
self.Roll=0
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
self.H1 = 30
|
148
|
-
|
149
|
-
self.H2 = 90
|
150
|
-
|
151
|
-
self.countHSV2 = 0
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
self.view1 = QGraphicsView()
|
158
|
-
|
159
|
-
self.scene1 = QGraphicsScene()
|
160
|
-
|
161
|
-
self.view2 = QGraphicsView()
|
162
|
-
|
163
|
-
self.scene2 = QGraphicsScene()
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
(GUIの設定なので中略)
|
172
|
-
|
173
|
-
簡単に説明
|
174
|
-
|
175
|
-
・GUI上のボタン「カメラスタート」を押すとカメラの出力画像をGUI上に表示
|
176
|
-
|
177
|
-
・また、カメラの傾きを考慮した画像処理を行っていおり、実際に画像処理される範囲をGUI上に表示(これが今回のしたいことです)
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
・GUI上のボタン「自律移動スタート」を押すと画像処理の情報を元に自律移動する
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
def camera_start(self):
|
190
|
-
|
191
|
-
self.camera_run = 1
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
#サーボモータ(正面カメラ)のsetup
|
196
|
-
|
197
|
-
Servo_pin=18
|
198
|
-
|
199
|
-
GPIO.setmode(GPIO.BCM)
|
200
|
-
|
201
|
-
GPIO.setup(Servo_pin, GPIO.OUT)
|
202
|
-
|
203
|
-
self.Servo = GPIO.PWM(Servo_pin, 50)
|
204
|
-
|
205
|
-
self.Servo.start(0)
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
self.camera_set()
|
210
|
-
|
211
|
-
repeatTime = 300
|
212
|
-
|
213
|
-
timer = QTimer(self.view1
|
214
|
-
|
215
|
-
timer.timeout.connect(self.camera_set)
|
216
|
-
|
217
|
-
timer.start(repeatTime)
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
def camera_stop(self):
|
224
|
-
|
225
|
-
self.camera_run = 0##カメラOFF
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
def camera_set(self):
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
#Gyro.pyからデータ取得
|
234
|
-
|
235
|
-
self.Pitch, self.Roll = Gyro.GyroSensor(self.Pitch, self.Roll)
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
#テキストにピッチ角、ロー角を出力
|
240
|
-
|
241
|
-
pitch = str(round(self.Pitch))
|
242
|
-
|
243
|
-
self.textbox7.setText(pitch + "°")
|
244
|
-
|
245
|
-
roll = str(round(self.Roll))
|
246
|
-
|
247
|
-
self.textbox8.setText(roll + "°")
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
ret, cv_img = self.cap.read()
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
if ret == False:
|
256
|
-
|
257
|
-
return
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
#カメラの角度制御
|
262
|
-
|
263
|
-
def servo_angle(angle):
|
264
|
-
|
265
|
-
duty = 2.5 + (12.0 - 2.5) * (angle + 90) / 180
|
266
|
-
|
267
|
-
self.Servo.ChangeDutyCycle(duty)
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
#ピッチ角分サーボを回転
|
272
|
-
|
273
|
-
servo_angle(round(self.Pitch))
|
274
|
-
|
275
|
-
#サーボモータをストップ
|
276
|
-
|
277
|
-
self.Servo.stop()
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
#ロー角分画像回転
|
282
|
-
|
283
|
-
height = cv_img.shape[0]
|
284
|
-
|
285
|
-
width = cv_img.shape[1]
|
286
|
-
|
287
|
-
center = (int(width/2),int(height/2))
|
288
|
-
|
289
|
-
angle = round(self.Roll*(-1))
|
290
|
-
|
291
|
-
scale = 1.0
|
292
|
-
|
293
|
-
trans = cv2.getRotationMatrix2D(center,angle,scale)
|
294
|
-
|
295
|
-
self.cv_img = cv2.warpAffine(cv_img,trans,(width,height))
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
#グレースケール変換
|
300
|
-
|
301
|
-
cv_img_RGB = cv2.cvtColor(self.cv_img,cv2.COLOR_BGR2RGB)
|
302
|
-
|
303
|
-
#リサイズ
|
304
|
-
|
305
|
-
cv_img_RGB = cv2.resize(cv_img_RGB,(320,240))
|
306
|
-
|
307
|
-
#画像処理範囲を赤枠で囲む(分かりやすいように)
|
308
|
-
|
309
|
-
cv_img_RGB = cv2.rectangle(cv_img_RGB, (40-round(460.7016*math.cos(math.radians(90-self.Roll))), 90), (280-round(460.7016*math.cos(math.radians(90-self.Roll))), 240), (255, 0, 0), 2, 3)
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
height, width, dim = cv_img_RGB.shape
|
314
|
-
|
315
|
-
bytesPerLine = dim * width
|
316
|
-
|
317
|
-
self.image1 = QImage(cv_img_RGB.data, width, height, bytesPerLine, QImage.Format_RGB888)
|
318
|
-
|
319
|
-
self.item1 = QGraphicsPixmapItem(QPixmap.fromImage(self.imag1e))
|
320
|
-
|
321
|
-
self.scene1.addItem(self.item1)
|
322
|
-
|
323
|
-
self.view1.setScene(self.scene1)
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
self.img_src = self.cv_img
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
print("あいうえお")
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
#画像処理範囲を切り抜いた画像をGUI上に出力
|
338
|
-
|
339
|
-
#画像処理した画像をimage_processing.pyから持ってくる
|
340
|
-
|
341
|
-
pixel, img_src0, elapsed_time, self.countM, img_dst3 = image_processing.img_process(self.auto_run,self.img_src,self.countM,self.H1,self.H2, self.Roll)
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
#マスク処理を施した画像を表示
|
346
|
-
|
347
|
-
height2, width2, dim2 = img_dst3.shape
|
348
|
-
|
349
|
-
bytesPerLine2 = dim2 * width2
|
350
|
-
|
351
|
-
self.image2 = QImage(img_dst3.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
|
352
|
-
|
353
|
-
self.item2 = QGraphicsPixmapItem(QPixmap.fromImage(self.image2))
|
354
|
-
|
355
|
-
self.scene2.addItem(self.item2)
|
356
|
-
|
357
|
-
self.view2.setScene(self.scene2)
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
#
|
362
|
-
|
363
|
-
pixcel = str(round(pixcel,3))
|
364
|
-
|
365
|
-
self.textbox4.setText(picxel)
|
366
|
-
|
367
|
-
elapsed_time = str(round(elapsed_time,3))
|
368
|
-
|
369
|
-
self.textbox5.setText(elapsed_time)
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
#出力データをファイルに書き込み
|
374
|
-
|
375
|
-
MPU= open('/home/pi/Desktop/data','a')
|
376
|
-
|
377
|
-
MPU.write( "Pitch:"+pitch+""+"Roll:"+roll + "\n")
|
378
|
-
|
379
|
-
MPU.close
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
def auto_move(self):
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
self.auto_run = 1
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
以下 自律移動を行うためのコード(省略)
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
if __name__ == "__main__":
|
398
|
-
|
399
|
-
app = QApplication(sys.argv)
|
400
|
-
|
401
|
-
ex = Tab1Widget()
|
402
|
-
|
403
|
-
sys.exit(app.exec_())
|
404
|
-
|
405
|
-
```
|
406
|
-
|
407
|
-
```python
|
408
|
-
|
409
|
-
ファイル名:image_processing.py
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
import numpy as np
|
414
|
-
|
415
|
-
import math
|
416
|
-
|
417
|
-
import cv2
|
418
|
-
|
419
|
-
import time
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
import P_data
|
424
|
-
|
425
|
-
import HSV
|
426
|
-
|
427
|
-
import motor
|
428
|
-
|
429
|
-
import RPi.GPIO as GPIO
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
def img_process(auto_run,img_src,countM,H1,H2,Roll):
|
436
|
-
|
437
|
-
t1 = time.time()
|
438
|
-
|
439
|
-
if auto_run == 0:
|
440
|
-
|
441
|
-
return
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
M = 1
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
#画像サイズ変更(処理速度に応じて変更すべし)
|
450
|
-
|
451
|
-
img_src=cv2.resize(img_src,(320,240))
|
452
|
-
|
453
|
-
img_src=img_src[90:240, 40-round(460.7016*math.cos(math.radians(90-Roll))):280-round(460.7016*math.cos(math.radians(90-Roll)))]
|
454
|
-
|
455
|
-
hsv = cv2.cvtColor(img_src, cv2.COLOR_BGR2HSV)
|
456
|
-
|
457
|
-
img_src0 = cv2.cvtColor(img_src, cv2.COLOR_BGR2RGB)
|
458
|
-
|
459
|
-
lower_green = np.array([H1, 50, 30])
|
460
|
-
|
461
|
-
upper_green = np.array([H2, 255,255])
|
462
|
-
|
463
|
-
img_mask = cv2.inRange(hsv, lower_green, upper_green)
|
464
|
-
|
465
|
-
neiborhood8 = np.array([[1, 1, 1],[1, 1, 1],[1, 1, 1]],np.uint8)
|
466
|
-
|
467
|
-
img_erosion = cv2.erode(img_mask,neiborhood8,iterations=2)
|
468
|
-
|
469
|
-
img_erosion = cv2.erode(img_erosion,neiborhood8,iterations=2)
|
470
|
-
|
471
|
-
img_dilation = cv2.dilate(img_erosion,neiborhood8,iterations=2)
|
472
|
-
|
473
|
-
img_dilation = cv2.dilate(img_dilation,neiborhood8,iterations=2)
|
474
|
-
|
475
|
-
img_dst = cv2.bitwise_and(img_src, img_src, mask=img_dilation)
|
476
|
-
|
477
|
-
img_dst2 = cv2.bitwise_and(img_src, img_src, mask=img_mask)
|
478
|
-
|
479
|
-
img_dst3 = cv2.bitwise_and(img_src, img_src, mask=img_dilation)
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
start = time.time()
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
row_mean_L , row_mean_R ,pixcel = P_data.Get_MotorParameter(img_mask, 1, F3)
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
F3 = int(-pixcel)
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
count_hsv += 1
|
498
|
-
|
499
|
-
if count_hsv == 20 or count_hsv == 25 or count_hsv == 30 :
|
500
|
-
|
501
|
-
img_dst, H1, H2 ,S1 , S2 , countHSV2 = HSV.HSV_value (img_dst, H1, H2,countHSV2)
|
502
|
-
|
503
|
-
countM += 1
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
t2 = time.time()
|
508
|
-
|
509
|
-
elapsed_time = t2 - t1
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
return pixcel, img_src0, elapsed_time, countM, img_dst3
|
516
54
|
|
517
55
|
```
|
518
56
|
|
@@ -528,7 +66,7 @@
|
|
528
66
|
|
529
67
|
しかし、その下にある
|
530
68
|
|
531
|
-
>
|
69
|
+
>
|
532
70
|
|
533
71
|
|
534
72
|
|