Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
evsuits
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
evsuits
Commits
f37e5603
提交
f37e5603
authored
7月 30, 2019
作者:
blu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init
上级
b377683a
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
20 行增加
和
10 行删除
+20
-10
motion_rtsp.py
opencv-motion-detect/motion_rtsp.py
+20
-10
没有找到文件。
opencv-motion-detect/motion_rtsp.py
浏览文件 @
f37e5603
...
@@ -3,7 +3,7 @@ __author__ = 'Bruce.Lu'
...
@@ -3,7 +3,7 @@ __author__ = 'Bruce.Lu'
__date__
=
'2019/07/30'
__date__
=
'2019/07/30'
__enhencements__
=
{
"1"
:
"state machine based algorithm"
,
"2"
:
"using avg frame instead of previous frame to calc delta"
,
__enhencements__
=
{
"1"
:
"state machine based algorithm"
,
"2"
:
"using avg frame instead of previous frame to calc delta"
,
"3"
:
"does not depend on accurate process step time"
,
"4"
:
"event notification machanism"
}
"3"
:
"does not depend on accurate process step time"
,
"4"
:
"event notification machanism"
}
__credits__
=
'Zhao LiPeng for the frame-delta algorithm'
__credits__
=
[
'Zhao LiPeng for the basic frame-delta algorithm'
,
'Ge.Xu for advices'
]
import
os
,
sys
,
datetime
,
time
,
cv2
,
imutils
,
json
,
atexit
,
traceback
import
os
,
sys
,
datetime
,
time
,
cv2
,
imutils
,
json
,
atexit
,
traceback
...
@@ -12,7 +12,7 @@ from collections import deque
...
@@ -12,7 +12,7 @@ from collections import deque
class
FrameFetcher
(
Thread
):
class
FrameFetcher
(
Thread
):
def
init
(
self
):
def
init
(
self
):
self
.
cap
=
cv2
.
VideoCapture
(
env
[
'STREAM_ADDR'
])
self
.
cap
=
cv2
.
VideoCapture
(
self
.
env
[
'STREAM_ADDR'
])
self
.
videoProto
=
'RTSP'
if
'rtsp'
in
self
.
env
[
'STREAM_ADDR'
]
else
'FILE'
self
.
videoProto
=
'RTSP'
if
'rtsp'
in
self
.
env
[
'STREAM_ADDR'
]
else
'FILE'
self
.
height
=
self
.
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
)
self
.
height
=
self
.
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
)
self
.
width
=
self
.
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
)
self
.
width
=
self
.
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
)
...
@@ -34,11 +34,12 @@ class FrameFetcher(Thread):
...
@@ -34,11 +34,12 @@ class FrameFetcher(Thread):
def
run
(
self
):
def
run
(
self
):
while
True
:
while
True
:
while
self
.
cap
.
isOpened
():
while
self
.
cap
.
isOpened
():
start
=
datetime
.
datetime
.
now
()
.
timestamp
()
ret
,
frame
=
self
.
cap
.
read
()
ret
,
frame
=
self
.
cap
.
read
()
if
ret
:
if
ret
:
try
:
try
:
self
.
frameHolder
.
append
(
frame
)
self
.
frameHolder
.
append
(
frame
)
if
self
.
frameCnt
%
(
self
.
fps
*
2
)
==
0
:
if
self
.
frameCnt
%
(
self
.
fps
*
60
)
==
0
:
print
(
"frameCnt: "
,
self
.
frameCnt
)
print
(
"frameCnt: "
,
self
.
frameCnt
)
except
:
except
:
self
.
failedPutCnt
+=
1
self
.
failedPutCnt
+=
1
...
@@ -48,9 +49,12 @@ class FrameFetcher(Thread):
...
@@ -48,9 +49,12 @@ class FrameFetcher(Thread):
self
.
frameCnt
+=
1
self
.
frameCnt
+=
1
else
:
else
:
print
(
"error read frame"
)
print
(
"error read frame"
)
time
.
sleep
(
1.0
/
self
.
fps
)
break
delta
=
datetime
.
datetime
.
now
()
.
timestamp
()
-
start
print
(
"error: cap is not opened, reconnecting..."
)
delta
=
1.0
/
self
.
fps
-
delta
if
delta
>
0
:
time
.
sleep
(
delta
)
print
(
"error: connecting to camera {}, reconnecting..."
.
format
(
self
.
env
[
'STREAM_ADDR'
]))
self
.
init
()
self
.
init
()
class
MotionDetector
(
Thread
):
class
MotionDetector
(
Thread
):
...
@@ -70,8 +74,12 @@ class MotionDetector(Thread):
...
@@ -70,8 +74,12 @@ class MotionDetector(Thread):
lastEventEnterTs
=
None
lastEventEnterTs
=
None
eventState
=
None
# pre, in, post, none
eventState
=
None
# pre, in, post, none
evtQue
=
self
.
evtQue
evtQue
=
self
.
evtQue
# statistic variables
cntContNoEvent
=
0
cntContNoEvent
=
0
cntContNoEventPrev
=
0
cntContEvent
=
0
cntContEvent
=
0
cntContEventPrev
=
0
while
True
:
while
True
:
start
=
datetime
.
datetime
.
now
()
.
timestamp
()
start
=
datetime
.
datetime
.
now
()
.
timestamp
()
try
:
try
:
...
@@ -97,19 +105,21 @@ class MotionDetector(Thread):
...
@@ -97,19 +105,21 @@ class MotionDetector(Thread):
hasEvent
=
True
hasEvent
=
True
break
break
# live
loggin
g
# live
sampling lo
g
if
hasEvent
:
if
hasEvent
:
cntContEvent
+=
1
cntContEvent
+=
1
cntContNoEvent
=
0
cntContNoEvent
=
0
if
cntContEvent
%
(
self
.
env
[
'PROC_FPS'
]
*
2
)
==
0
:
if
cntContEvent
%
(
self
.
env
[
'PROC_FPS'
]
*
10
)
==
0
and
cntContEvent
!=
cntContEventPrev
:
print
(
'continous event cnt: {}, current state: {}'
.
format
(
cntContEvent
,
eventState
))
print
(
'continous event cnt: {}, current state: {}'
.
format
(
cntContEvent
,
eventState
))
cntContEventPrev
=
cntContEvent
else
:
else
:
cntContNoEvent
+=
1
cntContNoEvent
+=
1
cntContEvent
=
0
cntContEvent
=
0
if
cntContNoEvent
%
(
self
.
env
[
'PROC_FPS'
]
*
2
)
==
0
:
if
cntContNoEvent
%
(
self
.
env
[
'PROC_FPS'
]
*
10
)
==
0
and
cntContNoEventPrev
!=
cntContNoEvent
:
print
(
'continous no event cnt: {}, current state: {}'
.
format
(
cntContNoEvent
,
eventState
))
print
(
'continous no event cnt: {}, current state: {}'
.
format
(
cntContNoEvent
,
eventState
))
cntContNoEventPrev
=
cntContNoEvent
# statemachine for event
# statemachine for event
states transaction
if
eventState
==
None
:
if
eventState
==
None
:
if
hasEvent
:
if
hasEvent
:
eventState
=
'PRE'
eventState
=
'PRE'
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论