質問編集履歴
5
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,7 +19,7 @@
|
|
19
19
|
nmsThreshold = 0.4
|
20
20
|
inpWidth = 416
|
21
21
|
inpHeight = 416
|
22
|
-
classesFile = "/realsense_yolo_v3_2d/coco.names"
|
22
|
+
classesFile = "/home/limlab/realsense_yolo_v3_2d/coco.names"
|
23
23
|
# Configure depth and color streams
|
24
24
|
pipeline = rs.pipeline()
|
25
25
|
config = rs.config()
|
@@ -33,43 +33,49 @@
|
|
33
33
|
|
34
34
|
def __init__ (self):
|
35
35
|
self.j = 0
|
36
|
+
|
36
37
|
def getOutputsNames(self,net):
|
37
38
|
layersNames = net.getLayerNames()
|
38
39
|
return [layersNames[i[0] -1] for i in net.getUnconnectedOutLayers()]
|
39
40
|
#検出
|
40
|
-
def drawPredicted(self,classId, conf, left, top,
|
41
|
+
def drawPredicted(self,classId, conf, left, top,right, bottom, frame,x,y):
|
41
|
-
#cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
|
42
|
-
#cv2.circle(frame,(x,y),radius=1,color=(0,255,0), thickness=5)#・
|
43
42
|
|
43
|
+
classes = None
|
44
|
+
with open(classesFile, "rt") as f:
|
45
|
+
classes = f.read().rstrip('\n').split('\n')
|
46
|
+
cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
|
47
|
+
cv2.circle(frame,(x,y),radius=1,color=(0,255,0), thickness=5)#・
|
48
|
+
|
44
49
|
label = '%.2f' % conf
|
45
|
-
|
46
|
-
if classes:
|
50
|
+
if classes:
|
51
|
+
assert(classId < len(classes))
|
47
|
-
label = '%s' %(classes[classId])
|
52
|
+
label = '%s' %(classes[classId])
|
48
|
-
|
49
|
-
|
53
|
+
cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
|
50
|
-
|
54
|
+
global first#グローバル変数firstを定義
|
51
|
-
|
55
|
+
first = True #実行一回目がTrueの場合
|
52
56
|
|
53
|
-
|
57
|
+
if first == True:
|
54
|
-
|
58
|
+
path = '/home/limlab/realsense_yolo_v3_2d/image/'
|
55
|
-
|
59
|
+
if label == 'bottle':
|
60
|
+
#labelがbottleの場合
|
61
|
+
self.j += 1
|
62
|
+
if self.j == 1:#def__init__()で0と定義している、0の場合実行
|
63
|
+
cv2.imwrite(path + 'image1.png',frame)
|
64
|
+
print('ペットボトルを検出しました')
|
65
|
+
playsound("/home/limlab/programs/bottle.mp3")
|
66
|
+
self.j += 1#iの変数が1になる
|
67
|
+
sys.exit()
|
68
|
+
if label == 'mouse':
|
69
|
+
self.j += 1
|
70
|
+
if self.j == 1:
|
71
|
+
cv2.imwrite(path + 'image1.png',frame)
|
72
|
+
print('マウスを検出しました')
|
73
|
+
playsound("/home/limlab/programs/mouse.mp3")
|
56
74
|
self.j += 1
|
57
|
-
if self.j == 1:
|
58
|
-
cv2.imwrite(path + 'image1.png',frame)
|
59
|
-
print('ペットボトルを検出しました')
|
60
|
-
playsound("/programs/bottle.mp3")
|
61
|
-
self.j += 1
|
62
|
-
|
75
|
+
sys.exit()
|
63
|
-
|
76
|
+
|
64
|
-
self.j += 1
|
65
|
-
if self.j == 1:
|
66
|
-
cv2.imwrite(path + 'image1.png',frame)
|
67
|
-
print('マウスを検出しました')
|
68
|
-
playsound("/programs/mouse.mp3")
|
69
|
-
self.j += 1
|
70
|
-
sys.exit()
|
71
|
-
|
77
|
+
labelSize= cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
72
|
-
|
78
|
+
top = max(top, labelSize[1])
|
73
79
|
cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,255,0),2)
|
74
80
|
|
75
81
|
|
@@ -104,15 +110,12 @@
|
|
104
110
|
height = box[3]
|
105
111
|
x = int(left+width/2)
|
106
112
|
y = int(top+ height/2)
|
107
|
-
self.drawPredicted(classIds[i], confidences[i], left, top,frame,x,y)
|
113
|
+
self.drawPredicted(classIds[i], confidences[i], left, top,left+width,top+height,frame,x,y)
|
108
114
|
|
109
|
-
def main():
|
115
|
+
def main():
|
110
|
-
cam = CAMDEMO()
|
116
|
+
cam = CAMDEMO()
|
111
|
-
classes = None
|
112
|
-
with open(classesFile, "rt") as f:
|
113
|
-
classes = f.read().rstrip('\n').split('\n')
|
114
|
-
modelConfiguration = "/realsense_yolo_v3_2d/yolov3.cfg"
|
117
|
+
modelConfiguration = "/home/limlab/realsense_yolo_v3_2d/yolov3.cfg"
|
115
|
-
modelWeights = "/realsense_yolo_v3_2d/yolov3.weights"
|
118
|
+
modelWeights = "/home/limlab/realsense_yolo_v3_2d/yolov3.weights"
|
116
119
|
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
|
117
120
|
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
|
118
121
|
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
|
@@ -143,4 +146,5 @@
|
|
143
146
|
if __name__ == "__main__":
|
144
147
|
main()
|
145
148
|
|
149
|
+
|
146
150
|
```
|
4
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,7 +19,7 @@
|
|
19
19
|
nmsThreshold = 0.4
|
20
20
|
inpWidth = 416
|
21
21
|
inpHeight = 416
|
22
|
-
classesFile = "/
|
22
|
+
classesFile = "/realsense_yolo_v3_2d/coco.names"
|
23
23
|
# Configure depth and color streams
|
24
24
|
pipeline = rs.pipeline()
|
25
25
|
config = rs.config()
|
@@ -111,8 +111,8 @@
|
|
111
111
|
classes = None
|
112
112
|
with open(classesFile, "rt") as f:
|
113
113
|
classes = f.read().rstrip('\n').split('\n')
|
114
|
-
modelConfiguration = "/
|
114
|
+
modelConfiguration = "/realsense_yolo_v3_2d/yolov3.cfg"
|
115
|
-
modelWeights = "/
|
115
|
+
modelWeights = "/realsense_yolo_v3_2d/yolov3.weights"
|
116
116
|
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
|
117
117
|
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
|
118
118
|
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
|
3
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -45,8 +45,7 @@
|
|
45
45
|
|
46
46
|
if classes:
|
47
47
|
label = '%s' %(classes[classId])
|
48
|
-
|
48
|
+
|
49
|
-
top = max(top, labelSize[1])
|
50
49
|
cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
|
51
50
|
global first
|
52
51
|
first = True
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -37,7 +37,7 @@
|
|
37
37
|
layersNames = net.getLayerNames()
|
38
38
|
return [layersNames[i[0] -1] for i in net.getUnconnectedOutLayers()]
|
39
39
|
#検出
|
40
|
-
def drawPredicted(self,classId, conf, left, top, frame
|
40
|
+
def drawPredicted(self,classId, conf, left, top, frame):#right, bottom,x ,y
|
41
41
|
#cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
|
42
42
|
#cv2.circle(frame,(x,y),radius=1,color=(0,255,0), thickness=5)#・
|
43
43
|
|
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,15 +8,38 @@
|
|
8
8
|
### 該当のソースコード
|
9
9
|
|
10
10
|
```ここに言語名を入力
|
11
|
+
import pyrealsense2 as rs
|
12
|
+
import numpy as np
|
13
|
+
import cv2
|
14
|
+
import sys
|
15
|
+
from playsound import playsound
|
16
|
+
|
17
|
+
# Initialize the parameters
|
18
|
+
confThreshold = 0.5
|
19
|
+
nmsThreshold = 0.4
|
20
|
+
inpWidth = 416
|
21
|
+
inpHeight = 416
|
11
|
-
classesFile = "/realsense_yolo_v3_2d/coco.names"
|
22
|
+
classesFile = "/home/limlab/realsense_yolo_v3_2d/coco.names"
|
23
|
+
# Configure depth and color streams
|
24
|
+
pipeline = rs.pipeline()
|
25
|
+
config = rs.config()
|
26
|
+
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
|
27
|
+
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
|
28
|
+
# Start streaming
|
29
|
+
pipeline.start(config)
|
30
|
+
|
31
|
+
|
12
32
|
class CAMDEMO:
|
13
33
|
|
14
|
-
|
34
|
+
def __init__ (self):
|
35
|
+
self.j = 0
|
15
36
|
def getOutputsNames(self,net):
|
16
37
|
layersNames = net.getLayerNames()
|
17
38
|
return [layersNames[i[0] -1] for i in net.getUnconnectedOutLayers()]
|
18
|
-
|
39
|
+
#検出
|
19
|
-
def drawPredicted(self,classId, conf, left, top, frame):
|
40
|
+
def drawPredicted(self,classId, conf, left, top, frame,classes):#right, bottom,x ,y
|
41
|
+
#cv2.rectangle(frame, (left,top), (right,bottom), (255,0,0),3)#囲み
|
42
|
+
#cv2.circle(frame,(x,y),radius=1,color=(0,255,0), thickness=5)#・
|
20
43
|
|
21
44
|
label = '%.2f' % conf
|
22
45
|
|
@@ -24,12 +47,101 @@
|
|
24
47
|
label = '%s' %(classes[classId])
|
25
48
|
labelSize= cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
26
49
|
top = max(top, labelSize[1])
|
27
|
-
cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
|
50
|
+
cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(255,255,0),2)
|
51
|
+
global first
|
52
|
+
first = True
|
53
|
+
|
54
|
+
if first == True:
|
55
|
+
path = '/realsense_yolo_v3_2d/image/'
|
56
|
+
if label == 'bottle':#labelがbottleの場合
|
57
|
+
self.j += 1
|
58
|
+
if self.j == 1:
|
59
|
+
cv2.imwrite(path + 'image1.png',frame)
|
60
|
+
print('ペットボトルを検出しました')
|
61
|
+
playsound("/programs/bottle.mp3")
|
62
|
+
self.j += 1
|
63
|
+
sys.exit()
|
64
|
+
if label == 'mouse':
|
65
|
+
self.j += 1
|
66
|
+
if self.j == 1:
|
67
|
+
cv2.imwrite(path + 'image1.png',frame)
|
68
|
+
print('マウスを検出しました')
|
69
|
+
playsound("/programs/mouse.mp3")
|
70
|
+
self.j += 1
|
71
|
+
sys.exit()
|
72
|
+
#labelSize= cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
73
|
+
#top = max(top, labelSize[1])
|
74
|
+
cv2.putText(frame, label,(left,top-5), cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,255,0),2)
|
28
75
|
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
def process_detection(self,frame, outs):
|
80
|
+
frameHeight = frame.shape[0]
|
81
|
+
frameWidth = frame.shape[1]
|
82
|
+
classIds = []
|
83
|
+
confidences = []
|
84
|
+
boxes = []
|
85
|
+
for out in outs:
|
86
|
+
for detection in out:
|
87
|
+
scores = detection[5:]
|
88
|
+
classId = np.argmax(scores)
|
89
|
+
confidence = scores[classId]
|
90
|
+
if confidence > confThreshold:
|
91
|
+
center_x = int(detection[0]*frameWidth)
|
92
|
+
center_y = int(detection[1]*frameHeight)
|
93
|
+
width = int(detection[2]*frameWidth)
|
94
|
+
height = int(detection[3]*frameHeight)
|
95
|
+
left = int(center_x - width/2)
|
96
|
+
top = int(center_y - height/2)
|
97
|
+
classIds.append(classId)
|
98
|
+
indices = cv2.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
|
99
|
+
for i in indices:
|
100
|
+
i = i[0]
|
101
|
+
box = boxes[i]
|
102
|
+
left = box[0]
|
103
|
+
top = box[1]
|
104
|
+
width = box[2]
|
105
|
+
height = box[3]
|
106
|
+
x = int(left+width/2)
|
107
|
+
y = int(top+ height/2)
|
108
|
+
self.drawPredicted(classIds[i], confidences[i], left, top,frame,x,y)#5left+width, 6top+height
|
109
|
+
|
29
110
|
def main():
|
30
111
|
cam = CAMDEMO()
|
31
112
|
classes = None
|
32
113
|
with open(classesFile, "rt") as f:
|
33
114
|
classes = f.read().rstrip('\n').split('\n')
|
115
|
+
modelConfiguration = "/home/limlab/realsense_yolo_v3_2d/yolov3.cfg"
|
116
|
+
modelWeights = "/home/limlab/realsense_yolo_v3_2d/yolov3.weights"
|
117
|
+
net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
|
118
|
+
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
|
119
|
+
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
|
120
|
+
try:
|
121
|
+
while True:
|
122
|
+
|
123
|
+
# Wait for a coherent pair of frames: depth and color
|
124
|
+
frames = pipeline.wait_for_frames()
|
125
|
+
color_frame = frames.get_color_frame()
|
126
|
+
if not color_frame:
|
127
|
+
continue
|
128
|
+
# Convert images to numpy arrays
|
129
|
+
color_image = np.asanyarray(color_frame.get_data())
|
130
|
+
blob = cv2.dnn.blobFromImage(color_image, 1/255, (inpWidth, inpHeight), [0,0,0],1,crop=False)
|
131
|
+
net.setInput(blob)
|
132
|
+
outs = net.forward(cam.getOutputsNames(net))
|
133
|
+
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
|
134
|
+
cam.process_detection(color_image,outs)
|
135
|
+
images = color_image
|
136
|
+
# Show images
|
137
|
+
cv2.imshow('Yolo in RealSense made by Tony', images)
|
138
|
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
139
|
+
break
|
140
|
+
finally:
|
141
|
+
# Stop streaming
|
142
|
+
pipeline.stop()
|
34
143
|
|
144
|
+
if __name__ == "__main__":
|
145
|
+
main()
|
146
|
+
|
35
147
|
```
|