Update README.md
Browse files
README.md
CHANGED
@@ -53,6 +53,25 @@ Or initialize everything for more flexibility:
|
|
53 |
```python
|
54 |
from transformers import VisionEncoderDecoderModel, GPT2TokenizerFast, ViTImageProcessor
|
55 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# a function to perform inference
|
58 |
def get_caption(model, image_processor, tokenizer, image_path):
|
@@ -77,6 +96,7 @@ url = "http://images.cocodataset.org/test-stuff2017/000000000019.jpg"
|
|
77 |
caption = get_caption(model, image_processor, tokenizer, url)
|
78 |
print(f"caption: {caption}")
|
79 |
|
|
|
80 |
```
|
81 |
Output:
|
82 |
```
|
|
|
53 |
```python
|
54 |
from transformers import VisionEncoderDecoderModel, GPT2TokenizerFast, ViTImageProcessor
|
55 |
import torch
|
56 |
+
import os
|
57 |
+
import urllib.parse as parse
|
58 |
+
from PIL import Image
|
59 |
+
import requests
|
60 |
+
|
61 |
+
# a function to determine whether a string is a URL or not
|
62 |
+
def is_url(string):
|
63 |
+
try:
|
64 |
+
result = parse.urlparse(string)
|
65 |
+
return all([result.scheme, result.netloc, result.path])
|
66 |
+
except:
|
67 |
+
return False
|
68 |
+
|
69 |
+
# a function to load an image
|
70 |
+
def load_image(image_path):
|
71 |
+
if is_url(image_path):
|
72 |
+
return Image.open(requests.get(image_path, stream=True).raw)
|
73 |
+
elif os.path.exists(image_path):
|
74 |
+
return Image.open(image_path)
|
75 |
|
76 |
# a function to perform inference
|
77 |
def get_caption(model, image_processor, tokenizer, image_path):
|
|
|
96 |
caption = get_caption(model, image_processor, tokenizer, url)
|
97 |
print(f"caption: {caption}")
|
98 |
|
99 |
+
|
100 |
```
|
101 |
Output:
|
102 |
```
|