質問編集履歴
3
文字修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -332,7 +332,7 @@
|
|
332
332
|
|
333
333
|
Traceback (most recent call last):
|
334
334
|
|
335
|
-
File "
|
335
|
+
File "kamera.py", line 35, in <module>
|
336
336
|
|
337
337
|
height, width, channels = c_frame.shape
|
338
338
|
|
2
エラーの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -330,6 +330,14 @@
|
|
330
330
|
|
331
331
|
このようなエラーがでてしまいます
|
332
332
|
|
333
|
+
Traceback (most recent call last):
|
334
|
+
|
335
|
+
File "test.py", line 35, in <module>
|
336
|
+
|
337
|
+
height, width, channels = c_frame.shape
|
338
|
+
|
333
339
|
AttributeError: 'NoneType' object has no attribute 'shape'
|
334
340
|
|
341
|
+
|
342
|
+
|
335
343
|
カメラが画像を読み込めていないことが原因だとおもうのですが、以前動いたものがうごかくなるということはどういったことなのでしょうか
|
1
添付ミス
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
以前動いたプログラムを一部書き換え試しました
|
6
|
+
|
5
|
-
以前動いたプログラム
|
7
|
+
以前動いたプログラムもエラーをはくようになってしまいました
|
6
8
|
|
7
9
|
```python
|
8
10
|
|
@@ -38,25 +40,25 @@
|
|
38
40
|
|
39
41
|
|
40
42
|
|
41
|
-
ORG_WINDOW_NAME = "
|
43
|
+
ORG_WINDOW_NAME = "ORG"
|
42
|
-
|
44
|
+
|
43
|
-
DST_WINDOW_NAME = "
|
45
|
+
DST_WINDOW_NAME = "DST"
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
|
47
|
+
|
48
|
+
|
47
|
-
FILE_NAME1 = "
|
49
|
+
FILE_NAME1 = "1.avi"
|
48
|
-
|
50
|
+
|
49
|
-
FILE_NAME2 = "
|
51
|
+
FILE_NAME2 = "2.avi"
|
50
|
-
|
52
|
+
|
51
|
-
FILE_NAME3 = "
|
53
|
+
FILE_NAME3 = "3.avi"
|
52
|
-
|
54
|
+
|
53
|
-
FILE_NAME4 = "
|
55
|
+
FILE_NAME4 = "4.avi"
|
54
|
-
|
56
|
+
|
55
|
-
FILE_NAME5 = "
|
57
|
+
FILE_NAME5 = "5.avi"
|
56
|
-
|
57
|
-
|
58
|
-
|
58
|
+
|
59
|
+
|
60
|
+
|
59
|
-
DEVICE_ID =
|
61
|
+
DEVICE_ID = 1
|
60
62
|
|
61
63
|
|
62
64
|
|
@@ -138,253 +140,179 @@
|
|
138
140
|
|
139
141
|
while end_flag == True:
|
140
142
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
r
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
#p
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
print(
|
194
|
-
|
195
|
-
i
|
196
|
-
|
197
|
-
#
|
198
|
-
|
199
|
-
#i
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
#d
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
p
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
p
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
e
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
#area = cv2.contourArea(contour)
|
288
|
-
|
289
|
-
#if area < 1000: #30
|
290
|
-
|
291
|
-
#continue
|
143
|
+
gauss = cv2.GaussianBlur(c_frame, (11, 11), 0)
|
144
|
+
|
145
|
+
gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY)
|
146
|
+
|
147
|
+
ret,th1 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
|
148
|
+
|
149
|
+
edges = cv2.Canny(th1, 50, 150)
|
150
|
+
|
151
|
+
img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
|
152
|
+
|
153
|
+
for i in range(0, len(contours)):
|
154
|
+
|
155
|
+
#for cnt in contours:
|
156
|
+
|
157
|
+
cnt = contours[i]
|
158
|
+
|
159
|
+
area = cv2.contourArea(cnt)
|
160
|
+
|
161
|
+
print(area)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
if 500 < area < 5000:
|
166
|
+
|
167
|
+
rect = cv2.minAreaRect(cnt)
|
168
|
+
|
169
|
+
box = cv2.boxPoints(rect)
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
(cx, cy), (width, height), angle = rect
|
174
|
+
|
175
|
+
diag = np.linalg.norm([width, height])
|
176
|
+
|
177
|
+
print(width, height, diag)
|
178
|
+
|
179
|
+
# 161.91236877441406 16.144147872924805 162.715237986
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
# 長方形の幅、高さ、対角線の長さは cv2.minAreaRect() の返り値
|
184
|
+
|
185
|
+
# を見ればわかるので計算しなくてもよい。
|
186
|
+
|
187
|
+
#x1, x2, x3, x4 = box
|
188
|
+
|
189
|
+
#A = np.linalg.norm(x1 - x2)
|
190
|
+
|
191
|
+
#B = np.linalg.norm(x1 - x3)
|
192
|
+
|
193
|
+
#C = np.linalg.norm(x1 - x4)
|
194
|
+
|
195
|
+
#print("頂点",x1, x2, x3, x4)
|
196
|
+
|
197
|
+
#print("Aの長さ", A) # Aの長さ 16.1441
|
198
|
+
|
199
|
+
#print("Bの長さ",B) # Bの長さ 162.715
|
200
|
+
|
201
|
+
#print("Cの長さ",C) # Cの長さ 161.912
|
202
|
+
|
203
|
+
A = width
|
204
|
+
|
205
|
+
B = height
|
206
|
+
|
207
|
+
C = diag
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
if A < B:
|
212
|
+
|
213
|
+
min = A
|
214
|
+
|
215
|
+
else:
|
216
|
+
|
217
|
+
min = B
|
218
|
+
|
219
|
+
if C < min:
|
220
|
+
|
221
|
+
min = C
|
222
|
+
|
223
|
+
print("最小", min)
|
224
|
+
|
225
|
+
if 5 < min < 25:
|
226
|
+
|
227
|
+
# drawContours() の contours 引数は int 型でないといけない。
|
228
|
+
|
229
|
+
cv2.drawContours(c_frame, [box.astype(int)], -1, (0, 255, 0), 2)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
def within(line, rect):
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
p1, p2 = tuple(line[:2]), tuple(line[2:])
|
240
|
+
|
241
|
+
# 線の始点と終点が長方形内に含まれるかどうか
|
242
|
+
|
243
|
+
return cv2.pointPolygonTest(box, p1, False) >= 0 and \
|
244
|
+
|
245
|
+
cv2.pointPolygonTest(box, p2, False) >= 0
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
LSD = cv2.createLineSegmentDetector()
|
250
|
+
|
251
|
+
lines, width, prec, nfa = LSD.detect(edges)
|
252
|
+
|
253
|
+
lines = np.squeeze(lines)
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
for line in lines:
|
258
|
+
|
259
|
+
p1, p2 = tuple(line[:2]), tuple(line[2:])
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if within(line, rect):
|
264
|
+
|
265
|
+
# 長方形に含まれる場合は赤色で描画
|
266
|
+
|
267
|
+
cv2.line(c_frame, p1, p2, (0, 0, 255), 2)
|
268
|
+
|
269
|
+
else:
|
270
|
+
|
271
|
+
# 長方形に含まれない場合は赤色で描画
|
272
|
+
|
273
|
+
cv2.line(c_frame, p1, p2, (255, 0, 0), 2)
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
cv2.imshow(ORG_WINDOW_NAME, c_frame)
|
278
|
+
|
279
|
+
#cv2.imshow('res',gauss)
|
280
|
+
|
281
|
+
#cv2.imshow('4', disp_in_img)
|
282
|
+
|
283
|
+
#cv2.imshow('3', gray)
|
284
|
+
|
285
|
+
#cv2.imshow("1", th1)
|
286
|
+
|
287
|
+
#cv2.imshow("2", edges)
|
292
288
|
|
293
289
|
#
|
294
290
|
|
295
|
-
#if image_size * 0.39 < area: #0.99
|
296
|
-
|
297
|
-
#continue
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
#x,y,w,h = cv2.boundingRect(edges)
|
302
|
-
|
303
|
-
#print(x)
|
304
|
-
|
305
|
-
#
|
291
|
+
#rec.write(gray)
|
292
|
+
|
306
|
-
|
293
|
+
# rec2.write(c_frame)
|
294
|
+
|
295
|
+
#rec3.write(th1)
|
296
|
+
|
307
|
-
#
|
297
|
+
#rec4.write(edges)
|
308
|
-
|
298
|
+
|
309
|
-
#
|
299
|
+
#rec5.write(gauss)
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
#c_frame = cv2.rectangle(c_frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
|
314
|
-
|
315
|
-
LSD = cv2.createLineSegmentDetector()
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
#
|
320
|
-
|
321
|
-
lines, width, prec, nfa = LSD.detect(edges)
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
#
|
326
|
-
|
327
|
-
color = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
if lines is not None:
|
332
|
-
|
333
|
-
for i in range(len(lines)):
|
334
|
-
|
335
|
-
for x1,y1,x2,y2 in lines[i] :
|
336
|
-
|
337
|
-
cv2.line(color,(x1,y1),(x2,y2),(0,0,255),2)
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
#
|
342
|
-
|
343
|
-
disp_in_img = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
|
344
300
|
|
345
301
|
|
346
302
|
|
347
303
|
#
|
348
304
|
|
349
|
-
cv2.imshow(ORG_WINDOW_NAME, c_frame)
|
350
|
-
|
351
|
-
cv2.imshow('res',gauss)
|
352
|
-
|
353
|
-
cv2.imshow('4', disp_in_img)
|
354
|
-
|
355
|
-
cv2.imshow('3', gray)
|
356
|
-
|
357
|
-
cv2.i
|
305
|
+
key = cv2.waitKey(INTERVAL)
|
358
|
-
|
306
|
+
|
359
|
-
|
307
|
+
if key == ESC_KEY:
|
308
|
+
|
309
|
+
break
|
310
|
+
|
311
|
+
|
360
312
|
|
361
313
|
#
|
362
314
|
|
363
|
-
#rec.write(gray)
|
364
|
-
|
365
|
-
rec2.write(c_frame)
|
366
|
-
|
367
|
-
#rec3.write(th1)
|
368
|
-
|
369
|
-
#rec4.write(edges)
|
370
|
-
|
371
|
-
#rec5.write(gauss)
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
#
|
376
|
-
|
377
|
-
key = cv2.waitKey(INTERVAL)
|
378
|
-
|
379
|
-
if key == ESC_KEY:
|
380
|
-
|
381
|
-
break
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
#
|
386
|
-
|
387
|
-
|
315
|
+
end_flag, c_frame = cap.read()
|
388
316
|
|
389
317
|
|
390
318
|
|