def face_predict(self, image): #依然是根据后端系统确定维度顺序 if K.image_dim_ordering() == 'th' and image.shape != (1, 1, IMAGE_SIZE, IMAGE_SIZE): image = resize_image(image) #尺寸必须与训练集一致都应该是IMAGE_SIZE x IMAGE_SIZE image = image.reshape( (1, 1, IMAGE_SIZE, IMAGE_SIZE)) #与模型训练不同,这次只是针对1张图片进行预测 elif K.image_dim_ordering() == 'tf' and image.shape != (1, IMAGE_SIZE, IMAGE_SIZE, 1): image = resize_image(image) image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 1)) #浮点并归一化 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]
def face_predict(self, image): #依然是根據後端系統確定維度順序 if K.common.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.common.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]
def face_predict(self, image): # K.set_image_dim_ordering('tf') --> K.set_image_data_format('channels_last') # K.set_image_dim_ordering('th') --> K.set_image_data_format('channels_first') # K.image_dim_ordering() == 'tf' --> K.image_data_format() == 'channels_last' # K.image_dim_ordering() == 'th' --> K.image_data_format() == 'channels_first' # 依然是根据后端系统确定维度顺序 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) # 给出类别预测(改) if max(result_probability[0]) >= 0.9: result = self.model.predict_classes(image) print('result:', result) # 返回类别预测结果 return result[0] else: return -1
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 image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 # 归一化 # 给出输入属于各个类别的概率 result_probability = self.model.predict_proba(image) print('result:', result_probability) # 给出类别预测(改) print(max(result_probability[0])) if max(result_probability[0]) >= 0.9: result = self.model.predict_classes(image) print('result:', result) # 返回类别预测结果 return result[0] else: return -1
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) #给出类别预测(改) if max(result_probability[0]) >= 0.9: #0.9为识别相似度 result = self.model.predict_classes(image) #返回索引 print('result:', result) #print(result_probability[0]) #输出data中每位人物的识别概率 # 返回类别预测结果 return result[0] else: return -1
def resize_test_image(self, image_path, image=None): if image is None: image = cv2.imread(image_path) i = resize_image(image) # 需要进行预测的数据也要进行等比缩放 inp = i.reshape( 1, 64, 64, 3) # 重新确定image的形状 测试数据输入维度应当为4维 数量*IMAGESIZE*IMAGESIZE*RGB inp = inp.astype('float32') # dtype需要一致 这里更该为 float32 return inp else: i = resize_image(image) # 需要进行预测的数据也要进行等比缩放 inp = i.reshape(1, 64, 64, 3) # 重新确定image的形状 测试数据输入维度应当为4维 inp = inp.astype('float32') # dtype需要一致 这里更该为 float32 return inp
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 = image.reshape((1, 3, IMAGE_SIZE, IMAGE_SIZE)) 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 = self.model.predict_proba(image) print 'result:', result result = self.model.predict_classes(image) return result[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 = image.reshape((1, 3, IMAGE_SIZE, IMAGE_SIZE)) 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 = self.model.predict_proba(image) print("result: ", result) result = self.model.predict_classes(image) return result[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 = image.reshape((1, 3, IMAGE_SIZE, IMAGE_SIZE)) 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 = self.model.predict_proba(image) print('result:', result) result = self.model.predict_classes(image) return result[0]
def read_path(path_name): for dir_item in os.listdir(path_name): # 从初始路径开始叠加,合并成可识别的操作路径 full_path = os.path.abspath(os.path.join(path_name, dir_item)) if os.path.isdir(full_path): # 如果是文件夹,继续递归调用 read_path(full_path) else: # 文件 if dir_item.endswith('.jpg'): image = cv2.imread(full_path) image = resize_image(image, IMAGE_SIZE, IMAGE_SIZE) # 从我们的文件夹中读取图片然后进行统一规格的处理 return image
def face_predict(self, image): face_labels = ['zhuang', 'deng', 'others', 'shang'] if image.shape != (1, IMAGE_SIZE, IMAGE_SIZE, 3): image = resize_image(image, IMAGE_SIZE, IMAGE_SIZE) image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, 3)) result = self.model.predict(image) print('result:', result[0]) result = self.model.predict_classes(image) faceID = face_labels[result[0]] return faceID
def face_predict_01(self, image): image = resize_image(image) image = image.astype('float32') image = image / 255 result = self.model.predict(image) return result
#-*- coding:utf-8 -*- #author:zhangwei import cv2 from face_dl import ModelFace from load_dataset import resize_image MODEL_PATH = '/home/zhangwei/face/myface.model.h5' if __name__ == '__main__': model = ModelFace() model.load_mdoel() imagepath = '/home/zhangwei/138439.jpg' image = cv2.imread(imagepath) image = resize_image(image) # cv2.imshow("image" , image) # cv2.imwrite("/home/zhangwei/1.jpg" , image) # cv2.waitKey(10) model = ModelFace() model.load_mdoel() model.face_predict_01(image)