Skip to content

Commit 38584c3

Browse files
committed
2017-10-14
1 parent d492ca9 commit 38584c3

File tree

17 files changed

+585
-60
lines changed

17 files changed

+585
-60
lines changed

CNTK/FasterRCNN/src/.idea/workspace.xml

Lines changed: 402 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CNTK/FasterRCNN/src/FasterRCNN_Detect_SE.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import utils.od_utils as od
1212
from utils.FasterRCNN_eval import FasterRCNN_Evaluator
1313

14+
import re
15+
1416
def get_configuration():
1517
from utils.configs.FasterRCNN_config import cfg as detector_cfg
1618
from utils.configs.VGG16_config import cfg as network_cfg
@@ -21,19 +23,36 @@ def prepare_detect(cfg, use_arg_parser=True):
2123
cfg.MB_SIZE = 1
2224
cfg.NUM_CHANNELS = 3
2325
np.random.seed(seed=cfg.RND_SEED)
26+
27+
def imreadEX(image_path):
28+
if re.compile('[^ ㄱ-ㅣ가-힣]+').sub('', image_path):
29+
stream = open(image_path, "rb")
30+
bytes = bytearray(stream.read())
31+
numpyarray = np.asarray(bytes, dtype=np.uint8)
32+
img = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
33+
else:
34+
img = cv2.imread(image_path)
35+
return img
36+
2437
STANDARD_RIGHTS_REQUIRED = 0x000F0000
2538
SYNCHRONIZE = 0x00100000
2639
MUTANT_QUERY_STATE = 0x0001
2740
MUTEX_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | MUTANT_QUERY_STATE
2841
#
2942
# argv[1]=shmem key
3043
# argv[2]=mutex key
31-
# argv[3]=model path
44+
# argv[3]=shmem size
45+
# argv[4]=model path
46+
# argv[5]=default threshold
3247
#
3348
shmem_key=sys.argv[1]
3449
mutex_key=sys.argv[2]
3550
shmem_size=int(sys.argv[3])
3651
model_path=sys.argv[4]
52+
threshold=float(sys.argv[5])
53+
54+
55+
3756
shm = mmap.mmap(0,shmem_size,shmem_key)
3857
mutex = win32event.OpenMutex(MUTEX_ALL_ACCESS, False, mutex_key)
3958
if not (mutex):
@@ -59,16 +78,17 @@ def prepare_detect(cfg, use_arg_parser=True):
5978
shm.write(bytes(str(1), "ascii"))
6079
win32event.ReleaseMutex(mutex)
6180
#=======================================================================================================================
62-
while(True):
81+
while(True ):
6382
win32event.WaitForSingleObject(mutex,win32event.INFINITE)
6483
shm.seek(0)
6584
run_type = shm.read(1)
6685
if(run_type == b'2'):
67-
img_path = str(shm.read(256),"ascii")
68-
#detect
86+
img_path = shm.read(256).decode('cp949');
87+
img_path = img_path.split('\0', 1)[0]
88+
# detect
6989
regressed_rois, cls_probs = od.predict_single_image(model, img_path, cfg, evaluator)
70-
#fix roi
71-
img = cv2.imread(img_path)
90+
# fix roi
91+
img = imreadEX(img_path)
7292
height, width = img.shape[:2]
7393
scale = 850.0 / max(width, height)
7494
pad = int((max(height, width) - min(height, width)) / 2)
@@ -81,7 +101,7 @@ def prepare_detect(cfg, use_arg_parser=True):
81101
if(m_score<cls_probs[i][j]):
82102
m_score=cls_probs[i][j]
83103
m_class=j
84-
if(m_class!=0 and cls_probs[i][m_class]>0.4):
104+
if(m_class!=0 and cls_probs[i][m_class]>threshold):
85105
regressed_rois[i] /= scale
86106
if (width > height):
87107
regressed_rois[i][1] -= pad
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
import sys
3+
import time
4+
import numpy as np
5+
import cv2
6+
import re
7+
import mmap
8+
import win32event
9+
import win32con
10+
from cntk import load_model
11+
from utils.config_helpers import merge_configs
12+
import utils.od_utils as od
13+
from utils.FasterRCNN_eval import FasterRCNN_Evaluator
14+
15+
def get_configuration():
16+
from utils.configs.FasterRCNN_config import cfg as detector_cfg
17+
from utils.configs.VGG16_config import cfg as network_cfg
18+
from user_config import cfg as dataset_cfg
19+
return merge_configs([detector_cfg, network_cfg, dataset_cfg])
20+
21+
def prepare_detect(cfg, use_arg_parser=True):
22+
cfg.MB_SIZE = 1
23+
cfg.NUM_CHANNELS = 3
24+
np.random.seed(seed=cfg.RND_SEED)
25+
26+
def imreadEX(image_path):
27+
if re.compile('[^ ㄱ-ㅣ가-힣]+').sub('', image_path):
28+
stream = open(image_path, "rb")
29+
bytes = bytearray(stream.read())
30+
numpyarray = np.asarray(bytes, dtype=np.uint8)
31+
img = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
32+
else:
33+
img = cv2.imread(image_path)
34+
return img
35+
36+
STANDARD_RIGHTS_REQUIRED = 0x000F0000
37+
SYNCHRONIZE = 0x00100000
38+
MUTANT_QUERY_STATE = 0x0001
39+
MUTEX_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | MUTANT_QUERY_STATE
40+
41+
cfg=get_configuration()
42+
prepare_detect(cfg, False)
43+
44+
model=load_model('../faster_rcnn_eval_VGG16_e2e.model')
45+
# make evaluator
46+
evaluator = FasterRCNN_Evaluator(model, cfg)
47+
# prepare main image detect. BCZ It is too slow first,second time.
48+
od.predict_dummy_image(model, cfg,evaluator)
49+
od.predict_dummy_image(model, cfg,evaluator)
50+
od.predict_dummy_image(model, cfg,evaluator)
51+
od.predict_dummy_image(model, cfg,evaluator)
52+
od.predict_dummy_image(model, cfg,evaluator)
53+
# model load finished
54+
55+
56+
img_path = '../이미지/0000.jpg'
57+
58+
regressed_rois, cls_probs = od.predict_single_image(model, img_path, cfg, evaluator)
59+
60+
img = imreadEX(img_path)
61+
height, width = img.shape[:2]
62+
scale = 850.0 / max(width, height)
63+
pad = int((max(height, width) - min(height, width)) / 2)
64+
65+
for i in range(0, len(regressed_rois)):
66+
m_class=0
67+
m_score=0
68+
for j in range(0,len(cls_probs[i])):
69+
if(m_score<cls_probs[i][j]):
70+
m_score=cls_probs[i][j]
71+
m_class=j
72+
if(m_class!=0 and cls_probs[i][m_class]>0.4):
73+
regressed_rois[i] /= scale
74+
if (width > height):
75+
regressed_rois[i][1] -= pad
76+
regressed_rois[i][3] -= pad
77+
else:
78+
regressed_rois[i][0] -= pad
79+
regressed_rois[i][2] -= pad
80+
cv2.rectangle(img, (regressed_rois[i][0],regressed_rois[i][1]),(regressed_rois[i][2],regressed_rois[i][3]),(0,0,255))
81+
82+
cv2.imshow('result',img)
83+
cv2.waitKey()
84+
85+
#http://blog.csdn.net/yatere/article/details/7237023
86+
#https://mail.python.org/pipermail/python-list/2005-April/311564.html
87+
#https://stackoverflow.com/questions/15526665/wait-until-a-mutex-is-created-in-python-win32
88+
#https://stackoverflow.com/questions/26114518/ipc-between-python-and-win32-on-windows-os
89+
#=====EXE=====
90+
#http://infocentre.tistory.com/3
-758 Bytes
Binary file not shown.

CNTK/FasterRCNN/src/user_config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
# data set config
1515
__C.DATA.DATASET = "CNTK_FasterRCNN_SpringEdition"
16-
#__C.DATA.MAP_FILE_PATH = "../../TrainDataSet"
16+
#__C.DATA.MAP_FILE_PATH = "../../TrainDataSet" #not used
1717
__C.DATA.CLASS_MAP_FILE = "class_map.txt"
1818
__C.DATA.TRAIN_MAP_FILE = "train_img_file.txt"
1919
__C.DATA.TRAIN_ROI_FILE = "train_roi_file.txt"
20-
#__C.DATA.TEST_MAP_FILE = "test_img_file.txt"
21-
#__C.DATA.TEST_ROI_FILE = "test_roi_file.txt"
22-
#__C.DATA.NUM_TRAIN_IMAGES = 60
23-
#__C.DATA.NUM_TEST_IMAGES = 10
20+
#__C.DATA.TEST_MAP_FILE = "test_img_file.txt" #not used
21+
#__C.DATA.TEST_ROI_FILE = "test_roi_file.txt" #not used
22+
#__C.DATA.NUM_TRAIN_IMAGES = 60 #automatically select
23+
#__C.DATA.NUM_TEST_IMAGES = 10 #not used
2424
__C.DATA.PROPOSAL_LAYER_SCALES = [8,16,32]
2525

2626

299 Bytes
Binary file not shown.
323 Bytes
Binary file not shown.

CNTK/FasterRCNN/src/utils/od_reader.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
import os
1111
from utils.proposal_helpers import ProposalProvider, compute_targets, compute_image_stats
12-
12+
import re
1313
DEBUG = False
1414
if DEBUG:
1515
import matplotlib.pyplot as mp
@@ -147,7 +147,13 @@ def _read_image(self, image_path):
147147
imgnp = np.array(bytearray(imgdata), dtype=np.uint8)
148148
img = cv2.imdecode(imgnp, 1)
149149
else:
150-
img = cv2.imread(image_path)
150+
if re.compile('[^ ㄱ-ㅣ가-힣]+').sub('', image_path):
151+
stream = open(image_path, "rb")
152+
bytes = bytearray(stream.read())
153+
numpyarray = np.asarray(bytes, dtype=np.uint8)
154+
img = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
155+
else:
156+
img = cv2.imread(image_path)
151157

152158
return img
153159

CNTK/FasterRCNN/src/utils/plot_helpers.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from utils.nms_wrapper import apply_nms_to_single_image_results
1818
from utils.rpn.bbox_transform import regress_rois
1919
import cv2 # pip install opencv-python
20+
import re
2021

2122
available_font = "arial.ttf"
2223
try:
@@ -31,8 +32,14 @@ def load_resize_and_pad(image_path, width, height, pad_value=114):
3132
if "@" in image_path:
3233
print("WARNING: zipped image archives are not supported for visualizing results.")
3334
exit(0)
34-
35-
img = cv2.imread(image_path)
35+
if re.compile('[^ ㄱ-ㅣ가-힣]+').sub('', image_path):
36+
stream = open(image_path, "rb")
37+
bytes = bytearray(stream.read())
38+
numpyarray = np.asarray(bytes, dtype=np.uint8)
39+
img = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
40+
else:
41+
img = cv2.imread(image_path)
42+
#img = cv2.imread(image_path)
3643
return resize_and_pad(img, width, height, pad_value)
3744
def load_dummy_image_resize_and_pad(width, height, pad_value=114):
3845
img = np.zeros((64, 64, 3), np.uint8)
Binary file not shown.

0 commit comments

Comments
 (0)