Suck the cat with code! This paper is participating in[Cat Essay Campaign]

One, foreword

Suddenly, I wondered if I could identify a cat in a picture and where the place was.

Second, preparation

API provided by Megvii Face and Calf Translation, a cat picture!

Three, the preparation process

Megvii Face uses API processes

Official tutorial:Scene and object recognition API tutorialRegister and log in to the console

Create application API Key (Application Management) Obtain THE API Key and API SecretRequested address:

<! -- Require POST request -->https://api-cn.faceplusplus.com/imagepp/beta/detectsceneandobject?api_key= you kuang see API Key&api _secret = you kuang see API Secret&image _URL = your image addressCopy the code

Calf translation uses API process

(PS: The purpose is to translate the results of the return of the sight (English) for translation)

Official process:The registration process,Get the APIKEY process,Test the APIKEY process

Register your account and log in to the console — “Get traffic according to process –” to get the provided APIkeyRequested address:

http://api.niutrans.com/NiuTransServer/translation?to= language translation direction & the from = direction of the source language & src_text apikey = translation content & apikey = youCopy the code

Other workflow

Find your own cat photo, or use one provided by the authorCat photosAddress:Nanfangzhe. Gitee. IO/cat – picture….

Code and testing

The core code

    var niutransUrl = "http://api.niutrans.com/NiuTransServer/translation";
    var faceUrl = "https://api-cn.faceplusplus.com/imagepp/beta/detectsceneandobject";
    var param = {
         // where param is the parameter -- key/value pair
        api_key:'HOTcabIupEcx19GrvVk_fpJpPmNeDAwz'.// Where to change 1️ one
        api_secret:'MOvF_DapqoMhhMUxBuwYcfPtA4OY_kzG'.// The place to be changed is 2️ one
        image_url:'https://nanfangzhe.gitee.io/cat-picture/database/images/cat3.png' // Test the image address
    }
    $.ajaxSettings.async = true;
     $.post(faceUrl, param, function(data){
        // console.log(" This request was successful ");
        // console.log(data);
        // console.log(data.objects[0].value);
        // console.log(data.scenes[0].value);

        if(data.objects.length == 0) {console.log("Image recognition error!");
            return;
        }
        var translateParam = {
            // where param is the parameter -- key/value pair
            to:'zh'.from:'en'.src_text:data.objects[0].value,
            apikey:'8ab87227fb9bedd396808aac58bb9257' // The place to be changed is 3️ one
        }
        $.getJSON(niutransUrl, translateParam, function(translateData){
            // console.log(translateData);
            if(translateData.tgt_text.indexOf("Cat") = = -1) {console.log("Your picture is not a cat, it is:", translateData.tgt_text);
                return;
            }
            console.log("Your picture is a cat.");
        });
        
        if(data.scenes.length == 0) {console.log("Current location, picture not recognized :(");
            return;
        }
        translateParam.src_text = data.scenes[0].value;
        $.getJSON(niutransUrl, translateParam, function(translateData){
            // console.log(translateData);
            console.log("Your cat is now at:",translateData.tgt_text);
        });
    });
    $.ajaxSettings.async = false;
Copy the code

The complete code

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    
    <title>Cat map identifies southerners - gold diggers</title>
</head>
<body>
    <center>
        <h1>Cat map identifies southerners - gold diggers</h1>
    </center>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js">
</script>
<script>
    console.log("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    console.log("********* Test Demo Southerners - Nuggets *********");
    console.log("https://juejin.cn/user/2840793779295133");
    console.log("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    var niutransUrl = "http://api.niutrans.com/NiuTransServer/translation";
    var faceUrl = "https://api-cn.faceplusplus.com/imagepp/beta/detectsceneandobject";
    var param = {
         // where param is the parameter -- key/value pair
        api_key:'HOTcabIupEcx19GrvVk_fpJpPmNeDAwz'.// Where to change 1️ one
        api_secret:'MOvF_DapqoMhhMUxBuwYcfPtA4OY_kzG'.// The place to be changed is 2️ one
        image_url:'https://nanfangzhe.gitee.io/cat-picture/database/images/cat3.png'
    }
    $.ajaxSettings.async = true;
     $.post(faceUrl, param, function(data){
        // console.log(" This request was successful ");
        // console.log(data);
        // console.log(data.objects[0].value);
        // console.log(data.scenes[0].value);

        if(data.objects.length == 0) {console.log("Image recognition error!");
            return;
        }
        var translateParam = {
            // where param is the parameter -- key/value pair
            to:'zh'.from:'en'.src_text:data.objects[0].value,
            apikey:'8ab87227fb9bedd396808aac58bb9257' // The place to be changed is 3️ one
        }
        $.getJSON(niutransUrl, translateParam, function(translateData){
            // console.log(translateData);
            if(translateData.tgt_text.indexOf("Cat") = = -1) {console.log("Your picture is not a cat, it is:", translateData.tgt_text);
                return;
            }
            console.log("Your picture is a cat.");
        });
        
        if(data.scenes.length == 0) {console.log("Current location, picture not recognized :(");
            return;
        }
        translateParam.src_text = data.scenes[0].value;
        $.getJSON(niutransUrl, translateParam, function(translateData){
            // console.log(translateData);
            console.log("Your cat is now at:",translateData.tgt_text);
        });
    });
    $.ajaxSettings.async = false;
</script>
</html>
Copy the code

Test case

【 the 】

Thank you for reading the end, if you have a different view, you are welcome to leave a comment below this article. I am a southerner who loves computers and loves the motherland. If the content of this article is only for learning reference, if there is any infringement, I am very sorry, please contact the author immediately delete.