Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,199 +1,189 @@
|
|
| 1 |
-
"""
|
| 2 |
-
CS5330 Fall 2024
|
| 3 |
-
Author: Calvin Lo
|
| 4 |
-
Lab 1: ASCII Art
|
| 5 |
-
|
| 6 |
-
This program generates the ASCII art representation of
|
| 7 |
-
an input image and compares the result to the original image
|
| 8 |
-
using SSIM.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
import gradio as gr
|
| 12 |
-
import cv2
|
| 13 |
-
import numpy as np
|
| 14 |
-
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageEnhance
|
| 15 |
-
from skimage.metrics import structural_similarity as ssim
|
| 16 |
-
|
| 17 |
-
# ASCII characters to map pixel intensity
|
| 18 |
-
ASCII_CHARS = [
|
| 19 |
-
' ', '.', ',', ':', ';', 'i', 'l', '!', 'I', '1', 't', 'o', 'r', 'x', 'z', 'v',
|
| 20 |
-
'u', 'n', 'm', 'w', 'Q', 'B', 'N', 'M', '@'
|
| 21 |
-
]
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def preprocess_image(image, new_width=150):
|
| 25 |
-
"""
|
| 26 |
-
This function performs the following operations on the input image:
|
| 27 |
-
-enhance contrast
|
| 28 |
-
-perform edge detection (Sobel)
|
| 29 |
-
-denoise edges
|
| 30 |
-
-enhance contrast
|
| 31 |
-
-resize image
|
| 32 |
-
"""
|
| 33 |
-
# Open the image and convert to grayscale
|
| 34 |
-
image = Image.fromarray(image)
|
| 35 |
-
image = image.convert("L")
|
| 36 |
-
|
| 37 |
-
# Contrast limited adaptive histogram equalization
|
| 38 |
-
opencv_image = np.array(image)
|
| 39 |
-
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
| 40 |
-
equalized_image = clahe.apply(opencv_image)
|
| 41 |
-
|
| 42 |
-
# Apply Sobel edge detection
|
| 43 |
-
sobelx = cv2.Sobel(equalized_image, cv2.CV_64F, 1, 0, ksize=5)
|
| 44 |
-
sobely = cv2.Sobel(equalized_image, cv2.CV_64F, 0, 1, ksize=5)
|
| 45 |
-
edges = np.hypot(sobelx, sobely) # Combine gradients
|
| 46 |
-
|
| 47 |
-
# Normalize the result to fit into the 0-255 grayscale range
|
| 48 |
-
edges = np.uint8(255 * edges / np.max(edges))
|
| 49 |
-
|
| 50 |
-
# Denoise the edges using Non-Local Means Denoising
|
| 51 |
-
edges = cv2.fastNlMeansDenoising(edges, None, h=30, templateWindowSize=7, searchWindowSize=21)
|
| 52 |
-
|
| 53 |
-
# Improve contrast of edges
|
| 54 |
-
edges_image = Image.fromarray(edges)
|
| 55 |
-
enhancer = ImageEnhance.Contrast(edges_image)
|
| 56 |
-
enhanced_edges = enhancer.enhance(1.5)
|
| 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 |
-
for
|
| 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 |
-
# Update interface when image is uploaded
|
| 190 |
-
image_input.change(fn=gradio_interface,
|
| 191 |
-
inputs=image_input,
|
| 192 |
-
outputs=[ascii_art_output, ssim_score_output, ascii_file_output])
|
| 193 |
-
|
| 194 |
-
# Launch interface
|
| 195 |
-
interface.launch()
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
if __name__ == "__main__":
|
| 199 |
main()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CS5330 Fall 2024
|
| 3 |
+
Author: Calvin Lo
|
| 4 |
+
Lab 1: ASCII Art
|
| 5 |
+
|
| 6 |
+
This program generates the ASCII art representation of
|
| 7 |
+
an input image and compares the result to the original image
|
| 8 |
+
using SSIM.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import cv2
|
| 13 |
+
import numpy as np
|
| 14 |
+
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageEnhance
|
| 15 |
+
from skimage.metrics import structural_similarity as ssim
|
| 16 |
+
|
| 17 |
+
# ASCII characters to map pixel intensity
|
| 18 |
+
ASCII_CHARS = [
|
| 19 |
+
' ', '.', ',', ':', ';', 'i', 'l', '!', 'I', '1', 't', 'o', 'r', 'x', 'z', 'v',
|
| 20 |
+
'u', 'n', 'm', 'w', 'Q', 'B', 'N', 'M', '@'
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def preprocess_image(image, new_width=150):
|
| 25 |
+
"""
|
| 26 |
+
This function performs the following operations on the input image:
|
| 27 |
+
-enhance contrast
|
| 28 |
+
-perform edge detection (Sobel)
|
| 29 |
+
-denoise edges
|
| 30 |
+
-enhance contrast
|
| 31 |
+
-resize image
|
| 32 |
+
"""
|
| 33 |
+
# Open the image and convert to grayscale
|
| 34 |
+
image = Image.fromarray(image)
|
| 35 |
+
image = image.convert("L")
|
| 36 |
+
|
| 37 |
+
# Contrast limited adaptive histogram equalization
|
| 38 |
+
opencv_image = np.array(image)
|
| 39 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
| 40 |
+
equalized_image = clahe.apply(opencv_image)
|
| 41 |
+
|
| 42 |
+
# Apply Sobel edge detection
|
| 43 |
+
sobelx = cv2.Sobel(equalized_image, cv2.CV_64F, 1, 0, ksize=5)
|
| 44 |
+
sobely = cv2.Sobel(equalized_image, cv2.CV_64F, 0, 1, ksize=5)
|
| 45 |
+
edges = np.hypot(sobelx, sobely) # Combine gradients
|
| 46 |
+
|
| 47 |
+
# Normalize the result to fit into the 0-255 grayscale range
|
| 48 |
+
edges = np.uint8(255 * edges / np.max(edges))
|
| 49 |
+
|
| 50 |
+
# Denoise the edges using Non-Local Means Denoising
|
| 51 |
+
edges = cv2.fastNlMeansDenoising(edges, None, h=30, templateWindowSize=7, searchWindowSize=21)
|
| 52 |
+
|
| 53 |
+
# Improve contrast of edges
|
| 54 |
+
edges_image = Image.fromarray(edges)
|
| 55 |
+
enhancer = ImageEnhance.Contrast(edges_image)
|
| 56 |
+
enhanced_edges = enhancer.enhance(1.5)
|
| 57 |
+
enhanced_edges = np.array(enhanced_edges)
|
| 58 |
+
|
| 59 |
+
# Resize the image for ASCII aspect ratio
|
| 60 |
+
width, height = image.size
|
| 61 |
+
aspect_ratio = height / width * 0.45 # Adjusting for ASCII aspect ratio
|
| 62 |
+
new_height = int(aspect_ratio * new_width)
|
| 63 |
+
resized_image = Image.fromarray(enhanced_edges).resize((new_width, new_height))
|
| 64 |
+
|
| 65 |
+
return resized_image
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def image_to_ascii(image):
|
| 69 |
+
"""
|
| 70 |
+
This function generates an ASCII representation of images.
|
| 71 |
+
"""
|
| 72 |
+
pixels = np.array(image)
|
| 73 |
+
ascii_str = ""
|
| 74 |
+
for row in pixels:
|
| 75 |
+
for pixel in row:
|
| 76 |
+
# Mapping pixel intensity to ASCII
|
| 77 |
+
ascii_str += ASCII_CHARS[pixel // 10]
|
| 78 |
+
ascii_str += "\n"
|
| 79 |
+
return ascii_str
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def ascii_to_image(ascii_art, font_size=12, output_file='ascii_art.png'):
|
| 83 |
+
""""
|
| 84 |
+
This function converts ASCII text art to a png image.
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
# Specify font
|
| 88 |
+
font_path = "DejaVuSansMono.ttf"
|
| 89 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 90 |
+
|
| 91 |
+
# Get the required image dimensions
|
| 92 |
+
lines = ascii_art.splitlines()
|
| 93 |
+
max_width = max(font.getbbox(line)[2] for line in lines) # Get max width
|
| 94 |
+
img_height = len(lines) * font_size
|
| 95 |
+
|
| 96 |
+
# Generate image with white background
|
| 97 |
+
image = Image.new("RGB", (max_width, img_height), "white")
|
| 98 |
+
draw = ImageDraw.Draw(image)
|
| 99 |
+
|
| 100 |
+
# Draw each character from the ASCII text on the image
|
| 101 |
+
for y, line in enumerate(lines):
|
| 102 |
+
draw.text((0, y * font_size), line, fill="black", font=font)
|
| 103 |
+
|
| 104 |
+
image.save(output_file)
|
| 105 |
+
return image
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def compare_ssim(image1, image2):
|
| 109 |
+
"""
|
| 110 |
+
This function generates the structural similarity index
|
| 111 |
+
measure for the two input images.
|
| 112 |
+
"""
|
| 113 |
+
# Convert original color image to grayscale
|
| 114 |
+
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
|
| 115 |
+
|
| 116 |
+
# Convert ASCII image to grayscale and cv2 format
|
| 117 |
+
image2 = np.array(image2.convert("L"))
|
| 118 |
+
|
| 119 |
+
# Resize so both images have the same dimensions.
|
| 120 |
+
image2 = cv2.resize(image2, (image1.shape[1], image1.shape[0]))
|
| 121 |
+
|
| 122 |
+
score, _ = ssim(image1, image2, full=True)
|
| 123 |
+
return score
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def process_image(image):
|
| 127 |
+
"""
|
| 128 |
+
This function takes an image and generates:
|
| 129 |
+
-ASCII art as a string
|
| 130 |
+
-ASCII text file
|
| 131 |
+
-Compute SSIM for the ASCII art image and the original image
|
| 132 |
+
"""
|
| 133 |
+
# Get image edges and convert to ASCII
|
| 134 |
+
edges = preprocess_image(image)
|
| 135 |
+
ascii_art = image_to_ascii(edges)
|
| 136 |
+
|
| 137 |
+
# Convert ASCII art back to an image for SSIM calculation
|
| 138 |
+
ascii_image = ascii_to_image(ascii_art)
|
| 139 |
+
|
| 140 |
+
# Calculate SSIM
|
| 141 |
+
ssim_score = compare_ssim(image, ascii_image)
|
| 142 |
+
|
| 143 |
+
# Write ASCII art to txt file
|
| 144 |
+
ascii_txt_path = "ascii_art_output.txt"
|
| 145 |
+
with open(ascii_txt_path, "w") as f:
|
| 146 |
+
f.write(ascii_art)
|
| 147 |
+
|
| 148 |
+
return ascii_art, ssim_score, ascii_txt_path
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def gradio_interface(image):
|
| 152 |
+
"""
|
| 153 |
+
This function generates the interface outputs for a given input image.
|
| 154 |
+
The outputs include the formatted SSIM, ASCII art, and ASCII art txt file path.
|
| 155 |
+
"""
|
| 156 |
+
ascii_art, ssim_score, ascii_txt_path = process_image(image)
|
| 157 |
+
ssim_formatted = f'<div style="font-size: 20px;">The structural similarity index measure is {ssim_score:.3f}</div>'
|
| 158 |
+
# Wrap the ASCII art in HTML with custom font size
|
| 159 |
+
styled_ascii_art = f'<pre style="font-family: monospace; font-size: 7px;">{ascii_art}</pre>'
|
| 160 |
+
|
| 161 |
+
return styled_ascii_art, ssim_formatted, ascii_txt_path
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def main():
|
| 165 |
+
# Setup Gradio interface
|
| 166 |
+
with gr.Blocks() as interface:
|
| 167 |
+
# Add title and description
|
| 168 |
+
gr.Markdown("<h1 style='font-size: 40px;'>Image to ASCII Art Converter</h1>")
|
| 169 |
+
gr.Markdown("<p style='font-size: 18px;'>Upload an image, and this tool will generate an ASCII art version of the image. It also calculates the Structural Similarity Index (SSIM) to evaluate the quality.</p>")
|
| 170 |
+
|
| 171 |
+
# Image input and ASCII art + SSIM score output
|
| 172 |
+
image_input = gr.Image(label="Upload an image")
|
| 173 |
+
ascii_art_output = gr.HTML()
|
| 174 |
+
ssim_score_output = gr.HTML()
|
| 175 |
+
|
| 176 |
+
# File output for downloading, initially hidden
|
| 177 |
+
ascii_file_output = gr.File(label="Download ASCII Art")
|
| 178 |
+
|
| 179 |
+
# Update interface when image is uploaded
|
| 180 |
+
image_input.change(fn=gradio_interface,
|
| 181 |
+
inputs=image_input,
|
| 182 |
+
outputs=[ascii_art_output, ssim_score_output, ascii_file_output])
|
| 183 |
+
|
| 184 |
+
# Launch interface
|
| 185 |
+
interface.launch()
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
main()
|