개정판 f3f9d47e
issue #1366: testing symbol using ai, streamer not yet, 90 degree angle only
Change-Id: I496ae7cdf87a4d33fce635b70495ec705c854246
DTI_PID/DTI_PID/MainWindow.py | ||
---|---|---|
3074 | 3074 | |
3075 | 3075 |
return itemList |
3076 | 3076 | |
3077 |
def make_diff_image(self): |
|
3078 |
""" make diff image """ |
|
3079 |
# test |
|
3080 | ||
3081 |
from RecognitionDialog import Worker |
|
3082 |
from symbol import Symbol |
|
3083 |
import math |
|
3084 |
from PIL import Image |
|
3085 | ||
3086 |
app_doc_data = AppDocData.instance() |
|
3087 |
img = app_doc_data.imgSrc.copy() |
|
3088 | ||
3089 |
# check break |
|
3090 |
symbols = [item for item in self.graphicsView.scene().items() if issubclass(type(item), SymbolSvgItem)] |
|
3091 | ||
3092 |
for symbol in symbols: |
|
3093 |
rect = symbol.sceneBoundingRect() |
|
3094 |
sName = symbol.name |
|
3095 |
sType = symbol.type |
|
3096 |
sp = (rect.x(), rect.y()) |
|
3097 |
w, h = rect.width(), rect.height() |
|
3098 |
rotatedAngle = round(math.degrees(symbol.angle)) |
|
3099 |
detectFlip = symbol.flip |
|
3100 | ||
3101 |
dummySym = Symbol(sName, sType, sp, w, h, 0, 0, 0, rotatedAngle, |
|
3102 |
1, 0, 1, 0, |
|
3103 |
','.join(str(x) for x in [0, 0]), |
|
3104 |
'/'.join('{},{},{},{}'.format(param[0], param[1], param[2], param[3]) for param in |
|
3105 |
[]), |
|
3106 |
'dummy', 'dummy', 0, detectFlip=detectFlip, |
|
3107 |
hasInstrumentLabel=0, text_area='') |
|
3108 | ||
3109 |
Worker.remove_detected_symbol_image(dummySym, img, lock=False) |
|
3110 | ||
3111 |
Image.fromarray(img).show() |
|
3077 | 3112 | |
3078 | 3113 |
if __name__ == '__main__': |
3079 | 3114 |
import locale |
DTI_PID/DTI_PID/RecognitionDialog.py | ||
---|---|---|
2100 | 2100 | |
2101 | 2101 |
# get Rotated Original Point |
2102 | 2102 |
sow, soh = symGray.shape[::-1] |
2103 |
offset = max(sow, soh) |
|
2103 | 2104 | |
2104 | 2105 |
roiItem = Worker.remove_small_objects(area.img, 0, sow * soh * 0.5) if hasInstrumentLabel else area.img.copy() |
2105 |
x_offset = round(rect[1]) - round(sow / 2) if round(rect[1]) - round(sow / 2) > 0 else 0
|
|
2106 |
y_offset = round(rect[2]) - round(soh / 2) if round(rect[2]) - round(soh / 2) > 0 else 0
|
|
2107 |
x_max = round(rect[1]) + round(rect[3]) + round(sow / 2) if round(rect[1]) + round(rect[3]) + round(sow / 2) < len(roiItem[0]) else len(roiItem[0]) - 1
|
|
2108 |
y_max = round(rect[2]) + round(rect[4]) + round(soh / 2) if round(rect[2]) + round(rect[4]) + round(soh / 2) < len(roiItem) else len(roiItem) - 1
|
|
2109 |
roiItem = roiItem[y_offset:y_max, x_offset:x_max]
|
|
2106 |
x_start = round(rect[1]) - round(offset) if round(rect[1]) - round(offset) > 0 else 0
|
|
2107 |
y_start = round(rect[2]) - round(offset) if round(rect[2]) - round(offset) > 0 else 0
|
|
2108 |
x_max = round(rect[1]) + round(rect[3]) + round(offset) if round(rect[1]) + round(rect[3]) + round(offset) < len(roiItem[0]) else len(roiItem[0]) - 1
|
|
2109 |
y_max = round(rect[2]) + round(rect[4]) + round(offset) if round(rect[2]) + round(rect[4]) + round(offset) < len(roiItem) else len(roiItem) - 1
|
|
2110 |
roiItem = roiItem[y_start:y_max, x_start:x_max]
|
|
2110 | 2111 | |
2111 | 2112 |
symGrayOri = copy.copy(symGray) |
2112 | 2113 | |
2113 |
searchedInfos = [] # score, x, y, angle, flip |
|
2114 |
searchedInfos = [] # score, x, y, angle, flip, originalPoint, connectionPoint, sw, sh
|
|
2114 | 2115 | |
2115 | 2116 |
steps = [False, True] if detectFlip else [False] |
2116 | 2117 |
for flipped in steps: |
... | ... | |
2134 | 2135 |
#col, row = divmod(maxIndex, colCount) |
2135 | 2136 | |
2136 | 2137 |
if max_val > symbolThreshold * 0.8: |
2137 |
searchedInfos.append([max_val, max_loc[0] + x_offset, max_loc[1] + y_offset, symbolRotatedAngle, flipped])
|
|
2138 |
searchedInfos.append([max_val, max_loc[0] + x_start, max_loc[1] + y_start, symbolRotatedAngle, flipped, originalPoint, connectionPoint, sw, sh])
|
|
2138 | 2139 | |
2139 | 2140 |
symGray = cv2.rotate(symGray, cv2.ROTATE_90_CLOCKWISE) |
2140 | 2141 |
symbolRotatedAngle = (symbolRotatedAngle + 90) % 360 |
... | ... | |
2142 | 2143 |
if searchedInfos: |
2143 | 2144 |
searchedInfos = sorted(searchedInfos, key=lambda param: param[0], reverse=True) |
2144 | 2145 |
searchedInfo = searchedInfos[0] |
2145 |
return ((searchedInfo[1] + area.x, searchedInfo[2] +area.y), searchedInfo[3], searchedInfo[4], originalPoint, connectionPoint, sw, sh) |
|
2146 |
return ((searchedInfo[1] + area.x, searchedInfo[2] + area.y), searchedInfo[3], searchedInfo[4], \ |
|
2147 |
searchedInfo[5], searchedInfo[6], searchedInfo[7], searchedInfo[8]) |
|
2146 | 2148 |
else: |
2147 | 2149 |
return (None, None, None, None, None, None, None) |
2148 | 2150 | |
... | ... | |
2329 | 2331 |
canvas[symbolSp[1]:symbolSp[1] + h, symbolSp[0]:symbolSp[0] + w], symImg) |
2330 | 2332 | |
2331 | 2333 |
@staticmethod |
2332 |
def remove_detected_symbol_image(sym, imgSrc): |
|
2334 |
def remove_detected_symbol_image(sym, imgSrc, lock=True):
|
|
2333 | 2335 |
"""remove detected symbol image from drawing image""" |
2334 |
global threadLock |
|
2336 |
if lock: |
|
2337 |
global threadLock |
|
2335 | 2338 | |
2336 | 2339 |
try: |
2337 | 2340 |
path = sym.getPath() |
2338 |
sp = sym.getSp()
|
|
2339 |
sw = sym.getWidth()
|
|
2340 |
sh = sym.getHeight()
|
|
2341 |
angle = sym.getRotatedAngle()
|
|
2341 |
sp = (int(sym.getSp()[0]), int(sym.getSp()[1]))
|
|
2342 |
sw = int(sym.getWidth())
|
|
2343 |
sh = int(sym.getHeight())
|
|
2344 |
angle = int(sym.getRotatedAngle())
|
|
2342 | 2345 |
# get symbol image |
2343 | 2346 |
sym_img = cv2.imread(path) |
2344 | 2347 |
sym_img = cv2.cvtColor(sym_img, cv2.COLOR_BGR2GRAY) |
... | ... | |
2350 | 2353 |
sym_img = cv2.rotate(sym_img, cv2.ROTATE_90_COUNTERCLOCKWISE) |
2351 | 2354 |
# up to here |
2352 | 2355 | |
2353 |
threadLock.acquire() |
|
2356 |
if lock: |
|
2357 |
threadLock.acquire() |
|
2354 | 2358 |
temp = imgSrc[sp[1]:sp[1] + sh, sp[0]:sp[0] + sw] |
2355 | 2359 |
sym_img = cv2.erode(sym_img, np.ones((5, 5), np.uint8)) |
2356 | 2360 |
mask = cv2.bitwise_or(temp, sym_img) |
... | ... | |
2362 | 2366 |
sys.exc_info()[-1].tb_lineno) |
2363 | 2367 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
2364 | 2368 |
finally: |
2365 |
threadLock.release() |
|
2369 |
if lock: |
|
2370 |
threadLock.release() |
|
2366 | 2371 | |
2367 | 2372 |
''' |
2368 | 2373 |
@brief get difference between given original and recognized image |
내보내기 Unified diff