'카카오'에 해당되는 글 1건

  1. 2023.01.01 카카오 개발자 REST API 포즈 php

포즈는 사진이나 영상에서 사람이 어떤 포즈를 취하고 있나를 확인해 주는 api 입니다.

참고문서
공식문서 : https://developers.kakao.com/docs/latest/ko/pose/dev-guide    

 

개발용으로 사용할 이미지 이므로, https://www.pexels.com/ko-kr/ 에서 무료 이미지를 하나 다운로드 받았습니다.

 

1. curl을 이용해 이미지 분석 요청.

이미지를 요청하는 curl은 아래와 같습니다.

curl -v -X POST "https://cv-api.kakaobrain.com/pose" \
  -H "Content-Type: multipart/form-data" \
  -H "Authorization: KakaoAK ${REST_API_KEY}" \
  -F "file=@example_pose.jpg"

 

이것을 php로 고치면 아래와 같이 됩니다.

function req_mutipart_post( $KakaoAK, $file_path ){

    $headers = array();
    $headers[] = "Authorization: KakaoAK {$KakaoAK}";
    $headers[] = "Content-Type: multipart/form-data";
    $url = "https://cv-api.kakaobrain.com/pose";

    $file_path = new CURLFile($file_path,'image/jpeg');
    $postvars = array("file"=>$file_path);

    $is_post = true;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, $is_post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $response = curl_exec ($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close ($ch);

    return $response;

}

 

2. 데이터 가공.

php gd를 이용해 이미지를 가공해 봤습니다. 카카오 포즈 기능 생각보다 사용하기 편리한 것 같습니다.

 

3 전체 소스 입니다.

<?php

    // 업로드 모듈
    function req_mutipart_post( $KakaoAK, $file_path ){ // 이미지 업로드용 메소드.

        $headers = array();
        $headers[] = "Authorization: KakaoAK {$KakaoAK}";
        $headers[] = "Content-Type: multipart/form-data";
        $url = "https://cv-api.kakaobrain.com/pose";

        $file_path = new CURLFile($file_path,'image/jpeg');
        $postvars = array("file"=>$file_path);

        $is_post = true;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, $is_post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        $response = curl_exec ($ch);
        $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close ($ch);

        return $response;

    }

    $source_image_file = "pexels-cottonbro-studio-10675908.jpg";

    // 이미지에 대한 정보를 가지고 옵니다.
    $res = req_mutipart_post( '6907940c4e12eb2a6805a74f6bd95d83', $source_image_file );
    $resObj = json_decode($res,true);

    // 사진 속 사람 출력.
    $human = count($resObj);
    echo "사진속 사람 {$human}명" . PHP_EOL;

    // 사진속 모양에 따라 이미지를 가공해 줍니다.
    $gd = imagecreatefromjpeg($source_image_file);
    foreach($resObj as $d){

        // 라인 그리기.
        $im = @imagecreatetruecolor(4, 4);
        $line_color = imagecolorallocate($im, 255, 0, 0);

        imagerectangle(
            $gd,
            $d['bbox'][0], $d['bbox'][1], $d['bbox'][2] + $d['bbox'][0], $d['bbox'][3] + $d['bbox'][1],
            $line_color
        );

        // 점찍기
        $point_weight = @imagecreatetruecolor(8, 8);
        $point_color = imagecolorallocate($point_weight, 0, 255, 0);

        for( $i=0; $i < count($d['keypoints']) ;$i+=3 ){
            $idx_x = round($d['keypoints'][$i + 0]);
            $idx_y = round($d['keypoints'][$i + 1]);
            // print_r([$idx_x, $idx_y]);
            // imagesetpixel($gd, $idx_x, $idx_y, $point_color);
            imagefilledarc(
                $gd, $idx_x, $idx_y, 4,
                4, 0, 360, $point_color,
                IMG_ARC_PIE
            );
        }

    }

    // 가공된 이미지 출력하기.
    imagejpeg($gd, 'result_' . time() . ".jpg");

 

 

전체 소스 파일 입니다.

php - upload.zip
0.91MB

 

 

 

 

 

 

 

Posted by 창업닉군
,