目錄
- 什么是目標(biāo)檢測(cè)
-
目標(biāo)檢測(cè)算法
- Two Stages
- One Stage
-
python實(shí)現(xiàn)
- 依賴(lài)
- 安裝
- 使用
- 附錄
什么是目標(biāo)檢測(cè)
目標(biāo)檢測(cè)關(guān)注圖像中特定的物體目標(biāo),需要同時(shí)解決解決定位(localization) + 識(shí)別(Recognition)。相比分類(lèi),檢測(cè)給出的是對(duì)圖片前景和背景的理解,我們需要從背景中分離出感興趣的目標(biāo),并確定這一目標(biāo)的描述(類(lèi)別和位置),因此檢測(cè)模型的輸出是一個(gè)列表,列表的每一項(xiàng)使用一個(gè)數(shù)組給出檢出目標(biāo)的類(lèi)別和位置(常用矩形檢測(cè)框的坐標(biāo)表示)。
通俗的說(shuō),Object Detection的目的是在目標(biāo)圖中將目標(biāo)用一個(gè)框框出來(lái),并且識(shí)別出這個(gè)框中的是啥,而且最好的話(huà)是能夠?qū)D片的所有物體都框出來(lái)。
目標(biāo)檢測(cè)算法
目前目標(biāo)檢測(cè)領(lǐng)域的深度學(xué)習(xí)方法主要分為兩類(lèi):兩階段(Two Stages)的目標(biāo)檢測(cè)算法;一階段(One Stage)目標(biāo)檢測(cè)算法。
Two Stages
首先由算法(algorithm)生成一系列作為樣本的候選框,再通過(guò)卷積神經(jīng)網(wǎng)絡(luò)進(jìn)行樣本(Sample)分類(lèi)。也稱(chēng)為基于候選區(qū)域(Region Proposal)的算法。常見(jiàn)的算法有R-CNN、Fast R-CNN、Faster R-CNN等等。
One Stage
不需要產(chǎn)生候選框,直接將目標(biāo)框定位的問(wèn)題轉(zhuǎn)化為回歸(Regression)問(wèn)題處理,也稱(chēng)為基于端到端(End-to-End)的算法。常見(jiàn)的算法有YOLO、SSD等等。
python實(shí)現(xiàn)
本文主要講述如何實(shí)現(xiàn)目標(biāo)檢測(cè),至于背后的原理不過(guò)多贅述,可以去看相關(guān)的論文。
ImageAI是一個(gè)簡(jiǎn)單易用的計(jì)算機(jī)視覺(jué)Python庫(kù),使得開(kāi)發(fā)者可以輕松的將最新的最先進(jìn)的人工智能功能整合進(jìn)他們的應(yīng)用。
ImageAI本著簡(jiǎn)潔的原則,支持最先進(jìn)的機(jī)器學(xué)習(xí)算法,用于圖像預(yù)測(cè),自定義圖像預(yù)測(cè),物體檢測(cè),視頻檢測(cè),視頻對(duì)象跟蹤和圖像預(yù)測(cè)訓(xùn)練。
依賴(lài)
- Python 3.5.1(及更高版本)
- pip3
- Tensorflow 1.4.0(及更高版本)
- Numpy 1.13.1(及更高版本)
- SciPy 0.19.1(及更高版本)
- OpenCV
- pillow
- Matplotlib
- h5py
- Keras 2.x
安裝
- 命令行安裝
pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.1/imageai-2.0.1-py3-none-any.whl
- 下載imageai-2.1.0-py3-none-any.whl 安裝文件并在命令行中指定安裝文件的路徑
pip3 install .\imageai-2.1.0-py3-none-any.whl
使用
Image支持的深度學(xué)習(xí)的算法有 RetinaNet , YOLOv3 , TinyYoLOv3 。ImageAI已經(jīng)在 COCO數(shù)據(jù)集 上預(yù)先訓(xùn)練好了對(duì)應(yīng)的三個(gè)模型,根據(jù)需要可以選擇不同的模型。可以通過(guò)下面的鏈接進(jìn)行下載使用:
- Download RetinaNet Model - resnet50_coco_best_v2.0.1.h5
- Download YOLOv3 Model - yolo.h5
- Download TinyYOLOv3 Model - yolo-tiny.h5
以上模型可以檢測(cè)并識(shí)別以下80種不同的目標(biāo):
person, bicycle, car, motorcycle, airplane,
bus, train, truck, boat, traffic light, fire hydrant, stop_sign,
parking meter, bench, bird, cat, dog, horse, sheep, cow,
elephant, bear, zebra, giraffe, backpack, umbrella,
handbag, tie, suitcase, frisbee, skis, snowboard,
sports ball, kite, baseball bat, baseball glove, skateboard,
surfboard, tennis racket, bottle, wine glass, cup, fork, knife,
spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot,
hot dog, pizza, donot, cake, chair, couch, potted plant, bed,
dining table, toilet, tv, laptop, mouse, remote, keyboard,
cell phone, microwave, oven, toaster, sink, refrigerator,
book, clock, vase, scissors, teddy bear, hair dryer,
toothbrush
先來(lái)看看完整的代碼,使用YOLOv3算法對(duì)13張照片進(jìn)行目標(biāo)識(shí)別。
from imageai.Detection import ObjectDetection
import os
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("./model/yolo.h5")
detector.loadModel()
path = os.getcwd()
input_image_list = os.listdir(path+"\pic\input")
input_image_list = sorted(input_image_list, key = lambda i:len(i),reverse = False)
size = len(input_image_list)
for i in range(size):
input_image_path = os.path.join(path+"\pic\input", input_image_list[i])
output_image_path = os.path.join(path+"\pic\output", input_image_list[i])
detections, extract_detected_objects = detector.detectObjectsFromImage(input_image=input_image_path,
output_image_path=output_image_path,
extract_detected_objects=True)
print('------------------- %d -------------------' % int(i + 1))
for eachObject in detections:
print(eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"])
print('------------------- %d -------------------' % int(i + 1))
首先第一行導(dǎo)入
ImageAI Object Detection
類(lèi),在第二行導(dǎo)入
os
庫(kù)。
然后創(chuàng)建了ObjectDetection類(lèi)的新實(shí)例,接著就可以選擇要使用的算法。分別有以下三個(gè)函數(shù):
.setModelTypeAsRetinaNet()
.setModelTypeAsYOLOv3()
.setModelTypeAsTinyYOLOv3()
選擇好算法之后就要設(shè)置模型文件路徑,這里給出的路徑必須要和選擇的算法一樣。
.setModelPath()
- 參數(shù)path(必須):模型文件的路徑
載入模型。
.loadModel()
- 參數(shù)detection_speed(可選):最多可以減少80%的時(shí)間,但是會(huì)導(dǎo)致精確度的下降。可選的值有: “normal”, “fast”, “faster”, “fastest” 和 “flash”。默認(rèn)值是 “normal”。
通過(guò)os庫(kù)的函數(shù)得到輸入輸出文件的路徑等,這不是本文重點(diǎn),跳過(guò)不表。
開(kāi)始對(duì)圖像進(jìn)行目標(biāo)檢測(cè)。
.detectObjectsFromImage()
- 參數(shù)input_image(必須):待檢測(cè)圖像的路徑
- 參數(shù)output_image(必須):輸出圖像的路徑
- 參數(shù)parameter minimum_percentage_probability(可選):能接受的最低預(yù)測(cè)概率。默認(rèn)值是50%。
- 參數(shù)display_percentage_probability(可選):是否展示預(yù)測(cè)的概率。默認(rèn)值是True。
- 參數(shù)display_object_name(可選):是否展示識(shí)別物品的名稱(chēng)。默認(rèn)值是True。
- 參數(shù)extract_detected_objects(可選):是否將識(shí)別出的物品圖片保存。默認(rèn)是False。
返回值根據(jù)不同的參數(shù)也有不同,但都會(huì)返回一個(gè)an array of dictionaries。字典包括以下幾個(gè)屬性:
* name (string)
* percentage_probability (float)
* box_points (tuple of x1,y1,x2 and y2 coordinates)
前面說(shuō)過(guò)可以識(shí)別80種目標(biāo),在這里也可以選擇只識(shí)別自己想要的目標(biāo)。
custom = detector.CustomObjects(person=True, dog=True)
detections = detector.detectCustomObjectsFromImage( custom_objects=custom, input_image=os.path.join(execution_path , "image3.jpg"), output_image_path=os.path.join(execution_path , "image3new-custom.jpg"), minimum_percentage_probability=30)
首先用定義自己想要的目標(biāo),其余的目標(biāo)會(huì)被設(shè)置為False。然后配合
.detectCustomObjectsFromImage()
進(jìn)行目標(biāo)檢測(cè)。
主要的代碼基本如上所述,接下來(lái)看結(jié)果。先看看圖片中只有一個(gè)目標(biāo)的效果。
------------------- 10 -------------------
dog : 98.83476495742798 : (117, 91, 311, 360)
dog : 99.24255609512329 : (503, 133, 638, 364)
dog : 99.274742603302 : (338, 38, 487, 379)
------------------- 10 -------------------
效果還是不錯(cuò)的。再看看如果圖片中有多個(gè)目標(biāo)識(shí)別的結(jié)果如何。
------------------- 4 -------------------
book : 55.76887130737305 : (455, 74, 487, 146)
book : 82.22097754478455 : (466, 11, 482, 69)
tv : 99.34800863265991 : (25, 40, 182, 161)
bed : 88.7190580368042 : (60, 264, 500, 352)
cat : 99.54025745391846 : (214, 125, 433, 332)
------------------- 4 -------------------
識(shí)別度還是很高的,背后人眼都看不清的書(shū)本都能被識(shí)別。
附錄
GitHub:https://github.com/Professorchen/Computer-Vision/tree/master/object-detection
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
