質問編集履歴
1
コードを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,163 @@
|
|
25
25
|
[ WARN:0] terminating async callback
|
26
26
|
|
27
27
|
```
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
### コード
|
32
|
+
|
33
|
+
``````
|
34
|
+
|
35
|
+
import cv2
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
cap = cv2.VideoCapture(0)
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
ok = False
|
44
|
+
|
45
|
+
before = None
|
46
|
+
|
47
|
+
detected_frame = None
|
48
|
+
|
49
|
+
bbox = (0,0,0,0)
|
50
|
+
|
51
|
+
while True:
|
52
|
+
|
53
|
+
# OpenCVでWebカメラの画像を取り込む
|
54
|
+
|
55
|
+
ret, frame = cap.read()
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
# スクリーンショットを撮りたい関係で1/2サイズに縮小
|
60
|
+
|
61
|
+
frame = cv2.resize(frame, (int(frame.shape[1]/2), int(frame.shape[0]/2)))
|
62
|
+
|
63
|
+
# 加工なし画像を表示する
|
64
|
+
|
65
|
+
cv2.imshow('Raw Frame', frame)
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
# 取り込んだフレームに対して差分をとって動いているところが明るい画像を作る
|
70
|
+
|
71
|
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
72
|
+
|
73
|
+
if before is None:
|
74
|
+
|
75
|
+
before = gray.copy().astype('float')
|
76
|
+
|
77
|
+
continue
|
78
|
+
|
79
|
+
cv2.accumulateWeighted(gray, before, 0.7)
|
80
|
+
|
81
|
+
mdframe = cv2.absdiff(gray, cv2.convertScaleAbs(before))
|
82
|
+
|
83
|
+
# 動いているところが明るい画像を表示する
|
84
|
+
|
85
|
+
# cv2.imshow('MotionDetected Frame', mdframe)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
# 動いているエリアの面積を計算してちょうどいい検知結果を抽出する
|
90
|
+
|
91
|
+
thresh = cv2.threshold(mdframe, 3, 255, cv2.THRESH_BINARY)[1]
|
92
|
+
|
93
|
+
image, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
94
|
+
|
95
|
+
max_area = 0
|
96
|
+
|
97
|
+
target = None
|
98
|
+
|
99
|
+
for cnt in contours:
|
100
|
+
|
101
|
+
area = cv2.contourArea(cnt)
|
102
|
+
|
103
|
+
if max_area < area and area < 40000 and area > 4000:
|
104
|
+
|
105
|
+
max_area = area;
|
106
|
+
|
107
|
+
target = cnt
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# 動いているエリアのうちそこそこの大きさのものがあればそれを矩形で表示する
|
112
|
+
|
113
|
+
# ちょうどいいエリアがなかったら最後の動いているエリアがあるフレームとエリア情報を用いてトラッキングをする
|
114
|
+
|
115
|
+
# どうしようもない時はどうしようもない旨を表示する
|
116
|
+
|
117
|
+
if max_area <= 4000:
|
118
|
+
|
119
|
+
track = False
|
120
|
+
|
121
|
+
if detected_frame is not None:
|
122
|
+
|
123
|
+
# インスタンスを作り直さなきゃいけないっぽい
|
124
|
+
|
125
|
+
tracker = cv2.TrackerKCF_create()
|
126
|
+
|
127
|
+
ok = tracker.init(detected_frame, bbox)
|
128
|
+
|
129
|
+
detected_frame = None
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
if ok:
|
134
|
+
|
135
|
+
track, bbox = tracker.update(frame)
|
136
|
+
|
137
|
+
if track:
|
138
|
+
|
139
|
+
p1 = (int(bbox[0]), int(bbox[1]))
|
140
|
+
|
141
|
+
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
|
142
|
+
|
143
|
+
cv2.rectangle(frame, p1, p2, (0,255,0), 2, 1)
|
144
|
+
|
145
|
+
cv2.putText(frame, "tracking", (10,50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1, cv2.LINE_AA)
|
146
|
+
|
147
|
+
else:
|
148
|
+
|
149
|
+
ok = False
|
150
|
+
|
151
|
+
cv2.putText(frame, "(^q^)", (10,50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1, cv2.LINE_AA)
|
152
|
+
|
153
|
+
else:
|
154
|
+
|
155
|
+
#areaframe = cv2.drawContours(frame, [target], 0, (0,255,0), 3)
|
156
|
+
|
157
|
+
x,y,w,h = cv2.boundingRect(target)
|
158
|
+
|
159
|
+
bbox = (x,y,w,h)
|
160
|
+
|
161
|
+
detected_frame = frame.copy()
|
162
|
+
|
163
|
+
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
|
164
|
+
|
165
|
+
cv2.putText(frame, "motion detected", (10,50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1, cv2.LINE_AA)
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
cv2.imshow('MotionDetected Area Frame', frame)
|
170
|
+
|
171
|
+
# キー入力を1ms待って、k が27(ESC)だったらBreakする
|
172
|
+
|
173
|
+
k = cv2.waitKey(1)
|
174
|
+
|
175
|
+
if k == 27:
|
176
|
+
|
177
|
+
break
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
# キャプチャをリリースして、ウィンドウをすべて閉じる
|
182
|
+
|
183
|
+
cap.release()
|
184
|
+
|
185
|
+
cv2.destroyAllWindows()
|
186
|
+
|
187
|
+
```
|