Commit 5f45f7e3 authored by Ivan Tyagov's avatar Ivan Tyagov

Oi sensor

See merge request !49
parents 5e739a2e f24d4b8a
...@@ -16,6 +16,20 @@ import numpy as np ...@@ -16,6 +16,20 @@ import numpy as np
import asyncio import asyncio
import logging import logging
from asyncua import Server from asyncua import Server
import argparse
SLEEP_DURATION = 10e-3 # 10 milliseconds
# command line handling
parser = argparse.ArgumentParser(description='Run optical inspection OPC UA server.')
a = parser.add_argument
a('--ipv4', help='The IPv4 address on which the OPC UA server runs', default="0.0.0.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)
args = parser.parse_args()
ipv4 = args.ipv4
port = args.port
camera = int(args.camera)
def nothing(x): def nothing(x):
# any operation # any operation
...@@ -26,7 +40,7 @@ async def main(): ...@@ -26,7 +40,7 @@ async def main():
# setup our server # setup our server
server = Server() server = Server()
await server.init() await server.init()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/") server.set_endpoint("opc.tcp://%s:%s/freeopcua/server/" %(ipv4, port))
# set up our own namespace, not really necessary but should as spec # set up our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io" uri = "http://examples.freeopcua.github.io"
...@@ -38,22 +52,23 @@ async def main(): ...@@ -38,22 +52,23 @@ async def main():
await myvar.set_writable() await myvar.set_writable()
# init camera # init camera
cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(camera)
cv2.namedWindow("Trackbars") cv2.namedWindow("Trackbars")
cv2.createTrackbar("L-H", "Trackbars", 0, 180, nothing) cv2.createTrackbar("L-H", "Trackbars", 0, 180, nothing)
cv2.createTrackbar("L-S", "Trackbars", 0, 255, nothing) cv2.createTrackbar("L-S", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("L-V", "Trackbars", 3, 255, nothing) cv2.createTrackbar("L-V", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("U-H", "Trackbars", 39, 180, nothing) cv2.createTrackbar("U-H", "Trackbars", 135, 180, nothing)
cv2.createTrackbar("U-S", "Trackbars", 155, 255, nothing) cv2.createTrackbar("U-S", "Trackbars", 190, 255, nothing)
cv2.createTrackbar("U-V", "Trackbars", 148, 255, nothing) cv2.createTrackbar("U-V", "Trackbars", 190, 255, nothing)
font = cv2.FONT_HERSHEY_COMPLEX font = cv2.FONT_HERSHEY_COMPLEX
_logger.info("Starting server!") _logger.info("Starting server!")
async with server: async with server:
while True: while True:
# XXX: find out why we need to sleep (otherwise OPC UA server stops work) # script runs concurrently OPC UA server and openCV optical shape
await asyncio.sleep(0.0001) # recognition. Thus give (roughly) some CPU time so both can work together.
await asyncio.sleep(SLEEP_DURATION)
# read and process camera # read and process camera
_, frame = cap.read() _, frame = cap.read()
...@@ -82,18 +97,16 @@ async def main(): ...@@ -82,18 +97,16 @@ async def main():
if area > 400: if area > 400:
cv2.drawContours(frame, [approx], 0, (0, 0, 0), 5) cv2.drawContours(frame, [approx], 0, (0, 0, 0), 5)
if len(approx) == 3: number_of_points = len(approx)
if number_of_points == 3:
cv2.putText(frame, "Triangle", (x, y), font, 1, (0, 0, 0)) cv2.putText(frame, "Triangle", (x, y), font, 1, (0, 0, 0))
await myvar.write_value(1.0) await myvar.write_value(1.0)
#_logger.info("Triangle") elif number_of_points == 4:
elif len(approx) == 4:
cv2.putText(frame, "Rectangle", (x, y), font, 1, (0, 0, 0)) cv2.putText(frame, "Rectangle", (x, y), font, 1, (0, 0, 0))
await myvar.write_value(2.0) await myvar.write_value(2.0)
#_logger.info("Rectangle") elif 7 < number_of_points < 20:
elif 7 < len(approx) < 20: cv2.putText(frame, "Circle (%s)" %number_of_points, (x, y), font, 1, (0, 0, 0))
cv2.putText(frame, "Circle", (x, y), font, 1, (0, 0, 0))
await myvar.write_value(3.0) await myvar.write_value(3.0)
#_logger.info("Circle")
else: else:
await myvar.write_value(0.0) await myvar.write_value(0.0)
......
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