无法使用python 2.7

时间:2017-08-22 14:46:19

标签: python python-2.7 opencv

我是Python的新手。我想一次下载并重新缩放许多图像,因此我可以将它们用作负样本来训练一个haar级联。也许我在我的程序中混合了一些python 3.0代码。我收到以下错误:

Traceback (most recent call last):
File "D:/!Servici/Python/Remake/negative_grab.py", line 26, in <module> store_raw_images()
File "D:/!Servici/Python/Remake/negative_grab.py", line 7, in store_raw_images
neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4837: ordinal not in range(128)
Process finished with exit code 1

我的代码:

import urllib2
import cv2
import os

def store_raw_images():
    neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n04335209'
    neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()

    if not os.path.exists('neg_sample'):
        os.makedirs('neg_samples')

    pic_num = 1

    for i in neg_image_urls.split('\n'):
        try:
            print(i)
            urllib2.urlretrieve(i, "neg/"+str(pic_num)+'.jpg')
            img = cv2.imread("neg/"+str(pic_num)+'.jpg', cv2.IMREAD_GRAYSCALE)
            resize_image = cv2.resize(img, (100,100))
            cv2.imwrite(("neg/")+str(pic_num)+'.jpg', resize_image)
            pic_num += 1

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

    store_raw_images()

1 个答案:

答案 0 :(得分:0)

您的解码遇到了错误。试试decode('utf-8')