Skip to content

Commit

Permalink
image set is not enough, crawl wikiart
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirtia committed Jun 28, 2023
1 parent 3f85597 commit 1e8c51f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ __pycache__
output
cx
.vscode
weights
weights
styles
Empty file added cropper.py
Empty file.
19 changes: 16 additions & 3 deletions image_upscaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@
from RealESRGAN import RealESRGAN
import os

# The `ImageUpscaler` class is a Python class that uses the RealESRGAN model to upscale images in a
# given directory. Not sure if this will be needed, it was just cool to try out.
class ImageUpscaler:

def __init__(self, input_dir):
self.input_dir = input_dir
self.model = self.initialize_model()

def initialize_model(self):
"""
The function initializes a RealESRGAN model and loads pre-trained weights.
:return: an instance of the RealESRGAN model that has been initialized and loaded with
pre-trained weights.
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = RealESRGAN(device)
model.load_weights("weights/RealESRGAN_x4.pth", download=True)
return model

def upscale_images(self):
"""
The function `upscale_images` takes an input directory, searches for image files, upscales them
using a pre-trained model, and saves the upscaled images in a separate directory.
"""
if os.path.exists(self.input_dir):
for root, _, files in os.walk(self.input_dir):
if "upscaled" in root:
continue
output_dir = os.path.join(root, "upscaled")
os.makedirs(output_dir, exist_ok=True)
for file in files:
image = Image.open(os.path.join(root, file)).convert("RGB")
upscaled_image = self.model.predict(image)
upscaled_image.save(os.path.join(output_dir, file))
upscaled_file = os.path.join(output_dir, file)
if not os.path.exists(upscaled_file):
image = Image.open(os.path.join(root, file)).convert("RGB")
upscaled_image = self.model.predict(image)
upscaled_image.save(upscaled_file)
else:
raise FileNotFoundError("Error: Input directory does not exist. Please provide an existing directory.")
Empty file added input/wikiart_input
Empty file.
7 changes: 7 additions & 0 deletions model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytorch

"""
https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Classification/CUSTOMTRAINING.md
"""
class ImageModel():
pass
4 changes: 2 additions & 2 deletions wikimedia_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ async def download_image(self, url, title, output_dir):
else:
chunks += chunk.decode("utf-8")
# print(f"Log: HTML page downloaded successfully: {output_file}")
soup = BeautifulSoup(chunks, 'html.parser')
soup = BeautifulSoup(chunks, "html.parser")
image_url = soup.find("img")["src"]
# print(f"Log: Image url extracted from HTML page: {image_url}")
else:
print(f"Error: Failed to download HTML page from {url}. Status code: {response.status}")

async with aio_session.get(image_url) as response:
if response.status == 200:
with open(output_file, 'wb') as file:
with open(output_file, "wb") as file:
while True:
chunk = await response.content.read(1024)
if not chunk:
Expand Down

0 comments on commit 1e8c51f

Please sign in to comment.