回答編集履歴

1

コード追加

2021/09/02 08:18

投稿

jbpb0
jbpb0

スコア7651

test CHANGED
@@ -1,39 +1,271 @@
1
- > なにかの変数が検出ごとに初期化されずに保存されていることが原因
2
-
3
-
4
-
5
- > postprocess()という関数共有すと不具合が起こっている
1
+ 当方で下記コードで確認したところ、「postprocess()」の「static」だけで直りました
6
-
7
-
8
2
 
9
3
  ```c++
10
4
 
5
+ (中略)
6
+
7
+ std::string keys =
8
+
9
+ (中略)
10
+
11
+ "{ nms | .4 | Non-maximum suppression threshold. }"
12
+
13
+ "{ scale2 | 1.0 | Scale 2 }"
14
+
15
+ "{ width2 | 100 | Width 2 }"
16
+
17
+ "{ height2 | 100 | Height 2 }"
18
+
19
+ "{ model2 | | Model 2 }"
20
+
21
+ "{ config2 | | Config 2 }"
22
+
23
+ "{ classes2 | | Optional path to a text file with names of classes to label detected objects. 2 }"
24
+
25
+ "{ backend | 0 | Choose one of computation backends: "
26
+
27
+ (中略)
28
+
29
+
30
+
31
+ using namespace cv;
32
+
33
+ using namespace dnn;
34
+
35
+
36
+
37
+ float confThreshold, nmsThreshold;
38
+
39
+ std::vector<std::string> classes;
40
+
41
+ std::vector<std::string> classes2;
42
+
43
+ int classesnum;
44
+
45
+
46
+
47
+ (中略)
48
+
49
+ int main(int argc, char** argv)
50
+
51
+ {
52
+
53
+ (中略)
54
+
55
+ float scale2 = parser.get<float>("scale2");
56
+
57
+ int inpWidth2 = parser.get<int>("width2");
58
+
59
+ int inpHeight2 = parser.get<int>("height2");
60
+
61
+ std::string modelPath2 = findFile(parser.get<String>("model2"));
62
+
63
+ std::string configPath2 = findFile(parser.get<String>("config2"));
64
+
65
+
66
+
67
+ // Open file with classes names.
68
+
69
+ if (parser.has("classes"))
70
+
71
+ {
72
+
73
+ std::string file = parser.get<String>("classes");
74
+
75
+ std::ifstream ifs(file.c_str());
76
+
77
+ if (!ifs.is_open())
78
+
79
+ CV_Error(Error::StsError, "File " + file + " not found");
80
+
81
+ std::string line;
82
+
83
+ while (std::getline(ifs, line))
84
+
85
+ {
86
+
87
+ classes.push_back(line);
88
+
89
+ }
90
+
91
+ }
92
+
93
+ if (parser.has("classes2"))
94
+
95
+ {
96
+
97
+ std::string file2 = parser.get<String>("classes2");
98
+
99
+ std::ifstream ifs2(file2.c_str());
100
+
101
+ if (!ifs2.is_open())
102
+
103
+ CV_Error(Error::StsError, "File " + file2 + " not found");
104
+
105
+ std::string line2;
106
+
107
+ while (std::getline(ifs2, line2))
108
+
109
+ {
110
+
111
+ classes2.push_back(line2);
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ for (int num = 0; num < 2; num++) {
120
+
121
+ classesnum = num;
122
+
123
+ // Load a model.
124
+
125
+ Net net = readNet(modelPath, configPath, parser.get<String>("framework"));
126
+
127
+ if (num != 0)
128
+
129
+ net = readNet(modelPath2, configPath2, parser.get<String>("framework"));
130
+
131
+ int backend = parser.get<int>("backend");
132
+
133
+ net.setPreferableBackend(backend);
134
+
135
+ net.setPreferableTarget(parser.get<int>("target"));
136
+
137
+ std::vector<String> outNames = net.getUnconnectedOutLayersNames();
138
+
139
+
140
+
141
+ (中略)
142
+
143
+
144
+
145
+ // Process the frame
146
+
147
+ if (!frame.empty())
148
+
149
+ {
150
+
151
+ if (num == 0)
152
+
153
+ preprocess(frame, net, Size(inpWidth, inpHeight), scale, mean, swapRB);
154
+
155
+ else
156
+
157
+ preprocess(frame, net, Size(inpWidth2, inpHeight2), scale2, mean, swapRB);
158
+
159
+ processedFramesQueue.push(frame);
160
+
161
+
162
+
163
+ (中略)
164
+
165
+
166
+
167
+ // Process frames.
168
+
169
+ Mat frame, blob;
170
+
171
+ while (waitKey(1) < 0)
172
+
173
+ {
174
+
175
+ cap >> frame;
176
+
177
+ if (frame.empty())
178
+
179
+ {
180
+
181
+ waitKey();
182
+
183
+ break;
184
+
185
+ }
186
+
187
+ if (num == 0)
188
+
189
+ preprocess(frame, net, Size(inpWidth, inpHeight), scale, mean, swapRB);
190
+
191
+ else
192
+
193
+ preprocess(frame, net, Size(inpWidth2, inpHeight2), scale2, mean, swapRB);
194
+
195
+
196
+
197
+ (中略)
198
+
199
+
200
+
201
+ }
202
+
203
+ return 0;
204
+
205
+ }
206
+
207
+
208
+
11
209
  void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net, int backend)
12
210
 
13
211
  {
14
212
 
213
+ //static std::vector<int> outLayers = net.getUnconnectedOutLayers();
214
+
215
+ //static std::string outLayerType = net.getLayer(outLayers[0])->type;
216
+
15
- static std::vector<int> outLayers = net.getUnconnectedOutLayers();
217
+ std::vector<int> outLayers = net.getUnconnectedOutLayers();
16
-
218
+
17
- static std::string outLayerType = net.getLayer(outLayers[0])->type;
219
+ std::string outLayerType = net.getLayer(outLayers[0])->type;
220
+
221
+
222
+
223
+ (中略)
224
+
225
+ }
226
+
227
+
228
+
229
+ void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
230
+
231
+ {
232
+
233
+ rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));
234
+
235
+
236
+
237
+ std::string label = format("%.2f", conf);
238
+
239
+ if (classesnum == 0) {
240
+
241
+ if (!classes.empty())
242
+
243
+ {
244
+
245
+ CV_Assert(classId < (int)classes.size());
246
+
247
+ label = classes[classId] + ": " + label;
248
+
249
+ }
250
+
251
+ } else {
252
+
253
+ if (!classes2.empty())
254
+
255
+ {
256
+
257
+ CV_Assert(classId < (int)classes2.size());
258
+
259
+ label = classes2[classId] + ": " + label;
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+
267
+ (中略)
268
+
269
+ }
18
270
 
19
271
  ```
20
-
21
- ↓ 「static」を取る
22
-
23
- ```c++
24
-
25
- void postprocess(Mat& frame, const std::vector<Mat>& outs, Net& net, int backend)
26
-
27
- {
28
-
29
- std::vector<int> outLayers = net.getUnconnectedOutLayers();
30
-
31
- std::string outLayerType = net.getLayer(outLayers[0])->type;
32
-
33
- ```
34
-
35
- 参考
36
-
37
- [c++関数内部で使えるstatic変数と通常のstatic変数の違い。](https://qiita.com/tukine_T/items/221b5f5f27ddb167a8b1)
38
-
39
- の「C++で使える関数内のstatic」