cv2.imread()和keras.preprocessing中的image.load_img()的区别

博客展示了image.load_img()和cv2.imread()的效果,涉及到图像处理相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. image.load_img()
from keras.preprocessing import image

img_keras = image.load_img('./original/dog/880.jpg')
print(img_keras)

img_keras = image.img_to_array(img_keras)
print(img_keras[:,1,1])

效果如下:

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=256x384 at 0x2E6999D37B8>
#image.load_img()只是加载了一个文件,没有形成numpy数组,
#下面的numpy数组是通过image.img_to_array()的函数形成的

[108. 108. 110. 115. 119. 120. 122. 125. 127. 127. 129. 131. 132. 134.
 1. 135. 138. 138. 139. 143. 141. 136. 132. 131. 135. 121. 103.  97.
 2.  85.  69.  65.  69.  67.  74.  80.  77.  82.  92.  99. 105. 113.
 3. 126. 128. 129. 132. 134. 135. 135. 135. 135. 134. 133. 131. 130.
 4. 124. 122. 120. 119. 119. 121. 122. 123. 121. 120. 120. 122. 124.
 5. 124. 123. 121. 120. 119. 119. 118. 116. 114. 121. 120. 117. 115.
 6. 112. 111. 111. 114. 105. 104. 107. 104. 103. 106. 105. 101.  71.
 7.  99.  99.  77.  71.  80.  69.  71.  69.  65.  63.  65.  64.  61.
 8.  67.  74.  77.  79.  81.  79.  76.  78.  78.  77.  75.  77.  79.
 9.  72.  68.  68.  67.  66.  64.  63.  61.  61.  57.  57.  56.  56.
 10.  51.  45.  42.  34.  31.  28.  26.  27.  28.  28.  28.  29.  29.
 11.  28.  27.  26.  25.  26.  24.  23.  22.  21.  21.  21.  22.  22.
 12.  21.  21.  20.  20.  20.  19.  19.  19.  18.  18.  18.  18.  18.
 13.  18.  18.  18.  17.  16.  14.  13.  12.  12.  10.  10.  10.  10.
 14.   9.   9.   8.  10.  10.  10.  10.  12.  15.  18.  20.  23.  20.
 15. 175. 229. 231. 230. 221. 219. 220. 227. 223. 213. 220. 227. 221.
 16. 216. 219. 214. 197. 187. 179. 165. 175. 160. 175. 201. 206. 207.
 17. 196. 178. 189. 207. 195. 190. 188. 152. 124.  97. 113. 179. 214.
 18. 122. 172. 178. 204. 196. 200. 184. 167. 147. 112. 106. 131. 193.
 19. 202. 188. 187. 199. 206. 207. 208. 172. 139. 147. 128. 130. 215.
 20. 224. 221. 219. 217. 218. 206. 185. 158. 180. 174. 173. 142. 139.
 21. 200. 202. 205. 174. 122. 119. 123. 120. 155. 206. 160. 191. 191.
 22. 182. 158. 116.  66.  29.   6.  22.  47.  54.  53.  55.  61.  64.
 23.  75.  80.  84.  86.  88.  87.  88.  89.  89.  88.  87.  86.  86.
 24.  71. 174. 136.  13.   7.  38.  68.  77.  79.  80.  81.  81.  80.
 25.  78.  77.  77.  77.  77.  76.  76.  76.  75.  74.  75.  75.  75.
 26.  73.  71.  70.  68.  65.  62.  59.  57.  55.  52.  49.  46.  43.
 27.  34.  31.  28.  25.  23.]
  1. cv2.imread()
import cv2

img_cv2 = cv2.imread('./original/dog/880.jpg')
print(img_cv2[:,1,1])

效果如下:

