質問編集履歴

5

修正

2021/09/14 01:48

投稿

reiya_123
reiya_123

スコア57

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
  inpHeight = 416
42
42
 
43
- classesFile = "/realsense_yolo_v3_2d/coco.names"
43
+ classesFile = "/home/limlab/realsense_yolo_v3_2d/coco.names"
44
44
 
45
45
  # Configure depth and color streams
46
46
 
@@ -68,6 +68,8 @@
68
68
 
69
69
  self.j = 0
70
70
 
71
+
72
+
71
73
  def getOutputsNames(self,net):
72
74
 
73
75
  layersNames = net.getLayerNames()
@@ -76,170 +78,174 @@
76
78
 
77
79
  #検出
78
80
 
79
- def drawPredicted(self,classId, conf, left, top, frame):#right, bottom,x ,y
81
+ def drawPredicted(self,classId, conf, left, top,right, bottom, frame,x,y):
80
-
81
- #cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
82
-
83
- #cv2.circle(frame,(x,y),radius=1,color=(0,255,0), thickness=5)#・
84
82
 
85
83
 
86
84
 
85
+ classes = None
86
+
87
+ with open(classesFile, "rt") as f:
88
+
89
+ classes = f.read().rstrip('\n').split('\n')
90
+
91
+ cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
92
+
93
+ cv2.circle(frame,(x,y),radius=1,color=(0,255,0), thickness=5)#・
94
+
95
+
96
+
87
97
  label = '%.2f' % conf
88
98
 
99
+ if classes:
100
+
101
+ assert(classId < len(classes))
102
+
103
+ label = '%s' %(classes[classId])
104
+
105
+ cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
106
+
107
+ global first#グローバル変数firstを定義
108
+
109
+ first = True #実行一回目がTrueの場合
110
+
111
+
112
+
113
+ if first == True:
114
+
115
+ path = '/home/limlab/realsense_yolo_v3_2d/image/'
116
+
117
+ if label == 'bottle':
118
+
119
+ #labelがbottleの場合
120
+
121
+ self.j += 1
122
+
123
+ if self.j == 1:#def__init__()で0と定義している、0の場合実行
124
+
125
+ cv2.imwrite(path + 'image1.png',frame)
126
+
127
+ print('ペットボトルを検出しました')
128
+
129
+ playsound("/home/limlab/programs/bottle.mp3")
130
+
131
+ self.j += 1#iの変数が1になる
132
+
133
+ sys.exit()
134
+
135
+ if label == 'mouse':
136
+
137
+ self.j += 1
138
+
139
+ if self.j == 1:
140
+
141
+ cv2.imwrite(path + 'image1.png',frame)
142
+
143
+ print('マウスを検出しました')
144
+
145
+ playsound("/home/limlab/programs/mouse.mp3")
146
+
147
+ self.j += 1
148
+
149
+ sys.exit()
150
+
151
+
152
+
153
+ labelSize= cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
154
+
155
+ top = max(top, labelSize[1])
156
+
157
+ cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,255,0),2)
158
+
159
+
160
+
89
161
 
90
162
 
163
+
164
+
165
+
166
+
167
+ def process_detection(self,frame, outs):
168
+
169
+ frameHeight = frame.shape[0]
170
+
171
+ frameWidth = frame.shape[1]
172
+
173
+ classIds = []
174
+
175
+ confidences = []
176
+
177
+ boxes = []
178
+
179
+ for out in outs:
180
+
181
+ for detection in out:
182
+
183
+ scores = detection[5:]
184
+
185
+ classId = np.argmax(scores)
186
+
187
+ confidence = scores[classId]
188
+
189
+ if confidence > confThreshold:
190
+
191
+ center_x = int(detection[0]*frameWidth)
192
+
193
+ center_y = int(detection[1]*frameHeight)
194
+
195
+ width = int(detection[2]*frameWidth)
196
+
197
+ height = int(detection[3]*frameHeight)
198
+
199
+ left = int(center_x - width/2)
200
+
201
+ top = int(center_y - height/2)
202
+
203
+ classIds.append(classId)
204
+
205
+ indices = cv2.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
206
+
207
+ for i in indices:
208
+
209
+ i = i[0]
210
+
211
+ box = boxes[i]
212
+
213
+ left = box[0]
214
+
215
+ top = box[1]
216
+
217
+ width = box[2]
218
+
219
+ height = box[3]
220
+
221
+ x = int(left+width/2)
222
+
223
+ y = int(top+ height/2)
224
+
225
+ self.drawPredicted(classIds[i], confidences[i], left, top,left+width,top+height,frame,x,y)
226
+
227
+
228
+
91
- if classes:
229
+ def main():
230
+
92
-
231
+ cam = CAMDEMO()
232
+
233
+ modelConfiguration = "/home/limlab/realsense_yolo_v3_2d/yolov3.cfg"
234
+
235
+ modelWeights = "/home/limlab/realsense_yolo_v3_2d/yolov3.weights"
236
+
93
- label = '%s' %(classes[classId])
237
+ net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
94
-
95
-
96
-
238
+
97
- cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
239
+ net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
240
+
98
-
241
+ net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
242
+
99
- global first
243
+ try:
100
-
244
+
101
- first = True
245
+ while True:
102
246
 
