Created
November 27, 2018 03:29
-
-
Save zoecarver/33a10cee6cf1c043183cb7bf4d4a526a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def randcolorvalue(): | |
return float(randint(0, 255)) / 255 | |
def randcolor(): | |
return randcolorvalue(), randcolorvalue(), randcolorvalue() | |
def draw_boxes(image, boxes): | |
image = np.copy(image) | |
for box in boxes: | |
color = randcolor() | |
x_min, x_max, y_min, y_max = box | |
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 2) | |
return image | |
def sliding_window( | |
image, | |
x_start=0, | |
x_stop=None, | |
y_start=0, | |
y_stop=None, | |
window=None, | |
overlap=None | |
): | |
if x_stop is None: | |
x_stop = image.shape[1] | |
if y_stop is None: | |
y_stop = image.shape[0] | |
x_span = x_stop - x_start | |
y_span = y_stop - y_start | |
x_pixel_per_step = np.int(window[0] * (1 - overlap[0])) | |
y_pixel_per_step = np.int(window[1] * (1 - overlap[1])) | |
x_buffer = np.int(window[0] * overlap[0]) | |
y_buffer = np.int(window[1] * overlap[1]) | |
x_window_count = np.int((x_span - x_buffer) / x_pixel_per_step) | |
y_window_count = np.int((y_span - y_buffer) / y_pixel_per_step) | |
window_list = [] | |
for y_index in range(y_window_count): | |
for x_index in range(x_window_count): | |
tmp_start_x = x_index * x_pixel_per_step + x_start # tmp so it is not confused with `x_start` | |
tmp_end_x = tmp_start_x + window[0] | |
tmp_start_y = y_index * y_pixel_per_step + y_start | |
tmp_end_y = tmp_start_y + window[1] | |
window_list.append((tmp_start_x, tmp_end_x, tmp_start_y, tmp_end_y)) | |
return window_list | |
def predict_windows(image, windows=None, classifier=None): | |
positive_windows = [] | |
for window in windows: | |
x_start, x_stop, y_start, y_stop = window | |
image_selection = image[y_start:y_stop, x_start:x_stop] | |
image_selection = cv2.resize(image_selection, (64, 64)) | |
features = get_features(image_selection) | |
prediction = classifier.predict(features) | |
if prediction == 1.: | |
positive_windows.append(window) | |
return positive_windows |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment