新版阿里云内容安全对接

本文涉及的产品
内容审核增强版开发者实践包,10万次资源包1年有效
简介: 新版阿里云内容安全对接

文档:

图片审核增强版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()));
    }
}
相关文章
|
6月前
|
Cloud Native 数据库 对象存储
《阿里云产品四月刊》—内容安全 新功能
阿里云瑶池数据库云原生化和一体化产品能力升级,多款产品更新迭代
|
5月前
|
存储 对象存储
|
Serverless 对象存储
用阿里云内容安全处理OSS的违禁图片
原文档,https://help.aliyun.com/document_detail/2400149.html 基于官方的文档做的一些拓展
726 1
|
人工智能
阿里云产品体系分为6大分类——人工智能——分为10种模块——内容安全
阿里云产品体系分为6大分类——人工智能——分为10种模块——内容安全自制脑图
214 1
在线实验 | 阿里云AIGC内容安全,守护每一份想象力
在线实验 | 阿里云AIGC内容安全,守护每一份想象力
180 0
|
机器学习/深度学习 自然语言处理 安全
《阿里云云通信短信服务安全白皮书》——安全架构——七、 内容安全
《阿里云云通信短信服务安全白皮书》——安全架构——七、 内容安全
235 0
|
云安全 人工智能 小程序
阿里云内容安全简介和购买流程
如大家所知,如果你要上线一款app或者小程序或者网站等等。你的项目涉及到用户上传信息,诸如文本,图片,视频,音频。但是你要那这些信息要合法合规,怎么办?如果用户少,你可以通过人工审核。但是如果每天有几万条,几百万条,甚至几千万条这样的信息要上传,这个时候怎么办?所以需要用到机器AI审核。而阿里云内容安全就起到这样的作用。
阿里云内容安全简介和购买流程
|
安全 Java 开发工具
阿里云内容安全java SDK实现本土图片审核
内容安全是一款多媒体内容智能识别服务,支持对图片、视频、文本、语音等对象进行多样化场景检测,帮助您有效降低内容违规风险。 此篇文章将简单介绍通过javasdk实现本地图片的安全检测
901 0
阿里云内容安全java SDK实现本土图片审核
|
机器学习/深度学习 人工智能 文字识别
当我们谈深度学习时,我们用它落地了什么?阿里云内容安全功能全新升级
近日,阿里云在深度学习方面动作频频,先后发布了OCR证件识别,声纹检测,人脸搜索,视频鉴黄服务以及相似图片搜索功能,下面小编就一一为大家介绍五大功能应用。
3672 0

热门文章

最新文章