teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問の訂正

2020/09/25 04:59

投稿

noooooooob
noooooooob

スコア16

title CHANGED
File without changes
body CHANGED
@@ -24,246 +24,15 @@
24
24
  ```python
25
25
  ファイル名:autonomous_vehicle.py
26
26
 
27
- import sys
28
- from PyQt5.QtWidgets import *
29
- from PyQt5.QtGui import *
30
- from PyQt5.QtCore import *
31
- import subprocess
32
- import time
33
- import numpy as np
34
- import cv2
35
- import math
36
- import RPi.GPIO as GPIO
37
27
 
38
- import image_processing
39
- import HSV
40
- import motor
41
- import Gyro
42
-
43
- class Tab1Widget(QWidget):
44
-
45
- def __init__(self):
46
- super().__init__()
47
- self.title = "自律走行車"
48
- self.left = 50
49
- self.top = 50
50
- self.width = 1300
51
- self.height = 1200
52
- self.initUI()
53
- self.counter = 0
54
-
55
- def initUI(self):
56
-
57
- super(Tab1Widget, self).__init__()
58
- self.cap = cv2.VideoCapture(0)
59
-
60
- self.cap.set(cv2.CAP_PROP_FRAME_WIDTH,320)
61
- self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT,240)
62
- self.cap.set(cv2.CAP_PROP_FPS,10)
63
-
64
-
65
- self.camera_run = 0
66
- self.auto_run = 0
67
- self.countM = 0
68
- self.M = 1
69
- self.Duty_R = self.Duty_L = 0
70
-
71
- self.Pitch=0
72
- self.Roll=0
73
-
74
- self.H1 = 30
75
- self.H2 = 90
76
- self.countHSV2 = 0
77
-
78
-
79
- self.view1 = QGraphicsView()
80
- self.scene1 = QGraphicsScene()
81
- self.view2 = QGraphicsView()
82
- self.scene2 = QGraphicsScene()
83
-
84
-
85
-
86
- (GUIの設定なので中略)
87
- 簡単に説明
88
- ・GUI上のボタン「カメラスタート」を押すとカメラの出力画像をGUI上に表示
89
- ・また、カメラの傾きを考慮した画像処理を行っていおり、実際に画像処理される範囲をGUI上に表示(これが今回のしたいことです)
90
-
91
- ・GUI上のボタン「自律移動スタート」を押すと画像処理の情報を元に自律移動する
92
-
93
-
94
-
95
- def camera_start(self):
96
- self.camera_run = 1
97
-
98
- #サーボモータ(正面カメラ)のsetup
99
- Servo_pin=18
100
- GPIO.setmode(GPIO.BCM)
101
- GPIO.setup(Servo_pin, GPIO.OUT)
102
- self.Servo = GPIO.PWM(Servo_pin, 50)
103
- self.Servo.start(0)
104
-
105
- self.camera_set()
106
- repeatTime = 300
107
- timer = QTimer(self.view1
108
- timer.timeout.connect(self.camera_set)
109
- timer.start(repeatTime)
110
-
111
-
112
- def camera_stop(self):
113
- self.camera_run = 0##カメラOFF
114
-
115
- def camera_set(self):
116
-
117
- #Gyro.pyからデータ取得
118
- self.Pitch, self.Roll = Gyro.GyroSensor(self.Pitch, self.Roll)
119
-
120
- #テキストにピッチ角、ロー角を出力
121
- pitch = str(round(self.Pitch))
122
- self.textbox7.setText(pitch + "°")
123
- roll = str(round(self.Roll))
124
- self.textbox8.setText(roll + "°")
125
-
126
- ret, cv_img = self.cap.read()
127
-
128
- if ret == False:
129
- return
130
-
131
- #カメラの角度制御
132
- def servo_angle(angle):
133
- duty = 2.5 + (12.0 - 2.5) * (angle + 90) / 180
134
- self.Servo.ChangeDutyCycle(duty)
135
-
136
- #ピッチ角分サーボを回転
137
- servo_angle(round(self.Pitch))
138
- #サーボモータをストップ
139
- self.Servo.stop()
140
-
141
- #ロー角分画像回転
142
- height = cv_img.shape[0]
143
- width = cv_img.shape[1]
144
- center = (int(width/2),int(height/2))
145
- angle = round(self.Roll*(-1))
146
- scale = 1.0
147
- trans = cv2.getRotationMatrix2D(center,angle,scale)
148
- self.cv_img = cv2.warpAffine(cv_img,trans,(width,height))
149
-
150
- #グレースケール変換
151
- cv_img_RGB = cv2.cvtColor(self.cv_img,cv2.COLOR_BGR2RGB)
152
- #リサイズ
153
- cv_img_RGB = cv2.resize(cv_img_RGB,(320,240))
154
- #画像処理範囲を赤枠で囲む(分かりやすいように)
155
- 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)
156
-
157
- height, width, dim = cv_img_RGB.shape
158
- bytesPerLine = dim * width
159
- self.image1 = QImage(cv_img_RGB.data, width, height, bytesPerLine, QImage.Format_RGB888)
160
- self.item1 = QGraphicsPixmapItem(QPixmap.fromImage(self.imag1e))
161
- self.scene1.addItem(self.item1)
162
- self.view1.setScene(self.scene1)
163
-
164
- self.img_src = self.cv_img
165
-
166
-
167
- print("あいうえお")
168
-
169
- #画像処理範囲を切り抜いた画像をGUI上に出力
170
- #画像処理した画像をimage_processing.pyから持ってくる
171
- 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)
172
-
173
- #マスク処理を施した画像を表示
174
- height2, width2, dim2 = img_dst3.shape
175
- bytesPerLine2 = dim2 * width2
176
- self.image2 = QImage(img_dst3.data, width2, height2, bytesPerLine2, QImage.Format_RGB888)
177
- self.item2 = QGraphicsPixmapItem(QPixmap.fromImage(self.image2))
178
- self.scene2.addItem(self.item2)
179
- self.view2.setScene(self.scene2)
180
-
181
- #
182
- pixcel = str(round(pixcel,3))
183
- self.textbox4.setText(picxel)
184
- elapsed_time = str(round(elapsed_time,3))
185
- self.textbox5.setText(elapsed_time)
186
-
187
- #出力データをファイルに書き込み
188
- MPU= open('/home/pi/Desktop/data','a')
189
- MPU.write( "Pitch:"+pitch+""+"Roll:"+roll + "\n")
190
- MPU.close
191
-
192
- def auto_move(self):
193
-
194
- self.auto_run = 1
195
-
196
- 以下 自律移動を行うためのコード(省略)
197
-
198
-
199
- if __name__ == "__main__":
200
- app = QApplication(sys.argv)
201
- ex = Tab1Widget()
202
- sys.exit(app.exec_())
203
28
  ```
