跑kcf算法的MATLAB代碼需要下載Visual Tracking Benchmark數據集,國內對官網的網速狀況不佳,運行代碼自帶的download_video.m下載速度很慢,觀察網頁http://cvlab.hanyang.ac.kr/tracker_benchmark/datasets.html源碼發現,下載鏈接的標籤有統一格式,編寫爬蟲可以很容易實現自動下載。
在Ubuntu上使用多線程下載工具axel, 可以實現快速下載。
# -*- coding: utf-8 -*-
import urllib.request
import re
import os
# open the url and read
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
page.close()
return html
# compile the regular expressions and find
# all stuff we need
def getUrl(html):
reg = r'href="(.*)" target="blank">'
url_re = re.compile(reg)
url_lst = url_re.findall(html.decode('UTF-8'))
return(url_lst)
def getFile(url):
file_name = url.split('/')[-1]
os.system('axel -n 100 '+url)
return
root_url = 'http://cvlab.hanyang.ac.kr/tracker_benchmark/' #下載地址中相同的部分
raw_url = 'http://cvlab.hanyang.ac.kr/tracker_benchmark/datasets.html'
html = getHtml(raw_url)
url_lst = getUrl(html)
print(url_lst)
if not os.path.isfile('benchmark_video'):
os.mkdir('benchmark_video')
os.chdir(os.path.join(os.getcwd(), 'benchmark_video'))
for url in url_lst[:]:
url = root_url + url #形成完整的下載地址
getFile(url)
享受風馳電掣的下載速度吧~