python识别麻将牌_图像识别之麻将识别源码( 三 )

2023-05-16

图像识别之麻将识别源码( 三 )

未经过允许不得转载,转载请联系我,如何联系,点我头像。

连载已经完结,

百度网盘测试APP下载地址:

提取码:pkbl

先见效果图,另外我已经上传到了抖音视频,想看NB效果,可以点击链接直接观看:

本人从事机器学习有一些时间,感觉与一般做APP应用也没有啥差别,现在每天就是准备样本 ,调整参数,训练,验证结果。可能是我还没有达到哪些教授的水平能设计神经网络吧,感觉也就是一般马龙该做的杂七杂八事情。另外我更加关注移动设备AI的实现与效果,体验一样重要,识别的速度要快!

接下来的博客开始记录我研究过程,过程是:采集样本->标注->训练->测试。

上一期讲了如何进行标注,本期将开始训练。

本次使用的是谷歌云平台训练,实际上就是一个ubuntu 系统,不过现在有免费的300美金使用。部署好服务器后。将训练的文件上传到云服务器。需要的文件有这些。

然后执行训练

python object_detection/model_main.py --model_dir=/home/softboyes/digeai/majiang/training --pipeline_config_path=/home/softboyes/digeai/majiang/ssd_mobilenet_v1_pets.config

由于我使用的8cpu+20G内存 训练36小时训练了34k 步法。

通过tensorboard观察,loss效果不太理想打算训练到60k。

下载后导出模型:

python object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path E:/AI_LAB/majiang/gitdata/ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix E:/AI_LAB/majiang/gitdata/out/model.ckpt-24720 --output_directory E:/AI_LAB/majiang/gitdata/export2

导出后模型如下:并写下测试test.py

importosimportsysimporttarfileimportcv2importnumpyasnpimporttensorflowastfsys.path.append("..")fromobject_detection.utilsimportlabel_map_utilfromobject_detection.utilsimportvisualization_utilsasvis_util# Path to frozen detection graphCWD_PATH = os.getcwd()PATH_TO_CKPT = os.path.join(CWD_PATH,'frozen_inference_graph.pb')# List of the strings that is used to add correct label for each box.PATH_TO_LABELS = os.path.join(CWD_PATH,'labelmap.pbtxt')NUM_CLASSES =28detection_graph = tf.Graph()withdetection_graph.as_default():    od_graph_def = tf.GraphDef()withtf.gfile.GFile(PATH_TO_CKPT,'rb')asfid:        serialized_graph = fid.read()        od_graph_def.ParseFromString(serialized_graph)        tf.import_graph_def(od_graph_def, name='')label_map = label_map_util.load_labelmap(PATH_TO_LABELS)categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)category_index = label_map_util.create_category_index(categories)defload_image_into_numpy_array(image):(im_width, im_height) = image.sizereturnnp.array(image.getdata()).reshape(      (im_height, im_width,3)).astype(np.uint8)withdetection_graph.as_default():withtf.Session(graph=detection_graph)assess:        image_np = cv2.imread("test.jpg")        cv2.imshow("input=", image_np)        print(image_np.shape)# image_np == [1, None, None, 3]image_np_expanded = np.expand_dims(image_np, axis=0)        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')        boxes = detection_graph.get_tensor_by_name('detection_boxes:0')        scores = detection_graph.get_tensor_by_name('detection_scores:0')        classes = detection_graph.get_tensor_by_name('detection_classes:0')        num_detections = detection_graph.get_tensor_by_name('num_detections:0')# Actual detection.(boxes, scores, classes, num_detections) = sess.run(            [boxes, scores, classes, num_detections],            feed_dict={image_tensor: image_np_expanded})# Visualization of the results of a detection.vis_util.visualize_boxes_and_labels_on_image_array(              image_np,              np.squeeze(boxes),              np.squeeze(classes).astype(np.int32),              np.squeeze(scores),              category_index,              use_normalized_coordinates=True,              min_score_thresh=0.4,              line_thickness=3)        cv2.imshow('object detection', image_np)        cv2.imwrite("run_result.png", image_np)        cv2.waitKey(0)        cv2.destroyAllWindows()sess.close()

测试模型,发现效果还算理想。接下来往安卓设备上面迁移。

迁移需要的问题:  frozen_inference_graph.pb  labelmap.pbtxt

拷贝到TensorFlow demo里面assert 文件夹。修改 DetectorActivity

修改后:

privatestaticfinalString TF_OD_API_MODEL_FILE ="file:///android_asset/frozen_inference_graph.pb";privatestaticfinalString TF_OD_API_LABELS_FILE ="file:///android_asset/majiang.txt";

然后用Android studio编译生成APK,然后安装到安卓手机。

下一章讲安卓手机效果与训练心得以及样本要求。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python识别麻将牌_图像识别之麻将识别源码( 三 ) 的相关文章

随机推荐