Writing image to local server

前端 未结 6 1194
孤街浪徒
孤街浪徒 2020-11-28 19:38

Update

The accepted answer was good for last year but today I would use the package everyone else uses: https://github.com/mikeal/request


6条回答
  •  天命终不由人
    2020-11-28 19:56

    I have an easier solution using fs.readFileSync(./my_local_image_path.jpg)

    This is for reading images from Azure Cognative Services's Vision API

    const subscriptionKey = 'your_azure_subscrition_key';
    const uriBase = // **MUST change your location (mine is 'eastus')**
        'https://eastus.api.cognitive.microsoft.com/vision/v2.0/analyze';
    
    // Request parameters.
    const params = {
        'visualFeatures': 'Categories,Description,Adult,Faces',
        'maxCandidates': '2',
        'details': 'Celebrities,Landmarks',
        'language': 'en'
    };
    
    const options = {
        uri: uriBase,
        qs: params,
        body: fs.readFileSync(./my_local_image_path.jpg),
        headers: {
            'Content-Type': 'application/octet-stream',
            'Ocp-Apim-Subscription-Key' : subscriptionKey
        }
    };
    
    request.post(options, (error, response, body) => {
    if (error) {
        console.log('Error: ', error);
        return;
    }
    let jsonString = JSON.stringify(JSON.parse(body), null, '  ');
    body = JSON.parse(body);
    if (body.code) // err
    {
        console.log("AZURE: " + body.message)
    }
    
    console.log('Response\n' + jsonString);
    

提交回复
热议问题