103
247
 
104
248
 
105
- if first == True:
106
-
107
- path = '/realsense_yolo_v3_2d/image/'
108
-
109
- if label == 'bottle':#labelがbottleの場合
110
-
111
- self.j += 1
112
-
113
- if self.j == 1:
114
-
115
- cv2.imwrite(path + 'image1.png',frame)
116
-
117
- print('ペットボトルを検出しました')
118
-
119
- playsound("/programs/bottle.mp3")
120
-
121
- self.j += 1
122
-
123
- sys.exit()
124
-
125
- if label == 'mouse':
126
-
127
- self.j += 1
128
-
129
- if self.j == 1:
130
-
131
- cv2.imwrite(path + 'image1.png',frame)
132
-
133
- print('マウスを検出しました')
134
-
135
- playsound("/programs/mouse.mp3")
136
-
137
- self.j += 1
138
-
139
- sys.exit()
140
-
141
- #labelSize= cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
142
-
143
- #top = max(top, labelSize[1])
144
-
145
- cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,255,0),2)
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
- def process_detection(self,frame, outs):
156
-
157
- frameHeight = frame.shape[0]
158
-
159
- frameWidth = frame.shape[1]
160
-
161
- classIds = []
162
-
163
- confidences = []
164
-
165
- boxes = []
166
-
167
- for out in outs:
168
-
169
- for detection in out:
170
-
171
- scores = detection[5:]
172
-
173
- classId = np.argmax(scores)
174
-
175
- confidence = scores[classId]
176
-
177
- if confidence > confThreshold:
178
-
179
- center_x = int(detection[0]*frameWidth)
180
-
181
- center_y = int(detection[1]*frameHeight)
182
-
183
- width = int(detection[2]*frameWidth)
184
-
185
- height = int(detection[3]*frameHeight)
186
-
187
- left = int(center_x - width/2)
188
-
189
- top = int(center_y - height/2)
190
-
191
- classIds.append(classId)
192
-
193
- indices = cv2.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
194
-
195
- for i in indices:
196
-
197
- i = i[0]
198
-
199
- box = boxes[i]
200
-
201
- left = box[0]
202
-
203
- top = box[1]
204
-
205
- width = box[2]
206
-
207
- height = box[3]
208
-
209
- x = int(left+width/2)
210
-
211
- y = int(top+ height/2)
212
-
213
- self.drawPredicted(classIds[i], confidences[i], left, top,frame,x,y)#5left+width, 6top+height
214
-
215
-
216
-
217
- def main():
218
-
219
- cam = CAMDEMO()
220
-
221
- classes = None
222
-
223
- with open(classesFile, "rt") as f:
224
-
225
- classes = f.read().rstrip('\n').split('\n')
226
-
227
- modelConfiguration = "/realsense_yolo_v3_2d/yolov3.cfg"
228
-
229
- modelWeights = "/realsense_yolo_v3_2d/yolov3.weights"
230
-
231
- net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
232
-
233
- net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
234
-
235
- net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
236
-
237
- try:
238
-
239
- while True:
240
-
241
-
242
-
243
249
  # Wait for a coherent pair of frames: depth and color
244
250
 
245
251
  frames = pipeline.wait_for_frames()
@@ -288,4 +294,6 @@
288
294
 
289
295
 
290
296
 
297
+
298
+
291
299
  ```

4

修正

2021/09/14 01:48

投稿

reiya_123
reiya_123

スコア57

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
  inpHeight = 416
42
42
 
43
- classesFile = "/home/limlab/realsense_yolo_v3_2d/coco.names"
43
+ classesFile = "/realsense_yolo_v3_2d/coco.names"
44
44
 
45
45
  # Configure depth and color streams
46
46
 
@@ -224,9 +224,9 @@
224
224
 
225
225
  classes = f.read().rstrip('\n').split('\n')
226
226
 
227
- modelConfiguration = "/home/limlab/realsense_yolo_v3_2d/yolov3.cfg"
227
+ modelConfiguration = "/realsense_yolo_v3_2d/yolov3.cfg"
228
-
228
+
229
- modelWeights = "/home/limlab/realsense_yolo_v3_2d/yolov3.weights"
229
+ modelWeights = "/realsense_yolo_v3_2d/yolov3.weights"
230
230
 
231
231
  net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
232
232
 

3

修正

2021/09/13 03:13

投稿

reiya_123
reiya_123

スコア57

test CHANGED
File without changes
test CHANGED
@@ -92,9 +92,7 @@
92
92
 
93
93
  label = '%s' %(classes[classId])
94
94
 
95
- labelSize= cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
95
+
96
-
97
- top = max(top, labelSize[1])
98
96
 
99
97
  cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
100
98
 

2

修正

2021/09/13 03:06

投稿

reiya_123
reiya_123

スコア57

test CHANGED
File without changes
test CHANGED
@@ -76,7 +76,7 @@
76
76
 
77
77
  #検出
78
78
 
79
- def drawPredicted(self,classId, conf, left, top, frame,classes):#right, bottom,x ,y
79
+ def drawPredicted(self,classId, conf, left, top, frame):#right, bottom,x ,y
80
80
 
81
81
  #cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
82
82
 

1

修正

2021/09/13 03:03

投稿

reiya_123
reiya_123

スコア57

test CHANGED
File without changes
test CHANGED
@@ -18,13 +18,55 @@
18
18
 
19
19
  ```ここに言語名を入力
20
20
 
21
+ import pyrealsense2 as rs
22
+
23
+ import numpy as np
24
+
25
+ import cv2
26
+
27
+ import sys
28
+
29
+ from playsound import playsound
30
+
31
+
32
+
33
+ # Initialize the parameters
34
+
35
+ confThreshold = 0.5
36
+
37
+ nmsThreshold = 0.4
38
+
39
+ inpWidth = 416
40
+
41
+ inpHeight = 416
42
+
21
- classesFile = "/realsense_yolo_v3_2d/coco.names"
43
+ classesFile = "/home/limlab/realsense_yolo_v3_2d/coco.names"
44
+
45
+ # Configure depth and color streams
46
+
47
+ pipeline = rs.pipeline()
48
+
49
+ config = rs.config()
50
+
51
+ config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
52
+
53
+ config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
54
+
55
+ # Start streaming
56
+
57
+ pipeline.start(config)
58
+
59
+
60
+
61
+
22
62
 
23
63
  class CAMDEMO:
24
64
 
25
65
 
26
66
 
27
-
67
+ def __init__ (self):
68
+
69
+ self.j = 0
28
70
 
29
71
  def getOutputsNames(self,net):
30
72
 
@@ -32,9 +74,13 @@
32
74
 
33
75
  return [layersNames[i[0] -1] for i in net.getUnconnectedOutLayers()]
34
76
 
35
-
77
+ #検出
36
-
78
+
37
- def drawPredicted(self,classId, conf, left, top, frame):
79
+ def drawPredicted(self,classId, conf, left, top, frame,classes):#right, bottom,x ,y
80
+
81
+ #cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
82
+
83
+ #cv2.circle(frame,(x,y),radius=1,color=(0,255,0), thickness=5)#・
38
84
 
39
85
 
40
86
 
@@ -50,7 +96,123 @@
50
96
 
51
97
  top = max(top, labelSize[1])
52
98
 
