从image-net.org下载图像的python代码,用于haar级联训练

时间:2017-08-01 20:10:23

标签: python opencv numpy

我有一个用于从“www.image-net.org”下载图像的python代码,用于haar级联训练。基本上它会检查每个图像网址并下载图像。

import urllib2  
import cv2
import numpy as np
import os
import urllib
import sys


reload(sys)
sys.setdefaultencoding('utf8')


def store_raw_images():

    pos_images_link = 'http://www.image-net.org/api/text/imagenet.synset.geturls?wnid=n04154340'   
    pos_image_urls = urllib2.urlopen(pos_images_link).read().decode()



    if not os.path.exists('pos'):
        os.makedirs('pos')

    pic_num = 1
    for i in pos_image_urls.split('\n'):
        try:
            print(i)
            urllib.urlretrieve(i, "pos/"+str(pic_num)+".jpg")
            img = cv2.imread("pos/"+str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
            # should be larger than samples / pos pic (so we can place our image on it)
            resized_image = cv2.resize(img, (100, 100))
            cv2.imwrite("pos/"+str(pic_num)+".jpg",resized_image)
            pic_num += 1

        except Exception as e:
                print(str(e))  
store_raw_images()

我复制粘贴url链接以在“pos_images_link”中下载,但代码只检查5张图片的网址,然后代码停止运行并在终端中显示一条消息:

"terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 140) > this->size() (which is 0)"

,我使用的是opencv 3.1.0和python 2.7.12

1 个答案:

答案 0 :(得分:1)

以下代码在Python 3中使用opencv

from urllib.request import Request, urlretrieve
import cv2
import numpy as np
import os
import urllib
import sys

def store_raw_images():
    url = 'http://www.image-net.org/api/text/imagenet.synset.geturls?wnid=n04154340'
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    urls = response.read().decode('utf-8')

    if not os.path.exists('pos'):
        os.makedirs('pos')

    pic_num = 1
    for i in urls.split('\n'):
        try:
            print(i)
            urlretrieve(i, "pos/"+str(pic_num)+".jpg")
            img = cv2.imread("pos/"+str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
            # should be larger than samples / pos pic (so we can place our image on it)
            resized_image = cv2.resize(img, (100, 100))
            cv2.imwrite("pos/"+str(pic_num)+".jpg",resized_image)
            pic_num += 1

        except Exception as e:
            print(str(e))  

store_raw_images()