[108 108 110 115 119 120 122 125 127 127 129 131 132 134 134 135 138 138
 139 143 141 136 132 131 135 121 103  97  97  85  69  65  69  67  74  80
  77  82  92  99 105 113 120 126 128 129 132 134 135 135 135 135 134 133
 131 130 126 124 122 120 119 119 121 122 123 121 120 120 122 124 124 124
 123 121 120 119 119 118 116 114 121 120 117 115 113 112 111 111 114 105
 104 107 104 103 106 105 101  71  72  99  99  77  71  80  69  71  69  65
  63  65  64  61  62  67  74  77  79  81  79  76  78  78  77  75  77  79
  76  72  68  68  67  66  64  63  61  61  57  57  56  56  54  51  45  42
  34  31  28  26  27  28  28  28  29  29  28  28  27  26  25  26  24  23
  22  21  21  21  22  22  21  21  21  20  20  20  19  19  19  18  18  18
  18  18  18  18  18  18  17  16  14  13  12  12  10  10  10  10   9   9
   9   8  10  10  10  10  12  15  18  20  23  20  27 175 229 231 230 221
 219 220 227 223 213 220 227 221 220 216 219 214 197 187 179 165 175 160
 175 201 206 207 207 196 178 189 207 195 190 188 152 124  97 113 179 214
 147 122 172 178 204 196 200 184 167 147 112 106 131 193 218 202 188 187
 199 206 207 208 172 139 147 128 130 215 228 224 221 219 217 218 206 185
 158 180 174 173 142 139 151 200 202 205 174 122 119 123 120 155 206 160
 191 191 192 182 158 116  66  29   6  22  47  54  53  55  61  64  69  75
  80  84  86  88  87  88  89  89  88  87  86  86  92  71 174 136  13   7
  38  68  77  79  80  81  81  80  79  78  77  77  77  77  76  76  76  75
  74  75  75  75  74  73  71  70  68  65  62  59  57  55  52  49  46  43
  38  34  31  28  25  23]
