File size: 10,348 Bytes
ffd62d6 4d447d5 f5fc4d8 6893772 ffd62d6 07a01fd ffd62d6 f5fc4d8 4d447d5 ffd62d6 07a01fd ffd62d6 f5fc4d8 6893772 f5fc4d8 ffd62d6 07a01fd 4d447d5 ffd62d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
from transformers import pipeline
from bs4 import BeautifulSoup
import requests
import urllib
import json
import argparse
from pathlib import Path
import os
from controlnet_aux.processor import Processor
from colorthief import ColorThief
from colors import name_that_color, rgb_to_hex, NTC_NAMES, CSS3_NAMES
from PIL import Image
class Scraper(object):
def __init__(self, urls):
self.urls = urls
def fetch(self, url):
return requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
def parse(self, text):
return BeautifulSoup(text, 'html.parser')
def download_image(self, url, path):
image = requests.get(url)
open(path, 'wb').write(image.content)
class GraffitiDatabaseScraper(Scraper):
def __init__(self):
Scraper.__init__(self, ["https://graffiti-database.com"])
def get_image_urls(self, parsed):
pages = parsed.select('.image-info a')
image_urls = []
for page in pages:
image_urls.append(page.get('href'))
return image_urls
def scrape_image(self, url):
print("Scraping image page {0}".format(url))
fetched = self.fetch(url)
if fetched.status_code != 200:
raise "Scraping image page did fail"
text = fetched.text
parsed = self.parse(text)
image = parsed.select('.img-fluid')[0].get('src')
tag_links = parsed.select('a.tag')
url_parts = urllib.parse.unquote(
urllib.parse.unquote(url)).rsplit("/", 2)
artist = url_parts[1]
return {
"image_url": image,
"city": tag_links[-1].get_text().strip(),
"artist": artist if artist != "Writer Unknown" and artist != "Writers Unknown" else None
}
def scrape_page(self, url):
print("Scraping page {0}".format(url))
text = self.fetch(url).text
parsed = self.parse(text)
image_urls = self.get_image_urls(parsed)
if len(image_urls) == 0:
return False
for image_url in image_urls:
try:
result = self.scrape_image(
"https://graffiti-database.com" + image_url)
except:
continue
file_name = result["image_url"].split('/')[-1]
self.download_image(result["image_url"],
'./images/' + file_name)
result["file"] = file_name
with open('./images/' + file_name + '.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(result, indent=2, ensure_ascii=False))
return True
def scrape(self):
url = self.urls[0]
print("Scraping {0}".format(
url))
count = 1
has_images = True
while has_images:
has_images = self.scrape_page(url + "?page=" + str(count))
count += 1
class GraffitiScraper(Scraper):
def __init__(self):
Scraper.__init__(self, ["https://www.graffiti.org/index/world.html",
"https://www.graffiti.org/index/europe.html", "https://www.graffiti.org/index/usa.html"])
def scrape_page(self, url, city_name):
print("Scraping page {0}".format(url))
text = self.fetch(url).text
parsed = self.parse(text)
image_elements = parsed.select('a[href*=".jpg"]')
for image_element in image_elements:
if image_element.get_text().strip() == "":
tags = image_element.find_next_sibling(string=True).get_text(
).strip() if image_element.find_next_sibling(string=True) else ""
else:
continue
image_url = image_element.get("href").replace("/", "_")
url_parts = url.split('/')
url_parts[-1] = image_element.get("href")
self.download_image("/".join(url_parts),
'./images/' + image_url)
with open('./images/' + image_url + '.json', 'w', encoding='utf-8') as f:
f.write(json.dumps({
"file": image_url,
"image_url": "/".join(url_parts),
"artist": tags,
"city": city_name if city_name != "Various cities" else None
}, indent=2, ensure_ascii=False))
def scrape_url(self, url):
print("Scraping url {0}".format(url))
text = self.fetch(url).text
parsed = self.parse(text)
cities = parsed.find_all("h4")
for city in cities:
city_name = city.get_text().split("\n")[0].strip()
pages = city.find_all("a")
for page in pages:
if page.get_text().strip() == "§":
continue
self.scrape_page(
"https://www.graffiti.org/index/" + page.get("href"), city_name)
def scrape(self):
for url in self.urls:
self.scrape_url(url)
class CLI():
def __init__(self):
parser = argparse.ArgumentParser(
prog='graffiti-cli',
description='Tools for setting up the dataset')
subparsers = parser.add_subparsers(dest="command", required=True)
scrape = subparsers.add_parser(
'scrape', help='Scrapes data sources and downloads images')
scrape.add_argument('--source',
default='graffiti.org',
choices=['graffiti.org', 'graffiti-database.com'],
help='Choose data source to scrape')
subparsers.add_parser('cleanup', help='Cleans up downloaded images')
subparsers.add_parser('caption', help='Captions downloaded images')
subparsers.add_parser('palette', help='Creates color palettes for downloaded images')
subparsers.add_parser('condition', help='Condition downloaded images')
metadata = subparsers.add_parser('metadata', help='Creates single meta files from metadata.jsonl')
metadata.add_argument('--source',
default='graffiti.org',
choices=['graffiti.org', 'graffiti-database.com'],
help='Choose data source to use')
args = parser.parse_args()
if args.command == 'scrape':
if args.source == 'graffiti.org':
GraffitiScraper().scrape()
elif args.source == 'graffiti-database.com':
GraffitiDatabaseScraper().scrape()
elif args.command == 'cleanup':
path = Path("./images").rglob("*.jpg")
for i, img_p in enumerate(path):
try:
Image.open(img_p).load()
except Exception:
path_name = str(img_p)
print(path_name + " is broken. Deleting!")
os.remove(path_name)
os.remove(path_name + ".json")
elif args.command == 'caption':
captioner = pipeline(
"image-to-text", model="Salesforce/blip-image-captioning-base")
path = Path("./images").rglob("*.jpg")
count = len(list(Path("./images").rglob("*.jpg")))
for i, img_p in enumerate(path):
path_name = str(img_p)
with open(path_name + ".json", 'r+', encoding='utf-8') as f:
data = json.load(f)
f.seek(0)
caption = captioner(path_name)[0]["generated_text"]
if "album" in caption:
caption = "a wall with graffiti on it"
json.dump({
"file": data["file"],
"image_url": data["image_url"],
"artist": data["artist"],
"city": data["city"],
"caption": caption
}, f, indent=2)
f.truncate()
print("{0} / {1}".format(i + 1, count), path_name, caption)
elif args.command == 'palette':
path = Path("./images").rglob("*.jpg")
count = len(list(Path("./images").rglob("*.jpg")))
for i, img_p in enumerate(path):
path_name = str(img_p)
with open(path_name + ".json", 'r+', encoding='utf-8') as f:
data = json.load(f)
f.seek(0)
color_thief = ColorThief(path_name)
palette = []
for color in color_thief.get_palette(color_count=6):
color_hex = rgb_to_hex(color)
palette.append([color_hex, name_that_color(color_hex, NTC_NAMES), name_that_color(color_hex, CSS3_NAMES)])
json.dump({
"file": data["file"],
"image_url": data["image_url"],
"artist": data["artist"],
"city": data["city"],
"caption": data["caption"],
"palette": palette
}, f, indent=2)
f.truncate()
print("{0} / {1}".format(i + 1, count), path_name, palette)
elif args.command == 'metadata':
with open("data/" + args.source + "/metadata.jsonl", encoding="utf-8") as f:
for row in f:
data = json.loads(row)
with open('./images/' + data["file"] + '.json', 'w') as j:
j.write(json.dumps(data, indent=2, ensure_ascii=False))
elif args.command == 'conditioning':
processor_id = 'softedge_hed'
processor = Processor(processor_id)
path = Path("./images").rglob("*.jpg")
count = len(list(Path("./images").rglob("*.jpg")))
for i, img_p in enumerate(path):
path_name = str(img_p)
output_path = path_name.replace("images/", "conditioning/")
img = Image.open(img_p).convert("RGB")
processed_image = processor(img, to_pil=True)
processed_image.save(output_path)
print("{0} / {1}".format(i + 1, count), path_name, output_path)
def main():
CLI()
if __name__ == "__main__":
main()
|