Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-104
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
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-104
2022-104
Commits
256ddd8b
Commit
256ddd8b
authored
May 24, 2022
by
G.W.M.N.U.I.B. Giragama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated measure object code
parent
cc85831b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
93 additions
and
0 deletions
+93
-0
measure_object_size_camera1.py
measure_object_size_camera1.py
+93
-0
No files found.
measure_object_size_camera1.py
0 → 100644
View file @
256ddd8b
import
cv2
from
object_detector
import
*
import
numpy
as
np
# Load Aruco detector
parameters
=
cv2
.
aruco
.
DetectorParameters_create
()
aruco_dict
=
cv2
.
aruco
.
Dictionary_get
(
cv2
.
aruco
.
DICT_5X5_50
)
# Load Object Detector
detector
=
HomogeneousBgDetector
()
# Load Cap
cap
=
cv2
.
VideoCapture
(
0
)
cap
.
set
(
cv2
.
CAP_PROP_FRAME_WIDTH
,
1280
)
cap
.
set
(
cv2
.
CAP_PROP_FRAME_HEIGHT
,
720
)
i
=
1
print
(
"Type C for Chille, G for gotukola,T for presentation and L for Ladie fingers"
)
name
=
input
(
"Enter a name: "
)
print
(
name
)
while
i
==
1
:
_
,
img
=
cap
.
read
()
# Get Aruco marker
corners
,
_
,
_
=
cv2
.
aruco
.
detectMarkers
(
img
,
aruco_dict
,
parameters
=
parameters
)
if
corners
:
# Draw polygon around the marker
int_corners
=
np
.
int0
(
corners
)
cv2
.
polylines
(
img
,
int_corners
,
True
,
(
0
,
255
,
0
),
5
)
# Aruco Perimeter
aruco_perimeter
=
cv2
.
arcLength
(
corners
[
0
],
True
)
# Pixel to cm ratio
pixel_cm_ratio
=
aruco_perimeter
/
20
contours
=
detector
.
detect_objects
(
img
)
# Draw objects boundaries
for
cnt
in
contours
:
# Get rect
rect
=
cv2
.
minAreaRect
(
cnt
)
(
x
,
y
),
(
w
,
h
),
angle
=
rect
# Get Width and Height of the Objects by applying the Ratio pixel to cm
object_width
=
w
/
pixel_cm_ratio
object_height
=
h
/
pixel_cm_ratio
# Display rectangle
box
=
cv2
.
boxPoints
(
rect
)
box
=
np
.
int0
(
box
)
cv2
.
circle
(
img
,
(
int
(
x
),
int
(
y
)),
5
,
(
0
,
0
,
255
),
-
1
)
cv2
.
polylines
(
img
,
[
box
],
True
,
(
255
,
0
,
0
),
2
)
cv2
.
putText
(
img
,
"Width {} cm"
.
format
(
round
(
object_width
,
1
)),
(
int
(
x
-
100
),
int
(
y
-
20
)),
cv2
.
FONT_HERSHEY_PLAIN
,
2
,
(
100
,
200
,
0
),
2
)
cv2
.
putText
(
img
,
"Height {} cm"
.
format
(
round
(
object_height
,
1
)),
(
int
(
x
-
100
),
int
(
y
+
15
)),
cv2
.
FONT_HERSHEY_PLAIN
,
2
,
(
100
,
200
,
0
),
2
)
tot
=
object_width
*
object_height
if
name
==
'C'
:
numb
=
int
(
tot
//
(
60
*
45
))
print
(
"Total number of Chille plants: "
+
str
(
numb
))
elif
name
==
'G'
:
numb
=
int
(
tot
//
(
22
*
30
))
print
(
"Total number of Gotu kola plants: "
+
str
(
numb
))
elif
name
==
'L'
:
numb
=
int
(
tot
//
(
75
*
40
))
print
(
"Total number of Ladies finger plants: "
+
str
(
numb
))
elif
name
==
'T'
:
numb
=
int
(
tot
//
(
15
*
20
))
print
(
"Total number of X plants: "
+
str
(
numb
))
i
=
i
+
1
cv2
.
imshow
(
"Image"
,
img
)
key
=
cv2
.
waitKey
(
1
)
if
key
==
27
:
break
#cap.release()
#cv2.destroyAllWindows()
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