Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
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
1
Merge Requests
1
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
nexedi
osie
Commits
2ae4060e
Commit
2ae4060e
authored
Jun 17, 2024
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Plain Diff
Oi sensor
See merge request
!55
parents
36059d04
89f5304d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
12 deletions
+53
-12
oi-sensor/oi-sensor.py
oi-sensor/oi-sensor.py
+53
-12
No files found.
oi-sensor/oi-sensor.py
View file @
2ae4060e
...
...
@@ -19,6 +19,7 @@ from asyncua import Server
import
argparse
import
time
SLEEP_DURATION
=
10e-3
# 10 milliseconds
# algorith defaults
...
...
@@ -30,7 +31,7 @@ DEFAULT_US = (190, 255,)
DEFAULT_UV
=
(
190
,
255
,)
# the minimal number of pixes which we consider as a shape
# and the maximal (configurable) such
DEFAULT_AREA
=
(
10
000
,
50000
,)
DEFAULT_AREA
=
(
6
000
,
50000
,)
# command line handling
parser
=
argparse
.
ArgumentParser
(
description
=
'Run optical inspection OPC UA server.'
)
...
...
@@ -39,6 +40,8 @@ a('--ipv4', help='The IPv4 address on which the OPC UA server runs', default="0.
a
(
'--port'
,
help
=
'The port on which the OPC UA server runs'
,
default
=
"4840"
)
a
(
'--camera'
,
help
=
'The index of the camera (i.e. indxed in /dev/videoX)'
,
default
=
0
)
a
(
'--headless'
,
help
=
'Run without screen in a headless mode (boolean, default=0)'
,
default
=
False
)
# XXX: allow to specify from CLI DEFAULT_LH & friends
args
=
parser
.
parse_args
()
ipv4
=
args
.
ipv4
port
=
args
.
port
...
...
@@ -65,6 +68,11 @@ async def main():
myvar
=
await
myobj
.
add_variable
(
idx
,
"shape"
,
0.0
)
await
myvar
.
set_writable
()
# Initialize
result
=
0.0
result_stack
=
[]
current_shape
=
0.0
# init camera
cap
=
cv2
.
VideoCapture
(
camera
)
font
=
cv2
.
FONT_HERSHEY_COMPLEX
...
...
@@ -124,26 +132,59 @@ async def main():
now
=
time
.
time
()
*
1000
diff
=
(
now
-
before
)
print
(
"Processing time = %.2f ms, countours = %d"
%
(
diff
,
len
(
contours
)))
contour_detected
=
False
for
cnt
in
contours
:
area
=
cv2
.
contourArea
(
cnt
)
approx
=
cv2
.
approxPolyDP
(
cnt
,
0.02
*
cv2
.
arcLength
(
cnt
,
True
),
True
)
x
=
approx
.
ravel
()[
0
]
y
=
approx
.
ravel
()[
1
]
number_of_points
=
len
(
approx
)
if
area
>
designated_area_min
and
area
<
designated_area_max
:
print
(
"
\
t
Detected area (px)=%.2f"
%
area
)
cv2
.
drawContours
(
frame
,
[
approx
],
0
,
(
0
,
0
,
0
),
5
)
number_of_points
=
len
(
approx
)
contour_detected
=
True
if
not
headless
:
cv2
.
drawContours
(
frame
,
[
approx
],
0
,
(
0
,
0
,
0
),
5
)
if
number_of_points
==
3
:
cv2
.
putText
(
frame
,
"Triangle"
,
(
x
,
y
),
font
,
1
,
(
0
,
0
,
0
))
await
myvar
.
write_value
(
1.0
)
if
not
headless
:
cv2
.
putText
(
frame
,
"Triangle"
,
(
x
,
y
),
font
,
1
,
(
0
,
0
,
0
))
result
=
1.0
elif
number_of_points
==
4
:
cv2
.
putText
(
frame
,
"Rectangle"
,
(
x
,
y
),
font
,
1
,
(
0
,
0
,
0
))
await
myvar
.
write_value
(
2.0
)
if
not
headless
:
cv2
.
putText
(
frame
,
"Rectangle"
,
(
x
,
y
),
font
,
1
,
(
0
,
0
,
0
))
result
=
2.0
elif
7
<
number_of_points
<
20
:
cv2
.
putText
(
frame
,
"Circle (%s)"
%
number_of_points
,
(
x
,
y
),
font
,
1
,
(
0
,
0
,
0
))
await
myvar
.
write_value
(
3.0
)
else
:
await
myvar
.
write_value
(
0.0
)
if
not
headless
:
cv2
.
putText
(
frame
,
"Circle (%s)"
%
number_of_points
,
(
x
,
y
),
font
,
1
,
(
0
,
0
,
0
))
result
=
3.0
# printout
print
(
"
\
t
Detected area (px)=%.2f, result=%d"
%
(
area
,
result
))
# update list for last X results (FILO)
default_occurence_number
=
5
result_stack
.
append
(
result
)
if
len
(
result_stack
)
>
default_occurence_number
:
# leave only last X items
result_stack
=
result_stack
[
-
default_occurence_number
:]
# to avoid sometimes errors in detection algorithm we change OPC UA node
# variable only if at least we have 2 (40%) of last 5 (100%) items detected as this
# shape. Not complex but avoid false results.
count
=
result_stack
.
count
(
result
)
if
count
>=
2
:
# update OPC UA server's node attribute
current_shape
=
result
await
myvar
.
write_value
(
result
)
# break current countour detection loop as in this example we care
# for first detected SHAPE, we do not expect more shapes
break
if
not
contour_detected
:
# no countours actually detected thus update OPC UA server's node attribute
result
=
0.0
result_stack
.
append
(
result
)
current_shape
=
result
await
myvar
.
write_value
(
result
)
# show current MASK and camera output windows
if
not
headless
:
...
...
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