204
- ```python
205
- ファイル名:image_processing.py
206
29
 
207
- import numpy as np
208
- import math
209
- import cv2
210
- import time
211
-
212
- import P_data
213
- import HSV
214
- import motor
215
- import RPi.GPIO as GPIO
216
-
217
-
218
- def img_process(auto_run,img_src,countM,H1,H2,Roll):
219
- t1 = time.time()
220
- if auto_run == 0:
221
- return
222
-
223
- M = 1
224
-
225
- #画像サイズ変更(処理速度に応じて変更すべし)
226
- img_src=cv2.resize(img_src,(320,240))
227
- 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)))]
228
- hsv = cv2.cvtColor(img_src, cv2.COLOR_BGR2HSV)
229
- img_src0 = cv2.cvtColor(img_src, cv2.COLOR_BGR2RGB)
230
- lower_green = np.array([H1, 50, 30])
231
- upper_green = np.array([H2, 255,255])
232
- img_mask = cv2.inRange(hsv, lower_green, upper_green)
233
- neiborhood8 = np.array([[1, 1, 1],[1, 1, 1],[1, 1, 1]],np.uint8)
234
- img_erosion = cv2.erode(img_mask,neiborhood8,iterations=2)
235
- img_erosion = cv2.erode(img_erosion,neiborhood8,iterations=2)
236
- img_dilation = cv2.dilate(img_erosion,neiborhood8,iterations=2)
237
- img_dilation = cv2.dilate(img_dilation,neiborhood8,iterations=2)
238
- img_dst = cv2.bitwise_and(img_src, img_src, mask=img_dilation)
239
- img_dst2 = cv2.bitwise_and(img_src, img_src, mask=img_mask)
240
- img_dst3 = cv2.bitwise_and(img_src, img_src, mask=img_dilation)
241
-
242
-
243
- start = time.time()
244
-
245
- row_mean_L , row_mean_R ,pixcel = P_data.Get_MotorParameter(img_mask, 1, F3)
246
-
247
- F3 = int(-pixcel)
248
-
249
- count_hsv += 1
250
- if count_hsv == 20 or count_hsv == 25 or count_hsv == 30 :
251
- img_dst, H1, H2 ,S1 , S2 , countHSV2 = HSV.HSV_value (img_dst, H1, H2,countHSV2)
252
- countM += 1
253
-
254
- t2 = time.time()
255
- elapsed_time = t2 - t1
256
-
257
-
258
- return pixcel, img_src0, elapsed_time, countM, img_dst3
259
- ```
260
-
261
30
  autonomous_vehicle.pyの中にある
262
31
  > print("あいうえお")
263
32
 
264
33
  によって、ターミナル上に「あいうえお」と出力されていることは確認しました。
265
34
  しかし、その下にある
266
- > 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)
35
+ >
267
36
 
268
37
  でエラーが表示されてしまいます。
269
38