Commit 7d08c150 authored by Ivan Tyagov's avatar Ivan Tyagov

Allow to sort on minimal and maximal contour pixel size.

More debugging information.
parent cce5355f
...@@ -17,6 +17,7 @@ import asyncio ...@@ -17,6 +17,7 @@ import asyncio
import logging import logging
from asyncua import Server from asyncua import Server
import argparse import argparse
import time
SLEEP_DURATION = 10e-3 # 10 milliseconds SLEEP_DURATION = 10e-3 # 10 milliseconds
...@@ -24,10 +25,12 @@ SLEEP_DURATION = 10e-3 # 10 milliseconds ...@@ -24,10 +25,12 @@ SLEEP_DURATION = 10e-3 # 10 milliseconds
DEFAULT_LH = (0, 180,) DEFAULT_LH = (0, 180,)
DEFAULT_LS = (0, 255,) DEFAULT_LS = (0, 255,)
DEFAULT_LV = (0, 255,) DEFAULT_LV = (0, 255,)
DEFAULT_UH = (135, 180,) DEFAULT_UH = (180, 180,)
DEFAULT_US = (190, 255,) DEFAULT_US = (190, 255,)
DEFAULT_UV = (190, 255,) DEFAULT_UV = (190, 255,)
DEFAULT_AREA = (400, 1000,) # the minimal number of pixes which we consider as a shape
# and the maximal (configurable) such
DEFAULT_AREA = (10000, 50000,)
# command line handling # command line handling
parser = argparse.ArgumentParser(description='Run optical inspection OPC UA server.') parser = argparse.ArgumentParser(description='Run optical inspection OPC UA server.')
...@@ -74,7 +77,8 @@ async def main(): ...@@ -74,7 +77,8 @@ async def main():
cv2.createTrackbar("U-H", "Trackbars", DEFAULT_UH[0], DEFAULT_UH[1], nothing) cv2.createTrackbar("U-H", "Trackbars", DEFAULT_UH[0], DEFAULT_UH[1], nothing)
cv2.createTrackbar("U-S", "Trackbars", DEFAULT_US[0], DEFAULT_US[1], nothing) cv2.createTrackbar("U-S", "Trackbars", DEFAULT_US[0], DEFAULT_US[1], nothing)
cv2.createTrackbar("U-V", "Trackbars", DEFAULT_UV[0], DEFAULT_UV[1], nothing) cv2.createTrackbar("U-V", "Trackbars", DEFAULT_UV[0], DEFAULT_UV[1], nothing)
cv2.createTrackbar("Area", "Trackbars", DEFAULT_AREA[0], DEFAULT_AREA[1], nothing) cv2.createTrackbar("Area (min)", "Trackbars", DEFAULT_AREA[0], DEFAULT_AREA[1], nothing)
cv2.createTrackbar("Area (max)", "Trackbars", DEFAULT_AREA[1], DEFAULT_AREA[1], nothing)
_logger.info("Starting server!") _logger.info("Starting server!")
async with server: async with server:
...@@ -84,6 +88,7 @@ async def main(): ...@@ -84,6 +88,7 @@ async def main():
# recognition. Thus give (roughly) some CPU time so both can work together. # recognition. Thus give (roughly) some CPU time so both can work together.
await asyncio.sleep(SLEEP_DURATION) await asyncio.sleep(SLEEP_DURATION)
before = time.time() * 1000
# read and process camera # read and process camera
_, frame = cap.read() _, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
...@@ -94,7 +99,8 @@ async def main(): ...@@ -94,7 +99,8 @@ async def main():
u_h = cv2.getTrackbarPos("U-H", "Trackbars") u_h = cv2.getTrackbarPos("U-H", "Trackbars")
u_s = cv2.getTrackbarPos("U-S", "Trackbars") u_s = cv2.getTrackbarPos("U-S", "Trackbars")
u_v = cv2.getTrackbarPos("U-V", "Trackbars") u_v = cv2.getTrackbarPos("U-V", "Trackbars")
designated_area = cv2.getTrackbarPos("Area", "Trackbars") designated_area_min = cv2.getTrackbarPos("Area (min)", "Trackbars")
designated_area_max = cv2.getTrackbarPos("Area (max)", "Trackbars")
else: else:
# read defaults provided # read defaults provided
l_h = DEFAULT_LH[0] l_h = DEFAULT_LH[0]
...@@ -103,7 +109,8 @@ async def main(): ...@@ -103,7 +109,8 @@ async def main():
u_h = DEFAULT_UH[0] u_h = DEFAULT_UH[0]
u_s = DEFAULT_US[0] u_s = DEFAULT_US[0]
u_v = DEFAULT_UV[0] u_v = DEFAULT_UV[0]
designated_area = DEFAULT_AREA[0] designated_area_min = DEFAULT_AREA[0]
designated_area_max = DEFAULT_AREA[1]
lower_red = np.array([l_h, l_s, l_v]) lower_red = np.array([l_h, l_s, l_v])
upper_red = np.array([u_h, u_s, u_v]) upper_red = np.array([u_h, u_s, u_v])
...@@ -114,14 +121,16 @@ async def main(): ...@@ -114,14 +121,16 @@ async def main():
# Contours detection # Contours detection
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
now = time.time() * 1000
diff = (now-before)
print("Processing time = %.2f ms, countours = %d" %(diff, len(contours)))
for cnt in contours: for cnt in contours:
area = cv2.contourArea(cnt) area = cv2.contourArea(cnt)
approx = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt, True), True) approx = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt, True), True)
x = approx.ravel()[0] x = approx.ravel()[0]
y = approx.ravel()[1] y = approx.ravel()[1]
if area > designated_area_min and area < designated_area_max:
if area > designated_area: print("\tDetected area (px)=%.2f" %area)
cv2.drawContours(frame, [approx], 0, (0, 0, 0), 5) cv2.drawContours(frame, [approx], 0, (0, 0, 0), 5)
number_of_points = len(approx) number_of_points = len(approx)
if number_of_points == 3: if number_of_points == 3:
......
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