iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 22
0
Google Developers Machine Learning

「Google Machine Learning」學習筆記系列 第 22

物件偵測Yolo筆記(4) - Colab Demo 2

昨天我們把 darknet 編譯好了,所以今天再來跑就不用再編譯了,只要把昨天的檔案複製回來就好。

!cp /content/gdrive/My\ Drive/darknet/bin/darknet ./darknet
!chmod +x ./darknet

第5步: 定義副程式 imShow/upload/download

然後準備幾個我們等一下會用到呼叫的程式。

  • imShow 用來顯示辨識完的圖
  • upload 用來上傳要辨識的圖
  • download 用來下載辨識完的圖
def imShow(path):
  import cv2
  import matplotlib.pyplot as plt
  %matplotlib inline

  image = cv2.imread(path)
  height, width = image.shape[:2]
  resized_image = cv2.resize(image,(3*width, 3*height), interpolation = cv2.INTER_CUBIC)

  fig = plt.gcf()
  fig.set_size_inches(18, 10)
  plt.axis("off")
  #plt.rcParams['figure.figsize'] = [10, 5]
  plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB))
  plt.show()
def upload():
  from google.colab import files
  uploaded = files.upload() 
  for name, data in uploaded.items():
    with open(name, 'wb') as f:
      f.write(data)
      print ('saved file', name)
def download(path):
  from google.colab import files
  files.download(path)

第6步: 下載 yolov3.weights

把預訓練權重下載過來。

!wget https://pjreddie.com/media/files/yolov3.weights

第7步: 辨識 person.jpg

他的 Data 目錄裡有幾張圖片可以測試,先試看看辨識他自己準備好的圖片 person.jpg。

!./darknet detect cfg/yolov3.cfg yolov3.weights data/person.jpg -dont-show
imShow('predictions.jpg')

我們看到他辨識出3個物體,還有他的機率。
dog: 99%
person: 100%
horse: 100%

第8步: 上傳自己的圖片 mytest.jpg

再來上傳我們自己的圖片。

upload()

第9步: 辨識自己的圖片 mytest.jpg

試看看辨識我們自己的圖片 mytest.jpg。

!./darknet detect cfg/yolov3.cfg yolov3.weights mytest.jpg -dont-show
imShow('predictions.jpg')

我們看到他辨識出4個物體,還有他的機率。
keyboard: 98%
cell phone: 33%
mouse: 100%
cup: 96%

第10步: 下載辨識完的圖片 predictions.jpg

最後你可以把檔案下載回來。

download("predictions.jpg")

以上就是在 Google Colab 用 yolo 辨識圖片的過程啦,有沒有很簡單。
有機會再跟大家聊聊影片物件偵測的部份。

好,第22天,結束。

/images/emoticon/emoticon01.gif


上一篇
物件偵測Yolo筆記(3) - Colab Demo 1
下一篇
GAN Lab(1) - GAN 簡介
系列文
「Google Machine Learning」學習筆記31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言