Skip to content

Instantly share code, notes, and snippets.

@ashbeats
Created September 7, 2016 10:52
Show Gist options
  • Save ashbeats/4b1f56903d014be6e38dc08f884aadb8 to your computer and use it in GitHub Desktop.
Save ashbeats/4b1f56903d014be6e38dc08f884aadb8 to your computer and use it in GitHub Desktop.
<?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