본문 바로가기
NaverBoost Camp 4기/[P stage] Final Project

[P stage][Final Proj] Data Prepare (gdown, ZipFile)

by 하람 Haram 2023. 1. 10.
728x90
사용한 데이터

https://aihub.or.kr/aihubdata/data/view.do?currMenu=115&topMenu=100&aihubDataSe=realm&dataSetSn=154 

 

AI-Hub

Json 형식 { "categories": [{ "id": int, "name": str, "supercategories": str }], "images": [{ "id": int, "file_name": str, "width": int, "height": int, "date_captured": datetime, "light": str }], "annotations": [{ "id": int, "image_id": int, "category_id"

aihub.or.kr

 

 

 

Gdown을 이용하여 데이터 공유

 

데이터가 많기도 하고 여러명이 다 다운로드 받고 github로 공유하기엔 적합하지 않다고 판단되어

gdown을 이용하여서 데이터를 공유하고자 한다

 

먼저 공유 문서함에 Dataset을 업로드 해준다

그 다음으로는 gdown 라이브러리를 다운로드 받는다

pip install gdown

 

주의점

gdown은 폴더 형식은 다운로드가 불가능하다고 하여 zip파일로 올리고 이를 푸는 과정이 필요하다고 한다

New_sample.zip 파일을 다시 올려주었다

 

다운로드 받고 싶은 zip파일을 더블 클릭을 하면 아래와 같은 화면이 나오는데

오른쪽 상단의 점 세개를 눌러 (더보기) 새 창에서 열기를 눌러준다

주소의 저 위의 드래그한 부분이 id가 되는데 저 부분을 복사한다

(만약 제대로 안 보일 경우 아래와 같은 과정 진행)

오른쪽 상단의 다운로드 버튼을 눌러주고

id= 뒤에서 부터 &export 앞 부분 까지 복사를 해준 다음 아래의 file_id 부분에 넣어준다

google_path = 'https://drive.google.com/uc?id=' #고정
file_id = '15o-JrJBQRbJ4tqHwzcY1NCabcTR3DpDy'  #이 부분만 수정해주기
output_name = 'New_sample.zip'
gdown.download(google_path+file_id,output_name,quiet=False)

current_workspace = os.getcwd() #현재 디렉토리 경로를 가져온다

with zipfile.ZipFile(current_workspace +"/" + output_name) as z:
    z.extractall()

output_name 즉, 다운로드 했을 때의 이름을 지정해주고 코드를 실행해주면  아래코드로 인해 압축까지 풀어진다

 

오류 상황 1 -> 접근권한 문제

접근 권한이 없다고 뜬다

저기 공유 탭에서

일반 액세스를 전체공개로 바꾸면 된다

잘 다운로드 받아지긴 하는데

이거 파일이름 왜 이래 ㅋㅋㅋㅋㅋㅋㅋㅋㅋ

 

알고 보니 한글로 파일명이 되어 있어서 이 부분을 다음과 같이 수정을 하였다

google_path = 'https://drive.google.com/uc?id=' #고정
file_id = '15o-JrJBQRbJ4tqHwzcY1NCzcbTR3BpDy'  #이 부분만 수정해주기
output_name = 'New_sample.zip'
gdown.download(google_path+file_id,output_name,quiet=False)

current_workspace = os.getcwd() #현재 디렉토리 경로를 가져온다

#파일명이 영어일 경우
#with zipfile.ZipFile(current_workspace +"/" + output_name) as z:
    #z.extractall() 

#파일명이 한글일 경우
with zipfile.ZipFile(current_workspace +"/" + output_name) as z:
    #z.extractall() #
	zipinfo = z.infolist()
	for info in zipinfo:
		info.filename = info.filename.encode('cp437').decode('euc-kr')
		z.extract(info)

데이터 준비완료

 

728x90