FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vcs/_4.python/__code/PythonHelp/Python_code_ch09/sentry.py at master · bunshue/vcs · GitHub
bunshue
/
vcs
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
0
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vcs
/
_4.python
/
__code
/
PythonHelp
/
Python_code_ch09
/
sentry.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
66 lines (57 loc) · 2.62 KB
Breadcrumbs
vcs
/
_4.python
/
__code
/
PythonHelp
/
Python_code_ch09
/
sentry.py
Copy path
File metadata and controls
66 lines (57 loc) · 2.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import
os
import
time
from
playsound
import
playsound
import
cv2
as
cv
xml_filename1
=
"D:/_git/vcs/_4.python/opencv/data/_xml/haarcascades/haarcascade_frontalface_default.xml"
face_cascade
=
cv
.
CascadeClassifier
(
xml_filename1
)
xml_filename2
=
"D:/_git/vcs/_4.python/opencv/data/_xml/haarcascades/haarcascade_eye.xml"
eye_cascade
=
cv
.
CascadeClassifier
(
xml_filename2
)
# 切換到放置影像的資料夾
os
.
chdir
(
'corridor_5'
)
contents
=
sorted
(
os
.
listdir
())
# 偵測人臉並判斷是否開火
for
image
in
contents
:
discharge_weapon
=
True
img_gray
=
cv
.
imread
(
image
,
cv
.
IMREAD_GRAYSCALE
)
height
,
width
=
img_gray
.
shape
cv
.
imshow
(
f'Motion detected
{
image
}
'
,
img_gray
)
cv
.
waitKey
(
2000
)
cv
.
destroyWindow
(
f'Motion detected
{
image
}
'
)
print
(
'a'
)
# 找到偵測到的人臉矩形
face_rect_list
=
[]
print
(
'b'
)
face_rect_list
.
append
(
face_cascade
.
detectMultiScale
(
image
=
img_gray
,
scaleFactor
=
1.1
,
minNeighbors
=
5
))
print
(
'c'
)
print
(
f"Searching
{
image
}
for eyes."
)
for
rect
in
face_rect_list
:
for
(
x
,
y
,
w
,
h
)
in
rect
:
rect_4_eyes
=
img_gray
[
y
:
y
+
h
,
x
:
x
+
w
]
eyes
=
eye_cascade
.
detectMultiScale
(
image
=
rect_4_eyes
,
scaleFactor
=
1.05
,
minNeighbors
=
2
)
for
(
xe
,
ye
,
we
,
he
)
in
eyes
:
print
(
"Eye detected."
)
center
=
(
int
(
xe
+
0.5
*
we
),
int
(
ye
+
0.5
*
he
))
radius
=
int
((
we
+
he
)
/
3
)
cv
.
circle
(
rect_4_eyes
,
center
,
radius
,
255
,
2
)
cv
.
rectangle
(
img_gray
, (
x
,
y
), (
x
+
w
,
y
+
h
), (
255
,
255
,
255
),
2
)
discharge_weapon
=
False
break
if
discharge_weapon
==
False
:
playsound
(
"tone.wav"
,
block
=
False
)
# 播放音訊檔案
cv
.
imshow
(
'Detected Faces'
,
img_gray
)
# 顯示當前偵測完成之影像
cv
.
waitKey
(
2000
)
# 視窗停滯 2 秒
cv
.
destroyWindow
(
'Detected Faces'
)
# 關閉視窗
time
.
sleep
(
5
)
# 暫停程式 5 秒
else
:
print
(
f"No face in
{
image
}
. Discharging weapon!"
)
# 印出開火訊息
cv
.
putText
(
img_gray
,
'FIRE!'
, (
int
(
width
/
2
)
-
20
,
int
(
height
/
2
)),
cv
.
FONT_HERSHEY_PLAIN
,
3
,
255
,
3
)
playsound
(
"gunfire.wav"
,
block
=
False
)
# 播放槍聲檔案
cv
.
imshow
(
'Mutant'
,
img_gray
)
# 顯示當前偵測完成之影像
cv
.
waitKey
(
2000
)
# 視窗停滯 2 秒
cv
.
destroyWindow
(
'Mutant'
)
# 關閉視窗
time
.
sleep
(
3
)
# 暫停程式 3 秒
Back
|
FazBrowse Home
|
New Git URL