• AI计算机视觉进阶项目(一)——带口罩识别检测(4)


    独热编码官方合作微信:gldz_super

    本专栏《AI计算机视觉进阶项目》主要以计算机视觉实战项目为主,第一个项目为口罩检测:该项目将分为几个模块进行展示。本项目已完成链接:

    1.AI计算机视觉进阶项目(一)——带口罩识别检测(1)_AI炮灰的博客-CSDN博客

    2.AI计算机视觉进阶项目(一)——带口罩识别检测(2)_AI炮灰的博客-CSDN博客

    3.AI计算机视觉进阶项目(一)——带口罩识别检测(3)_AI炮灰的博客-CSDN博客

    一、本节概述

    对AI计算机视觉进阶项目(一)——带口罩识别检测(3)中训练好的模型进行预测。

    二、任务实现

    2.1.加载模型

    1. # 加载模型
    2. model = keras.models.load_model('./face_mask_model.h5')
    3. model.summary()

     2.2选取测试图像

    1. # 挑选一个测试图像
    2. img = cv2.imread('img.png')
    3. plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    4. plt.show()

     2.3对测试数据进行裁剪和Blob

    1. """
    2. 由于在训练的时候将图像进行了裁剪以及blob变换,因此在测试的时候同样需要如此处理
    3. """
    4. # 加载SSD模型:将人脸剪切出来
    5. face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt',
    6. 'weights/res10_300x300_ssd_iter_140000.caffemodel')
    7. img_crop = face_detect(img)
    8. plt.imshow(cv2.cvtColor(img_crop, cv2.COLOR_BGR2RGB))
    9. plt.show()
    10. img_blob = imgBlob(img_crop)
    11. plt.imshow(cv2.cvtColor(img_blob, cv2.COLOR_BGR2RGB))
    12. plt.show()

    裁剪和blob函数如下所示:

    1. def face_detect(img):
    2. # 转为blob:进行均值相减,加快运算
    3. img_blob = cv2.dnn.blobFromImage(img, 1, (300, 300), (104, 177, 123), swapRB=True)
    4. # 输入
    5. face_detector.setInput(img_blob)
    6. # 推理
    7. detections = face_detector.forward()
    8. # print(detections.shape)
    9. # 遍历结果
    10. # 获取原图大小
    11. img_h, img_w = img.shape[:2]
    12. # 人数
    13. person_count = detections.shape[2]
    14. for face_index in range(person_count):
    15. # 置信度
    16. confidence = detections[0, 0, face_index, 2]
    17. # print(confidence) # 发现大部分置信度都是比较小,我们这挑选比较大的置信度
    18. if confidence >0.5: # 表示检测到人脸
    19. locations = detections[0, 0, face_index, 3:7]*np.array([img_w, img_h, img_w, img_h]) # 由于之前归一化了,所以拿到位置要乘以宽高返回到原图大小
    20. # 用矩形框画出人脸:由于画矩形框不能有小数,所以需要取整
    21. l, t, r, b = locations.astype('int')
    22. # cv2.rectangle(img, (l, t), (r, b), (0, 255, 0), 5)
    23. # plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
    24. # plt.show()
    25. return img[t:b, l:r]
    26. return None
    27. # 转为Blob图像
    28. def imgBlob(img):
    29. img_blob = cv2.dnn.blobFromImage(img, 1, (100, 100), (104, 177, 123), swapRB=True)
    30. # 转变后图像由三维会变为四维,所以需要进行维度压缩:变为:(1, 3, 300, 300)
    31. img_squeeze = np.squeeze(img_blob) # 变为(3, 300, 300)所以需要转置将3放在后面
    32. img_squeeze = img_squeeze.T # 输出人倒立横着的,所以需要旋转90
    33. # 旋转
    34. img_rotate = cv2.rotate(img_squeeze, cv2.ROTATE_90_CLOCKWISE)
    35. # 再进行镜像翻转图像就和原图位置一样
    36. img_flip = cv2.flip(img_rotate, 1)
    37. # 去除负数:将小于0设置为0,大于0不变
    38. img_flip = np.maximum(img_flip, 0)
    39. # 归一化
    40. img_blob = img_flip / img_flip.max()
    41. return img_blob

     2.4进行预测

    1. # 进行预测:
    2. print(img_blob.shape) # 由于模型的输入要是四维的,而本数据是(100, 100, 3),所以需要升高维度
    3. img_input = img_blob.reshape(1, 100, 100, 3)
    4. # 得到对应属于每一类别的概率
    5. result = model.predict(img_input) # 输出是一个二维的,我们要变为一维的
    6. print(result) # 输出的结果看不出谁大谁小,所以需要将其利用softmax变为0-1
    7. print(result[0]) # 变为一维的
    8. result = softmax(result[0]) # 很明显可以看出属于哪个类别概率大
    9. print(result)
    10. # 找到最大值的索引就是属于某个类别的概率
    11. max_index = result.argmax()
    12. max_value = result[max_index]
    13. print(max_value) # 输出最大值的概率
    14. label = os.listdir('images')
    15. # 预测图像所属类别
    16. pre_label = label[max_index]
    17. print(pre_label)

     3.完整代码

    1. from tensorflow.python import keras
    2. import cv2
    3. import numpy as np
    4. import matplotlib.pyplot as plt
    5. from scipy.special import softmax
    6. import os
    7. def face_detect(img):
    8. # 转为blob:进行均值相减,加快运算
    9. img_blob = cv2.dnn.blobFromImage(img, 1, (300, 300), (104, 177, 123), swapRB=True)
    10. # 输入
    11. face_detector.setInput(img_blob)
    12. # 推理
    13. detections = face_detector.forward()
    14. # print(detections.shape)
    15. # 遍历结果
    16. # 获取原图大小
    17. img_h, img_w = img.shape[:2]
    18. # 人数
    19. person_count = detections.shape[2]
    20. for face_index in range(person_count):
    21. # 置信度
    22. confidence = detections[0, 0, face_index, 2]
    23. # print(confidence) # 发现大部分置信度都是比较小,我们这挑选比较大的置信度
    24. if confidence >0.5: # 表示检测到人脸
    25. locations = detections[0, 0, face_index, 3:7]*np.array([img_w, img_h, img_w, img_h]) # 由于之前归一化了,所以拿到位置要乘以宽高返回到原图大小
    26. # 用矩形框画出人脸:由于画矩形框不能有小数,所以需要取整
    27. l, t, r, b = locations.astype('int')
    28. # cv2.rectangle(img, (l, t), (r, b), (0, 255, 0), 5)
    29. # plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
    30. # plt.show()
    31. return img[t:b, l:r]
    32. return None
    33. # 转为Blob图像
    34. def imgBlob(img):
    35. img_blob = cv2.dnn.blobFromImage(img, 1, (100, 100), (104, 177, 123), swapRB=True)
    36. # 转变后图像由三维会变为四维,所以需要进行维度压缩:变为:(1, 3, 300, 300)
    37. img_squeeze = np.squeeze(img_blob) # 变为(3, 300, 300)所以需要转置将3放在后面
    38. img_squeeze = img_squeeze.T # 输出人倒立横着的,所以需要旋转90
    39. # 旋转
    40. img_rotate = cv2.rotate(img_squeeze, cv2.ROTATE_90_CLOCKWISE)
    41. # 再进行镜像翻转图像就和原图位置一样
    42. img_flip = cv2.flip(img_rotate, 1)
    43. # 去除负数:将小于0设置为0,大于0不变
    44. img_flip = np.maximum(img_flip, 0)
    45. # 归一化
    46. img_blob = img_flip / img_flip.max()
    47. return img_blob
    48. if __name__ =="__main__":
    49. # 加载模型
    50. model = keras.models.load_model('./face_mask_model.h5')
    51. model.summary()
    52. # 挑选一个测试图像
    53. img = cv2.imread('img.png')
    54. plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    55. plt.show()
    56. """
    57. 由于在训练的时候将图像进行了裁剪以及blob变换,因此在测试的时候同样需要如此处理
    58. """
    59. # 加载SSD模型:将人脸剪切出来
    60. face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt',
    61. 'weights/res10_300x300_ssd_iter_140000.caffemodel')
    62. img_crop = face_detect(img)
    63. plt.imshow(cv2.cvtColor(img_crop, cv2.COLOR_BGR2RGB))
    64. plt.show()
    65. img_blob = imgBlob(img_crop)
    66. plt.imshow(cv2.cvtColor(img_blob, cv2.COLOR_BGR2RGB))
    67. plt.show()
    68. # 进行预测:
    69. print(img_blob.shape) # 由于模型的输入要是四维的,而本数据是(100, 100, 3),所以需要升高维度
    70. img_input = img_blob.reshape(1, 100, 100, 3)
    71. # 得到对应属于每一类别的概率
    72. result = model.predict(img_input) # 输出是一个二维的,我们要变为一维的
    73. print(result) # 输出的结果看不出谁大谁小,所以需要将其利用softmax变为0-1
    74. print(result[0]) # 变为一维的
    75. result = softmax(result[0]) # 很明显可以看出属于哪个类别概率大
    76. print(result)
    77. # 找到最大值的索引就是属于某个类别的概率
    78. max_index = result.argmax()
    79. max_value = result[max_index]
    80. print(max_value) # 输出最大值的概率
    81. label = os.listdir('images')
    82. # 预测图像所属类别
    83. pre_label = label[max_index]
    84. print(pre_label)

     

  • 相关阅读:
    使用maven打包项目时报错:[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
    多模态说话人开源项目3D-Speaker
    如何在自己的项目中实现脚手架的命令行交互
    Win10禁止应用独占麦克风
    Spring Cloud Alibaba全套笔记,几乎涵盖了所有操作
    手写RPC框架--4.服务注册
    C#编程学习与实践
    小侃设计模式(七)-桥接模式
    一种指纹生物特征的隐私保护和认证方法
    基于 Laravel 封装参数校验
  • 原文地址:https://blog.csdn.net/bigData1994pb/article/details/126456982