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)
效果圖:





轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282839.html
標籤:AI
上一篇:YOLO演算法之YOLOv3精講
下一篇:二叉樹,堆詳解
