def get_features(directory): features = [] for fn in iglob('%s/*.png' % directory): image = color.rgb2gray(io.imread(fn)) features.append(get_histogram(image).reshape(-1)) features.append(get_histogram(np.fliplr(image)).reshape(-1)) return features
def get_detections(svm, target): # Detection window size, must be same train image size WIDTH, HEIGHT = (64, 64) CELL_SIZE = 8 THRESHOLD = 3.0 target_scaled = target + 0 scale_factor = 2.0**(-1.0 / 8.0) # Set result list detections = [] for s in range(16): # Get image histogram histogram = get_histogram(target_scaled) # Check detction window with sliding cell size for y in range(0, histogram.shape[0] - int(HEIGHT / CELL_SIZE)): for x in range(0, histogram.shape[1] - int(WIDTH / CELL_SIZE)): # Get feature vector and score feature = histogram[y:(y + int(HEIGHT / CELL_SIZE)), x:(x + int(WIDTH / CELL_SIZE))].reshape( 1, -1) # score = svm.decision_function(feature) score = svm.predict(feature) # if score[0] > THRESHOLD: if score[0] == 1: scale = (scale_factor**s) detections.append({ 'x': x * CELL_SIZE / scale, 'y': y * CELL_SIZE / scale, 'width': WIDTH / scale, 'height': HEIGHT / scale, 'score': score }) target_scaled = transform.rescale(target_scaled, scale_factor) return detections
def get_features(directory): # 空配列を作成 features = [] # 変数 directory の中の PNG 形式の画像ファイル名を要素として取り出す for fn in iglob('%s/*.png' % directory): # グレースケールで画像を変数 image に格納 image = color.rgb2gray(io.imread(fn)) # get_histgram() 関数に画像データを渡し、画像一枚分のヒストグラムを取得し、一次元配列、つまり特徴ベクトルにする # append で、要素として追加、一つの要素が配列なので注意、入れ子配列 features.append(get_histogram(image).reshape(-1)) # import pdb; pdb.set_trace() # 学習データを増やすために fliplr() 関数で画像を反転させて、同じ処理を実施 features.append(get_histogram(np.fliplr(image)).reshape(-1)) # import pdb; pdb.set_trace() return features
import sys WIDTH, HEIGHT = (64, 64) # 検出窓サイズ、学習画像の大きさと同じ CELL_SIZE = 4 THRESHOLD = 3.0 HEIGHT_SIZE = int(HEIGHT / CELL_SIZE) WIDTH_SIZE = int(WIDTH / CELL_SIZE) svm = pickle.load(open(sys.argv[1], 'rb')) target = color.rgb2gray(io.imread(sys.argv[2])) target_scaled = target + 0 scale_factor = 2.0**(-1.0 / 8.0) detections = [] for s in range(16): histogram = get_histogram(target_scaled) for y in range(0, histogram.shape[0] - HEIGHT_SIZE): for x in range(0, histogram.shape[1] - WIDTH_SIZE): feature = histogram[y:y + HEIGHT_SIZE, x:x + WIDTH_SIZE].reshape(-1) score = svm.decision_function(feature) if score[0] > THRESHOLD: # 検出 scale = (scale_factor**s) detections.append({ 'x': x * CELL_SIZE / scale, 'y': y * CELL_SIZE / scale, 'width': WIDTH / scale, 'height': HEIGHT / scale,
import sys WIDTH, HEIGHT = (64, 64) # 検出窓サイズ、学習画像の大きさと同じ CELL_SIZE = 4 THRESHOLD = 3.0 HEIGHT_SIZE = int(HEIGHT / CELL_SIZE) WIDTH_SIZE = int(WIDTH / CELL_SIZE) svm = pickle.load(open(sys.argv[1], 'rb')) target = color.rgb2gray(io.imread(sys.argv[2])) target_scaled = target + 0 scale_factor = 2.0 ** (-1.0 / 8.0) detections = [] for s in range(16): histogram = get_histogram(target_scaled) for y in range(0, histogram.shape[0] - HEIGHT_SIZE): for x in range(0, histogram.shape[1] - WIDTH_SIZE): feature = histogram[ y:y + HEIGHT_SIZE, x:x + WIDTH_SIZE ].reshape(-1) score = svm.decision_function(feature) if score[0] > THRESHOLD: # 検出 scale = (scale_factor ** s) detections.append({ 'x': x * CELL_SIZE / scale, 'y': y * CELL_SIZE / scale,