找出这两个图像之间的差异

时间:2018-06-01 20:32:18

标签: image image-processing jpeg python-imaging-library imaging

以编程方式,我的代码检测两类图像之间的差异,并始终拒绝一个类,同时始终允许另一个类。

我还没有发现产生错误的图像与不产生错误的图像之间存在任何差异。但是必须有一些区别,因为产生错误的那些在100%的时间都这样做,而其他的则在100%的时间内按预期工作。

特别是,我检查了颜色格式:两组中的RGB;尺寸:无显着差异;数据类型:两者中的uint8;像素值的大小:两者都相似。

以下两张图片从不起作用,其次是两张始终有效的图片:

我如何发现差异?

方案是我使用Firebase和Swift iOS前端将这些图像发送到Google Cloud ML-engine托管的网站。有些图像一直在工作,有些图像从不如上所述。此外,当我使用gcloud版本预测CLI时,所有图像都可以工作。对我来说,问题必然是图像中的问题。因此,我在这里为成像组发布。代码包含在完整性要求中。

包含index.js文件的CODE:

'use strict';

const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage');
const admin = require('firebase-admin');
const exec = require('child_process').exec;
const path = require('path');
const fs = require('fs');
const google = require('googleapis');
const sizeOf = require('image-size');

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const rtdb = admin.database();
const dbRef = rtdb.ref();

function cmlePredict(b64img) {
    return new Promise((resolve, reject) => {
        google.auth.getApplicationDefault(function (err, authClient) {
            if (err) {
                reject(err);
            }
            if (authClient.createScopedRequired && authClient.createScopedRequired()) {
                authClient = authClient.createScoped([
                    'https://www.googleapis.com/auth/cloud-platform'
                ]);
            }

        var ml = google.ml({
           version: 'v1'
        });

        const params = {
            auth: authClient,
            name: 'projects/myproject-18865/models/my_model',
            resource: {
                instances: [
                {
                    "image_bytes": {
                    "b64": b64img
                    }
                }
                ]
            }
        };

        ml.projects.predict(params, (err, result) => {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
});
}

function resizeImg(filepath) {
    return new Promise((resolve, reject) => {
        exec(`convert ${filepath} -resize 224x ${filepath}`, (err) => {
          if (err) {
            console.error('Failed to resize image', err);
            reject(err);
          } else {
            console.log('resized image successfully');
            resolve(filepath);
          }
        });
      });
}

exports.runPrediction = functions.storage.object().onChange((event) => {

    fs.rmdir('./tmp/', (err) => {
        if (err) {
            console.log('error deleting tmp/ dir');
        }
    });

const object = event.data;
const fileBucket = object.bucket;
const filePath = object.name;
const bucket = gcs().bucket(fileBucket);
const fileName = path.basename(filePath);
const file = bucket.file(filePath);

if (filePath.startsWith('images/')) {  
    const destination = '/tmp/' + fileName;
    console.log('got a new image', filePath);
    return file.download({
        destination: destination
    }).then(() => {
        if(sizeOf(destination).width > 224) {
            console.log('scaling image down...');
            return resizeImg(destination);
        } else {
            return destination;
        }
    }).then(() => {
        console.log('base64 encoding image...');
        let bitmap = fs.readFileSync(destination);
        return new Buffer(bitmap).toString('base64');
    }).then((b64string) => {
        console.log('sending image to CMLE...');
        return cmlePredict(b64string);
    }).then((result) => {
        console.log(`results just returned and is: ${result}`);  



        let predict_proba = result.predictions[0]

        const res_pred_val = Object.keys(predict_proba).map(k => predict_proba[k])
        const res_val = Object.keys(result).map(k => result[k])

        const class_proba = [1-res_pred_val,res_pred_val]
        const opera_proba_init = 1-res_pred_val
        const capitol_proba_init = res_pred_val-0

        // convert fraction double to percentage int
        let opera_proba = (Math.floor((opera_proba_init.toFixed(2))*100))|0
        let capitol_proba = (Math.floor((capitol_proba_init.toFixed(2))*100))|0
        let feature_list = ["houses", "trees"]


        let outlinedImgPath = '';
        let imageRef = db.collection('predicted_images').doc(filePath.slice(7));
               outlinedImgPath = `outlined_img/${filePath.slice(7)}`;
               imageRef.set({
                image_path: outlinedImgPath,
                opera_proba: opera_proba,
                capitol_proba: capitol_proba
            });

        let predRef = dbRef.child("prediction_categories");
        let arrayRef = dbRef.child("prediction_array");

    predRef.set({
            opera_proba: opera_proba,
            capitol_proba: capitol_proba,
            });

       arrayRef.set({first: {
           array_proba: [opera_proba,capitol_proba],
           brief_description: ["a","b"],
           more_details: ["aaaa","bbbb"],
           feature_list: feature_list},
        zummy1: "",
        zummy2: ""});

        return bucket.upload(destination, {destination: outlinedImgPath});

    });
} else {
    return 'not a new image';
}
}); 

1 个答案:

答案 0 :(得分:0)

问题是坏图像是灰度,而不是我的模型预期的RGB。我最初通过查看形状来检查这个。但是“糟糕”'图像有3个颜色通道,这3个通道中的每一个存储相同的数字---所以我的模型拒绝接受它们。此外,正如预期的那样,与我最初的想法相反,结果显示gcloud ML-engine预测CLI实际上也失败了这些图像。花了我2天的时间来搞清楚这一点!