“TensorFlow” A Best ML Packages in PHP/Laravel

Combining TensorFlow, a potent machine learning framework, with Laravel, a popular PHP framework for web development, enables the creation of intelligent web applications. By merging TensorFlow’s AI capabilities with Laravel’s robust architecture, we can build dynamic websites that leverage machine learning for tasks like image recognition, language processing, and predictive analytics.

Laravel is a popular web framework made by Taylor Otwell in 2011. People like it because it’s easy to use, has clear rules, and lots of helpful tools. It makes developers happy because they can work faster and focus on new ideas. With Laravel, you write less code and it’s easier to understand.

It’s also good at keeping your code clean and safe. Plus, it works well with big projects and can handle lots of users. Lots of people use Laravel, and they help each other learn and make it better all the time. Overall, it is a great tool for building websites because it’s simple, safe, and helps developers be creative.

Also read Will React 19 Impact NextJS 14?

TensorFlow

TensorFlow is a smart tool made by Google for super-smart computer stuff called machine learning and deep neural networks. It helps computers learn and understand complicated things like pictures and language. You can do cool stuff like figure out what’s in a picture or predict what might happen next.

It’s like having a really smart assistant for your computer! And if you’re using a web tool called Laravel to build websites with PHP, you can even connect TensorFlow to it. That way, your website can also do smart things like understanding pictures or making predictions. It’s like adding superpowers to your website!

For more information you can visit officile website of TensorFlow

Steps For TensorFlow Object Detection

Follow the following steps to object detection

Step: 1 Install the ONNX Runtime package

composer require ankane/onnxruntime

Step: 2 Download the shared library

composer exec -- php -r "require 'vendor/autoload.php'; OnnxRuntime\Vendor::check();" 

Step: 3 Load the image

$img = imagecreatefromjpeg('bears.jpg');

Step: 4 Get the pixel values

function getPixels($img)
{
    $pixels = [];
    $width = imagesx($img);
    $height = imagesy($img);
    for ($y = 0; $y < $height; $y++) {
        $row = [];
        for ($x = 0; $x < $width; $x++) {
            $rgb = imagecolorat($img, $x, $y);
            $color = imagecolorsforindex($img, $rgb);
            $row[] = [$color['red'], $color['green'], $color['blue']];
        }
        $pixels[] = $row;
    }
    return $pixels;
}

$pixels = getPixels($img);

Step: 5 load the model

Also read Intelligent Assistants With Flutter And ChatGPT

$model = new OnnxRuntime\Model('model.onnx');

Step: 6 Check the model inputs

print_r($model->inputs());

Step: 7 Run the model

$result = $model->predict(['inputs' => [$pixels]]);
print_r($result['num_detections']);
// [3]
print_r($result['detection_classes']);
// [[23, 23, 88, ...]]

Step: 8 Add boxes and labels to the image to see the output better

$coco_labels = [
    23 => 'bear',
    88 => 'teddy bear'
];

function drawBox(&$img, $label, $box)
{
    $width = imagesx($img);
    $height = imagesy($img);

    $top = round($box[0] * $height);
    $left = round($box[1] * $width);
    $bottom = round($box[2] * $height);
    $right = round($box[3] * $width);

    // draw box
    $red = imagecolorallocate($img, 255, 0, 0);
    imagerectangle($img, $left, $top, $right, $bottom, $red);

    // draw text
    $font = '/System/Library/Fonts/HelveticaNeue.ttc';
    imagettftext($img, 16, 0, $left, $top - 5, $red, $font, $label);
}

foreach ($result['num_detections'] as $idx => $n) {
    for ($i = 0; $i < $n; $i++) {
        $label = intval($result['detection_classes'][$idx][$i]);
        $label = $coco_labels[$label] ?? $label;
        $box = $result['detection_boxes'][$idx][$i];
        drawBox($img, $label, $box);
    }
}

// save image
imagejpeg($img, 'labeled.jpg');

Result: The Result is Looks Like:

Result given by TensorFlow
Result given by TensorFlow

TensorFlow, an acclaimed open-source machine learning framework developed by Google. With TensorFlow PHP, you can seamlessly integrate TensorFlow models and execute machine learning tasks within your Laravel application, enhancing its functionality with AI capabilities.

Visit EnlightInfo for more similar articles.

Leave a Reply

Your email address will not be published. Required fields are marked *