Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ImageProcess/ImageDetectPython/ImageDetectPython.py at master · ErrorGz/ImageProcess · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ErrorGz
ImageProcess
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ImageProcess
/
ImageDetectPython
/
ImageDetectPython.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
232 lines (178 loc) · 8.67 KB
master
Breadcrumbs
ImageProcess
/
ImageDetectPython
/
ImageDetectPython.py
Copy path
Top
File metadata and controls
Code
Blame
232 lines (178 loc) · 8.67 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
190
191
192
193
194
195
196
197
198
199
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
226
227
228
229
230
231
232
import
time
from
ultralytics
import
YOLO
import
cv2
import
numpy
as
np
import
math
from
collections
import
defaultdict
# Load the YOLOv8 model
model
=
YOLO
(
'yolov8n-pose.pt'
)
# 新建颜色数组
colors
=
[(
255
,
0
,
0
), (
0
,
255
,
0
), (
0
,
0
,
255
), (
255
,
255
,
0
), (
0
,
255
,
255
), (
255
,
0
,
255
), (
128
,
0
,
0
),
(
0
,
128
,
0
), (
0
,
0
,
128
), (
128
,
128
,
0
), (
0
,
128
,
128
), (
128
,
0
,
128
), (
128
,
128
,
128
), (
64
,
0
,
0
),
(
0
,
64
,
0
), (
0
,
0
,
64
), (
64
,
64
,
0
), (
0
,
64
,
64
), (
64
,
0
,
64
), (
64
,
64
,
64
)]
# Open the video file
video_path
=
"E:\BaiduSyncdisk\PY\DataSet\视频
\9
月14日.mp4"
RTSP
=
'rtsp://guest:123456@10.103.8.236:554/avstream/channel=1/stream=1.sdp'
usb_cam
=
0
cap
=
cv2
.
VideoCapture
(
RTSP
)
# Store the track history
track_history
=
defaultdict
(
lambda
: [])
# 定义变量来保存当前视频的时间位置,单位为毫秒
current_time
=
0
# 定义回退和前进的时间间隔,单位为毫秒
skip_interval
=
10000
# 10秒
# 定义变量来计算帧率
fps_start_time
=
time
.
time
()
fps_counter
=
0
# 创建一个标志变量,用于控制暂停和继续
paused
=
False
# Loop through the video frames
while
cap
.
isOpened
():
# Read a frame from the video
success
,
frame
=
cap
.
read
()
if
success
:
# 对frame缩小比例
frame
=
cv2
.
resize
(
frame
, (
640
,
480
))
# Run YOLOv8 tracking on the frame, persisting tracks between frames
results
=
model
.
track
(
frame
,
persist
=
True
)
# 读取结果
boxes
=
results
[
0
].
boxes
.
xywh
.
cpu
().
tolist
()
cls_list
=
results
[
0
].
boxes
.
cls
.
int
().
cpu
().
tolist
()
track_ids
=
results
[
0
].
boxes
.
id
.
int
().
cpu
().
tolist
()
if
results
[
0
].
boxes
.
id
is
not
None
else
[]
keypoints
=
results
[
0
].
keypoints
.
xy
.
cpu
()
annotated_frame
=
results
[
0
].
orig_img
# for i, (track_id, cls, box, points) in enumerate(zip(track_ids, cls_list, boxes, keypoints)):
# # 绘制追行跟进
# cv2.rectangle(annotated_frame, (int(box[1]), int(box[0])), (int(box[3]), int(box[2])), colors[cls], 2)
# cv2.putText(annotated_frame, str(track_id), (int(box[0]), int(box[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[cls], 2)
# Plot the tracks
for
box
,
track_id
in
zip
(
boxes
,
track_ids
):
x
,
y
,
w
,
h
=
box
track
=
track_history
[
track_id
]
track
.
append
((
float
(
x
),
float
(
y
)))
# x, y center point
if
len
(
track
)
>
30
:
# retain 90 tracks for 90 frames
track
.
pop
(
0
)
# Draw the tracking lines
points
=
np
.
hstack
(
track
).
astype
(
np
.
int32
).
reshape
((
-
1
,
1
,
2
))
# cv2.polylines(annotated_frame, [points], isClosed=False, color=(
# 230, 230, 230), thickness=10)
# 绘制track_id
track_id_text
=
f"Track ID:
{
track_id
}
"
cv2
.
putText
(
annotated_frame
,
track_id_text
, (
int
(
x
-
w
/
2
),
int
(
y
-
h
/
2
)),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.75
, (
0
,
0
,
255
),
2
)
for
i
in
range
(
keypoints
.
shape
[
0
]):
points
=
keypoints
[
i
].
tolist
()
box
=
boxes
[
i
]
if
boxes
is
not
None
and
len
(
boxes
)
>
i
else
None
cls
=
cls_list
[
i
]
if
cls_list
is
not
None
and
len
(
cls_list
)
>
i
else
None
name
=
results
[
0
].
names
[
cls
]
if
cls_list
is
not
None
and
len
(
cls_list
)
>
i
and
cls_list
[
i
]
is
not
None
else
"Default Name"
# center_x, center_y, width, height = box if box is not None else (None, None, None, None)
# # 绘制 box
# if center_x is not None and center_y is not None and width is not None and height is not None:
# top_left = (int(center_x - width / 2), int(center_y - height / 2))
# bottom_right = (int(center_x + width / 2), int(center_y + height / 2))
# cv2.rectangle(annotated_frame, top_left, bottom_right, colors[cls], 2)
# cv2.putText(annotated_frame, str(name), top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[cls], 2)
# 绘制关键点连线
if
len
(
points
)
==
17
:
# 计算并连接中间点a和b
p1
=
points
[
1
]
p2
=
points
[
2
]
p3
=
points
[
3
]
p4
=
points
[
4
]
a
=
(
int
((
p1
[
0
]
+
p2
[
0
])
/
2
),
int
((
p1
[
1
]
+
p2
[
1
])
/
2
))
b
=
(
int
((
p3
[
0
]
+
p4
[
0
])
/
2
),
int
((
p3
[
1
]
+
p4
[
1
])
/
2
))
# 计算圆的半径
radius
=
1.5
*
max
(
math
.
sqrt
((
points
[
0
][
0
]
-
p3
[
0
])
**
2
+
(
points
[
0
][
1
]
-
p3
[
1
])
**
2
),
math
.
sqrt
((
points
[
0
][
0
]
-
p4
[
0
])
**
2
+
(
points
[
0
][
1
]
-
p4
[
1
])
**
2
))
# 计算线段的终点坐标
angle
=
math
.
atan2
(
a
[
1
]
-
b
[
1
],
a
[
0
]
-
b
[
0
])
endpoint
=
((
int
)(
b
[
0
]
+
radius
*
math
.
cos
(
angle
)),
(
int
)(
b
[
1
]
+
radius
*
math
.
sin
(
angle
)))
# 绘制线段
cv2
.
line
(
annotated_frame
,
b
,
endpoint
,
colors
[
i
%
len
(
colors
)],
2
)
# 计算第15点和第16点的中间点作为底
bottomPointX
=
(
points
[
15
][
0
]
+
points
[
16
][
0
])
/
2
bottomPointY
=
(
points
[
15
][
1
]
+
points
[
16
][
1
])
/
2
# 连接第5点和第6点
p5
=
points
[
5
]
p6
=
points
[
6
]
cv2
.
line
(
annotated_frame
, (
int
(
p5
[
0
]),
int
(
p5
[
1
])),
(
int
(
p6
[
0
]),
int
(
p6
[
1
])),
colors
[
i
%
len
(
colors
)],
2
)
# 连接第11点和第12点
p11
=
points
[
11
]
p12
=
points
[
12
]
cv2
.
line
(
annotated_frame
, (
int
(
p11
[
0
]),
int
(
p11
[
1
])),
(
int
(
p12
[
0
]),
int
(
p12
[
1
])),
colors
[
i
%
len
(
colors
)],
2
)
# 连接第5点、第7点和第9点
p7
=
points
[
7
]
p9
=
points
[
9
]
cv2
.
line
(
annotated_frame
, (
int
(
p5
[
0
]),
int
(
p5
[
1
])),
(
int
(
p7
[
0
]),
int
(
p7
[
1
])),
colors
[
i
%
len
(
colors
)],
2
)
cv2
.
line
(
annotated_frame
, (
int
(
p7
[
0
]),
int
(
p7
[
1
])),
(
int
(
p9
[
0
]),
int
(
p9
[
1
])),
colors
[
i
%
len
(
colors
)],
2
)
p8
=
points
[
8
]
p10
=
points
[
10
]
cv2
.
line
(
annotated_frame
, (
int
(
p6
[
0
]),
int
(
p6
[
1
])),
(
int
(
p8
[
0
]),
int
(
p8
[
1
])),
colors
[
i
%
len
(
colors
)],
2
)
cv2
.
line
(
annotated_frame
, (
int
(
p8
[
0
]),
int
(
p8
[
1
])),
(
int
(
p10
[
0
]),
int
(
p10
[
1
])),
colors
[
i
%
len
(
colors
)],
2
)
# 计算并显示帧率
fps_counter
+=
1
if
time
.
time
()
-
fps_start_time
>=
1.0
:
fps
=
fps_counter
/
(
time
.
time
()
-
fps_start_time
)
print
(
"FPS:"
,
round
(
fps
,
2
))
fps_text
=
f"FPS:
{
round
(
fps
,
2
)
}
"
fps_start_time
=
time
.
time
()
fps_counter
=
0
cv2
.
putText
(
annotated_frame
,
fps_text
, (
0
,
30
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
1
, (
0
,
255
,
0
),
2
)
# Display the annotated frame
cv2
.
imshow
(
"YOLOv8 Tracking"
,
annotated_frame
)
# 更新当前时间位置
current_time
=
cap
.
get
(
cv2
.
CAP_PROP_POS_MSEC
)
# 检查是否按下了键盘上的键
key
=
cv2
.
waitKey
(
1
)
# 按下空格键暂停/继续
if
key
==
ord
(
" "
):
paused
=
not
paused
# 切换暂停状态
if
paused
:
print
(
"Paused. Press any key to continue."
)
else
:
print
(
"Resumed."
)
# 如果处于暂停状态,等待按下任意键继续
if
paused
:
cv2
.
waitKey
(
0
)
paused
=
False
# 切换为非暂停状态
# 按下左键回退10秒
if
key
==
ord
(
"a"
):
# 计算目标时间位置
target_time
=
current_time
-
skip_interval
# 调整视频的时间位置
cap
.
set
(
cv2
.
CAP_PROP_POS_MSEC
,
target_time
)
# 更新当前时间位置
current_time
=
target_time
print
(
"回退10秒,当前时间位置:"
,
current_time
)
# 按下右键前进10秒
if
key
==
ord
(
"d"
):
# 计算目标时间位置
target_time
=
current_time
+
skip_interval
# 调整视频的时间位置
cap
.
set
(
cv2
.
CAP_PROP_POS_MSEC
,
target_time
)
# 更新当前时间位置
current_time
=
target_time
print
(
"前进10秒,当前时间位置:"
,
current_time
)
# 按下q退出循环
if
key
==
ord
(
"q"
)
or
key
==
27
:
# 27 对应 ESC 键的 ASCII 值
break
else
:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap
.
release
()
cv2
.
destroyAllWindows
()
You can’t perform that action at this time.