Commit 2075d848 authored by Ivan Tyagov's avatar Ivan Tyagov

Implement some optimization in shape detection.

parent 69a175a4
...@@ -19,6 +19,7 @@ from asyncua import Server ...@@ -19,6 +19,7 @@ from asyncua import Server
import argparse import argparse
import time import time
SLEEP_DURATION = 10e-3 # 10 milliseconds SLEEP_DURATION = 10e-3 # 10 milliseconds
# algorith defaults # algorith defaults
...@@ -39,6 +40,8 @@ a('--ipv4', help='The IPv4 address on which the OPC UA server runs', default="0. ...@@ -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('--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('--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) 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() args = parser.parse_args()
ipv4 = args.ipv4 ipv4 = args.ipv4
port = args.port port = args.port
...@@ -65,6 +68,10 @@ async def main(): ...@@ -65,6 +68,10 @@ async def main():
myvar = await myobj.add_variable(idx, "shape", 0.0) myvar = await myobj.add_variable(idx, "shape", 0.0)
await myvar.set_writable() await myvar.set_writable()
# Initialize
result_stack = []
current_shape = 0.0
# init camera # init camera
cap = cv2.VideoCapture(camera) cap = cv2.VideoCapture(camera)
font = cv2.FONT_HERSHEY_COMPLEX font = cv2.FONT_HERSHEY_COMPLEX
...@@ -149,8 +156,22 @@ async def main(): ...@@ -149,8 +156,22 @@ async def main():
result = 0.0 result = 0.0
# printout # printout
print("\tDetected area (px)=%.2f, result=%d" %(area, result)) 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 40% (2) of last 5 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 # update OPC UA server's node attribute
await myvar.write_value(0.0) current_shape = result
await myvar.write_value(result)
# show current MASK and camera output windows # show current MASK and camera output windows
if not headless: 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