|
imageai庫(kù)里面提供了目標(biāo)識(shí)別,其實(shí)也可以說(shuō)是目標(biāo)檢測(cè),和現(xiàn)在很多的收集一樣就是物體識(shí)別。他可以幫你識(shí)別出各種各樣生活中遇見(jiàn)的事物。比如貓、狗、車(chē)、馬、人、電腦、收集等等。 感覺(jué)imageai有點(diǎn)差就是沒(méi)有返回檢測(cè)目標(biāo)的坐標(biāo)出來(lái),所以感覺(jué)很low,而且和計(jì)算消耗很大,耗時(shí)很大,與opencv做實(shí)時(shí)檢測(cè)效果很差。不推薦使用。 安裝imageai方法見(jiàn):https://github.com/OlafenwaMoses/ImageAI resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/ 下面提供兩種調(diào)用方式。 第一文件流調(diào)用: 1 # coding:utf-8 2 # imageai下載地址:https://github.com/OlafenwaMoses/ImageAI 3 # resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/ 4 from imageai.Detection import ObjectDetection # 導(dǎo)入了 ImageAI 目標(biāo)檢測(cè)類(lèi) 5 import os 6 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 7 8 execution_path = os.path.join(os.getcwd(),'imgData/') # 定義了一個(gè)變量用來(lái)保存我們的 python 文件 9 print(execution_path) 10 detector = ObjectDetection() # 定義了目標(biāo)檢測(cè)類(lèi) 11 detector.setModelTypeAsRetinaNet() # 模型的類(lèi)型設(shè)置為 RetinaNet 12 detector.setModelPath(os.path.join(execution_path, "resnet50_coco_best_v2.1.0.h5")) # 將模型路徑設(shè)置為 RetinaNet 模型的路徑 13 detector.loadModel() # 模型加載到的目標(biāo)檢測(cè)類(lèi) 14 # 調(diào)用目標(biāo)檢測(cè)函數(shù),解析輸入的和輸出的圖像路徑。 15 detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, "ji.jpg"), 16 output_image_path=os.path.join(execution_path, "imagenew1.jpg"),input_type='file') 17 18 for eachObject in detections: 19 print(eachObject["name"] + " : " + eachObject["percentage_probability"]) # 打印出所檢測(cè)到的每個(gè)目標(biāo)的名稱(chēng)及其概率值。 20 21 print(detections) 第二種numpy數(shù)據(jù)類(lèi)型調(diào)用: 1 # coding:utf-8 2 # imageai下載地址:https://github.com/OlafenwaMoses/ImageAI 3 # resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/ 4 from imageai.Detection import ObjectDetection # 導(dǎo)入了 ImageAI 目標(biāo)檢測(cè)類(lèi) 5 import cv2 6 import os 7 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 8 import matplotlib.pyplot as plt 9 10 def targetDetection(imgArray,model_path): 11 """ 12 :param imgArray: 圖片數(shù)據(jù),類(lèi)型為ndarray 13 :param model_path: retinanet模型路徑 14 :return: 15 """ 16 path = os.path.abspath(model_path) 17 detector = ObjectDetection() # 定義了目標(biāo)檢測(cè)類(lèi) 18 detector.setModelTypeAsRetinaNet() # 模型的類(lèi)型設(shè)置為 RetinaNet 19 detector.setModelPath(path) # 將模型路徑設(shè)置為 RetinaNet 模型的路徑 20 detector.loadModel() # 模型加載到的目標(biāo)檢測(cè)類(lèi) 21 # 調(diào)用目標(biāo)檢測(cè)函數(shù),解析輸入的和輸出的圖像路徑。 22 detections = detector.detectObjectsFromImage(input_image=imgArray, 23 input_type='array',output_type='array') 24 return detections 25 26 data = plt.imread('./imgData/avenue.jpg') 27 model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5') 28 imgInfo = targetDetection(data,model_path) 29 plt.imshow(imgInfo[0]) 30 plt.show()
下面內(nèi)容作為擴(kuò)展,有興趣的朋友可以試試,但是很不理想。
1 # coding:utf-8 2 # imageai下載地址:https://github.com/OlafenwaMoses/ImageAI 3 # resnet50_coco_best_v2.1.0.h5 模型下載地址:https://github.com/fizyr/keras-retinanet/releases/ 4 from imageai.Detection import ObjectDetection # 導(dǎo)入了 ImageAI 目標(biāo)檢測(cè)類(lèi) 5 import cv2 6 import os 7 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 8 import matplotlib.pyplot as plt 9 10 def targetDetection(imgArray,model_path): 11 """ 12 :param imgArray: 圖片數(shù)據(jù),類(lèi)型為ndarray 13 :param model_path: retinanet模型路徑 14 :return: 15 """ 16 path = os.path.abspath(model_path) 17 detector = ObjectDetection() # 定義了目標(biāo)檢測(cè)類(lèi) 18 detector.setModelTypeAsRetinaNet() # 模型的類(lèi)型設(shè)置為 RetinaNet 19 detector.setModelPath(path) # 將模型路徑設(shè)置為 RetinaNet 模型的路徑 20 detector.loadModel() # 模型加載到的目標(biāo)檢測(cè)類(lèi) 21 # 調(diào)用目標(biāo)檢測(cè)函數(shù),解析輸入的和輸出的圖像路徑。 22 detections = detector.detectObjectsFromImage(input_image=imgArray, 23 input_type='array',output_type='array') 24 return detections 25 26 # data = plt.imread('./imgData/avenue.jpg') 27 # model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5') 28 # imgInfo = targetDetection(data,model_path) 29 # plt.imshow(imgInfo[0]) 30 # plt.show() 31 32 33 if __name__=='__main__': 34 # 獲取攝像頭0表示第一個(gè)攝像頭 35 model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5') 36 cap = cv2.VideoCapture(0) 37 while (True): # 逐幀顯示 38 ret, img = cap.read() # 強(qiáng)調(diào)img是ndarray類(lèi)型的。 39 imgData=targetDetection(img,model_path) 40 cv2.imshow('image',imgData[0]) 41 if cv2.waitKey(1) & 0xFF == ord(' '): 42 break 43 cap.release() # 釋放攝像頭 44 cv2.destroyAllWindows() # 釋放窗口資源
|
|
|
來(lái)自: 看見(jiàn)就非常 > 《待分類(lèi)》