python imageai 對象檢測、對象識別


  imageai庫里面提供了目標識別,其實也可以說是目標檢測,和現在很多的收集一樣就是物體識別。他可以幫你識別出各種各樣生活中遇見的事物。比如貓、狗、車、馬、人、電腦、收集等等。

  感覺imageai有點差就是沒有返回檢測目標的坐標出來,所以感覺很low,而且和計算消耗很大,耗時很大,與opencv做實時檢測效果很差。不推薦使用。

  安裝imageai方法見:https://github.com/OlafenwaMoses/ImageAI

  resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/

下面提供兩種調用方式。

  第一文件流調用:

# coding:utf-8
#  imageai下載地址:https://github.com/OlafenwaMoses/ImageAI
#  resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/
from imageai.Detection import ObjectDetection  # 導入了 ImageAI 目標檢測類
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

execution_path = os.path.join(os.getcwd(),'imgData/')  # 定義了一個變量用來保存我們的 python 文件
print(execution_path)
detector = ObjectDetection()  # 定義了目標檢測類
detector.setModelTypeAsRetinaNet()  # 模型的類型設置為 RetinaNet
detector.setModelPath(os.path.join(execution_path, "resnet50_coco_best_v2.1.0.h5"))  # 將模型路徑設置為 RetinaNet 模型的路徑
detector.loadModel()  # 模型加載到的目標檢測類
# 調用目標檢測函數,解析輸入的和輸出的圖像路徑。
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, "ji.jpg"),
                                             output_image_path=os.path.join(execution_path, "imagenew1.jpg"),input_type='file')

for eachObject in detections:
    print(eachObject["name"] + " : " + eachObject["percentage_probability"])  # 打印出所檢測到的每個目標的名稱及其概率值。

print(detections)

  

  第二種numpy數據類型調用:

# coding:utf-8
#  imageai下載地址:https://github.com/OlafenwaMoses/ImageAI
#  resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/
from imageai.Detection import ObjectDetection  # 導入了 ImageAI 目標檢測類
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt

def targetDetection(imgArray,model_path):
    """
    :param imgArray: 圖片數據,類型為ndarray
    :param model_path: retinanet模型路徑
    :return:
    """
    path = os.path.abspath(model_path)
    detector = ObjectDetection()  # 定義了目標檢測類
    detector.setModelTypeAsRetinaNet()  # 模型的類型設置為 RetinaNet
    detector.setModelPath(path)  # 將模型路徑設置為 RetinaNet 模型的路徑
    detector.loadModel()  # 模型加載到的目標檢測類
    # 調用目標檢測函數,解析輸入的和輸出的圖像路徑。
    detections = detector.detectObjectsFromImage(input_image=imgArray,
                                                 input_type='array',output_type='array')
    return detections

data = plt.imread('./imgData/avenue.jpg')
model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')
imgInfo = targetDetection(data,model_path)
plt.imshow(imgInfo[0])
plt.show()

  

下面內容作為擴展,有興趣的朋友可以試試,但是很不理想。

# coding:utf-8
#  imageai下載地址:https://github.com/OlafenwaMoses/ImageAI
#  resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/
from imageai.Detection import ObjectDetection  # 導入了 ImageAI 目標檢測類
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt

def targetDetection(imgArray,model_path):
    """
    :param imgArray: 圖片數據,類型為ndarray
    :param model_path: retinanet模型路徑
    :return:
    """
    path = os.path.abspath(model_path)
    detector = ObjectDetection()  # 定義了目標檢測類
    detector.setModelTypeAsRetinaNet()  # 模型的類型設置為 RetinaNet
    detector.setModelPath(path)  # 將模型路徑設置為 RetinaNet 模型的路徑
    detector.loadModel()  # 模型加載到的目標檢測類
    # 調用目標檢測函數,解析輸入的和輸出的圖像路徑。
    detections = detector.detectObjectsFromImage(input_image=imgArray,
                                                 input_type='array',output_type='array')
    return detections

# data = plt.imread('./imgData/avenue.jpg')
# model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')
# imgInfo = targetDetection(data,model_path)
# plt.imshow(imgInfo[0])
# plt.show()


if __name__=='__main__':
    # 獲取攝像頭0表示第一個攝像頭
    model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')
    cap = cv2.VideoCapture(0)
    while (True):  # 逐幀顯示
        ret, img = cap.read() # 強調img是ndarray類型的。
        imgData=targetDetection(img,model_path)
        cv2.imshow('image',imgData[0])
        if cv2.waitKey(1) & 0xFF == ord(' '):
            break
    cap.release()  # 釋放攝像頭
    cv2.destroyAllWindows()  # 釋放窗口資源

打開本地攝像頭進行實時檢測

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM