# How to run image classification on image url

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604%3E%5C
**Category:** 🤗Datasets\
**Created:** [July 20, 2022, 5:59am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604 "2022-07-20T05:59:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheNoob3131](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TheNoob3131](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FTheNoob3131)%5C
**Post date:** [July 20, 2022, 5:59am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604%2F1 "2022-07-20T05:59:53Z")

</div>

My dataset has all of its photos as jpg urls, which are all strings. How can I run an image classification model like base ViT or ResNet-50 to convert the string to images?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [July 20, 2022, 9:23am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604%2F2 "2022-07-20T09:23:28Z")

</div>

I usually use the requests module for that (which is part of Python):

```auto
from PIL import Image
import requests

url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
image

```

---

<div class="post-metadata">

**Author:** ![TheNoob3131](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TheNoob3131](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FTheNoob3131)%5C
**Post date:** [July 20, 2022, 5:19pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604%2F3 "2022-07-20T17:19:45Z")

</div>

So would I have to define a function to go through all of the rows in the ‘Image’ column? How would I accomplish that?

---

<div class="post-metadata">

**Author:** ![TheNoob3131](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@TheNoob3131](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FTheNoob3131)%5C
**Post date:** [July 20, 2022, 5:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604%2F4 "2022-07-20T17:21:04Z")

</div>

Or is there a way to convert the column into the feature type for images?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [July 21, 2022, 10:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604%2F5 "2022-07-21T10:52:27Z")

</div>

Yes you can probably map them all to Pillow images and then cast to the Image feature:

```auto
from datasets import load_dataset
import requests
from PIL import Image

def to_pillow(examples):
    urls = examples['Photo']
    images = []
    for url in urls:
      image = Image.open(requests.get(url, stream=True).raw)
      images.append(image)
    
    examples['image'] = images
    
    return examples

dataset = load_dataset("TheNoob3131/mosquito-data")
dataset = dataset.map(to_pillow, batched=True)

from datasets import Image

dataset = dataset.cast_column('image', Image)

```

I’m going to cc @mariosasko here (`map` seems to very slow). Alternatively, you can do:

```auto
dataset.set_transform(to_pillow)

```

to do this on the fly.

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [July 21, 2022, 10:58am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-run-image-classification-on-image-url%2F20604%2F6 "2022-07-21T10:58:29Z")

</div>

You can make it much faster by doing it in parallel by the way 🙂

```python
dataset = dataset.map(to_pillow, batched=True, num_proc=4)

```
