weget() 으로 이미지 데이터 수집

Pear_Mh·2022년 8월 3일
0

1. weget() 으로 이미지 데이터 수집


Open dataset을 weget으로 받는 상황에 대한 데이터 수집 및 정리 방법입니다.

1) wget

Python에서 wget을 사용하기 위한 방법

$ pip install wget
import wget

url = "URL"

wget.download(url, out={output_dir})

2) tarfile

wget으로 받는 파일의 경우, 특히 CV 계열은 용량이 매우 커 압축파일 형태로 제공되는 경우가 많습니다. 이로 인해 tarfile 모듈을 사용하여 압축해제까지 해볼 수 있습니다.

import tarfile

def decompression(path):
    file_path = os.path.join(path,os.listdir(path)[0])
    tar_file = tarfile.open(file_path)
    tar_file.extractall(path)
    tar_file.close()

3) os.remove()

압축 해제 후, 압축파일을 제거하기 위해서는 os.remove 를 사용하면 간단히 해결할 수 있습니다.

2. 완성 코드


"""
Do it once!!
"""

import os
import tarfile
import wget

def file_down(path, url):
    if not os.path.isdir(path):
        os.makedirs(path)
    
    if not os.path.isfile(path):
        wget.download(url,out=path)

def decompression(path):
    file_path = os.path.join(path,os.listdir(path)[0])
    tar_file = tarfile.open(file_path)
    tar_file.extractall(path)
    tar_file.close()
    os.remove(file_path)

if __name__=="__main__":
    
    path = "path"
    url = "url.tar.gz"
    
    if not os.path.isfile(path):
        file_down(path,url)
        decompression(path)
profile
Beyond the new era.

0개의 댓글