53
- cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
99
+ cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
100
+
101
+ global first
102
+
103
+ first = True
104
+
105
+
106
+
107
+ if first == True:
108
+
109
+ path = '/realsense_yolo_v3_2d/image/'
110
+
111
+ if label == 'bottle':#labelがbottleの場合
112
+
113
+ self.j += 1
114
+
115
+ if self.j == 1:
116
+
117
+ cv2.imwrite(path + 'image1.png',frame)
118
+
119
+ print('ペットボトルを検出しました')
120
+
121
+ playsound("/programs/bottle.mp3")
122
+
123
+ self.j += 1
124
+
125
+ sys.exit()
126
+
127
+ if label == 'mouse':
128
+
129
+ self.j += 1
130
+
131
+ if self.j == 1:
132
+
133
+ cv2.imwrite(path + 'image1.png',frame)
134
+
135
+ print('マウスを検出しました')
136
+
137
+ playsound("/programs/mouse.mp3")
138
+
139
+ self.j += 1
140
+
141
+ sys.exit()
142
+
143
+ #labelSize= cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
144
+
145
+ #top = max(top, labelSize[1])
146
+
147
+ cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,255,0),2)
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+ def process_detection(self,frame, outs):
158
+
159
+ frameHeight = frame.shape[0]
160
+
161
+ frameWidth = frame.shape[1]
162
+
163
+ classIds = []
164
+
165
+ confidences = []
166
+
167
+ boxes = []
168
+
169
+ for out in outs:
170
+
171
+ for detection in out:
172
+
173
+ scores = detection[5:]
174
+
175
+ classId = np.argmax(scores)
176
+
177
+ confidence = scores[classId]
178
+
179
+ if confidence > confThreshold:
180
+
181
+ center_x = int(detection[0]*frameWidth)
182
+
183
+ center_y = int(detection[1]*frameHeight)
184
+
185
+ width = int(detection[2]*frameWidth)
186
+
187
+ height = int(detection[3]*frameHeight)
188
+
189
+ left = int(center_x - width/2)
190
+
191
+ top = int(center_y - height/2)
192
+
193
+ classIds.append(classId)
194
+
195
+ indices = cv2.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
196
+
197
+ for i in indices:
198
+
199
+ i = i[0]
200
+
201
+ box = boxes[i]
202
+
203
+ left = box[0]
204
+
205
+ top = box[1]
206
+
207
+ width = box[2]
208
+
209
+ height = box[3]
210
+
211
+ x = int(left+width/2)
212
+
213
+ y = int(top+ height/2)
214
+
215
+ self.drawPredicted(classIds[i], confidences[i], left, top,frame,x,y)#5left+width, 6top+height
54
216
 
55
217
 
56
218
 
@@ -64,6 +226,68 @@
64
226
 
65
227
  classes = f.read().rstrip('\n').split('\n')
66
228
 
229
+ modelConfiguration = "/home/limlab/realsense_yolo_v3_2d/yolov3.cfg"
230
+
231
+ modelWeights = "/home/limlab/realsense_yolo_v3_2d/yolov3.weights"
232
+
233
+ net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
234
+
235
+ net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
236
+
237
+ net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
238
+
239
+ try:
240
+
241
+ while True:
242
+
243
+
244
+
245
+ # Wait for a coherent pair of frames: depth and color
246
+
247
+ frames = pipeline.wait_for_frames()
248
+
249
+ color_frame = frames.get_color_frame()
250
+
251
+ if not color_frame:
252
+
253
+ continue
254
+
255
+ # Convert images to numpy arrays
256
+
257
+ color_image = np.asanyarray(color_frame.get_data())
258
+
259
+ blob = cv2.dnn.blobFromImage(color_image, 1/255, (inpWidth, inpHeight), [0,0,0],1,crop=False)
260
+
261
+ net.setInput(blob)
262
+
263
+ outs = net.forward(cam.getOutputsNames(net))
264
+
265
+ # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
266
+
267
+ cam.process_detection(color_image,outs)
268
+
269
+ images = color_image
270
+
271
+ # Show images
272
+
273
+ cv2.imshow('Yolo in RealSense made by Tony', images)
274
+
275
+ if cv2.waitKey(1) & 0xFF == ord('q'):
276
+
277
+ break
278
+
279
+ finally:
280
+
281
+ # Stop streaming
282
+
283
+ pipeline.stop()
284
+
285
+
286
+
287
+ if __name__ == "__main__":
288
+
289
+ main()
290
+
67
291
 
68
292
 
69
293
  ```