简体   繁体   中英

PHP opencv Parse error: syntax error, unexpected '{',

i downloaded php opencv library but i get the following error;

[08-Dec-2020 19:31:29 Europe/Istanbul] PHP Fatal error: Uncaught Error: Call to undefined function CV\imread() in /home/yusufkar/opencv.yusufkarakaya.com.tr/detect_objects_by_dnn_mobilenet.php:11 Stack trace: #0 {main} thrown in /home/yusufkar/opencv.yusufkarakaya.com.tr/detect_objects_by_dnn_mobilenet.php on line 11

php file;

<?php

use CV\Scalar;
use function CV\imread;
use function CV\cvtColor;

//$categories = explode("\n", file_get_contents('models/ssd_mobilenet_v1_coco/classes.txt'));
$categories = explode("\n", file_get_contents('models/ssdlite_mobilenet_v2_coco/classes.txt'));

$src = imread("images/objects.jpg"); // opencv loads image to matrix with BGR order
//var_export($src);

$blob = \CV\DNN\blobFromImage($src, 0.017, new \CV\Size(300,300), new Scalar(127.5, 127.5, 127.5), true, false); // convert image to 4 dimensions matrix
//var_export($blob);

//$net = \CV\DNN\readNetFromTensorflow('models/ssd_mobilenet_v2_coco/frozen_inference_graph.pb', 'models/ssd_mobilenet_v2_coco/ssd_mobilenet_v2_coco.pbtxt');
//$net = \CV\DNN\readNetFromTensorflow('models/ssd_mobilenet_v1_coco/frozen_inference_graph.pb', 'models/ssd_mobilenet_v1_coco/ssd_mobilenet_v1_coco.pbtxt');
$net = \CV\DNN\readNetFromTensorflow('models/ssdlite_mobilenet_v2_coco/frozen_inference_graph.pb', 'models/ssdlite_mobilenet_v2_coco/ssdlite_mobilenet_v2_coco.pbtxt');
$net->setInput($blob, "");

$r = $net->forward();
var_export($r);

$rectangles = [];
for ($i = 0; $i < $r->shape[2]; $i++) {
    $classId = $r->atIdx([0,0,$i,1]);
    $confidence = intval($r->atIdx([0,0,$i,2]) * 100);
    if ($classId && $confidence > 30) {
        $startX = $r->atIdx([0,0,$i,3]) * $src->cols;
        $startY = $r->atIdx([0,0,$i,4]) * $src->rows;
        $endX = $r->atIdx([0,0,$i,5]) * $src->cols;
        $endY = $r->atIdx([0,0,$i,6]) * $src->rows;

        $scalar = new Scalar(0, 0, 255);
        \CV\rectangle($src, $startX, $startY, $endX, $endY, $scalar, 2);

        $text = "{$categories[$classId]} $confidence%";
        \CV\rectangle($src, $startX, $startY + 10, $startX + 20 * strlen($text), $startY - 30, new Scalar(255,255,255), -2);
        \CV\putText($src, "{$categories[$classId]} $confidence%", new \CV\Point($startX, $startY - 2), 0, 1.5, new Scalar(), 2);
    }
}

\CV\imwrite("results/_detect_objects_by_dnn_mobilenet.png", $src);

Github php opencv: https://github.com/php-opencv/php-opencv-examples

Grouped use declaration is only allowed since PHP 7.0

Make sure you are using at least version 7.0 of PHP, anything less will give you this error caused by use function CV\{imread, cvtColor};

Try splitting the use declaration into multiple.

use function CV\imread;
use function CV\cvtColor;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM