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
import asyncio
import logging
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):
# any operation
......@@ -26,7 +40,7 @@ async def main():
# setup our server
server = Server()
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
uri = "http://examples.freeopcua.github.io"
......@@ -38,22 +52,23 @@ async def main():
await myvar.set_writable()
# init camera
cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture(camera)
cv2.namedWindow("Trackbars")
cv2.createTrackbar("L-H", "Trackbars", 0, 180, nothing)
cv2.createTrackbar("L-S", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("L-V", "Trackbars", 3, 255, nothing)
cv2.createTrackbar("U-H", "Trackbars", 39, 180, nothing)
cv2.createTrackbar("U-S", "Trackbars", 155, 255, nothing)
cv2.createTrackbar("U-V", "Trackbars", 148, 255, nothing)
cv2.createTrackbar("L-V", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("U-H", "Trackbars", 135, 180, nothing)
cv2.createTrackbar("U-S", "Trackbars", 190, 255, nothing)
cv2.createTrackbar("U-V", "Trackbars", 190, 255, nothing)
font = cv2.FONT_HERSHEY_COMPLEX
_logger.info("Starting server!")
async with server:
while True:
# XXX: find out why we need to sleep (otherwise OPC UA server stops work)
await asyncio.sleep(0.0001)
# script runs concurrently OPC UA server and openCV optical shape
# recognition. Thus give (roughly) some CPU time so both can work together.
await asyncio.sleep(SLEEP_DURATION)
# read and process camera
_, frame = cap.read()
......@@ -82,18 +97,16 @@ async def main():
if area > 400:
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))
await myvar.write_value(1.0)
#_logger.info("Triangle")
elif len(approx) == 4:
elif number_of_points == 4:
cv2.putText(frame, "Rectangle", (x, y), font, 1, (0, 0, 0))
await myvar.write_value(2.0)
#_logger.info("Rectangle")
elif 7 < len(approx) < 20:
cv2.putText(frame, "Circle", (x, y), font, 1, (0, 0, 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)
#_logger.info("Circle")
else:
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