imageai官网地址: 传送门
详细版地址:https://blog.csdn.net/Baldprogrammer/article/details/116358180
项目地址:传送门
1、安装环境(最终版)
安装python-3.6.8-amd64 一键安装,勾选添加到path中
安装imageAI --官网地址:https://imageai-cn.readthedocs.io/zh_CN/latest/ImageAI.html#id1
cmd执行:
pip3 install --index-url https://pypi.douban.com/simple -U tensorflow==1.4.0
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U numpy==1.16.0
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U scipy==1.4.1
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U opencv-python==4.5.1.48
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U pillow==8.2.0
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U matplotlib==3.3.4
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U h5py==2.10.0
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U keras==2.2.0
pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.1/imageai-2.0.1-py3-none-any.whl
模型文件下载地址:传送门
图像预测:
from imageai.Detection import ObjectDetection
import os
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path+"\\model\\" , "resnet50_coco_best_v2.0.1.h5"))
# 设置模型的检测速度 detection_speed="fast" “normal”(default), “fast”, “faster” , “fastest” and “flash”
detector.loadModel(detection_speed="normal")
# 定义使用的模型
custom_objects = detector.CustomObjects( person=True, bicycle=True, car=True, motorcycle=True, airplane=True,
bus=True, train=True, truck=True, boat=True, traffic_light=True, fire_hydrant=True, stop_sign=True,
parking_meter=True, bench=True, bird=True, cat=True, dog=True, horse=True, sheep=True, cow=True, elephant=True, bear=True, zebra=True,
giraffe=True, backpack=True, umbrella=True, handbag=True, tie=True, suitcase=True, frisbee=True, skis=True, snowboard=True,
sports_ball=True, kite=True, baseball_bat=True, baseball_glove=True, skateboard=True, surfboard=True, tennis_racket=True,
bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True,
broccoli=True, carrot=True, hot_dog=True, pizza=True, donot=True, cake=True, chair=True, couch=True, potted_plant=True, bed=True,
dining_table=True, toilet=True, tv=True, laptop=True, mouse=True, remote=True, keyboard=True, cell_phone=True, microwave=True,
oven=True, toaster=True, sink=True, refrigerator=True, book=True, clock=True, vase=True, scissors=True, teddy_bear=True, hair_dryer=True,
toothbrush=True)
# 是否把识别对象抠出来放到文件夹里
isSingle=True
inPath=execution_path+"\\image\\in\\"
outPath=execution_path+"\\image\\out\\"
all_images_array = []
all_files = os.listdir(inPath)
for each_file in all_files:
if(each_file.endswith(".jpg") or each_file.endswith(".png")):
all_images_array.append(each_file)
for path in all_images_array:
# 进行识别
detections, objects_path = detector.detectCustomObjectsFromImage(
extract_detected_objects=isSingle, # 将识别的图片单独保存出来
minimum_percentage_probability=50, # 置信度
input_type="file", # 输入文件格式 file/array/stream
output_type="file", # 输出文件格式 file/array/stream
custom_objects=custom_objects, # 选择可用模型
input_image=os.path.join(inPath, path), # 输入文件
output_image_path=os.path.join(outPath, "copy_"+path), # 输出文件
)
# 输出识别信息
if isSingle:
for eachObject, eachObjectPath in zip(detections, objects_path):
print(eachObject["name"] + " : " + eachObject["percentage_probability"])
print("Object's image saved in " + eachObjectPath)
else:
for eachObject in detections:
print(eachObject["name"] + " : " + eachObject["percentage_probability"])
print("--------------------------------")
对象检测
from imageai.Detection import ObjectDetection
import os
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path+"\\model\\" , "resnet50_coco_best_v2.0.1.h5"))
# 设置模型的检测速度 detection_speed="fast" “normal”(default), “fast”, “faster” , “fastest” and “flash”
detector.loadModel(detection_speed="normal")
# 定义使用的模型
custom_objects = detector.CustomObjects( person=True, bicycle=True, car=True, motorcycle=True, airplane=True,
bus=True, train=True, truck=True, boat=True, traffic_light=True, fire_hydrant=True, stop_sign=True,
parking_meter=True, bench=True, bird=True, cat=True, dog=True, horse=True, sheep=True, cow=True, elephant=True, bear=True, zebra=True,
giraffe=True, backpack=True, umbrella=True, handbag=True, tie=True, suitcase=True, frisbee=True, skis=True, snowboard=True,
sports_ball=True, kite=True, baseball_bat=True, baseball_glove=True, skateboard=True, surfboard=True, tennis_racket=True,
bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True,
broccoli=True, carrot=True, hot_dog=True, pizza=True, donot=True, cake=True, chair=True, couch=True, potted_plant=True, bed=True,
dining_table=True, toilet=True, tv=True, laptop=True, mouse=True, remote=True, keyboard=True, cell_phone=True, microwave=True,
oven=True, toaster=True, sink=True, refrigerator=True, book=True, clock=True, vase=True, scissors=True, teddy_bear=True, hair_dryer=True,
toothbrush=True)
# 是否把识别对象抠出来放到文件夹里
isSingle=True
# 进行识别
detections, objects_path = detector.detectCustomObjectsFromImage(
extract_detected_objects=isSingle, #将识别的图片单独保存出来
minimum_percentage_probability=50, #置信度
input_type="file", #输入文件格式 file/array/stream
output_type="file", #输出文件格式 file/array/stream
custom_objects=custom_objects, #选择可用模型
input_image=os.path.join(execution_path+"\\image\\in\\", "13.jpg"), # 输入文件
output_image_path=os.path.join(execution_path+"\\image\\out\\", "13(2).jpg"), # 输出文件
)
#输出识别信息
if isSingle:
for eachObject, eachObjectPath in zip(detections, objects_path):
print(eachObject["name"] + " : " + eachObject["percentage_probability"])
print("Object's image saved in " + eachObjectPath)
else:
for eachObject in detections:
print(eachObject["name"] + " : " + eachObject["percentage_probability"])
print("--------------------------------")
视频检测
from imageai.Detection import VideoObjectDetection
import os
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error
# os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path+"\\model\\", "resnet50_coco_best_v2.0.1.h5"))
# 模型等级 “normal”(default), “fast”, “faster” , “fastest” and “flash”
detector.loadModel(detection_speed="fastest")
# 定义使用的模型
custom_objects = detector.CustomObjects( person=True, bicycle=True, car=True, motorcycle=True, airplane=True,
bus=True, train=True, truck=True, boat=True, traffic_light=True, fire_hydrant=True, stop_sign=True,
parking_meter=True, bench=True, bird=True, cat=True, dog=True, horse=True, sheep=True, cow=True, elephant=True, bear=True, zebra=True,
giraffe=True, backpack=True, umbrella=True, handbag=True, tie=True, suitcase=True, frisbee=True, skis=True, snowboard=True,
sports_ball=True, kite=True, baseball_bat=True, baseball_glove=True, skateboard=True, surfboard=True, tennis_racket=True,
bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True,
broccoli=True, carrot=True, hot_dog=True, pizza=True, donot=True, cake=True, chair=True, couch=True, potted_plant=True, bed=True,
dining_table=True, toilet=True, tv=True, laptop=True, mouse=True, remote=True, keyboard=True, cell_phone=True, microwave=True,
oven=True, toaster=True, sink=True, refrigerator=True, book=True, clock=True, vase=True, scissors=True, teddy_bear=True, hair_dryer=True,
toothbrush=True)
video_path = detector.detectCustomObjectsFromVideo(custom_objects=custom_objects, #指定模型
input_file_path=os.path.join(execution_path+"\\video\\in\\", "holo1.mp4"), #输入输入视频路径
output_file_path=os.path.join(execution_path+"\\video\\out\\", "holo1_1"), #不指定默认为avi格式
frames_per_second=20, #输出视频的帧数
frame_detection_interval=5,#帧间隔 每隔几帧检测一回
log_progress=True) #输出日志
print(video_path)
效果图: