資源簡介
基于YOLOv3的行人檢測

代碼片段和文件信息
#?coding=utf-8
#?使用說明
#?需要先安裝coco?tools
#?git?clone?https://github.com/pdollar/coco.git
#?cd?coco/PythonAPI
#?make?install(可能會缺少相關依賴,根據提示安裝依賴即可)
#?執行腳本前需在train2014和val2014目錄下分別創建JPEGImages和labels目錄,并將原來train2014和val2014目錄下的圖片移到JPEGImages下
#?COCO數據集的filelist目錄下會生成圖片路徑列表
#?COCO數據集的子集的labels目錄下會生成yolo需要的標注文件
from?pycocotools.coco?import?COCO
import?shutil
import?os
#?將ROI的坐標轉換為yolo需要的坐標
#?size是圖片的w和h
#?box里保存的是ROI的坐標(x,y的最大值和最小值)
#?返回值為ROI中心點相對于圖片大小的比例坐標,和ROI的w、h相對于圖片大小的比例
def?convert(size?box):
????dw?=?1.?/?size[0]
????dh?=?1.?/?size[1]
????x?=?box[0]?+?box[2]?/?2.0
????y?=?box[1]?+?box[3]?/?2.0
????w?=?box[2]
????h?=?box[3]
????x?=?x?*?dw
????w?=?w?*?dw
????y?=?y?*?dh
????h?=?h?*?dh
????return?(x?y?w?h)
#?獲取所需要的類名和id
#?path為類名和id的對應關系列表的地址(標注文件中可能有很多類,我們只加載該path指向文件中的類)
#?返回值是一個字典,鍵名是類名,鍵值是id
def?get_classes_and_index(path):
????D?=?{}
????f?=?open(path)
????for?line?in?f:
????????temp?=?line.rstrip().split(‘‘?2)
????????print(“temp[0]:“?+?temp[0]?+?“\n“)
????????print(“temp[1]:“?+?temp[1]?+?“\n“)
????????D[temp[1]]?=?temp[0]
????return?D
dataDir?=?‘/home/pascal/COCO‘??#?COCO數據集所在的路徑
dataType?=?‘train2017‘??#?要轉換的COCO數據集的子集名
annFile?=?‘%s/annotations/instances_%s.json‘?%?(dataDir?dataType)??#?COCO數據集的標注文件路徑
classes?=?get_classes_and_index(‘/home/pascal/COCO/coco_list.txt‘)
#?labels?目錄若不存在,創建labels目錄。若存在,則清空目錄
if?not?os.path.exists(‘%s/%s/labels/‘?%?(dataDir?dataType)):
????os.makedirs(‘%s/%s/labels/‘?%?(dataDir?dataType))
else:
????shutil.rmtree(‘%s/%s/labels/‘?%?(dataDir?dataType))
????os.makedirs(‘%s/%s/labels/‘?%?(dataDir?dataType))
#?filelist?目錄若不存在,創建filelist目錄。
if?not?os.path.exists(‘%s/filelist/‘?%?dataDir):
????os.makedirs(‘%s/filelist/‘?%?dataDir)
coco?=?COCO(annFile)??#?加載解析標注文件
list_file?=?open(‘%s/filelist/%s.txt‘?%?(dataDir?dataType)?‘w‘)??#?數據集的圖片list保存路徑
imgIds?=?coco.getImgIds()??#?獲取標注文件中所有圖片的COCO?Img?ID
catIds?=?coco.getCatIds()??#?獲取標注文件總所有的物體類別的COCO?Cat?ID
for?imgId?in?imgIds:
????objCount?=?0??#?一個標志位,用來判斷該img是否包含我們需要的標注
????print(‘imgId?:%s‘?%?imgId)
????Img?=?coco.loadImgs(imgId)[0]??#?加載圖片信息
????print(‘Img?:%s‘?%?Img)
????filename?=?Img[‘file_name‘]??#?獲取圖片名
????width?=?Img[‘width‘]??#?獲取圖片尺寸
????height?=?Img[‘height‘]??#?獲取圖片尺寸
????print(‘filename?:%s?width?:%s?height?:%s‘?%?(filename?width?height))
????annIds?=?coco.getAnnIds(imgIds=imgId?catIds=catIds?iscrowd=None)??#?獲取該圖片對應的所有COCO物體類別標注ID
????print(‘annIds?:%s‘?%?annIds)
????for?annId?in?annIds:
????????anns?=?coco.loadAnns(annId)[0]??#?加載標注信息
????????catId?=?anns[‘category_id‘]??#?獲取該標注對應的物體類別的COCO?Cat?ID
????????cat?=?coco.loadCats(catId)[0][‘name‘]??#?獲取該COCO?Cat?ID對應的物體種類名
????????#?print?‘anns?:%s‘?%?anns
????????#?print?‘catId?:%s??cat?:%s‘?%?(catIdcat)
????????#?如果該類名在我們需要的物體種類列表中,將標注文件轉換為YOLO需要的格式
????????if?cat?in?classes:
????????????objCount?=?objCount?+?1
????????????out_file?=?open(‘%s/%s/labels/%s.txt‘?%?(dataDir?dataType?filename[:-4])?‘a‘)
????????????cls_id?=?classes[cat]??#?獲取該類物體在yolo訓練中的id
????????????box?=?ann
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\
?????文件???????11357??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\LICENSE
?????文件????????9207??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\README.md
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\images\
?????文件??????415034??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\images\kite-7-final.jpg
?????文件??????426628??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\images\kite-pj.jpg
?????文件???????27957??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\images\loss_analyse_result.png
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\YOLO_VOC2007\
?????文件????????2521??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\YOLO_VOC2007\extract_person.py
?????文件????????2075??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\YOLO_VOC2007\voc_label.py
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\YOLO_VOC2007_2012\
?????文件????????2521??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\YOLO_VOC2007_2012\extract_person_2007.py
?????文件????????2422??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\YOLO_VOC2007_2012\extract_person_2012.py
?????文件????????2093??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\YOLO_VOC2007_2012\voc_label.py
?????文件????????4270??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\coco_to_yolo.py
?????文件????????1611??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\helmet_to_yolo.py
?????文件????????2059??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\show_voc_xm
?????文件????????2026??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\show_yolo_label.py
?????文件????????6187??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\make_yolo_dataset\xm
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\yolo_compute_mAP_on_VOC2007\
?????文件????????3529??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\yolo_compute_mAP_on_VOC2007\reval_voc_py3.py
?????文件????????6994??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\yolo_compute_mAP_on_VOC2007\voc_eval_py3.py
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\yolo_loss_analyse\
?????文件????????2140??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\yolo_loss_analyse\analyse.py
?????目錄???????????0??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\yolo_loss_analyse\loss\
?????文件?????3425747??2018-10-25?10:58??pascal1129-yolo_person_detect-da3d03c\yolo_loss_analyse\loss\train7-loss.txt
評論
共有 條評論