프로젝트

일반

사용자정보

개정판 3c61c95d

ID3c61c95d366e83f9db1f02ee95819afa745da275
상위 16417631
하위 5df0df41

gaqhf 이(가) 4년 이상 전에 추가함

dev issue #663 : add flange logic

Change-Id: I10c32c72383f9dd0d1b56b36015eed95ab28a4c9

차이점 보기:

DTI_PID/DTI_PID/RecognitionDialog.py
58 58
'''
59 59
    @history    2018.05.25  Jeongwoo    Add pyqtSignal(recognizeLine, loadRecognitionResult)
60 60
'''
61

  
61
from enum import Enum
62
class Arrow(Enum):
63
    NULL = 1
64
    DOWN = 2
65
    UP = 3
66
    LEFT = 4
67
    RIGHT = 5
62 68

  
63 69
class Worker(QObject):
64 70
    from PyQt5.QtCore import QThread
......
1245 1251
                        worker.displayLog.emit(MessageType.Error, message)
1246 1252
                    # up to here
1247 1253

  
1248
                    # TODO: detect flange
1254

  
1249 1255
                    # detect flange
1250
                    for sym in searchedSymbolList:
1251
                        worker.detectFlangeOnPid(sym, app_doc_data.activeDrawing.image_origin, worker)
1256
                    try:
1257
                        for sym in symbols:
1258
                            if sym.conn_type:
1259
                                for index in range(len(sym.conn_type)):
1260
                                    item = sym.connectors[index].connectedItem
1261
                                    if item and type(item) is QEngineeringLineItem:
1262
                                        #worker.detectFlangeOnPid(sym, app_doc_data.activeDrawing.image_origin, worker, app_doc_data._imgFilePath, app_doc_data)
1263
                                        worker.detectFlangeOnPid(sym, sym.connectors[index], item, app_doc_data.activeDrawing.image_origin, worker)
1264
                    except Exception as ex:
1265
                        message = f"error occurred({repr(ex)}) in {sys.exc_info()[-1].tb_frame.f_code.co_filename}:" \
1266
                                  f"{sys.exc_info()[-1].tb_lineno}"
1267
                        worker.displayLog.emit(MessageType.Error, message)
1268
                    # up to here
1269

  
1270

  
1252 1271

  
1253 1272
                    # remove line has not connected item
1254 1273
                    try:
......
1831 1850
                                                           sys.exc_info()[-1].tb_lineno)
1832 1851
        return results
1833 1852

  
1853
    # TODO: detect flange
1834 1854
    @staticmethod
1835
    def detectFlangeOnPid(symbol, img, worker):
1836

  
1837
        pt = [float(x) for x in symbol.getSp()]
1838
        connPts = []
1839
        if symbol.getConnectionPoint() is not None and symbol.getConnectionPoint() != '':
1840
            for param in symbol.getConnectionPoint().split('/'):
1841
                tokens = param.split(',')
1842
                connPts.append(
1843
                    ('AUTO', pt[0] + float(tokens[0]), pt[1] + float(tokens[1]), '0') if len(tokens) == 2 else \
1844
                        (tokens[0], pt[0] + float(tokens[1]), pt[1] + float(tokens[2]), '0') if len(
1845
                            tokens) == 3 else \
1846
                            (tokens[0], pt[0] + float(tokens[1]), pt[1] + float(tokens[2]), tokens[3]) if len(
1847
                                tokens) == 4 else None)
1848

  
1849
        for point in connPts:
1850
            # worker.displayLog.emit(MessageType.Error, "eer")
1851
            continue
1855
    def detectFlangeOnPid(symbol, connector, line, image, worker):
1856
        pt = connector.sceneConnectPoint
1857
        x = int(pt[0])
1858
        y = int(pt[1])
1859
        center_x = int(symbol.loc[0] + symbol.size[0] / 2)
1860
        center_y = int(symbol.loc[1] + symbol.size[1] / 2)
1861
        line_center_x = int((line.start_point()[0] + line.end_point()[0]) / 2)
1862
        line_center_y = int((line.start_point()[1] + line.end_point()[1]) / 2)
1863

  
1864
        arrow = Arrow.NULL
1865
        if line.isHorizontal():
1866
            if center_x < line_center_x:
1867
                arrow = Arrow.RIGHT
1868
            else:
1869
                arrow = Arrow.LEFT
1870
        elif line.isVertical():
1871
            if center_y < line_center_y:
1872
                arrow = Arrow.DOWN
1873
            else:
1874
                arrow = Arrow.UP
1852 1875

  
1876
        if arrow is not Arrow.NULL:
1877
            worker.displayLog.emit(MessageType.Error, str(arrow))
1878
            result = Worker.detectFlangeOnPidArrow(x, y, arrow, image, worker)
1879
            if result:
1880
                return [x, y]
1881
                #center_coordinates = (x, y) 
1882
                ## Radius of circle 
1883
                #radius = 8
1884
                ## Blue color in BGR 
1885
                #color = (0, 0, 255) 
1886
                ## Line thickness of 2 px 
1887
                #thickness = 10
1888
                #image = cv2.circle(image, center_coordinates, radius, color, thickness)
1889
                #from PIL import Image
1890
                #Image.fromarray(image).show()
1891
            
1853 1892
        return None
1893

  
1894
    @staticmethod
1895
    def detectFlangeOnPidArrow(start_x, start_y, arrow, image, worker):
1896
        loopRange = []
1897
        if arrow is Arrow.DOWN:
1898
            loopRange = range(start_y - 20, start_y + 40)
1899
        elif arrow is Arrow.UP:
1900
            loopRange = range(start_y + 20, start_y - 40, -1)
1901
        elif arrow is Arrow.LEFT:
1902
            loopRange = range(start_x + 20, start_x - 40, -1)
1903
        elif arrow is Arrow.RIGHT:
1904
            loopRange = range(start_x - 20, start_x + 40)
1905
        else:
1906
            return None
1907

  
1908
        find = False
1909
        # 검은색 점을 찾는 범위
1910
        search_length = 10
1911
        # Line 최소 Length
1912
        checkLineLength = 20;
1913
        # Line 최대 Width
1914
        line_width = 8;
1915
        # flange min length
1916
        flange_min = 30
1917
        # flange max length
1918
        flange_max = 50
1919
        # 임시
1920
        temp_count = 0;
1921
        find_list_x = []
1922
        find_list_y = []
1923
        for i in loopRange:
1924
            loop_find = False
1925
            for j in range(search_length):
1926
                width = 0
1927
                color_1 = 0
1928
                color_2 = 0
1929
                find_x = 0
1930
                find_y = 0
1931

  
1932
                if arrow is Arrow.DOWN or arrow is Arrow.UP:
1933
                    color_1 = image[i, start_x - j]
1934
                    color_2 = image[i, start_x + j]
1935
                    if color_1 is 0:
1936
                        width = Worker.getWidth(start_x - j, i, arrow, image)
1937
                        find_x = start_x - j
1938
                        find_y = i
1939
                    elif color_2 is 0:
1940
                        width = Worker.getWidth(start_x + j, i, arrow, image)
1941
                        find_x = start_x + j
1942
                        find_y = i
1943
                    else:
1944
                        pass
1945
                elif arrow is Arrow.LEFT or arrow is Arrow.RIGHT:
1946
                    color_1 = image[start_y - j, i]
1947
                    color_2 = image[start_y + j, i]
1948
                    if int(color_1) is 0:
1949
                        width = Worker.getWidth(i, start_y - j, arrow, image)
1950
                        find_x = i
1951
                        find_y = start_y - j
1952
                    elif int(color_2) is 0:
1953
                        width = Worker.getWidth(i, start_y + j, arrow, image)
1954
                        find_x = i
1955
                        find_y = start_y + j
1956
                    else:
1957
                        pass
1958
                else:
1959
                    pass
1960
                if width > 0 and width <= line_width and loop_find is False:
1961
                    find = True
1962
                    loop_find = True
1963
                    find_list_x.append(find_x)
1964
                    find_list_y.append(find_y)
1965
                    break
1966

  
1967
            if find and loop_find:
1968
                if temp_count > checkLineLength:
1969
                    break
1970
                temp_count = temp_count + 1
1971
            else:
1972
                find_list_x.clear()
1973
                find_list_y.clear()
1974
                temp_count = 0
1975
                find = False
1976
        
1977
        if find:
1978
            count = 0
1979
            temp_list = []
1980
            find_white = False
1981
            average = 0
1982
            if arrow is Arrow.DOWN or arrow is Arrow.UP:
1983
                average = Worker.getAverage(find_list_x)
1984
            elif arrow is Arrow.LEFT or arrow is Arrow.RIGHT:
1985
                average = Worker.getAverage(find_list_y)
1986
            else:
1987
                pass
1988

  
1989
            count = 0
1990
            for i in range(20):
1991
                width = 0
1992
                if arrow is Arrow.DOWN:
1993
                    width = Worker.getWidth(average, find_list_y[0] - i, arrow, image)
1994
                elif arrow is Arrow.UP:
1995
                    width = Worker.getWidth(average, find_list_y[0] + i, arrow, image)
1996
                elif arrow is Arrow.LEFT:
1997
                    width = Worker.getWidth(find_list_x[0] + i, average, arrow, image)
1998
                elif arrow is Arrow.RIGHT:
1999
                    width = Worker.getWidth(find_list_x[0] - i, average, arrow, image)
2000
                else:
2001
                    pass
2002

  
2003
                worker.displayLog.emit(MessageType.Error, str(width))
2004
                if flange_min < width < flange_max:
2005
                    count = count + 1
2006
                elif width <= 0:
2007
                    find_white = True
2008
                    break
2009
                else:
2010
                    pass
2011
            worker.displayLog.emit(MessageType.Error, str(count))
2012
            if 0 < count < 10 and find_white:
2013
                return True
2014
            else:
2015
                return False
2016

  
2017
        return False
2018

  
2019
    @staticmethod
2020
    def getWidth(x, y, arrow, image):
2021
        width = 0
2022
        move_x1 = 0
2023
        move_y1 = 0
2024
        move_x2 = 0
2025
        move_y2 = 0
2026

  
2027
        if arrow is Arrow.DOWN or arrow is Arrow.UP:
2028
            while True:
2029
                color = image[y, x - move_x1]
2030
                if int(color) is 0:
2031
                    move_x1 = move_x1 + 1
2032
                else:
2033
                    break
2034
            while True:
2035
                color = image[y, x + move_x2]
2036
                if int(color) is 0:
2037
                    move_x2 = move_x2 + 1
2038
                else:
2039
                    break
2040
            width = move_x1 + move_x2 - 1
2041

  
2042
        elif arrow is Arrow.LEFT or arrow is Arrow.RIGHT:
2043
            while True:
2044
                color = image[y - move_y1, x]
2045
                if int(color) is 0:
2046
                    move_y1 = move_y1 + 1
2047
                else:
2048
                    break
2049
            while True:
2050
                color = image[y + move_y2, x]
2051
                if int(color) is 0:
2052
                    move_y2 = move_y2 + 1
2053
                else:
2054
                    break
2055
            width = move_y1 + move_y2 - 1
2056

  
2057
        return width
2058
    @staticmethod
2059
    def getAverage(datas):
2060
        result = 0
2061

  
2062
        for x in datas:
2063
            result = result + x
2064

  
2065
        result = result / len(datas)
2066

  
2067
        return int(result)
2068

  
2069

  
1854 2070
    # Convert into Grayscale image
1855 2071
    @staticmethod
1856 2072
    def cvtGrayImage(img):

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)