Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
evsuits
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
evsuits
Commits
5fed4a48
提交
5fed4a48
authored
7月 25, 2019
作者:
blu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init
上级
07773efd
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
74 行增加
和
0 行删除
+74
-0
motion.py
opencv-motion-detect/motion.py
+74
-0
没有找到文件。
opencv-motion-detect/motion.py
0 → 100644
浏览文件 @
5fed4a48
#!/bin/python
import
cv2
,
imutils
import
os
,
sys
,
datetime
import
time
class
MotionDetector
:
def
__init__
(
self
,
env
):
self
.
env
=
env
self
.
cap
=
cv2
.
VideoCapture
(
env
[
'STREAM_ADDR'
])
self
.
height
=
self
.
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
)
self
.
width
=
self
.
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
)
self
.
fps
=
self
.
cap
.
get
(
cv2
.
CAP_PROP_FPS
)
self
.
avg
=
None
print
(
self
.
width
,
self
.
height
,
self
.
fps
)
def
run
(
self
):
while
self
.
cap
.
isOpened
()
and
cv2
.
waitKey
(
1
)
<
0
:
start
=
datetime
.
datetime
.
now
()
.
timestamp
()
ret
,
frame
=
self
.
cap
.
read
()
if
not
ret
:
break
frame
=
imutils
.
resize
(
frame
,
width
=
500
)
gray
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
gray
=
cv2
.
GaussianBlur
(
gray
,
(
21
,
21
),
0
)
if
self
.
avg
is
None
:
self
.
avg
=
gray
.
copy
()
.
astype
(
"float"
)
continue
cv2
.
accumulateWeighted
(
gray
,
self
.
avg
,
0.5
)
frameDelta
=
cv2
.
absdiff
(
gray
,
cv2
.
convertScaleAbs
(
self
.
avg
))
# threshold the delta image, dilate the thresholded image to fill
# in holes, then find contours on thresholded image
thresh
=
cv2
.
threshold
(
frameDelta
,
self
.
env
[
'SENSITIVITY'
],
255
,
cv2
.
THRESH_BINARY
)[
1
]
thresh
=
cv2
.
dilate
(
thresh
,
None
,
iterations
=
2
)
cnts
=
cv2
.
findContours
(
thresh
.
copy
(),
cv2
.
RETR_EXTERNAL
,
cv2
.
CHAIN_APPROX_SIMPLE
)
cnts
=
imutils
.
grab_contours
(
cnts
)
text
=
'No Motion'
# loop over the contours
for
c
in
cnts
:
# if the contour is too small, ignore it
if
cv2
.
contourArea
(
c
)
<
self
.
env
[
'AREA_SIZE'
]:
continue
# compute the bounding box for the contour, draw it on the frame,
# and update the text
(
x
,
y
,
w
,
h
)
=
cv2
.
boundingRect
(
c
)
cv2
.
rectangle
(
frame
,
(
x
,
y
),
(
x
+
w
,
y
+
h
),
(
0
,
255
,
0
),
2
)
text
=
"Motion Detected"
# draw the text and timestamp on the frame
ts
=
datetime
.
datetime
.
now
()
.
strftime
(
"
%
A
%
d
%
B
%
Y
%
I:
%
M:
%
S
%
p"
)
cv2
.
putText
(
frame
,
"Room Status: {}"
.
format
(
text
),
(
10
,
20
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
(
0
,
0
,
255
),
2
)
cv2
.
putText
(
frame
,
ts
,
(
10
,
frame
.
shape
[
0
]
-
10
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.35
,
(
0
,
0
,
255
),
1
)
cv2
.
imshow
(
'frame'
,
frame
)
# fps implication
delta
=
1.0
/
self
.
fps
-
(
datetime
.
datetime
.
now
()
.
timestamp
()
-
start
)
delta
=
0
if
delta
<
0
else
delta
time
.
sleep
(
delta
)
self
.
cap
.
release
()
cv2
.
destroyAllWindows
()
if
__name__
==
'__main__'
:
env
=
{}
env
[
'STREAM_ADDR'
]
=
os
.
getenv
(
'STREAM_ADDR'
,
'rtsp://172.31.0.121/live/0/sub'
)
env
[
'PROC_FPS'
]
=
int
(
os
.
getenv
(
'PROC_FPS'
,
10
))
env
[
'SENSITIVITY'
]
=
int
(
os
.
getenv
(
'SENSITIVITY'
,
20
))
env
[
'AREA_SIZE'
]
=
int
(
os
.
getenv
(
'AREA_SIZE'
,
200
))
detector
=
MotionDetector
(
env
)
detector
.
run
()
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论