1、通过imageai.Detection做对象检测
其中, resnet50_coco_best_v2.0.1.h5 可通过 http://link.zhihu.com/?target=https%3A//github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5进行下载。
示例代码:
from imageai.Detection import ObjectDetection
import os
# 获取当前路径
execution_path = os.getcwd()
# 初始化检测器
detector = ObjectDetection()
# 设置检测器的网络类型为resnet
detector.setModelTypeAsRetinaNet()
# 导入模型权值文件
detector.setModelPath(os.path.join(execution_path, 'resnet50_coco_best_v2.0.1.h5'))
# 加载模型
detector.loadModel()
# 对图片进行测试并输出测试结果
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, 'test.jpg'),
output_image_path=os.path.join(execution_path, 'result.jpg'))
# 输出检测到的对象及相应的置信度
for object in detections:
print('name:' + object['name'] + " " + 'probability:' + object['percentage_probability'])
原图:
结果:
name:person probability:80.5445909500122
name:person probability:90.69478511810303
name:car probability:95.79920172691345
2、通过imageai.Prediction做对象检测
其中, resnet50_weights_tf_dim_ordering_tf_kernels.h5 可通过https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_weights_tf_dim_ordering_tf_kernels.h5进行下载。
示例代码:
from imageai.Prediction import ImagePrediction
import os
# 获取当前路径
execution_path = os.getcwd()
# 初始化预测器
predictor = ImagePrediction()
# 设置预测器的网络类型为resnet
predictor.setModelTypeAsResNet()
# 导入模型权值文件
predictor.setModelPath(os.path.join(execution_path, 'resnet50_weights_tf_dim_ordering_tf_kernels.h5'))
# 加载模型
predictor.loadModel()
# 对图片进行测试并输出测试结果
predictions, probabilities = predictor.predictImage(os.path.join(execution_path, 'test.jpg'), result_count=5)
# 输出预测到的对象及相应的置信度
for prediction, probability in zip(predictions, probabilities):
print('name:' + prediction + " " + 'probability:' + probability)
原图:
结果:
name:sports_car probability:72.6273238658905
name:tow_truck probability:7.000575959682465
name:racer probability:4.8392243683338165
name:convertible probability:4.6900734305381775
name:car_wheel probability:3.936982899904251
识别网络:
- SqueezeNet(预测速度最快 正确率中等)
- ResNet50 (预测速度快 正确率较高)
- InceptionV3(预测速度慢 正确率高)
-
DenseNet121(预测速度更慢 正确率最高)