[6] 구글 이미지 크롤러

2019. 12. 18. 23:14· Capstone/Capstone1 - 2019-2학기
목차
  1. 1. python 코드를 통한 이미지 크롤링
  2.  
  3. 2. Google Images Download 라는 python 스크립트를 github에서 가져와 사용하기
  4.  
  5. 참고
반응형

전에 설명하였던 yolo를 통해 자신의 데이터를 학습 시키기 위해 데이터 세트를 수집하는 방법에 대해 설명드리겠습니다.

저와 같은 경우 구글에 이미지 검색을 통해 나오는 이미지를 학습에 사용하였습니다.

구글의 이미지를 크롤링 하는 방법은 두가지 방법을 사용하여봤습니다.

 

1. python 코드를 통한 이미지 크롤링

 

우선 이미지를 크롤링 하기위해선 chrome을 사용시 chromedriver를 설치해주셔야됩니다.

Chrome : https://sites.google.com/a/chromium.org/chromedriver/downloads

 

Downloads - ChromeDriver - WebDriver for Chrome

WebDriver for Chrome

sites.google.com

설치를 완료하셨다면 selenium을 설치해줍니다.

$ pip install selenium

 

설치를 완료하셨다음 다음과 같은 코드를 이용해 크롤링을 해줍니다.

 

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import json
import os
import urllib2
import argparse
 
searchterm = 'pothole' # will also be the name of the folder
url = "https://www.google.co.in/search?q="+searchterm+"&source=lnms&tbm=isch"
# NEED TO DOWNLOAD CHROMEDRIVER, insert path to chromedriver inside parentheses in following line
browser = webdriver.Chrome('/Users/sungjin/Desktop/univ/capstone/chromedriver')
browser.get(url)
header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"}
counter = 0
succounter = 0
 
if not os.path.exists(searchterm):
    os.mkdir(searchterm)
 
for _ in range(500):
    browser.execute_script("window.scrollBy(0,10000)")
 
for x in browser.find_elements_by_xpath('//div[contains(@class,"rg_meta")]'):
    counter = counter + 1
    print "Total Count:", counter
    print "Succsessful Count:", succounter
    print "URL:",json.loads(x.get_attribute('innerHTML'))["ou"]
 
    img = json.loads(x.get_attribute('innerHTML'))["ou"]
    imgtype = json.loads(x.get_attribute('innerHTML'))["ity"]
    try:
        req = urllib2.Request(img, headers={'User-Agent': header})
        raw_img = urllib2.urlopen(req).read()
        File = open(os.path.join(searchterm , searchterm + "_" + str(counter) + "." + imgtype), "wb")
        File.write(raw_img)
        File.close()
        succounter = succounter + 1
    except:
            print "can't get img"
 
print succounter, "pictures succesfully downloaded"
browser.close()

 

위의 코드는 python 를 통해 구글 이미지를 크롤링하는 코드입니다.

다음과 같이 이미지가 다운로드 되어집니다.

 

2. Google Images Download 라는 python 스크립트를 github에서 가져와 사용하기

 

자신이 작업을 원하는 디렉토리로 이동하여 다음의 명령어를 입력해줍니다.

$ git clone https://github.com/hardikvasa/google-images-download.git
$ cd google-images-download && sudo python setup.py install

 

다음 명령어를 입력해준다음 다음의 옵션들을 통하여 google images download라는 스크립트를 실행할수있습니다.

 

이렇게 이미지를 다운로드하게 되면 다음과 같은 google-images-download의 디렉토리 내의 downloads 라는 디렉토리에 다운로드가 됩니다.

 

 

참고

 

 

① 이미지 크롤링

① 이미지 크롤링 (Image Crawling) Google 이미지에서 거미(spider)이미지를 가져옵니다. Google 이미지는 페이지 스크롤 없이 최대 100개만 가져옵니다. 하지만 Selenium을 사용하면 더 많은 이미지를 가져올 수..

jeongmin-lee.tistory.com

 

 

반응형
저작자표시 (새창열림)

'Capstone > Capstone1 - 2019-2학기' 카테고리의 다른 글

[8] yolo custom training  (0) 2019.12.23
[7] 이미지 부풀리기  (0) 2019.12.18
[5] Yolo mark 사용법  (3) 2019.12.18
[4] YOLO 설치 및 실행 방법  (0) 2019.12.13
[3] YOLO 설치전 OpenCV 3.2 설치  (1) 2019.12.13
  1. 1. python 코드를 통한 이미지 크롤링
  2.  
  3. 2. Google Images Download 라는 python 스크립트를 github에서 가져와 사용하기
  4.  
  5. 참고
'Capstone/Capstone1 - 2019-2학기' 카테고리의 다른 글
  • [8] yolo custom training
  • [7] 이미지 부풀리기
  • [5] Yolo mark 사용법
  • [4] YOLO 설치 및 실행 방법
흐성진
흐성진
개발자 흐성진의 블로그 입니다.
반응형
흐성진
망각의 코딩러 흐성진
흐성진
전체
오늘
어제
  • 분류 전체보기 (56) N
    • 📱Flutter (17)
    • 🌐 Web (0)
      • ⚛️ React (0)
      • 🇯🇸‌JavaScript (0)
    • 💡CS (2) N
    • iOS (8)
    • Capstone (22)
      • Capstone1 - 2019-2학기 (13)
      • Caostone2 - 2020-1학기 (9)
    • Linux, Mac (4)
    • Me (3)

인기 글

최근 글

hELLO · Designed By 정상우.v4.2.1
흐성진
[6] 구글 이미지 크롤러
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.