123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- namespace App\Services\Vendor\BaiduAi;
- use App\Services\Service;
- class ImageCensor extends Service
- {
- /** @var ApiBase $base */
- protected $base;
- /**
- * @var string
- */
- private $imageCensorCombUrl = 'https://aip.baidubce.com/api/v1/solution/direct/img_censor';
- public function __construct()
- {
- $this->base = new ApiBase(
- config('baidu.face.appid'),
- config('baidu.face.apikey'),
- config('baidu.face.secretKey')
- );
- }
- /**
- * @param $imageUrl
- * @return array
- */
- public function image($imageUrl)
- {
- $suggestion = 1;
- $labels = [];
- // antiporn 色情;terror 暴恐;watermark: 广告检测
- $scenes = ['antiporn', 'terror', 'watermark', 'politician', 'quality'];
- $response = $this->request($imageUrl, $scenes);
- if (isset($response['result'])) {
- $array = $response['result'];
- foreach ($array as $key => $value) {
- switch ($key) {
- case "antiporn":
- if (!isset($value['error_code'])) {
- foreach ($value['result'] as $v) {
- switch ($v['class_name']) {
- case "性感":
- if ($v['probability'] >= 0.7 && $v['probability'] <= 1) {
- $suggestion = 0;
- array_push($labels, "性感");
- }
- break;
- case "色情":
- if ($v['probability'] >= 0.7 && $v['probability'] <= 0.9) {
- $suggestion = 0;
- array_push($labels, "色情");
- } elseif ($v['probability'] >= 0.9) {
- $suggestion = -1;
- array_push($labels, "色情");
- }
- break;
- }
- }
- } else {
- $suggestion = 0;
- }
- break;
- case "terror":
- if (!isset($value['error_code'])) {
- foreach ($value['result_coarse'] as $v) {
- if ($v['name'] == '暴恐' && $v['score'] > 0.7 && $v['score'] < 0.9) {
- $suggestion = 0;
- array_push($labels, "暴恐");
- } elseif ($v['name'] == '暴恐' && $v['score'] >= 0.9 && $v['score'] <= 1) {
- $suggestion = -1;
- array_push($labels, "暴恐");
- }
- }
- foreach ($value['result_fine'] as $v) {
- switch ($v['name']) {
- case "警察部队":
- if ($v['score'] >= 0.7) {
- $suggestion = 0;
- array_push($labels, "暴恐");
- }
- break;
- case "血腥":
- if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
- $suggestion = 0;
- array_push($labels, "血腥");
- } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
- $suggestion = -1;
- array_push($labels, "血腥");
- }
- break;
- case "尸体":
- if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
- $suggestion = 0;
- array_push($labels, "尸体");
- } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
- $suggestion = -1;
- array_push($labels, "尸体");
- }
- break;
- case "杀人":
- if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
- $suggestion = 0;
- array_push($labels, "杀人");
- } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
- $suggestion = -1;
- array_push($labels, "杀人");
- }
- break;
- case "暴乱":
- if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
- $suggestion = 0;
- array_push($labels, "暴乱");
- } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
- $suggestion = -1;
- array_push($labels, "暴乱");
- }
- break;
- case "暴恐人物":
- if ($v['score'] >= 0.7 && $v['score'] < 0.9) {
- $suggestion = 0;
- array_push($labels, "暴恐人物");
- } elseif ($v['score'] >= 0.9 && $v['score'] > 1) {
- $suggestion = -1;
- array_push($labels, "暴恐人物");
- }
- break;
- case "血腥动物或动物尸体":
- case "暴恐旗帜":
- case "军事武器":
- case "车祸":
- case "爆炸火灾":
- break;
- }
- }
- } else {
- $suggestion = 0;
- }
- break;
- case "watermark":
- if (!isset($value['error_code'])) {
- foreach ($value['result'] as $v) {
- switch ($v['type']) {
- case "watermark":
- if ($v['probability'] >= 0.7) {
- $suggestion = 0;
- array_push($labels, "水印");
- }
- break;
- case "QR code":
- if ($v['probability'] >= 0.7) {
- $suggestion = 0;
- array_push($labels, "二维码");
- }
- break;
- case "bar code":
- if ($v['probability'] >= 0.7) {
- $suggestion = 0;
- array_push($labels, "条形码");
- }
- break;
- }
- }
- } else {
- $suggestion = 0;
- }
- break;
- case "politician":
- if (!isset($value['error_code'])) {
- if ($value['include_politician'] == '是' && $value['result_confidence'] == '确定') {
- $suggestion = -1;
- array_push($labels, "政治人物");
- } elseif ($value['include_politician'] == '是' && $value['result_confidence'] == '不确定') {
- $suggestion = 0;
- array_push($labels, "政治人物");
- } elseif ($value['include_politician'] == '否' && $value['result_confidence'] == '不确定') {
- $suggestion = 0;
- array_push($labels, "政治人物");
- }
- } else {
- $suggestion = 0;
- }
- break;
- case "quality":
- if (!isset($value['error_code'])) {
- } else {
- $suggestion = 0;
- }
- break;
- }
- }
- }
- return [
- 'machine_result' => $response,
- 'suggestion' => $suggestion,
- 'labels' => array_unique($labels),
- ];
- }
- /**
- * 请求
- * @param $imageUrl
- * @param array $scenes
- * @return mixed
- */
- public function request($imageUrl, array $scenes)
- {
- $response = $this->base->request(
- $this->imageCensorCombUrl,
- json_encode([
- 'imgUrl' => $imageUrl,
- 'scenes' => $scenes,
- ]),
- [
- 'Content-Type' => 'application/json',
- ]
- );
- return $response;
- }
- }
|