PHP code example of alkauni / planogrid

1. Go to this page and download the library: Download alkauni/planogrid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alkauni / planogrid example snippets


use Alkauni\Planogrid\PlanogramProcessor;
use Alkauni\Planogrid\Strategies\CenterYOverlapStrategy;

// 1. Raw detection data from AWS Rekognition Custom Labels
$customLabels = [
    [
        'Name' => 'Product Alpha 250ml',
        'Confidence' => 98.45,
        'Geometry' => ['BoundingBox' => ['Width' => 0.12, 'Height' => 0.25, 'Left' => 0.10, 'Top' => 0.15]],
    ],
    [
        'Name' => 'Product Beta 500ml',
        'Confidence' => 99.10,
        'Geometry' => ['BoundingBox' => ['Width' => 0.15, 'Height' => 0.25, 'Left' => 0.30, 'Top' => 0.16]],
    ],
];

// 2. Initialize Processor with Chosen Strategy
$processor = new PlanogramProcessor();
$gridResult = $processor
    ->setRowStrategy(new CenterYOverlapStrategy())
    ->process(
        customLabels: $customLabels,
        imageWidth: 1000,   // Image width in pixels
        imageHeight: 1000   // Image height in pixels
    );

// 3. Retrieve Formatted Output
$resultGeometry = $gridResult->getResultGeometry(); // Sorted pixel geometry array [row][col]
$resultBrands   = $gridResult->getResult();         // Matrix of "Brand 1", "Brand 2"
$jsonOutput     = $gridResult->toJson();

use Alkauni\Planogrid\PlanogramProcessor;
use Alkauni\Planogrid\DTO\PlanogramTemplate;
use Alkauni\Planogrid\DTO\PlanogramRow;
use Alkauni\Planogrid\DTO\PlanogramItem;
use Alkauni\Planogrid\Strategies\VerticalIoUStrategy;

// 1. Define Target Planogram Template (Ideal Grid Layout)
$expectedTemplate = new PlanogramTemplate([
    // Row 1 (Top Shelf)
    new PlanogramRow([
        new PlanogramItem('Product Alpha 250ml'),
        new PlanogramItem('Product Beta 500ml'),
    ]),
    // Row 2 (Bottom Shelf)
    new PlanogramRow([
        new PlanogramItem('Product Gamma 1L'),
        new PlanogramItem('Product Delta 100g'),
    ]),
]);

// 2. Load Input Photo Binary
$imageBinary = file_get_contents('shelf_photo.jpg');

// 3. Execute Complete Verification Workflow
$processor = new PlanogramProcessor();
$evaluation = $processor
    ->setRowStrategy(new VerticalIoUStrategy())
    ->setThresholdScore(100.0) // Compliance score threshold (100%)
    ->verify(
        imageBinary: $imageBinary,
        customLabels: $customLabels,
        expectedTemplate: $expectedTemplate,
        imageWidth: 1000,
        imageHeight: 1000
    );

// 4. Inspect Verification Results
echo "Status: " . $evaluation->getStatus(); // "correct" or "incorrect"
echo "Compliance Score: " . $evaluation->getComplianceScore() . "%";
echo "Matched Items: " . $evaluation->getMatchedCount() . " of " . $evaluation->totalExpected;

// 5. Save Annotated Output PNG Image
file_put_contents('output_annotated.png', $evaluation->getAnnotatedImage());

use Alkauni\Planogrid\DTO\ImageAnnotationConfig;

$config = new ImageAnnotationConfig(
    matchColor: '#00d400',          // Green for match
    mismatchColor: '#ff0000',       // Red for mismatch
    lowConfidenceColor: '#ffcc00',  // Yellow for low confidence
    confidenceThreshold: 85.0,      // Items with < 85% confidence get yellow box
    fontPath: '/path/to/Inter.ttf', // Custom TTF Font
    fontSize: 14,
    borderThickness: 2,
    adaptiveFontSize: true,
    showConfidenceText: true
);

$processor->setImageConfig($config);

new ImageAnnotationConfig(
    string $matchColor = '#00d400',         // HEX color for matched items
    string $mismatchColor = '#ff0000',      // HEX color for mismatched/competitor items
    string $lowConfidenceColor = '#ffcc00', // HEX color for low confidence score items
    float $confidenceThreshold = 85.0,     // Confidence score threshold (%)
    ?string $fontPath = null,               // Path to custom TrueType font (.ttf) file
    int $fontSize = 12,                     // Base font size
    int $borderThickness = 2,               // Bounding box border line thickness (px)
    bool $adaptiveFontSize = true,          // Auto font sizing based on product name length
    bool $showConfidenceText = true         // Render confidence score percentage text tag
);

use Alkauni\Planogrid\PlanogramProcessor;

class PlanogramController extends Controller
{
    public function verify(Request $request, PlanogramProcessor $processor)
    {
        $evaluation = $processor->verify(
            imageBinary: $request->file('photo')->get(),
            customLabels: $request->input('custom_labels'),
            expectedTemplate: $expectedTemplate
        );

        return response()->json($evaluation->toArray());
    }
}
bash
php artisan vendor:publish --tag=planogrid-config