Created
September 7, 2016 10:52
-
-
Save ashbeats/4b1f56903d014be6e38dc08f884aadb8 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/*** | |
This script runs a local image against the image labeler. | |
~/tensorflow/bazel-bin/tensorflow/examples/label_image/label_image \ | |
--graph=$graph_model --labels=$labels \ | |
--output_layer=final_result \ | |
--image=$image_path | |
Make sure you build 'label_image' first and copy it + the models into this folder. | |
*/ | |
$command = "bazel_builds/label_image/label_image "; | |
$output_errors_flag = " 2>&1"; | |
# run the recognizer. | |
// local copy | |
$graph_model = "bazel_builds/my_models/flowers_output_graph.pb"; | |
$labels = "bazel_builds/my_models/flowers_output_labels.txt"; | |
$image_path = "/vagrant/my_custom_images/test_images/purple-daisy-1234957.jpg"; | |
$command .= "--graph=$graph_model --labels=$labels --output_layer=final_result --image=$image_path"; | |
# permissions need to be handled. add apache's user group | |
exec($command . $output_errors_flag, $output, $return_code); | |
//print_r($output); | |
/* | |
Array | |
( | |
[0] => W tensorflow/core/framework/op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization(). | |
[1] => I tensorflow/examples/label_image/main.cc:204] daisy (4): 0.922651 | |
[2] => I tensorflow/examples/label_image/main.cc:204] sunflowers (3): 0.0645185 | |
[3] => I tensorflow/examples/label_image/main.cc:204] dandelion (2): 0.00621706 | |
[4] => I tensorflow/examples/label_image/main.cc:204] tulips (0): 0.00595134 | |
[5] => I tensorflow/examples/label_image/main.cc:204] roses (1): 0.000662373 | |
) | |
*/ | |
$jsonized_output = []; | |
foreach($output as $line) | |
{ | |
if (preg_match('%label_image/main\.cc:\d+\] (.*?) \(([\d]+)\): ([\d\.]+)$%im', $line, $groups)) { | |
$result = $groups[1]; | |
$jsonized_output['matches'][] = [ | |
"label" => $groups[1], | |
"valx" => (int) $groups[2], | |
"confidence" => (float) $groups[3], | |
]; | |
}else{ | |
$jsonized_output['messages'][] = $line; | |
} | |
} | |
echo json_encode($jsonized_output, JSON_PRETTY_PRINT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment