可以对数组重新排序吗?

时间:2018-11-09 11:16:35

标签: php arrays google-vision

所以我正在使用Google Vision TEXT_DETECTION,它的基础是-读取车牌,然后使用PHPGD将其覆盖为多边形。现在一切都很好,但是似乎坐标的排列顺序错误,我把头砸在墙上,希望您能帮忙:)

numberplate

在上面的图像中,您可以看到车牌和包围它的多边形。您会看到它应该是2个正方形,但它是一个正方形和一个十字形

这是我的代码,在其中我得到了coordiantes并用它们放置多边形

$response = json_decode($json_response, true);

//dd($response);
$red =0;
$green =0;
$blue = 0;
$i =0;
foreach($response['responses'][0]['fullTextAnnotation']['pages'] as $box) {
    $points = array();
    foreach ($box['blocks'] as $block) {
        foreach ($block['paragraphs'] as $paragraph) {
            foreach ($paragraph['words'] as $word) {
                foreach ($word['boundingBox']['vertices'] as $vertex) {
                    array_push($points, $vertex['x'], $vertex['y']);
                }
                $count_points = count($points) / 2;
                $color = imagecolorallocate($im, round(0), round(0), round(0));
                imagefilledpolygon($im, $points, $count_points, $color);
                var_dump($points);
            }

        }

    }

}

这里是$ points(coordintes)的var_dump

    array(8) { 
[0]=> int(424) 
[1]=> int(224) 
[2]=> int(446) 
[3]=> int(218) 
[4]=> int(451) 
[5]=> int(235) 
[6]=> int(429) 
[7]=> int(241) }
 array(16) { 
[0]=> int(424)
 [1]=> int(224)
 [2]=> int(446)
 [3]=> int(218)
 [4]=> int(451) 
[5]=> int(235) 
[6]=> int(429) 
[7]=> int(241) 
[8]=> int(454) 
[9]=> int(216)
 [10]=> int(472)
 [11]=> int(211)
 [12]=> int(477)
 [13]=> int(228)
 [14]=> int(459)
 [15]=> int(233)
 }

1 个答案:

答案 0 :(得分:0)

我不是Google视觉专家,但我的直觉是您只是滥用视觉API。主要问题是您以某种方式将两个多边形的顶点数据合并为一个-这就是为什么得到array(16)的原因。而且您需要将该数组切成两部分,以获得2个array(8)数组。示例:

$points = 
[
    424,224,
    446,218,
    451,235,
    429,241,
    // cut vertexes in half here to form two polygons
    454,216,
    472,211,
    477,228,
    459,233
];

$image = imagecreatetruecolor(210, 300);

// Colors
$col_1 = imagecolorallocate($image, 0, 0, 255);
$col_2 = imagecolorallocate($image, 255, 0, 0);
$textcolor = imagecolorallocate($image, 0, 255, 0);

// Extract two polygons
$poly_1 = array_slice($points, 0, 8);
$poly_2 = array_slice($points, 8, 8);

// Translate & Scale for better visual affect
translateAndScale($poly_1, -800, -200, 2);
translateAndScale($poly_2, -800, -200, 2);
translateAndScale($points, -820, -350, 2);

// Drawing merged polygon
imagestring($image, 4, 20, 30, 'Two polygons as one :', $textcolor);
imagepolygon($image, $points, 8, $col_2);

// Drawing separated polygons
imagestring($image, 4, 20, 180, 'Separated polygons :', $textcolor);
imagepolygon($image, $poly_1, 4, $col_1);
imagepolygon($image, $poly_2, 4, $col_1);

// output data
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

function translateAndScale(&$points, $tran_x = 0, $tran_y = 0, $scale = 1) {
    $i = 1;
    foreach ($points as $k => $coord) {
        $translate = $i & 1 ? $tran_x : $tran_y;
        $points[$k] = ((int)($coord * $scale) + $translate) ;
        $i++;
    }
}

生成的图片:

enter image description here