Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yolov5_detection_code
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sikhin.vc
yolov5_detection_code
Commits
ec8fa9bf
Commit
ec8fa9bf
authored
Jun 26, 2023
by
Sikhin VC
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added auto annotation logic
parent
1f161aed
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
571 additions
and
7 deletions
+571
-7
README.md
README.md
+496
-1
inference.py
inference.py
+75
-6
No files found.
README.md
View file @
ec8fa9bf
This diff is collapsed.
Click to expand it.
inference.py
View file @
ec8fa9bf
...
@@ -14,7 +14,7 @@ from utils.general import (check_img_size, non_max_suppression)
...
@@ -14,7 +14,7 @@ from utils.general import (check_img_size, non_max_suppression)
from
utils.torch_utils
import
select_device
,
smart_inference_mode
from
utils.torch_utils
import
select_device
,
smart_inference_mode
class
ExecuteInference
:
class
ExecuteInference
:
def
__init__
(
self
,
weight
,
confidence
=
0.4
,
img_size
=
640
,
agnostic_nms
=
False
,
gpu
=
False
,
iou
=
0.5
):
def
__init__
(
self
,
weight
,
confidence
=
0.4
,
img_size
=
416
,
agnostic_nms
=
False
,
gpu
=
False
,
iou
=
0.5
):
self
.
weight
=
weight
self
.
weight
=
weight
self
.
confidence
=
confidence
self
.
confidence
=
confidence
self
.
gpu
=
gpu
self
.
gpu
=
gpu
...
@@ -45,6 +45,7 @@ class ExecuteInference:
...
@@ -45,6 +45,7 @@ class ExecuteInference:
return
model
,
names
,
colors
return
model
,
names
,
colors
def
predict
(
self
,
image
):
def
predict
(
self
,
image
):
image2
=
image
img
=
letterbox
(
image
,
new_shape
=
self
.
img_size
)[
0
]
img
=
letterbox
(
image
,
new_shape
=
self
.
img_size
)[
0
]
img
=
img
[:,
:,
::
-
1
]
.
transpose
(
2
,
0
,
1
)
img
=
img
[:,
:,
::
-
1
]
.
transpose
(
2
,
0
,
1
)
img
=
np
.
ascontiguousarray
(
img
)
img
=
np
.
ascontiguousarray
(
img
)
...
@@ -58,14 +59,82 @@ class ExecuteInference:
...
@@ -58,14 +59,82 @@ class ExecuteInference:
_output
=
list
()
_output
=
list
()
for
i
,
det
in
enumerate
(
pred
):
for
i
,
det
in
enumerate
(
pred
):
if
det
is
not
None
and
len
(
det
):
if
det
is
not
None
and
len
(
det
):
det
[:,
:
4
]
=
scale_boxes
(
img
.
shape
[
2
:],
det
[:,
:
4
],
img
.
shape
)
.
round
()
print
(
det
)
det
[:,
:
4
]
=
scale_boxes
(
img
.
shape
[
2
:],
det
[:,
:
4
],
image2
.
shape
)
.
round
()
for
*
xyxy
,
conf
,
cls
in
reversed
(
det
):
for
*
xyxy
,
conf
,
cls
in
reversed
(
det
):
print
(
xyxy
)
_output
.
append
({
"points"
:
xyxy
,
"conf"
:
conf
,
"class"
:
cls
})
_output
.
append
({
"points"
:
xyxy
,
"conf"
:
conf
,
"class"
:
cls
})
return
_output
return
_output
import
os
x
=
ExecuteInference
(
weight
=
"yolov5s.pt"
)
img
=
cv2
.
imread
(
r"C:\Users\sikhin.vc\PycharmProjects\cuda_enabled_repo\custom_image_augmentation\yolov5l_results12\confusion_matrix.png"
)
output
=
x
.
predict
(
img
)
print
(
output
)
def
get_scaled_point
(
iheight
,
iwidth
,
points
):
\ No newline at end of file
x1
,
y1
,
x2
,
y2
=
points
x1
=
x1
/
iwidth
x2
=
x2
/
iwidth
y1
=
y1
/
iheight
y2
=
y2
/
iheight
centerx
=
(
x2
+
x1
)
/
2
centery
=
(
y2
+
y1
)
/
2
widthi
=
y2
-
y1
heighti
=
x2
-
x1
return
centerx
,
centery
,
widthi
,
heighti
yp
=
ExecuteInference
(
weight
=
r"C:\Users\sikhin.vc\PycharmProjects\yolov5_auto_annotation\voc_to_yolo\packer_5.pt"
)
directory
=
r"C:\Users\sikhin.vc\Pictures\Packer5_Merged\Packer5_Merged"
target_dir
=
r"C:\Users\sikhin.vc\Pictures\Packer5_Merged\Packer5_Merged"
# img = cv2.imread(r"C:\Users\sikhin.vc\Documents\loader7_vinod_frames\loader7_vinod_frames\images\Frame_vinod_3768.jpg")
# output = x.predict(img)
# print(output)
for
each_file
in
os
.
listdir
(
directory
):
title
,
ext
=
os
.
path
.
splitext
(
os
.
path
.
basename
(
each_file
))
if
ext
!=
".jpg"
:
continue
print
(
each_file
)
image
=
cv2
.
imread
(
os
.
path
.
join
(
directory
,
each_file
))
image
=
cv2
.
resize
(
image
,
(
416
,
416
))
img
=
image
.
copy
()
predict
=
yp
.
predict
(
image
)
print
(
predict
)
c
=
[]
for
p
in
predict
:
c
.
append
([
int
(
p
[
"points"
][
0
]),
int
(
p
[
"points"
][
1
]),
int
(
p
[
"points"
][
2
]),
int
(
p
[
"points"
][
3
])])
# print(predict)
print
(
c
)
height
,
width
,
_
=
image
.
shape
# cv2.imwrite(os.path.join(target_dir, title + ".jpg"), image)
# for each_pred1 in predict:
# if(each_pred1["class"] in cls_li):
with
open
(
os
.
path
.
join
(
target_dir
,
title
+
".txt"
),
'a'
)
as
f
:
for
each_pred
in
c
:
mrp_label
=
[]
# labels = ["cell phone"]
points
=
[
each_pred
[
0
],
each_pred
[
1
],
each_pred
[
2
],
each_pred
[
3
]]
# img = cv2.rectangle(img, (each_pred[0], each_pred[1]),
# (each_pred[2], each_pred[3]), (255, 0, 0), 2)
# cv2.imshow("out", img)
# cv2.waitKey(0)
cx
,
cy
,
w
,
h
=
get_scaled_point
(
height
,
width
,
each_pred
)
# if each_pred["class"] in cls_li:
f
.
write
(
"0 {} {} {} {}
\n
"
.
format
(
cx
,
cy
,
h
,
w
))
# else:
#
# mrp_label.append([cx, cy, w, h])
# else:
#
# # f.write("0 {} {} {} {}\n".format(cx, cy, h, w))
# for label in mrp_label:
# f.write("1 {} {} {} {}\n".format(label[0], label[1], label[3], label[2]))
cv2
.
imshow
(
"image"
,
img
)
cv2
.
waitKey
(
1
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment