def test_network(model, classifier, test_x, test_y): for (image, label) in zip(test_x, test_y): prediction = classifier.predict(model, image) best_pred = classifier_util.get_best_prediction(prediction)[0] label_num = classifier_util.get_best_prediction(label.reshape((1, -1)))[0] print(f"Label: {classifier.LABELS[label_num]} - Prediction: {classifier.LABELS[best_pred]}") cv2.imshow("Test", image.reshape(image.shape[1:] + (3,))) key = cv2.waitKey(0) if key == ord('q'): return cv2.destroyWindow("Test")
def get_symbols(img, model): masks, coords = get_characters(img) masks = np.array([dataset_util.reshape(mask, config.SYMBOLS_INPUT_DIM[1:]) for mask in masks]) prediction = classifier.predict(model, masks) best_pred = classifier_util.get_best_prediction(prediction) log(f"Symbols: {[classifier.LABELS[x] for x in best_pred]}", LOG_DEBUG, "Symbols") return best_pred, coords
def get_labels(img, model): masks, coords = get_characters(img) masks = array([ dataset_util.reshape(mask, config.CHAR_INPUT_DIM[1:]) for mask in masks ]) prediction = classifier.predict(model, masks) return [x for x in classifier_util.get_best_prediction(prediction)], coords
def get_indicator_features(image, model): masks, lit = get_characters(image) masks = [ dataset_util.reshape(mask, config.CHAR_INPUT_DIM[1:]) for mask in masks ] prediction = classifier.predict(model, np.array(masks)) best_pred = classifier_util.get_best_prediction(prediction) return lit, format_label([classifier.LABELS[p] for p in best_pred])
def get_password(img, model): masks = get_characters(img) masks = array([ dataset_util.reshape(mask, config.CHAR_INPUT_DIM[1:]) for mask in masks ]) prediction = classifier.predict(model, masks) best_pred = classifier_util.get_best_prediction(prediction) characters = [classifier.LABELS[x] for x in best_pred] return "".join(characters)
def get_word(prediction): characters = [ classifier.LABELS[x] for x in classifier_util.get_best_prediction(prediction) ] if characters[:4] == ["w", "h", "a", "t"] and len(characters) == 5: characters[ 4] = "?" # Handle special case with question-mark after 'what'. return "".join(characters)
def identify_side_features(sides, model): flattened = [] for side in sides: for img in side: converted = dataset_util.reshape(convert_to_cv2(img), config.MODULE_INPUT_DIM[1:]) flattened.append(converted) preds = module_classifier.predict(model, array(flattened)) predictions = classifier_util.get_best_prediction(preds) return predictions
def get_serial_number(img, model): masks, alignment = get_characters(img) if not alignment: log("ERROR: Could not determine alignment of serial number", LOG_WARNING, "Serial Number") return None masks = np.array([ dataset_util.reshape(mask, config.CHAR_INPUT_DIM[1:]) for mask in masks ]) prediction = classifier.predict(model, masks) best_pred = classifier_util.get_best_prediction(prediction) return create_serial_string(best_pred, alignment) # Return actual string.
def get_details_async(model, holder): duration_masks = get_duration_characters() if len(duration_masks) != 3: log(f"WARNING: Bomb duration string length != 3 (len={len(get_duration_characters)}).", config.LOG_WARNING) module_masks = get_module_characters() masks = duration_masks + module_masks masks = np.array([dataset_util.reshape(mask, config.CHAR_INPUT_DIM[1:]) for mask in masks]) prediction = classifier.predict(model, masks) best_pred = classifier_util.get_best_prediction(prediction) labels = [classifier.LABELS[p] for p in best_pred] holder.append(format_time(labels[:3])) holder.append(fix_number(labels[3:5]))
def get_button_features(image, model): masks, color = get_characters(image) if len(masks) == 4: return "Hold", color elif len(masks) == 8: return "Detonate", color else: masks = np.array( [dataset_util.reshape(masks[0], config.CHAR_INPUT_DIM[1:])]) prediction = classifier.predict(model, masks[0]) best_pred = classifier_util.get_best_prediction(prediction) if classifier.LABELS[best_pred[0]] == "a": return "Abort", color return "Press", color
def process_bomb_data(images): IMAGES_CAPTURED = 0 predictions = [] for img in images: cv2_img = convert_to_cv2(img) path = f"../resources/training_images/" if AUTO_LABEL: resized = dataset_util.reshape(cv2_img, config.MODULE_INPUT_DIM[1:]) pred = classifier.predict(MODEL, resized) label = classifier_util.get_best_prediction(pred)[0] predictions.append(label) if label in INCLUDED_LABELS: if label < 7: label_name = clean_file_path(classifier.LABELS[label]) path = f"{path}{label_name}/" if DATA_TYPE != "modules": num_images = len(glob(path + "*.png")) imwrite(f"{path}{num_images:03d}.png", cv2_img) IMAGES_CAPTURED += 1 log(f"Captured {IMAGES_CAPTURED} bomb images.") return predictions