28 Star 128 Fork 25

cungudafa / hand-keras-yolo3-recognize

Clone or download
beyes.py
Copy Edit Web IDE Raw Normal History
cungudafa   sign 2020-08-08
"""2.朴素贝叶斯分类识别将12*33=396张图像按照训练集为70%,测试集为30%的比例随机划分,再获取每张图像的骨骼和手部特征点的距离和角度根据像素的特征分布情况进行图像分类分析。"""# -*- coding: utf-8 -*-import osfrom cv2 import cv2import timeimport numpy as npfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import confusion_matrix, classification_reportfrom sklearn.naive_bayes import BernoulliNBfrom sklearn.externals import joblibfrom pose_hand import getImgInfofrom yolo import YOLOfrom pose.coco import general_coco_model# ----------------------------------------------------------------------------------# 第一步 切分训练集和测试集# ----------------------------------------------------------------------------------X = []  # 定义图像名称Y = []  # 定义图像分类类标# Z = [] #定义图像像素path = 'D:/myworkspace/dataset/My_test/bagofwords'for idx, labelname in enumerate(os.listdir(path)):    if ".txt" not in labelname:        f = os.path.join(path, labelname)        for i, imgname in enumerate(os.listdir(f)):            imgpath = os.path.join(f, imgname)            X.append(imgpath)            Y.append(labelname)X = np.array(X)Y = np.array(Y)#随机率为100% 选取其中的30%作为测试集X_train, X_test, y_train, y_test = train_test_split(    X, Y, test_size=0.3, random_state=1)print(len(X_train), len(X_test), len(y_train), len(y_test))# ----------------------------------------------------------------------------------# 第二步 图像读取及转换为像素直方图# ----------------------------------------------------------------------------------# cocomodelpath = "model/"start = time.time()pose_model = general_coco_model(modelpath)  # 1.加载模型print("[INFO]Pose Model loads time: ", time.time() - start)# yolostart = time.time()_yolo = YOLO()  # 1.加载模型print("[INFO]yolo Model loads time: ", time.time() - start)# # 训练集# XX_train = []# for i in X_train:#     image = cv2.imread(i) #     hist,_= getImgInfo(image, pose_model, _yolo)#     XX_train.append(hist)# 测试集XX_test = []for i in X_test:    image = cv2.imread(i)      hist,_ = getImgInfo(image, pose_model, _yolo)    XX_test.append(hist)# orb = open('D:/myworkspace/dataset/My_test/bagofwords/y_train.txt', 'w')# for i, info in enumerate(y_train):#     orb.write('\n'+str(info)+str(XX_train[i]))# orb.close()# orb = open('D:/myworkspace/dataset/My_test/bagofwords/y_test.txt', 'w')# for i, info in enumerate(y_test):#     orb.write('\n'+str(info)+str(XX_test[i]))# orb.close()# ----------------------------------------------------------------------------------# 第三步 基于朴素贝叶斯的图像分类处理# ----------------------------------------------------------------------------------# 使用训练集训练模型# clf = BernoulliNB().fit(XX_train, y_train)  # 伯努利贝叶斯分类器os.chdir("model/")# ticks = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) # 时间戳# #joblib.dump(clf, ticks+"train_model.m") # 保存# joblib.dump(clf, ticks+"train_model.pkl") # 保存clf = joblib.load("train_model.pkl")predictions_labels = clf.predict(XX_test)#查看模型参数print(clf.get_params())   # 参看模型的所有参数# from sklearn.model_selection import cross_val_score# scores = cross_val_score(clf, XX_train, y_train, cv=5,n_jobs=1)  # 采用5折交叉验证# print(scores)# # 平均得分和95%的置信区间# print("Accuracy: %0.2f(+/-%0.3f)"%(scores.mean(), scores.std()*2))# # 95%的置信区间在平均值两倍标准差之内# 使用测试集预测结果print(u'预测结果:')print(predictions_labels)# 生成文本型分类报告print(u'算法评价:')  # 算法评价准确率(Precision)、召回率(Recall)和F值(F1-score)print((classification_report(y_test, predictions_labels)))# 生成字典型分类报告report = classification_report(y_test, predictions_labels, output_dict=True)# print(u'sun的字典型分类报告:')# for key, value in report["sun"].items():#     print(f"{key:10s}:{value:10.2f}")# 输出前10张图片及预测结果from PIL import Imageimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号k = 0while k < 10:    # 读取图像    print(X_test[k])    image = cv2.imread(X_test[k])    print(predictions_labels[k])    # 显示图像#    cv2.imshow("img", image)#     cv2.waitKey(0)#     cv2.destroyAllWindows()#     plt.figure(figsize=[5, 5])#     plt.subplot(1, 1, 1)#     plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))#     plt.ylabel(u'%s'%str(predictions_labels[k]), fontsize=15)#     plt.xlabel(u'%s'%str(X_test[k]))#     plt.axis("off")#     plt.show()    k = k + 1
Python
1
https://gitee.com/cungudafa/hand-keras-yolo3-recognize.git
git@gitee.com:cungudafa/hand-keras-yolo3-recognize.git
cungudafa
hand-keras-yolo3-recognize
hand-keras-yolo3-recognize
master

Search