示例#1
0
    def face_predict(self, image):
        # 依然是根据后端系统确定维度顺序
        if K.image_dim_ordering() == 'th' and image.shape != (1, 3, IMAGE_SIZE,
                                                              IMAGE_SIZE):
            image = resize_image(
                image)  # 尺寸必须与训练集一致都应该是IMAGE_SIZE x IMAGE_SIZE
            image = image.reshape(
                (1, 3, IMAGE_SIZE, IMAGE_SIZE))  # 与模型训练不同,这次只是针对1张图片进行预测
        elif K.image_dim_ordering() == 'tf' and image.shape != (1, IMAGE_SIZE,
                                                                IMAGE_SIZE, 3):
            image = resize_image(image)
            image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3))

        # 数据浮点并归一化
        image = image.astype('float32')
        image /= 255
        # 给出输入属于各个类别的概率
        result_probability = self.model.predict_proba(image)
        print('result:', result_probability, max(result_probability[0]))

        # 给出类别预测:0-n
        result = self.model.predict_classes(image)

        # 返回类别预测结果
        return max(result_probability[0]), result[0]
示例#2
0
    def face_predict(self, image):
        # 依然是根据后端系统确定维度顺序
        if K.image_dim_ordering() == 'th' and image.shape != (1, 3, IMAGE_SIZE,
                                                              IMAGE_SIZE):
            image = resize_image(
                image)  # 尺寸必须与训练集一致都应该是IMAGE_SIZE x IMAGE_SIZE
            image = image.reshape(
                (1, 3, IMAGE_SIZE, IMAGE_SIZE))  # 与模型训练不同,这次只是针对1张图片进行预测
        elif K.image_dim_ordering() == 'tf' and image.shape != (1, IMAGE_SIZE,
                                                                IMAGE_SIZE, 3):
            image = resize_image(image)
            image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3))

            # 浮点并归一化
        image = image.astype('float32')
        image /= 255

        # 给出输入属于各个类别的概率,我们是二值类别,则该函数会给出输入图像属于0和1的概率各为多少
        result = self.model.predict_proba(image)
        print('result:', result)

        # 给出类别预测:0或者1
        result = self.model.predict_classes(image)

        # 返回类别预测结果
        return result[0]
示例#3
0
    def face_predict(self, image):
        #依然是根據後端系統確定維度順序
        if K.image_dim_ordering() == 'th' and image.shape != (1, 3, IMAGE_SIZE,
                                                              IMAGE_SIZE):
            image = resize_image(image)  #尺寸必須與訓練集一致都應該是IMAGE_SIZE x IMAGE_SIZE
            image = image.reshape(
                (1, 3, IMAGE_SIZE, IMAGE_SIZE))  #與模型訓練不同,這次只是針對1張圖片進行預測
        elif K.image_dim_ordering() == 'tf' and image.shape != (1, IMAGE_SIZE,
                                                                IMAGE_SIZE, 3):
            image = resize_image(image)
            image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3))

        #浮點並歸一化
        image = image.astype('float32')
        image /= 255

        #給出輸入屬於各個類別的概率,我們是二值類別,則該函式會給出輸入影象屬於0和1的概率各為多少
        result = self.model.predict_proba(image)
        print('result:', result)

        #給出類別預測:0或者1
        result = self.model.predict_classes(image)

        #返回類別預測結果
        return result[0]
示例#4
0
    def face_predict(self, image):
        # 依然是根据后端系统确定维度顺序
        if K.image_data_format() == 'channels_first' and image.shape != (
                1, 3, IMAGE_SIZE, IMAGE_SIZE):
            image = resize_image(
                image)  # 尺寸必须与训练集一致都应该是IMAGE_SIZE x IMAGE_SIZE
            image = image.reshape(
                (1, 3, IMAGE_SIZE, IMAGE_SIZE))  # 与模型训练不同,这次只是针对1张图片进行预测
        elif K.image_data_format() == 'channels_last' and image.shape != (
                1, IMAGE_SIZE, IMAGE_SIZE, 3):
            image = resize_image(image)
            image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3))

        # 浮点并归一化
        image = image.astype('float32')
        image /= 255

        # 给出输入属于各个类别的概率
        result_probability = self.model.predict(image)
        # print('result:', result_probability)

        # 给出类别预测(改)
        if max(result_probability[0]) >= 0.9:
            result = self.model.predict_classes(image)
            print('result:', result)
            # 返回类别预测结果
            return result[0]
        else:
            return -1
示例#5
0
    def face_predict(self, image):
        # 判断后端系统:TensorFlow/Theano,确定维度顺序
        if K.image_data_format() == 'channels_first' and image.shape != (
                1, 3, IMAGE_SIZE, IMAGE_SIZE):
            image = resize_image(image)  # 尺寸与训练集一致:IMAGE_SIZE x IMAGE_SIZE
            image = image.reshape((1, 3, IMAGE_SIZE, IMAGE_SIZE))  # 针对1张图片进行预测
        elif K.image_data_format() == 'channels_last' and image.shape != (
                1, IMAGE_SIZE, IMAGE_SIZE, 3):
            image = resize_image(image)
            image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3))

        # 改为浮点数并归一化
        image = image.astype('float32')
        image /= 255

        # 计算输入属于各个类别的概率,输出概率最大的类别
        result_probability = self.model.predict_proba(image)
        print('result:', result_probability, max(result_probability[0]))

        # 给出类别预测
        result = self.model.predict_classes(image)

        # 返回类别预测结果
        return max(result_probability[0]), result[0]