在 Colab 上用 YOLO v7 判讀照片, 並將結果存到 Google Drive

文章目錄

程式

試玩一下, 在 Google Colab 上, 用 YOLOv7 做簡單的照片判讀, 並將結果存回 Google Drive. 程式碼如下, 他會直接判讀內建的 inference/images/horses.jpg, 然後把結果寫回 Google Drive 的根目錄.

 1# Connect to Google Drive
 2from google.colab import drive
 3drive.mount('/content/drive')
 4
 5# Download YOLOv7
 6!git clone https://github.com/WongKinYiu/yolov7
 7%cd /content
 8%ll
 9
10# Install YOLOv7 related libraries
11%cd yolov7
12!pip install -r requirements.txt
13!wget "https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt"
14
15# Analyze the image
16!python detect.py --weights yolov7-e6e.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
17
18# Copy the image to Google Drive
19%cp runs/detect/exp/horses.jpg /content/drive/MyDrive
20%ll /content/drive/MyDrive/horses.jpg

下面是把 Colab 上的 Jupyter notebook 直接存到 GitHub Gist 上, 可直接點 Open in Colab, 就可以直接打開來測試了.

測試結果

這是內建的照片測試結果, 看來還不錯.

這是 pixabay 上找來的免費照片, 貓和狗都找到了, 但蝴蝶不知道是不是太小被忽略. (圖片來源: https://pixabay.com/photos/2222007/)

這也是 pixabay 上找來的免費照片, 有些動物(犀牛猩猩河馬...)就沒找到了. (圖片來源: https://pixabay.com/photos/6939895/)

這也是 pixabay 上找來的免費照片, 這張動物更多, 沒找到的也更多了. (圖片來源: https://pixabay.com/photos/6963987/)

再來用 AI 產生出來的照片試試看, 給 AI 下的指令是"貓,狗,兔,鹿,長頸鹿,大象. 有些動物的比例很怪 (可能我找的免費 AI 產生器還不夠厲害), 譬如中間那個是兔子, 可能是高度的關係, 被判讀成是長頸鹿. 左下角那兩個是貓, 但長得還真的很奇怪.

這也是用 AI 產生出來的照片, 中間那個是鹿, 不過被判別成長頸鹿.

Posts in this Series