개정판 bacd6a02
dev issue #663 : flange
Change-Id: I7454a1e01d0e3126c6f83140b165a4837aacb3ff
DTI_PID/DTI_PID/RecognitionDialog.py | ||
---|---|---|
1270 | 1270 |
item = sym.connectors[index].connectedItem |
1271 | 1271 |
if item and type(item) is QEngineeringLineItem: |
1272 | 1272 |
#worker.detectFlangeOnPid(sym, app_doc_data.activeDrawing.image_origin, worker, app_doc_data._imgFilePath, app_doc_data) |
1273 |
worker.detectFlangeOnPid(sym, sym.connectors[index], item, app_doc_data.activeDrawing.image_origin, worker) |
|
1273 |
point = worker.detectFlangeOnPid(sym, sym.connectors[index], item, app_doc_data.activeDrawing.image_origin) |
|
1274 |
if point is not None: |
|
1275 |
#center_coordinates = (point[0], point[1]) |
|
1276 |
## Radius of circle |
|
1277 |
#radius = 8 |
|
1278 |
## Blue color in BGR |
|
1279 |
#color = (0, 0, 255) |
|
1280 |
## Line thickness of 2 px |
|
1281 |
#thickness = 10 |
|
1282 |
#image = app_doc_data.activeDrawing.image_origin |
|
1283 |
#image = cv2.circle(image, center_coordinates, radius, color, thickness) |
|
1284 |
#from PIL import Image |
|
1285 |
#Image.fromarray(image).show() |
|
1286 |
pass |
|
1274 | 1287 |
except Exception as ex: |
1275 | 1288 |
message = f"error occurred({repr(ex)}) in {sys.exc_info()[-1].tb_frame.f_code.co_filename}:" \ |
1276 | 1289 |
f"{sys.exc_info()[-1].tb_lineno}" |
... | ... | |
1862 | 1875 |
|
1863 | 1876 |
# TODO: detect flange |
1864 | 1877 |
@staticmethod |
1865 |
def detectFlangeOnPid(symbol, connector, line, image, worker):
|
|
1878 |
def detectFlangeOnPid(symbol, connector, line, image): |
|
1866 | 1879 |
pt = connector.sceneConnectPoint |
1867 | 1880 |
x = int(pt[0]) |
1868 | 1881 |
y = int(pt[1]) |
... | ... | |
1884 | 1897 |
arrow = Arrow.UP |
1885 | 1898 |
|
1886 | 1899 |
if arrow is not Arrow.NULL: |
1887 |
worker.displayLog.emit(MessageType.Error, str(arrow)) |
|
1888 |
result = Worker.detectFlangeOnPidArrow(x, y, arrow, image, worker) |
|
1900 |
result = Worker.detectFlangeOnPidArrow(symbol, x, y, arrow, image) |
|
1889 | 1901 |
if result: |
1890 | 1902 |
return [x, y] |
1891 |
#center_coordinates = (x, y) |
|
1892 |
## Radius of circle |
|
1893 |
#radius = 8 |
|
1894 |
## Blue color in BGR |
|
1895 |
#color = (0, 0, 255) |
|
1896 |
## Line thickness of 2 px |
|
1897 |
#thickness = 10 |
|
1898 |
#image = cv2.circle(image, center_coordinates, radius, color, thickness) |
|
1899 |
#from PIL import Image |
|
1900 |
#Image.fromarray(image).show() |
|
1901 |
|
|
1903 |
|
|
1902 | 1904 |
return None |
1903 | 1905 |
|
1904 | 1906 |
@staticmethod |
1905 |
def detectFlangeOnPidArrow(start_x, start_y, arrow, image, worker):
|
|
1907 |
def detectFlangeOnPidArrow(symbol, start_x, start_y, arrow, image):
|
|
1906 | 1908 |
loopRange = [] |
1907 | 1909 |
if arrow is Arrow.DOWN: |
1908 | 1910 |
loopRange = range(start_y - 20, start_y + 40) |
... | ... | |
1942 | 1944 |
if arrow is Arrow.DOWN or arrow is Arrow.UP: |
1943 | 1945 |
color_1 = image[i, start_x - j] |
1944 | 1946 |
color_2 = image[i, start_x + j] |
1945 |
if color_1 is 0:
|
|
1947 |
if int(color_1) is 0:
|
|
1946 | 1948 |
width = Worker.getWidth(start_x - j, i, arrow, image) |
1947 | 1949 |
find_x = start_x - j |
1948 | 1950 |
find_y = i |
1949 |
elif color_2 is 0:
|
|
1951 |
elif int(color_2) is 0:
|
|
1950 | 1952 |
width = Worker.getWidth(start_x + j, i, arrow, image) |
1951 | 1953 |
find_x = start_x + j |
1952 | 1954 |
find_y = i |
... | ... | |
1996 | 1998 |
else: |
1997 | 1999 |
pass |
1998 | 2000 |
|
1999 |
count = 0 |
|
2001 |
flange_count = 0
|
|
2000 | 2002 |
for i in range(20): |
2001 | 2003 |
width = 0 |
2002 | 2004 |
if arrow is Arrow.DOWN: |
... | ... | |
2009 | 2011 |
width = Worker.getWidth(find_list_x[0] - i, average, arrow, image) |
2010 | 2012 |
else: |
2011 | 2013 |
pass |
2012 |
|
|
2013 |
worker.displayLog.emit(MessageType.Error, str(width)) |
|
2014 |
|
|
2014 | 2015 |
if flange_min < width < flange_max: |
2015 |
count = count + 1
|
|
2016 |
flange_count = flange_count + 1
|
|
2016 | 2017 |
elif width <= 0: |
2017 | 2018 |
find_white = True |
2018 | 2019 |
break |
2019 | 2020 |
else: |
2020 | 2021 |
pass |
2021 |
worker.displayLog.emit(MessageType.Error, str(count)) |
|
2022 |
if 0 < count < 10 and find_white: |
|
2023 |
return True |
|
2022 |
|
|
2023 |
if 0 < flange_count < 10 and find_white: |
|
2024 |
crop_image = None |
|
2025 |
x = find_list_x[0] - (int(symbol.loc[0]) - int(symbol.size[0]) - 1) |
|
2026 |
y = find_list_y[0] - (int(symbol.loc[1]) - int(symbol.size[1]) - 1) |
|
2027 |
crop_image = Worker.removeLine(symbol, find_list_x[0], find_list_y[0], arrow, image) |
|
2028 |
if arrow is Arrow.DOWN: |
|
2029 |
y = y - 1 |
|
2030 |
elif arrow is Arrow.UP: |
|
2031 |
y = y + 1 |
|
2032 |
elif arrow is Arrow.LEFT: |
|
2033 |
x = x + 1 |
|
2034 |
elif arrow is Arrow.RIGHT: |
|
2035 |
x = x - 1 |
|
2036 |
else: |
|
2037 |
return None |
|
2038 |
crop_image = cv2.copyMakeBorder(crop_image, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=255) |
|
2039 |
not_image = cv2.bitwise_not(crop_image) |
|
2040 |
image_contours, image_hierachy = cv2.findContours(not_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
2041 |
for contour in image_contours: |
|
2042 |
c_x, c_y, c_w, c_h = cv2.boundingRect(contour) |
|
2043 |
area = cv2.contourArea(contour) |
|
2044 |
if c_x <= x and c_x + c_w >= x and c_y <= y and c_y + c_h >= y: |
|
2045 |
if flange_count * flange_max >= area: |
|
2046 |
#not_image[y, x] = 0 |
|
2047 |
#from PIL import Image |
|
2048 |
#Image.fromarray(not_image).show() |
|
2049 |
return True |
|
2050 |
|
|
2024 | 2051 |
else: |
2025 | 2052 |
return False |
2026 | 2053 |
|
... | ... | |
2048 | 2075 |
else: |
2049 | 2076 |
break |
2050 | 2077 |
width = move_x1 + move_x2 - 1 |
2051 |
|
|
2052 | 2078 |
elif arrow is Arrow.LEFT or arrow is Arrow.RIGHT: |
2053 | 2079 |
while True: |
2054 | 2080 |
color = image[y - move_y1, x] |
... | ... | |
2065 | 2091 |
width = move_y1 + move_y2 - 1 |
2066 | 2092 |
|
2067 | 2093 |
return width |
2094 |
|
|
2095 |
@staticmethod |
|
2096 |
def removeLine(symbol, x, y, arrow, image): |
|
2097 |
symbol_width = int(symbol.size[0]) |
|
2098 |
symbol_height = int(symbol.size[1]) |
|
2099 |
|
|
2100 |
if arrow is Arrow.DOWN or arrow is Arrow.UP: |
|
2101 |
for i in range(symbol_width): |
|
2102 |
image[y, x - i] = 255 |
|
2103 |
image[y, x + i] = 255 |
|
2104 |
elif arrow is Arrow.LEFT or arrow is Arrow.RIGHT: |
|
2105 |
for i in range(symbol_height): |
|
2106 |
image[y - i, x] = 255 |
|
2107 |
image[y + i, x] = 255 |
|
2108 |
|
|
2109 |
crop_x1 = int(symbol.loc[0]) - symbol_width |
|
2110 |
crop_y1 = int(symbol.loc[1]) - symbol_height |
|
2111 |
crop_x2 = int(symbol.loc[0]) + symbol_width * 2 |
|
2112 |
crop_y2 = int(symbol.loc[1]) + symbol_height * 2 |
|
2113 |
image = image[crop_y1:crop_y2, crop_x1:crop_x2] |
|
2114 |
|
|
2115 |
return image |
|
2116 |
|
|
2068 | 2117 |
@staticmethod |
2069 | 2118 |
def getAverage(datas): |
2070 | 2119 |
result = 0 |
내보내기 Unified diff