質問編集履歴

1

コードの追加

2018/10/10 01:04

投稿

uriuri
uriuri

スコア47

test CHANGED
File without changes
test CHANGED
@@ -97,3 +97,285 @@
97
97
  コードが見づらくて申し訳ないのですが
98
98
 
99
99
  txtファイルを経由する以外にpythonで物体を見つけた際、cのほうで条件文を使用しstop_flag = 1にするいい方法があればご教授ください。
100
+
101
+
102
+
103
+ ```c
104
+
105
+ #include <stdio.h>//入出力
106
+
107
+ #include <math.h> //数学
108
+
109
+ #include <unistd.h> //
110
+
111
+ #include <stdlib.h> //一般ユーティリティ
112
+
113
+ #include <signal.h> //シグナル操作
114
+
115
+ #include <pthread.h>//
116
+
117
+ #include <getopt.h> //
118
+
119
+ #include <time.h> //日付および時間
120
+
121
+ #include <Python.h>
122
+
123
+
124
+
125
+
126
+
127
+ void set_path(void){
128
+
129
+ PyObject *sys = PyImport_ImportModule("sys");
130
+
131
+ PyObject *sys_path = PyObject_GetAttrString(sys, "path");
132
+
133
+ PyObject *dir = PyUnicode_FromString(".");
134
+
135
+ PyList_Append(sys_path, dir);
136
+
137
+ }
138
+
139
+
140
+
141
+ int main(void){
142
+
143
+
144
+
145
+ /* pythonインタプリタを初期化 */
146
+
147
+ Py_Initialize();
148
+
149
+ set_path();
150
+
151
+
152
+
153
+ PyRun_SimpleString("import cv2\n"
154
+
155
+ "import math\n"
156
+
157
+ "import numpy as np\n"
158
+
159
+ "import csv\n"
160
+
161
+
162
+
163
+ "def cv_fourcc(c1, c2, c3, c4):\n"
164
+
165
+ " return (ord(c1) & 255) + ((ord(c2) & 255) << 8) + \
166
+
167
+ ((ord(c3) & 255) << 16) + ((ord(c4) & 255) << 24)\n"
168
+
169
+ "if __name__ == '__main__':\n"
170
+
171
+ " ESC_KEY = 27\n"
172
+
173
+ " INTERVAL= 33\n" /*# 33*/
174
+
175
+ " FRAME_RATE = 30\n" /*# fps*/
176
+
177
+
178
+
179
+ " ORG_WINDOW_NAME = 'ORG'\n"
180
+
181
+ " DST_WINDOW_NAME = 'DST'\n"
182
+
183
+ " FILE_NAME1 = '1.avi'\n"
184
+
185
+
186
+
187
+ " DEVICE_ID = 1\n"
188
+
189
+
190
+
191
+ " cap = cv2.VideoCapture(DEVICE_ID)\n"
192
+
193
+
194
+
195
+ " end_flag, c_frame = cap.read()\n"
196
+
197
+ " height, width, channels = c_frame.shape\n"
198
+
199
+ " image_size = height * width\n"
200
+
201
+ " rec = cv2.VideoWriter(FILE_NAME1, \
202
+
203
+ cv_fourcc('X', 'V', 'I', 'D'), \
204
+
205
+ FRAME_RATE, \
206
+
207
+ (width, height), \
208
+
209
+ True)\n"
210
+
211
+
212
+
213
+
214
+
215
+ " cv2.namedWindow(ORG_WINDOW_NAME)\n"
216
+
217
+ " cv2.namedWindow(DST_WINDOW_NAME)\n"
218
+
219
+
220
+
221
+ " while end_flag == True:\n"
222
+
223
+ " def contains_lines(rect, lines):\n" /*#2 line
224
+
225
+ */
226
+
227
+ " cnt = 0\n"
228
+
229
+ " for line in lines:\n"
230
+
231
+ " p1, p2 = tuple(line[:2]), tuple(line[2:])\n"
232
+
233
+ " if cv2.pointPolygonTest(rect, p1, False) >= 0 and \
234
+
235
+ cv2.pointPolygonTest(rect, p2, False) >= 0:\n"
236
+
237
+ " cnt+=1\n"
238
+
239
+ /*#return True*/
240
+
241
+ " return cnt>=2\n"//2line over
242
+
243
+ /* " return False\n"*/
244
+
245
+
246
+
247
+ " def detect(img):\n"
248
+
249
+ " dst = []\n"
250
+
251
+
252
+
253
+
254
+
255
+ " gauss = cv2.GaussianBlur(img, (11, 11), 0)\n"
256
+
257
+ " gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY)\n"
258
+
259
+ " _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)\n"
260
+
261
+
262
+
263
+
264
+
265
+ " edges = cv2.Canny(binary, 50, 150)\n"
266
+
267
+ " LSD = cv2.createLineSegmentDetector()\n"
268
+
269
+ " lines, width, prec, nfa = LSD.detect(edges)\n"
270
+
271
+ " if lines is None:\n"
272
+
273
+ " return\n"
274
+
275
+ " lines = np.squeeze(lines, axis=1)\n"
276
+
277
+
278
+
279
+ " _, contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)\n"
280
+
281
+ " for cnt in contours:\n"
282
+
283
+ " area = cv2.contourArea(cnt)\n"
284
+
285
+ " if not 2500 < area < 10000:\n"//500,1000
286
+
287
+ " continue\n"
288
+
289
+
290
+
291
+ " rotated_rect = cv2.minAreaRect(cnt)\n"
292
+
293
+ " rect = cv2.boxPoints(rotated_rect)\n"
294
+
295
+ " (cx, cy), (width, height), angle = rotated_rect\n"
296
+
297
+ " print('center: ({:.1f}, {:.1f}), size: ({:.1f}, {:.1f}),' 'angle: {:.1f}'.format(cx, cy, width, height, angle))\n"
298
+
299
+
300
+
301
+ " min_edge = min(width, height)\n"
302
+
303
+ " if not 10 < min_edge < 50:\n"//5,25
304
+
305
+ " continue\n"
306
+
307
+ " if contains_lines(rect, lines):\n"
308
+
309
+ " dst.append(rect)\n"
310
+
311
+
312
+
313
+ " return dst\n"
314
+
315
+ " rects = detect(c_frame)\n"
316
+
317
+ " if rects is not None:\n"
318
+
319
+ " for rect in rects:\n"
320
+
321
+ " cv2.drawContours(c_frame, [rect.astype(int)], -1, (0, 255, 0), 2)\n"
322
+
323
+ " print(rect)\n"
324
+
325
+ " print('hakken')\n"
326
+
327
+
328
+
329
+ " f = open('line.txt', 'w')\n"
330
+
331
+ " line_flag = '1'\n"
332
+
333
+ " f.writer(line_flag)\n"
334
+
335
+ /* " writer.writerow(line_flag)\n" //1行
336
+
337
+ /* " writer.writerow(array2d)\n" //複数行*/
338
+
339
+ " f.close()\n"
340
+
341
+
342
+
343
+ " cv2.imshow(ORG_WINDOW_NAME, c_frame)\n"
344
+
345
+ " key = cv2.waitKey(INTERVAL)\n"
346
+
347
+ " if key == ESC_KEY:\n"
348
+
349
+ " break\n"
350
+
351
+
352
+
353
+
354
+
355
+ " end_flag, c_frame = cap.read()\n"
356
+
357
+
358
+
359
+
360
+
361
+ " cv2.destroyAllWindows()\n"
362
+
363
+ " cap.release()\n"
364
+
365
+ " rec.release()\n");
366
+
367
+
368
+
369
+ /* pythonインタプリタを終了 */
370
+
371
+ Py_Finalize();
372
+
373
+ return 0;
374
+
375
+ }
376
+
377
+
378
+
379
+ ```
380
+
381
+ こちらが物体を見つけるためのコードです