from architecture import * import os import cv2 import mtcnn import pickle import numpy as np from sklearn.preprocessing import Normalizer from tensorflow.keras.models import load_model +from PIL import Image os.environ['KMP_DUPLICATE_LIB_OK'] = 'True' ######pathsandvairables######### face_data = 'Faces/' required_shape = (160,160) face_encoder = InceptionResNetV2() path = "facenet_keras_weights.h5" face_encoder.load_weights(path) face_detector = mtcnn.MTCNN() encodes = [] encoding_dict = dict() l2_normalizer = Normalizer('l2') ############################### def normalize(img): mean, std = img.mean(), img.std() return (img - mean) / std for face_names in os.listdir(face_data): person_dir = os.path.join(face_data,face_names) # print(person_dir) encodes = [] for image_name in os.listdir(person_dir): # print(os.listdir(person_dir)) image_path = os.path.join(person_dir,image_name) # print(image_path) try: img_BGR = cv2.imread(image_path) if img_BGR is None: raise Exception("无法读取图片") img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB) # print(img_RGB.shape) x = face_detector.detect_faces(img_RGB) # print(x) # print(x[0]['box']) x1, y1, width, height = x[0]['box'] x1, y1 = abs(x1) , abs(y1) x2, y2 = x1+width , y1+height face = img_RGB[y1:y2 , x1:x2] face = normalize(face) face = cv2.resize(face, required_shape) face_d = np.expand_dims(face, axis=0) encode = face_encoder.predict(face_d)[0] encodes.append(encode) except Exception as e: print(f"无法读取图片 {image_name},原因:{e}") continue if encodes: encode = np.sum(encodes, axis=0 ) encode = l2_normalizer.transform(np.expand_dims(encode, axis=0))[0] encoding_dict[face_names] = enco
03-18
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 完全禁用TensorFlow日志(0-3) os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' # 禁用oneDNN优化 os.environ['KERAS_FACENET_PATH'] = './facenet_cache' # 提前设置FaceNet缓存路径 import cv2 import numpy as np import time from sklearn.svm import SVC from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from mtcnn import MTCNN from keras_facenet import FaceNet import joblib import urllib.request import tensorflow as tf # 禁用TensorFlow的进度条冗余日志 tf.get_logger().setLevel('ERROR') tf.autograph.set_verbosity(0) def load_facenet_manually(cache_dir='.facenet_cache', max_retries=5, retry_delay=10): """ 增强版:带重试机制的模型下载加载(兼容所有Python环境) """ # 创建缓存目录 os.makedirs(cache_dir, exist_ok=True) # 本地路径 model_path = os.path.join(cache_dir, "facenet_weights.h5") # 设置环境变量告诉 FaceNet 模型位置 os.environ['KERAS_FACENET_PATH'] = cache_dir print("正在加载FaceNet模型...") try: # 创建 FaceNet 实例 return FaceNet() except Exception as e: print(f"模型加载失败: {e}") return None class DormFaceRecognizer: def __init__(self, threshold=0.7, facenet_cache_dir='./facenet_cache'): """ 初始化人脸识别系统 """ # 加载人脸检测器(MTCNN) self.detector = MTCNN() # 加载人脸特征提取器(FaceNet) print("正在初始化FaceNet...") self.embedder = load_facenet_manually(cache_dir=facenet_cache_dir) if self.embedder is None: raise RuntimeError("无法加载FaceNet模型") # 初始化其他组件... self.classifier = None self.encoder = LabelEncoder() self.threshold = threshold self.dorm_members = [] def create_dataset(self, data_dir, min_samples=10): """ 从文件夹创建数据集 文件夹结构: data_dir/ ├── member1/ │ ├── img1.jpg │ ├── img2.jpg │ └── ... ├── member2/ │ ├── img1.jpg │ └── ... └── ... """ faces = [] labels = [] self.dorm_members = [] # 遍历每个成员文件夹 for member_dir in os.listdir(data_dir): member_path = os.path.join(data_dir, member_dir) if not os.path.isdir(member_path): continue # 记录寝室成员 self.dorm_members.append(member_dir) # 遍历成员的所有照片 member_faces = [] for img_file in os.listdir(member_path): img_path = os.path.join(member_path, img_file) img = cv2.imread(img_path) # 转换为RGB (MTCNN需要RGB格式) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 检测人脸 results = self.detector.detect_faces(img_rgb) if len(results) > 0: # 获取最大的人脸(假设每张照片只有一个人) face = max(results, key=lambda x: x['box'][2] * x['box'][3]) # 提取人脸区域 x, y, w, h = face['box'] face_img = img_rgb[y:y + h, x:x + w] # 调整大小为FaceNet所需尺寸(160x160) face_img = cv2.resize(face_img, (160, 160)) member_faces.append(face_img) # 确保每个成员有足够样本 if len(member_faces) < min_samples: print(f"警告: {member_dir}只有{len(member_faces)}个有效样本,至少需要{min_samples}个") continue # 添加成员数据 faces.extend(member_faces) labels.extend([member_dir] * len(member_faces)) # 添加陌生人样本 stranger_faces = self._generate_stranger_samples(len(faces) // 4) faces.extend(stranger_faces) labels.extend(['stranger'] * len(stranger_faces)) # 转换为numpy数组 faces = np.array(faces) labels = np.array(labels) return faces, labels def _generate_stranger_samples(self, num_samples): """生成陌生人样本""" stranger_faces = [] # 这里可以使用公开数据集的人脸作为陌生人 # 实际项目中应使用真实的陌生人照片 # 此处使用随机噪声模拟 for _ in range(num_samples): # 生成随机人脸(实际应使用真实陌生人照片) random_face = np.random.randint(0, 255, (160, 160, 3), dtype=np.uint8) stranger_faces.append(random_face) return stranger_faces def extract_features(self, faces): """提取人脸特征向量""" # 转换为浮点数并归一化 faces = faces.astype('float32') / 255.0 # 提取特征向量 (128维) embeddings = self.embedder.embeddings(faces) return embeddings def train_classifier(self, embeddings, labels): """训练SVM分类器""" # 编码标签 encoded_labels = self.encoder.fit_transform(labels) # 划分训练集测试集 X_train, X_test, y_train, y_test = train_test_split( embeddings, encoded_labels, test_size=0.2, random_state=42 ) # 创建并训练SVM分类器 self.classifier = SVC(kernel='linear', probability=True) self.classifier.fit(X_train, y_train) # 评估模型 y_pred = self.classifier.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print(f"模型准确率: {accuracy:.2f}") return accuracy def recognize_face(self, image): """ 识别单张图像中的人脸 返回: (姓名, 置信度) 或 ("陌生人", 距离) """ # 转换为RGB img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 检测人脸 results = self.detector.detect_faces(img_rgb) if len(results) == 0: return "未检测到人脸", 0.0 # 识别每个人脸 recognitions = [] for face in results: # 提取人脸区域 x, y, w, h = face['box'] face_img = img_rgb[y:y + h, x:x + w] # 调整大小 face_img = cv2.resize(face_img, (160, 160)) # 提取特征向量 face_img = face_img.astype('float32') / 255.0 embedding = self.embedder.embeddings([face_img])[0] # 预测 probabilities = self.classifier.predict_proba([embedding])[0] max_prob = np.max(probabilities) pred_class = self.classifier.predict([embedding])[0] pred_label = self.encoder.inverse_transform([pred_class])[0] # 判断是否为陌生人 if max_prob < self.threshold or pred_label == 'stranger': recognitions.append(("陌生人", max_prob, (x, y, w, h))) else: recognitions.append((pred_label, max_prob, (x, y, w, h))) return recognitions def save_model(self, file_path): """保存模型""" joblib.dump({ 'classifier': self.classifier, 'encoder': self.encoder, 'threshold': self.threshold, 'dorm_members': self.dorm_members }, file_path) print(f"模型已保存至: {file_path}") def load_model(self, file_path): """加载模型""" data = joblib.load(file_path) self.classifier = data['classifier'] self.encoder = data['encoder'] self.threshold = data['threshold'] self.dorm_members = data['dorm_members'] print(f"模型已加载,寝室成员: {', '.join(self.dorm_members)}") # 主函数 - 训练使用模型 def main(): print(f"[{time.strftime('%H:%M:%S')}] 程序启动") # 初始化识别器 - 指定FaceNet缓存目录 recognizer = DormFaceRecognizer( threshold=0.6, facenet_cache_dir='./facenet_cache' # 自定义缓存目录 ) # 数据集路径 (包含每个成员的文件夹) data_dir = "dorm_faces" # 步骤1: 创建数据集 print("正在创建数据集...") faces, labels = recognizer.create_dataset(data_dir, min_samples=10) # 步骤2: 提取特征 print("正在提取特征...") embeddings = recognizer.extract_features(faces) # 步骤3: 训练分类器 print("正在训练分类器...") accuracy = recognizer.train_classifier(embeddings, labels) # 保存模型 recognizer.save_model("dorm_face_model.pkl") # 测试识别 test_image = cv2.imread("test_photo.jpg") recognitions = recognizer.recognize_face(test_image) # 在图像上绘制结果 result_image = test_image.copy() for name, confidence, (x, y, w, h) in recognitions: label = f"{name} ({confidence:.2f})" color = (0, 255, 0) if name != "陌生人" else (0, 0, 255) # 绘制矩形框 cv2.rectangle(result_image, (x, y), (x + w, y + h), color, 2) # 绘制标签 cv2.putText(result_image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # 显示结果 cv2.imshow("人脸识别结果", result_image) cv2.waitKey(0) cv2.destroyAllWindows() # 保存结果图像 cv2.imwrite("recognition_result.jpg", result_image) if __name__ == "__main__": main() 运行后出现:[16:42:39] 程序启动 正在初始化FaceNet... 正在加载FaceNet模型... 并且程序还没有结束
最新发布
06-24
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值