文档:
图片审核增强版SDK_内容安全(Content Moderation)-阿里云帮助中心
代码:
import cn.hutool.core.thread.AsyncUtil; import cn.hutool.core.util.RandomUtil; import com.alibaba.nacos.common.utils.JacksonUtils; import com.aliyun.auth.credentials.Credential; import com.aliyun.auth.credentials.provider.StaticCredentialProvider; import com.aliyun.sdk.service.green20220302.AsyncClient; import com.aliyun.sdk.service.green20220302.models.ImageModerationRequest; import com.aliyun.sdk.service.green20220302.models.ImageModerationResponse; import com.aliyun.sdk.service.green20220302.models.ImageModerationResponseBody; import darabonba.core.client.ClientOverrideConfiguration; import lombok.experimental.UtilityClass; import java.util.Map; import java.util.Objects; /** * GreenUtil * * @author achao@apache.org */ @UtilityClass public final class GreenUtil { private static final AliyunGreenProperties aliyunGreenProperties = SpringContextHolder.getBean(AliyunGreenProperties.class); /** * 图片检测 * * @param url 需要检测的图片地址 * @param label 标签 * @return result */ public static ImageModerationResponseBody.Result inspectImg(String url, GreenLabelEnum label) { StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() .accessKeyId(aliyunGreenProperties.getAccessKeyId()) .accessKeySecret(aliyunGreenProperties.getAccessKeySecret()) .build()); ImageModerationResponse resp; try (var client = AsyncClient.builder() .region("cn-beijing") .credentialsProvider(provider) .overrideConfiguration(ClientOverrideConfiguration.create() .setEndpointOverride("green-cip.cn-beijing.aliyuncs.com")) .build()) { var dataId = RandomUtil.randomString(64); var map = Map.of("imageUrl", url, "dataId", dataId); ImageModerationRequest imageModerationRequest = ImageModerationRequest.builder() .service(label.getValue()) .serviceParameters(JacksonUtils.toJson(map)) .build(); var response = client.imageModeration(imageModerationRequest); resp = AsyncUtil.get(response); } var results = resp.getBody().getData().getResult(); if (results.isEmpty()) { return ImageModerationResponseBody.Result.builder().label(GreenliveStreamLabelEnum.NON_LABEL.getLabel()).build(); } return results.get(0); } public static boolean isViolation(ImageModerationResponseBody.Result normalRes) { if (GreenliveStreamLabelEnum.NON_LABEL.getLabel().equals(normalRes.getLabel())) { return false; } var labelEnum = GreenliveStreamLabelEnum.getByLabel(normalRes.getLabel()); return Objects.nonNull(labelEnum) && normalRes.getConfidence() >= labelEnum.getConfidence(); } }
对应俩枚举
import cn.hutool.core.util.EnumUtil; import lombok.AllArgsConstructor; import lombok.Getter; /** * GreenChildLabelEnum * * @author achao@apache.org */ @Getter @AllArgsConstructor public enum GreenliveStreamLabelEnum { NON_LABEL("nonLabel", DEFAULT_CONFIDENCE, "暂无匹配"), PORNOGRAPHIC_ADULTCONTENT("pornographic_adultContent", DEFAULT_CONFIDENCE, "疑似含有成人色情内容"), PORNOGRAPHIC_ADULTTOYS("pornographic_adultToys", DEFAULT_CONFIDENCE, "画面中疑似含有成人器具内容"), PORNOGRAPHIC_ARTWORK("pornographic_artwork", DEFAULT_CONFIDENCE, "画面中疑似含有艺术品色情内容"), PORNOGRAPHIC_ADULTCONTENT_TII("pornographic_adultContent_tii", DEFAULT_CONFIDENCE, "图片中文字疑似色情内容"), SEXUAL_SUGGESTIVECONTENT("sexual_suggestiveContent", DEFAULT_CONFIDENCE, "画面疑似低俗或性暗示内容"), SEXUAL_BREASTBUMP("sexual_breastBump", DEFAULT_CONFIDENCE, "画面疑似含有凸点轮廓内容"), SEXUAL_CLEAVAGE("sexual_cleavage", DEFAULT_CONFIDENCE, "画面疑似含有女性乳沟特征"), SEXUAL_FEMALEUNDERWEAR("sexual_femaleUnderwear", DEFAULT_CONFIDENCE, "画面疑似含有内衣泳衣内容"), SEXUAL_FEMALESHOULDER("sexual_femaleShoulder", DEFAULT_CONFIDENCE, "画面疑似含有肩部性感内容"), SEXUAL_FEMALELEG("sexual_femaleLeg", DEFAULT_CONFIDENCE, "画面疑似含有腿部性感内容"), SEXUAL_MALETOPLESS("sexual_maleTopless", DEFAULT_CONFIDENCE, "画面疑似含有男性赤膊内容"), SEXUAL_CARTOON("sexual_cartoon", DEFAULT_CONFIDENCE, "画面疑似含有卡通性感内容"), SEXUAL_PREGNANCY("sexual_pregnancy", DEFAULT_CONFIDENCE, "画面疑似含有孕照哺乳内容"), SEXUAL_UNDERAGE("sexual_underage", DEFAULT_CONFIDENCE, "画面疑似含有儿童性感内容"), POLITICAL_HISTORICALNIHILITY("political_historicalNihility", DEFAULT_CONFIDENCE, "画面疑似涉及虚无历史或不宜传播的历史事件"), POLITICAL_HISTORICALNIHILITY_TII("political_historicalNihility_tii", DEFAULT_CONFIDENCE, "图中文字疑似历史虚无"), POLITICAL_POLITICALFIGURE_1("political_politicalFigure_1", DEFAULT_CONFIDENCE, "画面疑似含现任历任领导人"), POLITICAL_POLITICALFIGURE_2("political_politicalFigure_2", DEFAULT_CONFIDENCE, "画面疑似涉及领导人家属"), POLITICAL_POLITICALFIGURE_3("political_politicalFigure_3", DEFAULT_CONFIDENCE, "画面疑似涉及省、市政府人员"), POLITICAL_POLITICALFIGURE_4("political_politicalFigure_4", DEFAULT_CONFIDENCE, "画面疑似涉及国外领导人及家属"), POLITICAL_POLITICALFIGURE_NAME_TII("political_politicalFigure_name_tii", DEFAULT_CONFIDENCE, "图中文字含领导人姓名"), POLITICAL_POLITICALFIGURE_METAPHOR_TII("political_politicalFigure_metaphor_tii", DEFAULT_CONFIDENCE, "图中文字疑似含有对主要领导人的代称、暗喻"), POLITICAL_PROHIBITEDPERSON_1("political_prohibitedPerson_1", DEFAULT_CONFIDENCE, "画面疑似含有国家级落马官员"), POLITICAL_PROHIBITEDPERSON_2("political_prohibitedPerson_2", DEFAULT_CONFIDENCE, "画面疑似含有省市级落马官员"), POLITICAL_PROHIBITEDPERSON_TII("political_prohibitedPerson_tii", DEFAULT_CONFIDENCE, " 图中文字含落马官员的姓名 "), POLITICAL_TAINTEDCELEBRITY("political_taintedCelebrity", DEFAULT_CONFIDENCE, "画面疑似包含劣迹或重大负面的公众人物"), POLITICAL_TAINTEDCELEBRITY_TII("political_taintedCelebrity_tii", DEFAULT_CONFIDENCE, "图中文字疑似有劣迹艺人的姓名"), POLITICAL_CHINAFLAG("political_Chinaflag", DEFAULT_CONFIDENCE, "画面疑似含有中国国旗"), POLITICAL_OTHERFLAG("political_otherflag", DEFAULT_CONFIDENCE, "画面疑似含有其他国家国旗"), POLITICAL_CHINAMAP("political_Chinamap", DEFAULT_CONFIDENCE, "画面疑似含有中国地图"), POLITICAL_LOGO("political_logo", DEFAULT_CONFIDENCE, "画面疑似含有禁宣媒体标识"), POLITICAL_OUTFIT("political_outfit", DEFAULT_CONFIDENCE, "画面疑似含有军警服装、作战部队服装"), POLITICAL_BADGE("political_badge", DEFAULT_CONFIDENCE, "画面疑似含有国徽、党徽"), POLITICAL_RACISM_TII("political_racism_tii", DEFAULT_CONFIDENCE, "图中文字疑似含有特殊的表达。更多信息,请参见内容安全控制台"), VIOLENT_EXPLOSION("violent_explosion", DEFAULT_CONFIDENCE, "画面疑似含有烟火类内容元素"), VIOLENT_ARMEDFORCES("violent_armedForces", DEFAULT_CONFIDENCE, "画面疑似包含暴恐组织"), VIOLENT_CROWDING("violent_crowding", DEFAULT_CONFIDENCE, "画面疑似有人群聚集"), VIOLENT_GUN("violent_gun", DEFAULT_CONFIDENCE, "画面疑似包含枪支"), VIOLENT_KNIVES("violent_Knives", DEFAULT_CONFIDENCE, "画面疑似包含刀具"), VIOLENT_GUNKNIVES_TII("violent_gunKnives_tii", DEFAULT_CONFIDENCE, "图中文字含枪支刀具的描述"), VIOLENT_BLOOD("violent_blood", DEFAULT_CONFIDENCE, "画面疑似含有血腥内容"), VIOLENT_HORRIFIC("violent_horrific", DEFAULT_CONFIDENCE, "画面疑似包含惊悚内容"), VIOLENT_HORRIFIC_TII("violent_horrific_tii", DEFAULT_CONFIDENCE, "图中文字疑似描述暴力、恐怖的内容"), VIOLENT_ACU("violent_ACU", DEFAULT_CONFIDENCE, "画面疑似包含作战服"), CONTRABAND_DRUG("contraband_drug", DEFAULT_CONFIDENCE, "画面疑似毒品、药品"), CONTRABAND_DRUG_TII("contraband_drug_tii", DEFAULT_CONFIDENCE, "图中文字疑似描述违禁毒品"), CONTRABAND_GAMBLE("contraband_gamble", DEFAULT_CONFIDENCE, "画面疑似赌博物品"), CONTRABAND_GAMBLE_TII("contraband_gamble_tii", DEFAULT_CONFIDENCE, "图中文字疑似描述赌博行为"), CONTRABAND_CERTIFICATE_TII("contraband_certificate_tii", DEFAULT_CONFIDENCE, "图中文字疑似含办证套现类广告引流"), RELIGION_FUNERAL("religion_funeral", DEFAULT_CONFIDENCE, "画面疑似含有葬礼灵堂内容"), RELIGION_BUDDHISM("religion_buddhism", DEFAULT_CONFIDENCE, "疑似含有特定服饰或标识。更多信息,请参见内容安全控制台"), RELIGION_CHRISTIANITY("religion_christianity", DEFAULT_CONFIDENCE, ""), RELIGION_MUSLIM("religion_muslim", DEFAULT_CONFIDENCE, ""), RELIGION_TII("religion_tii", DEFAULT_CONFIDENCE, ""), RACISM_TII("racism_tii", DEFAULT_CONFIDENCE, ""), PDA_KISS("PDA_kiss", DEFAULT_CONFIDENCE, "画面疑似包含亲吻内容"), PDA_PHYSICALCONTACT("PDA_physicalContact", DEFAULT_CONFIDENCE, "画面疑似包含亲密行为"), OBJECT_LANDMARK("object_landmark", DEFAULT_CONFIDENCE, "画面疑似包含国内地标内容"), OBJECT_RMB("object_rmb", DEFAULT_CONFIDENCE, "画面疑似包含人民币、硬币"), OBJECT_WN("object_wn", DEFAULT_CONFIDENCE, "画面疑似包含特定形象"), PT_LOGOTOSOCIALNETWORK("pt_logotoSocialNetwork", DEFAULT_CONFIDENCE, "画面中含有常见网络社交平台水印"), PT_QRCODE("pt_qrCode", DEFAULT_CONFIDENCE, "图中包含二维码"), PT_PROGRAMCODE("pt_programCode", DEFAULT_CONFIDENCE, "图中包含小程序码"), PT_TODIRECTCONTACT_TII("pt_toDirectContact_tii", DEFAULT_CONFIDENCE, "图中文字疑似含有特定引流信息。更多信息,请参见内容安全控制台"), PT_TOSOCIALNETWORK_TII("pt_toSocialNetwork_tii", DEFAULT_CONFIDENCE, ""), PT_TOSHORTVIDEOS_TII("pt_toShortVideos_tii", DEFAULT_CONFIDENCE, ""), PT_INVESTMENT_TII("pt_investment_tii", DEFAULT_CONFIDENCE, ""), PT_RECRUITMENT_TII("pt_recruitment_tii", DEFAULT_CONFIDENCE, ""), PT_CERTIFICATE_TII("pt_certificate_tii", DEFAULT_CONFIDENCE, ""), INAPPROPRIATE_SMOKING("inappropriate_smoking", DEFAULT_CONFIDENCE, "画面疑似包含烟相关内容"), INAPPROPRIATE_DRINKING("inappropriate_drinking", DEFAULT_CONFIDENCE, "画面疑似包含酒相关内容"), INAPPROPRIATE_TATTOO("inappropriate_tattoo", DEFAULT_CONFIDENCE, "画面疑似包含纹身内容"), INAPPROPRIATE_MIDDLEFINGER("inappropriate_middleFinger", DEFAULT_CONFIDENCE, "画面疑似包含竖中指内容"), INAPPROPRIATE_FOODWASTING("inappropriate_foodWasting", DEFAULT_CONFIDENCE, "画面疑似包含浪费粮食内容"), QUALITY_MEANINGLESS("quality_meaningless", DEFAULT_CONFIDENCE, "图片元素过少"), ; private final String label; private final float confidence; private final String desc; public static GreenliveStreamLabelEnum getByLabel(String label) { return EnumUtil.getBy(GreenliveStreamLabelEnum::getLabel, label); } }
以及
import lombok.AllArgsConstructor; import lombok.Getter; /** * GreenLabelEnum * * @author achao@apache.org */ @Getter @AllArgsConstructor public enum GreenLabelEnum { BASELINE_CHECK("baselineCheck", "通用基线检测"), BASELINE_CHECK_PRO("baselineCheck_pro", "通用基线检测_专业版"), BASELINE_CHECK_CB("baselineCheck_cb", "通用基线检测_海外版"), TONALITY_IMPROVE("tonalityImprove", "内容治理检测"), AIGC_CHECK("aigcCheck", "AIGC图片检测"), PROFILE_PHOTO_CHECK("profilePhotoCheck", "头像图片检测"), ADVERTISING_CHECK("advertisingCheck", "营销素材检测"), LIVE_STREAM_CHECK("liveStreamCheck", "视频/直播截图检测"), ; private final String value; private final String desc; }
使用:
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; /** * GreenTest * * @author achao@apache.org */ @SpringBootTest class GreenUtilTest { @Test void inspectImgNotViolationTest() { var normalUrl = "xxx"; var normalRes = GreenUtil.inspectImg(normalUrl, GreenLabelEnum.LIVE_STREAM_CHECK); Assertions.assertFalse(GreenUtil.isViolation(normalRes)); } @Test void inspectImgViolationTest() { var yellowUrl = "xxx"; var yellowRes = GreenUtil.inspectImg(yellowUrl, GreenLabelEnum.LIVE_STREAM_CHECK); Assertions.assertTrue(GreenUtil.isViolation(yellowRes)); Assertions.assertEquals(GreenliveStreamLabelEnum.PORNOGRAPHIC_ADULTCONTENT, GreenliveStreamLabelEnum.getByLabel(yellowRes.getLabel())); } }