Commit 2ae4060e authored by Ivan Tyagov's avatar Ivan Tyagov

Oi sensor

See merge request !55
parents 36059d04 89f5304d
......@@ -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 = (10000, 50000,)
DEFAULT_AREA = (6000, 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("\tDetected 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("\tDetected 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:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment