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/ImageDetectPythonEN.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
/
ImageDetectPythonEN.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
212 lines (161 loc) · 7.18 KB
master
Breadcrumbs
ImageProcess
/
ImageDetectPython
/
ImageDetectPythonEN.py
Copy path
Top
File metadata and controls
Code
Blame
212 lines (161 loc) · 7.18 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
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'
)
# Create a color array
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
: [])
# Define variable to save current video time position, unit is milliseconds
current_time
=
0
# Define the time interval for rewinding and fast forwarding, unit is milliseconds
skip_interval
=
10000
# 10秒
# Define variable to calculate frame rate
fps_start_time
=
time
.
time
()
fps_counter
=
0
# Create a flag variable to control pause and resume
paused
=
False
# Loop through the video frames
while
cap
.
isOpened
():
# Read a frame from the video
success
,
frame
=
cap
.
read
()
if
success
:
# Downscale the frame
frame
=
cv2
.
resize
(
frame
, (
640
,
480
))
# Run YOLOv8 tracking on the frame, persisting tracks between frames
results
=
model
.
track
(
frame
,
persist
=
True
)
# Read the results
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
# 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
))
# Draw track_id text
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
()
# Draw keypoint connections
if
len
(
points
)
==
17
:
# connecting middle points a and 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 of circle
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
))
# coordinates of endpoint
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
)))
# Draw line segment
cv2
.
line
(
annotated_frame
,
b
,
endpoint
,
colors
[
i
%
len
(
colors
)],
2
)
# midpoint of point 15 and 16 as bottom
bottomPointX
=
(
points
[
15
][
0
]
+
points
[
16
][
0
])
/
2
bottomPointY
=
(
points
[
15
][
1
]
+
points
[
16
][
1
])
/
2
# Connect point 5 and 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
)
# Connect point 11 and 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
)
# Connect point 5, 7 and 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
)
# Calculate and display frame rate
fps_counter
+=
1
if
time
.
time
()
-
fps_start_time
>=
1.0
:
fps
=
fps_counter
/
(
time
.
time
()
-
fps_start_time
)
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
)
# Update current time position
current_time
=
cap
.
get
(
cv2
.
CAP_PROP_POS_MSEC
)
# Check if any key is pressed on the keyboard
key
=
cv2
.
waitKey
(
1
)
# Pause/resume if space key is pressed
if
key
==
ord
(
" "
):
paused
=
not
paused
# Toggle pause state
if
paused
:
print
(
"Paused. Press any key to continue."
)
else
:
print
(
"Resumed."
)
# If in paused state, wait for any key press to continue
if
paused
:
cv2
.
waitKey
(
0
)
paused
=
False
# Toggle to non-paused state
# Rewind 10 seconds if left key is pressed
if
key
==
ord
(
"a"
):
# Calculate target time position
target_time
=
current_time
-
skip_interval
# Adjust video time position
cap
.
set
(
cv2
.
CAP_PROP_POS_MSEC
,
target_time
)
# Update current time position
current_time
=
target_time
print
(
"Rewind 10 seconds, current time position:"
,
current_time
)
# Fast forward 10 seconds if right key is pressed
if
key
==
ord
(
"d"
):
# Calculate target time position
target_time
=
current_time
+
skip_interval
# Adjust video time position
cap
.
set
(
cv2
.
CAP_PROP_POS_MSEC
,
target_time
)
# Update current time position
current_time
=
target_time
print
(
"Fast forward 10 seconds, current time position:"
,
current_time
)
# Break loop if q key or ESC (27) is pressed
if
key
==
ord
(
"q"
)
or
key
==
27
:
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.