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; } }