개정판 61f271c0
thread fix test
Change-Id: Ie0bba3bded76e4c3ac9faf80923b58000d665d5b
DTI_PID/DTI_PID/DetectSymbolDialog.py | ||
---|---|---|
9 | 9 |
import numpy as np |
10 | 10 |
import math |
11 | 11 |
import threading |
12 |
import multiprocessing |
|
13 |
from multiprocessing import Process, Queue |
|
12 |
#import multiprocessing |
|
13 |
#from multiprocessing import Process, Queue |
|
14 |
|
|
15 |
import concurrent.futures as futures |
|
14 | 16 |
|
15 | 17 |
from shapely.geometry import Point |
16 | 18 |
from xml.etree.ElementTree import Element, SubElement, dump, ElementTree, parse |
... | ... | |
21 | 23 |
from AppDocData import AppDocData |
22 | 24 |
import DetectSymbol_UI |
23 | 25 |
|
26 |
THREAD_MAX_WORKER = os.cpu_count() |
|
27 |
PercentGage = 0 |
|
28 |
threadLock = threading.Lock() |
|
29 |
VerticalLineList = [] |
|
30 |
HorizontalLineList = [] |
|
31 |
|
|
24 | 32 |
|
25 | 33 |
class QDetectSymbolDialog(QDialog): |
26 | 34 |
|
... | ... | |
310 | 318 |
''' |
311 | 319 |
|
312 | 320 |
def detectSymbol(self): |
321 |
global VerticalLineList |
|
322 |
global HorizontalLineList |
|
323 |
|
|
313 | 324 |
if not self.ui.listWidgetDrawings.count(): |
314 | 325 |
return |
315 | 326 |
|
... | ... | |
360 | 371 |
areaImg = cv2.bitwise_not(areaImg) |
361 | 372 |
|
362 | 373 |
# find lines |
363 |
verticalLineList = []
|
|
364 |
horizontalLineList = []
|
|
365 |
self.findLines(areaImg, verticalLineList, horizontalLineList) |
|
374 |
VerticalLineList = []
|
|
375 |
HorizontalLineList = []
|
|
376 |
self.findLines(areaImg)#, verticalLineList, horizontalLineList)
|
|
366 | 377 |
|
367 | 378 |
# chage color |
368 |
for vLine in verticalLineList:
|
|
379 |
for vLine in VerticalLineList:
|
|
369 | 380 |
p1 = vLine[0] |
370 | 381 |
p2 = vLine[1] |
371 | 382 |
x = p1[0] |
372 | 383 |
areaImg[p1[1]:p2[1], x] = 255 |
373 | 384 |
# chage color |
374 |
for vLine in horizontalLineList:
|
|
385 |
for vLine in HorizontalLineList:
|
|
375 | 386 |
p1 = vLine[0] |
376 | 387 |
p2 = vLine[1] |
377 | 388 |
y = p1[1] |
... | ... | |
396 | 407 |
@date 2018.10.01 |
397 | 408 |
''' |
398 | 409 |
|
399 |
def findLines(self, areaImg, verticalLineList, horizontalLineList): |
|
410 |
def findLines(self, areaImg):#, verticalLineList, horizontalLineList): |
|
411 |
global PercentGage |
|
412 |
|
|
413 |
vJumpValue = int(areaImg.shape[1] / THREAD_MAX_WORKER) |
|
414 |
hJumpValue = int(areaImg.shape[0] / THREAD_MAX_WORKER) |
|
415 |
PercentGage = int(100 / vJumpValue + hJumpValue) |
|
416 |
|
|
417 |
value = 0 |
|
418 |
values = [] |
|
419 |
ranges = [] |
|
420 |
for cpuIndex in range(THREAD_MAX_WORKER): |
|
421 |
values.append(value) |
|
422 |
ranges.append(value + vJumpValue) if os.cpu_count() - 1 != cpuIndex else ranges.append(areaImg.shape[1]) |
|
423 |
|
|
424 |
with futures.ThreadPoolExecutor(max_workers=THREAD_MAX_WORKER) as pool: |
|
425 |
verticalList = {pool.submit(QDetectSymbolDialog.isVerticalLineThread, values[index], ranges[index], areaImg, self.progress): |
|
426 |
index for index in range(THREAD_MAX_WORKER)} |
|
427 |
futures.wait(verticalList) |
|
400 | 428 |
|
429 |
value = 0 |
|
430 |
values = [] |
|
431 |
ranges = [] |
|
432 |
for cpuIndex in range(THREAD_MAX_WORKER): |
|
433 |
values.append(value) |
|
434 |
ranges.append(value + hJumpValue) if os.cpu_count() - 1 != cpuIndex else ranges.append(areaImg.shape[0]) |
|
435 |
|
|
436 |
with futures.ThreadPoolExecutor(max_workers=THREAD_MAX_WORKER) as pool: |
|
437 |
horizontalList = {pool.submit(QDetectSymbolDialog.isHorizontalLineThread, values[index], ranges[index], areaImg, self.progress): |
|
438 |
index for index in range(THREAD_MAX_WORKER)} |
|
439 |
futures.wait(horizontalList) |
|
440 |
|
|
441 |
''' |
|
401 | 442 |
# for multiprocessing |
402 | 443 |
verticalProcessList = [] |
403 | 444 |
horizontalProcessList = [] |
404 | 445 |
|
405 | 446 |
# Find VerticalLine using multiprocessing |
406 | 447 |
# Set Vertical Line |
407 |
jumpValue = int(areaImg.shape[1] / os.cpu_count())
|
|
448 |
jumpValue = int(areaImg.shape[1] / THREAD_MAX_WORKER)
|
|
408 | 449 |
value = 0 |
409 |
for cpuIndex in range(os.cpu_count()):
|
|
450 |
for cpuIndex in range(THREAD_MAX_WORKER):
|
|
410 | 451 |
_queue = Queue() |
411 | 452 |
_range = value + jumpValue |
412 | 453 |
if os.cpu_count() - 1 == cpuIndex: |
... | ... | |
430 | 471 |
_process.daemon = True |
431 | 472 |
horizontalProcessList.append((_process, _queue)) |
432 | 473 |
value = value + jumpValue |
474 |
''' |
|
433 | 475 |
|
476 |
''' |
|
434 | 477 |
# set percent |
435 | 478 |
progressCount = len(verticalProcessList) + len(horizontalProcessList) + 1 |
436 | 479 |
percentGage = int(100 / progressCount) |
... | ... | |
463 | 506 |
percent = percent + percentGage |
464 | 507 |
self.progress.setValue(percent) |
465 | 508 |
QApplication.processEvents() |
509 |
''' |
|
466 | 510 |
|
467 | 511 |
''' |
468 | 512 |
@brief get contours |
... | ... | |
627 | 671 |
|
628 | 672 |
QDialog.accept(self) |
629 | 673 |
|
674 |
@staticmethod |
|
675 |
def isVerticalLineThread(start, end, img, progress): |
|
676 |
global VerticalLineList |
|
677 |
global threadLock |
|
678 |
global PercentGage |
|
679 |
|
|
680 |
minLineSize = 40 |
|
681 |
|
|
682 |
## Vertical |
|
683 |
find = False |
|
684 |
startY = 0 |
|
685 |
lineList = [] |
|
686 |
for x in range(start, end): |
|
687 |
for y in range(0, img.shape[0]): |
|
688 |
if img[y, x] == 0: |
|
689 |
if find: |
|
690 |
continue |
|
691 |
else: |
|
692 |
find = True |
|
693 |
startY = y |
|
694 |
else: |
|
695 |
if find: |
|
696 |
if y - startY > minLineSize: |
|
697 |
lineList.append(((x, startY), (x, y))) |
|
698 |
find = False |
|
699 |
|
|
700 |
threadLock.acquire() |
|
701 |
VerticalLineList.extend(lineList) |
|
702 |
progress.setValue(progress.value() + PercentGage) |
|
703 |
QApplication.processEvents() |
|
704 |
threadLock.release() |
|
705 |
|
|
706 |
@staticmethod |
|
707 |
def isHorizontalLineThread(start, end, img, progress): |
|
708 |
global HorizontalLineList |
|
709 |
global threadLock |
|
710 |
global PercentGage |
|
711 |
|
|
712 |
minLineSize = 40 |
|
713 |
|
|
714 |
## Horizontal |
|
715 |
find = False |
|
716 |
startX = 0 |
|
717 |
lineList = [] |
|
718 |
for y in range(start, end): |
|
719 |
for x in range(0, img.shape[1]): |
|
720 |
if img[y, x] == 0: |
|
721 |
if find: |
|
722 |
continue |
|
723 |
else: |
|
724 |
find = True |
|
725 |
startX = x |
|
726 |
else: |
|
727 |
if find: |
|
728 |
if x - startX > minLineSize: |
|
729 |
lineList.append(((startX, y), (x, y))) |
|
730 |
|
|
731 |
find = False |
|
732 |
|
|
733 |
threadLock.acquire() |
|
734 |
HorizontalLineList.extend(lineList) |
|
735 |
progress.setValue(progress.value() + PercentGage) |
|
736 |
QApplication.processEvents() |
|
737 |
threadLock.release() |
|
738 |
|
|
630 | 739 |
|
631 | 740 |
''' |
632 | 741 |
@brief Check Vertical Line using Multiprocessing |
633 | 742 |
@author kyouho |
634 | 743 |
@date 2018.09.27 |
635 | 744 |
''' |
636 |
|
|
637 |
|
|
745 |
''' |
|
638 | 746 |
def isVerticalLineThread(start, end, img, _queue): |
639 | 747 |
minLineSize = 40 |
640 | 748 |
|
... | ... | |
656 | 764 |
lineList.append(((x, startY), (x, y))) |
657 | 765 |
find = False |
658 | 766 |
_queue.put(lineList) |
659 |
|
|
767 |
''' |
|
660 | 768 |
|
661 | 769 |
''' |
662 | 770 |
@brief Check Horizontal Line using Multiprocessing |
663 | 771 |
@author kyouho |
664 | 772 |
@date 2018.09.27 |
665 | 773 |
''' |
666 |
|
|
667 |
|
|
774 |
''' |
|
668 | 775 |
def isHorizontalLineThread(start, end, img, _queue): |
669 | 776 |
minLineSize = 40 |
670 | 777 |
|
... | ... | |
687 | 794 |
|
688 | 795 |
find = False |
689 | 796 |
_queue.put(lineList) |
797 |
''' |
내보내기 Unified diff