YOLO3訓練自己標註的數據 將檢測到的目標保存爲圖像
使用darknet下的python/darknet.py遇到的問題
- 找不到libdarknet.so文件
將so文件放在darknet.py文件同目錄下,或在lib=CDLL()中寫so的全路徑名 - load_net參數不正確
darknet使用C/C++編寫,在調用load_net,輸入字符參數時,要在前面加b以表示類型,如b’cfg/yolov3-voc.cfg’ - 找不到配置文件
我在pycharm中執行,找不到相關文件,是因爲工作目錄不在darknet文件夾下,在pycharm中修改Working directory爲darknet所在目錄
檢測區域
-
檢測函數
net = load_net(b'cfg', b'weights', 0) meta = load_meta(b'data') r = dectect(net, meta, b'test.png')
r [(b’text’, 0.8, (600, 700, 170, 25))]
r[0][0] b’text’ 類別名
r[0][1] 0.8 類別可能性
r[0][2][0] 600 b.x 中心點x座標
r[0][2][0] 700 b.y 中心點y座標
r[0][2][0] 170 b.w 目標區域 寬度
r[0][2][3] 25 b.h 目標區域 高度 -
使用open cv保存檢測區域
def save_region(im_path, x, y, width, height): image = cv2.imread(im_path) cropped = imge[int(y - height / 2) : int(y + height /2), int(x - width / 2):int(x + width /2)] cv2.imwrite('save.jpg', cropped)
opencv 保存圖像是用[y:y+height, x:x+width]來處理的
讀取圖像,截取[高度,寬度] -
使用PIL庫保存圖像
def save_pil(im_path, x, y, width, heigth): image = Image.open(file) # cropped = image.crop[x0, y0, x1, y1] cropped = image.crop((int(x - width / 2), int(y - height / 2), int(x + width /2), int(y + height /2)))
Image圖像以左上角爲(0,0)點
-
opencv保存的圖像比pillow保存圖像體積大一些