Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
66cf5c28
Unverified
提交
66cf5c28
authored
6月 09, 2021
作者:
Glenn Jocher
提交者:
GitHub
6月 09, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor detect.py arguments (#3559)
* Refactor detect.py arguments @SkalskiP @KalenMike * unused ok * comment arguments
上级
a9553c04
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
27 行删除
+46
-27
detect.py
detect.py
+46
-27
没有找到文件。
detect.py
浏览文件 @
66cf5c28
...
@@ -15,20 +15,42 @@ from utils.torch_utils import select_device, load_classifier, time_synchronized
...
@@ -15,20 +15,42 @@ from utils.torch_utils import select_device, load_classifier, time_synchronized
@torch.no_grad
()
@torch.no_grad
()
def
detect
(
opt
):
def
detect
(
weights
=
'yolov5s.pt'
,
# model.pt path(s)
source
,
weights
,
view_img
,
save_txt
,
imgsz
=
opt
.
source
,
opt
.
weights
,
opt
.
view_img
,
opt
.
save_txt
,
opt
.
img_size
source
=
'data/images'
,
# file/dir/URL/glob, 0 for webcam
save_img
=
not
opt
.
nosave
and
not
source
.
endswith
(
'.txt'
)
# save inference images
imgsz
=
640
,
# inference size (pixels)
conf_thres
=
0.25
,
# confidence threshold
iou_thres
=
0.45
,
# NMS IOU threshold
max_det
=
1000
,
# maximum detections per image
device
=
''
,
# cuda device, i.e. 0 or 0,1,2,3 or cpu
view_img
=
False
,
# show results
save_txt
=
False
,
# save results to *.txt
save_conf
=
False
,
# save confidences in --save-txt labels
save_crop
=
False
,
# save cropped prediction boxes
nosave
=
False
,
# do not save images/videos
classes
=
None
,
# filter by class: --class 0, or --class 0 2 3
agnostic_nms
=
False
,
# class-agnostic NMS
augment
=
False
,
# augmented inference
update
=
False
,
# update all models
project
=
'runs/detect'
,
# save results to project/name
name
=
'exp'
,
# save results to project/name
exist_ok
=
False
,
# existing project/name ok, do not increment
line_thickness
=
3
,
# bounding box thickness (pixels)
hide_labels
=
False
,
# hide labels
hide_conf
=
False
,
# hide confidences
half
=
False
,
# use FP16 half-precision inference
):
save_img
=
not
nosave
and
not
source
.
endswith
(
'.txt'
)
# save inference images
webcam
=
source
.
isnumeric
()
or
source
.
endswith
(
'.txt'
)
or
source
.
lower
()
.
startswith
(
webcam
=
source
.
isnumeric
()
or
source
.
endswith
(
'.txt'
)
or
source
.
lower
()
.
startswith
(
(
'rtsp://'
,
'rtmp://'
,
'http://'
,
'https://'
))
(
'rtsp://'
,
'rtmp://'
,
'http://'
,
'https://'
))
# Directories
# Directories
save_dir
=
increment_path
(
Path
(
opt
.
project
)
/
opt
.
name
,
exist_ok
=
opt
.
exist_ok
)
# increment run
save_dir
=
increment_path
(
Path
(
project
)
/
name
,
exist_ok
=
exist_ok
)
# increment run
(
save_dir
/
'labels'
if
save_txt
else
save_dir
)
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
# make dir
(
save_dir
/
'labels'
if
save_txt
else
save_dir
)
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
# make dir
# Initialize
# Initialize
set_logging
()
set_logging
()
device
=
select_device
(
opt
.
device
)
device
=
select_device
(
device
)
half
=
opt
.
half
and
device
.
type
!=
'cpu'
# half precision only supported on CUDA
half
&=
device
.
type
!=
'cpu'
# half precision only supported on CUDA
# Load model
# Load model
model
=
attempt_load
(
weights
,
map_location
=
device
)
# load FP32 model
model
=
attempt_load
(
weights
,
map_location
=
device
)
# load FP32 model
...
@@ -66,11 +88,10 @@ def detect(opt):
...
@@ -66,11 +88,10 @@ def detect(opt):
# Inference
# Inference
t1
=
time_synchronized
()
t1
=
time_synchronized
()
pred
=
model
(
img
,
augment
=
opt
.
augment
)[
0
]
pred
=
model
(
img
,
augment
=
augment
)[
0
]
# Apply NMS
# Apply NMS
pred
=
non_max_suppression
(
pred
,
opt
.
conf_thres
,
opt
.
iou_thres
,
opt
.
classes
,
opt
.
agnostic_nms
,
pred
=
non_max_suppression
(
pred
,
conf_thres
,
iou_thres
,
classes
,
agnostic_nms
,
max_det
=
max_det
)
max_det
=
opt
.
max_det
)
t2
=
time_synchronized
()
t2
=
time_synchronized
()
# Apply Classifier
# Apply Classifier
...
@@ -89,7 +110,7 @@ def detect(opt):
...
@@ -89,7 +110,7 @@ def detect(opt):
txt_path
=
str
(
save_dir
/
'labels'
/
p
.
stem
)
+
(
''
if
dataset
.
mode
==
'image'
else
f
'_{frame}'
)
# img.txt
txt_path
=
str
(
save_dir
/
'labels'
/
p
.
stem
)
+
(
''
if
dataset
.
mode
==
'image'
else
f
'_{frame}'
)
# img.txt
s
+=
'
%
gx
%
g '
%
img
.
shape
[
2
:]
# print string
s
+=
'
%
gx
%
g '
%
img
.
shape
[
2
:]
# print string
gn
=
torch
.
tensor
(
im0
.
shape
)[[
1
,
0
,
1
,
0
]]
# normalization gain whwh
gn
=
torch
.
tensor
(
im0
.
shape
)[[
1
,
0
,
1
,
0
]]
# normalization gain whwh
imc
=
im0
.
copy
()
if
opt
.
save_crop
else
im0
# for opt.
save_crop
imc
=
im0
.
copy
()
if
save_crop
else
im0
# for
save_crop
if
len
(
det
):
if
len
(
det
):
# Rescale boxes from img_size to im0 size
# Rescale boxes from img_size to im0 size
det
[:,
:
4
]
=
scale_coords
(
img
.
shape
[
2
:],
det
[:,
:
4
],
im0
.
shape
)
.
round
()
det
[:,
:
4
]
=
scale_coords
(
img
.
shape
[
2
:],
det
[:,
:
4
],
im0
.
shape
)
.
round
()
...
@@ -103,15 +124,15 @@ def detect(opt):
...
@@ -103,15 +124,15 @@ def detect(opt):
for
*
xyxy
,
conf
,
cls
in
reversed
(
det
):
for
*
xyxy
,
conf
,
cls
in
reversed
(
det
):
if
save_txt
:
# Write to file
if
save_txt
:
# Write to file
xywh
=
(
xyxy2xywh
(
torch
.
tensor
(
xyxy
)
.
view
(
1
,
4
))
/
gn
)
.
view
(
-
1
)
.
tolist
()
# normalized xywh
xywh
=
(
xyxy2xywh
(
torch
.
tensor
(
xyxy
)
.
view
(
1
,
4
))
/
gn
)
.
view
(
-
1
)
.
tolist
()
# normalized xywh
line
=
(
cls
,
*
xywh
,
conf
)
if
opt
.
save_conf
else
(
cls
,
*
xywh
)
# label format
line
=
(
cls
,
*
xywh
,
conf
)
if
save_conf
else
(
cls
,
*
xywh
)
# label format
with
open
(
txt_path
+
'.txt'
,
'a'
)
as
f
:
with
open
(
txt_path
+
'.txt'
,
'a'
)
as
f
:
f
.
write
((
'
%
g '
*
len
(
line
))
.
rstrip
()
%
line
+
'
\n
'
)
f
.
write
((
'
%
g '
*
len
(
line
))
.
rstrip
()
%
line
+
'
\n
'
)
if
save_img
or
opt
.
save_crop
or
view_img
:
# Add bbox to image
if
save_img
or
save_crop
or
view_img
:
# Add bbox to image
c
=
int
(
cls
)
# integer class
c
=
int
(
cls
)
# integer class
label
=
None
if
opt
.
hide_labels
else
(
names
[
c
]
if
opt
.
hide_conf
else
f
'{names[c]} {conf:.2f}'
)
label
=
None
if
hide_labels
else
(
names
[
c
]
if
hide_conf
else
f
'{names[c]} {conf:.2f}'
)
plot_one_box
(
xyxy
,
im0
,
label
=
label
,
color
=
colors
(
c
,
True
),
line_thickness
=
opt
.
line_thickness
)
plot_one_box
(
xyxy
,
im0
,
label
=
label
,
color
=
colors
(
c
,
True
),
line_thickness
=
line_thickness
)
if
opt
.
save_crop
:
if
save_crop
:
save_one_box
(
xyxy
,
imc
,
file
=
save_dir
/
'crops'
/
names
[
c
]
/
f
'{p.stem}.jpg'
,
BGR
=
True
)
save_one_box
(
xyxy
,
imc
,
file
=
save_dir
/
'crops'
/
names
[
c
]
/
f
'{p.stem}.jpg'
,
BGR
=
True
)
# Print time (inference + NMS)
# Print time (inference + NMS)
...
@@ -145,19 +166,22 @@ def detect(opt):
...
@@ -145,19 +166,22 @@ def detect(opt):
s
=
f
"
\n
{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}"
if
save_txt
else
''
s
=
f
"
\n
{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}"
if
save_txt
else
''
print
(
f
"Results saved to {save_dir}{s}"
)
print
(
f
"Results saved to {save_dir}{s}"
)
if
update
:
strip_optimizer
(
weights
)
# update model (to fix SourceChangeWarning)
print
(
f
'Done. ({time.time() - t0:.3f}s)'
)
print
(
f
'Done. ({time.time() - t0:.3f}s)'
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
()
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'--weights'
,
nargs
=
'+'
,
type
=
str
,
default
=
'yolov5s.pt'
,
help
=
'model.pt path(s)'
)
parser
.
add_argument
(
'--weights'
,
nargs
=
'+'
,
type
=
str
,
default
=
'yolov5s.pt'
,
help
=
'model.pt path(s)'
)
parser
.
add_argument
(
'--source'
,
type
=
str
,
default
=
'data/images'
,
help
=
'
source'
)
# file/folder, 0 for webcam
parser
.
add_argument
(
'--source'
,
type
=
str
,
default
=
'data/images'
,
help
=
'
file/dir/URL/glob, 0 for webcam'
)
parser
.
add_argument
(
'--img-size'
,
type
=
int
,
default
=
640
,
help
=
'inference size (pixels)'
)
parser
.
add_argument
(
'--img
sz'
,
'--img'
,
'--img
-size'
,
type
=
int
,
default
=
640
,
help
=
'inference size (pixels)'
)
parser
.
add_argument
(
'--conf-thres'
,
type
=
float
,
default
=
0.25
,
help
=
'
object
confidence threshold'
)
parser
.
add_argument
(
'--conf-thres'
,
type
=
float
,
default
=
0.25
,
help
=
'confidence threshold'
)
parser
.
add_argument
(
'--iou-thres'
,
type
=
float
,
default
=
0.45
,
help
=
'
IOU threshold for NMS
'
)
parser
.
add_argument
(
'--iou-thres'
,
type
=
float
,
default
=
0.45
,
help
=
'
NMS IOU threshold
'
)
parser
.
add_argument
(
'--max-det'
,
type
=
int
,
default
=
1000
,
help
=
'maximum
number of
detections per image'
)
parser
.
add_argument
(
'--max-det'
,
type
=
int
,
default
=
1000
,
help
=
'maximum detections per image'
)
parser
.
add_argument
(
'--device'
,
default
=
''
,
help
=
'cuda device, i.e. 0 or 0,1,2,3 or cpu'
)
parser
.
add_argument
(
'--device'
,
default
=
''
,
help
=
'cuda device, i.e. 0 or 0,1,2,3 or cpu'
)
parser
.
add_argument
(
'--view-img'
,
action
=
'store_true'
,
help
=
'
display
results'
)
parser
.
add_argument
(
'--view-img'
,
action
=
'store_true'
,
help
=
'
show
results'
)
parser
.
add_argument
(
'--save-txt'
,
action
=
'store_true'
,
help
=
'save results to *.txt'
)
parser
.
add_argument
(
'--save-txt'
,
action
=
'store_true'
,
help
=
'save results to *.txt'
)
parser
.
add_argument
(
'--save-conf'
,
action
=
'store_true'
,
help
=
'save confidences in --save-txt labels'
)
parser
.
add_argument
(
'--save-conf'
,
action
=
'store_true'
,
help
=
'save confidences in --save-txt labels'
)
parser
.
add_argument
(
'--save-crop'
,
action
=
'store_true'
,
help
=
'save cropped prediction boxes'
)
parser
.
add_argument
(
'--save-crop'
,
action
=
'store_true'
,
help
=
'save cropped prediction boxes'
)
...
@@ -177,9 +201,4 @@ if __name__ == '__main__':
...
@@ -177,9 +201,4 @@ if __name__ == '__main__':
print
(
opt
)
print
(
opt
)
check_requirements
(
exclude
=
(
'tensorboard'
,
'thop'
))
check_requirements
(
exclude
=
(
'tensorboard'
,
'thop'
))
if
opt
.
update
:
# update all models (to fix SourceChangeWarning)
detect
(
**
vars
(
opt
))
for
opt
.
weights
in
[
'yolov5s.pt'
,
'yolov5m.pt'
,
'yolov5l.pt'
,
'yolov5x.pt'
]:
detect
(
opt
=
opt
)
strip_optimizer
(
opt
.
weights
)
else
:
detect
(
opt
=
opt
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论