• 基于口罩佩戴公开赛数据实践口罩佩戴识别


    这是之前网上举办的口罩佩戴公开赛的数据集,我周末正好有时间就想着拿来做一个口罩佩戴识别系统,跟我前面的一篇博文相互照应。链接在下面,感兴趣可以看下:

                                  《助力防疫,基于安防摄像头的人脸佩戴口罩检测》 

    这里做的不是目标检测,而是图像识别任务。

    效果如下:

     网上公开赛的数据集下载解压缩后如下:

     sample_submit.csv是提交样例格式,这里可以直接删掉。

    进入train目录,如下:

     可见:大赛给定的数据集一共是划分了三个类别,从上到下分别表示:未正确佩戴口罩、佩戴口罩、未佩戴口罩。

    我自己开发不打算分成三个类别,这里将未正确佩戴口罩划归到佩戴口罩类别下,当然了这个本身无所谓的,我只是个人做法而已。

    首先解析数据集:

    1. def parseData2H5():
    2. '''
    3. 加载本地数据集创建H5数据集
    4. '''
    5. X_train, y_train =[], []
    6. X_test, y_test =[], []
    7. picDir='data/train/'
    8. for one_label in os.listdir(picDir):
    9. for one_pic in os.listdir(picDir+one_label+'/'):
    10. if one_pic.endswith("jpg") or one_pic.endswith("png") or one_pic.endswith("jpeg"):
    11. try:
    12. one_path = picDir+one_label+'/' + one_pic
    13. print("one_path: ", one_path)
    14. #图片
    15. one_img = cv2.imread(one_path)
    16. one_img = cv2.resize(one_img,(100,100))
    17. one_img = one_img.transpose((2,0,1))
    18. #标签
    19. one_pic_classes = one_label
    20. one_y = getY(one_pic_classes)
    21. #整合
    22. X_train.append(one_img)
    23. y_train.append(one_y)
    24. except Exception as e:
    25. print("train Exception: ", e)
    26. X_train = np.array(X_train)
    27. picDir='data/test/'
    28. for one_label in os.listdir(picDir):
    29. for one_pic in os.listdir(picDir+one_label+'/'):
    30. if one_pic.endswith("jpg") or one_pic.endswith("png") or one_pic.endswith("jpeg"):
    31. try:
    32. one_path = picDir+one_label+'/' + one_pic
    33. print("one_path: ", one_path)
    34. #图片
    35. one_img = cv2.imread(one_path)
    36. one_img = cv2.resize(one_img,(100,100))
    37. one_img = one_img.transpose((2,0,1))
    38. #标签
    39. one_pic_classes = one_label
    40. one_y = getY(one_pic_classes)
    41. #整合
    42. X_test.append(one_img)
    43. y_test.append(one_y)
    44. except Exception as e:
    45. print("test Exception: ", e)
    46. X_test = np.array(X_test)
    47. f = h5py.File("dataset.h5")
    48. f['X_train'] = X_train
    49. f['X_test'] = X_test
    50. f['y_train'] = y_train
    51. f['y_test'] = y_test
    52. print("y_train_0: ", y_train[0])
    53. print("y_test_0: ", y_test[0])
    54. f.close()

    之后构建模型:

    1. def initModel(h=224, w=224, way=3):
    2. """
    3. 构建模型
    4. """
    5. model = Sequential()
    6. input_shape = (h, w, way)
    7. model.add(Conv2D(64, (3, 3), input_shape=input_shape))
    8. model.add(Activation("relu"))
    9. model.add(Dropout(0.3))
    10. model.add(Conv2D(64, (3, 3)))
    11. model.add(Activation("relu"))
    12. model.add(MaxPooling2D(pool_size=(2, 2)))
    13. model.add(Flatten())
    14. model.add(Dense(1024))
    15. model.add(Activation("relu"))
    16. model.add(Dropout(0.3))
    17. model.add(Dense(numbers))
    18. model.add(Activation("sigmoid"))
    19. lrate = 0.01
    20. decay = lrate / 100
    21. sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
    22. model.compile(loss="binary_crossentropy", optimizer=sgd, metrics=["accuracy"])
    23. print(model.summary())
    24. return model

    训练模型就是正常的流程了,这里就不在介绍了,在我之前的文章里面有很多图像识别的项目,这里就不再赘述细节了。训练过程可视化如下:

     这里开发了批量测试函数用于测试整体的识别精度:

    1. def batchTest(dataDir="data/test/"):
    2. '''
    3. 测试集测试
    4. '''
    5. count_dict={}
    6. for one_label in os.listdir(dataDir):
    7. print("one_label: ", one_label)
    8. count_dict[one_label]={}
    9. count_dict[one_label]["right"]=0
    10. count_dict[one_label]["wrong"]=0
    11. oneDir=dataDir+one_label+"/"
    12. for one_pic in os.listdir(oneDir):
    13. one_path=oneDir+one_pic
    14. one_pred,one_proba=singleTest(image_path=one_path)
    15. if one_pred==one_label:
    16. count_dict[one_label]["right"]+=1
    17. else:
    18. count_dict[one_label]["wrong"]+=1
    19. print("count_dict: ", count_dict)
    20. with open("count_dict.json","w") as f:
    21. f.write(json.dumps(count_dict))
    22. acc_dict={}
    23. for one_label in count_dict:
    24. one_right,one_wrong=count_dict[one_label]["right"],count_dict[one_label]["wrong"]
    25. one_acc=one_right/(one_right+one_wrong)
    26. print("one_label: ", one_label)
    27. print("one_acc: ", one_acc)
    28. acc_dict[one_label]=one_acc
    29. print("acc_dict: ", acc_dict)

    感兴趣可以直接使用,也可以基于自己的个性化应用进行改造。

  • 相关阅读:
    关于并发和并行,Go和Erlang之父都弄错了?
    这些编程语言你需要了解一下
    红黑树(Red Black Tree)
    一文带你入门UML!
    windows powershell 将U盘启动盘还原回普通U盘
    VMware vCenter Server 7 升级
    SpringBoot如何保证接口安全?
    Vim实用技巧_2.普通模式和插入模式
    LeetCode 2342. 数位和相等数对的最大和:哈希表
    人工智能神经网络是什么,人工神经网络应用范围
  • 原文地址:https://blog.csdn.net/Together_CZ/article/details/126908156