v0.0.2 -- first upload to Hugging Face
#1
by
babaochou2420
- opened
- app.py +119 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# v0.0.2 by babaochou
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
class ImageProcesser:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
pass
|
| 10 |
+
|
| 11 |
+
# Function -- Turn Image into Sketch with modes select-able
|
| 12 |
+
def sketch_image(self, image, pencilShadow, outlineSimplify, R, G, B, mode):
|
| 13 |
+
try:
|
| 14 |
+
outlineBlur = (2*outlineSimplify)+1
|
| 15 |
+
|
| 16 |
+
lineColour = (R, G, B)
|
| 17 |
+
|
| 18 |
+
# if (bgMode == 'Remove Background'):
|
| 19 |
+
# # image = removeBackground(image)
|
| 20 |
+
# None
|
| 21 |
+
# elif (bgMode == 'Keep Background'):
|
| 22 |
+
# None
|
| 23 |
+
|
| 24 |
+
# Turn image into grayscale
|
| 25 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 26 |
+
# Make a inverted grayscale for after-processing
|
| 27 |
+
gray_image_inverted = cv2.bitwise_not(gray_image)
|
| 28 |
+
# Apply GaussianBlur to the inverted image
|
| 29 |
+
blurred_gray_image = cv2.GaussianBlur(
|
| 30 |
+
gray_image_inverted, (pencilShadow, pencilShadow), 0)
|
| 31 |
+
# Make a inverted GaussianBlur for after-processing
|
| 32 |
+
inverted_blurred_image = cv2.bitwise_not(blurred_gray_image)
|
| 33 |
+
# For Canny-Algorithm
|
| 34 |
+
edgeFindingImage = cv2.GaussianBlur(
|
| 35 |
+
gray_image, (outlineBlur, outlineBlur), 0)
|
| 36 |
+
# Using Canny-Algorithm to get better outline from the grayscale
|
| 37 |
+
edges = cv2.bitwise_not(cv2.Canny(edgeFindingImage, 100, 200))
|
| 38 |
+
|
| 39 |
+
cv2.normalize(
|
| 40 |
+
edges, edges, 0, 255, norm_type=cv2.NORM_MINMAX)
|
| 41 |
+
# Craete a mask for outline colouring
|
| 42 |
+
edge_mask = np.zeros_like(image)
|
| 43 |
+
edge_mask[:, :] = lineColour
|
| 44 |
+
# Changing outline colour
|
| 45 |
+
edges = cv2.bitwise_or(
|
| 46 |
+
edge_mask, cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR))
|
| 47 |
+
# # Split the 3-channels
|
| 48 |
+
# _b, _g, _r = cv2.split(edges)
|
| 49 |
+
# # Create a alpha channel
|
| 50 |
+
# _a = np.full(image.shape, A)
|
| 51 |
+
# # Merge into a RGBA
|
| 52 |
+
# edges = cv2.merge((_b, _g, _r, _a))
|
| 53 |
+
# # Changing back to 3-channels
|
| 54 |
+
# edges = cv2.cvtColor(edges, cv2.COLOR_BGRA2BGR)
|
| 55 |
+
|
| 56 |
+
# If user want outline only
|
| 57 |
+
if (mode == 'Outline Only'):
|
| 58 |
+
return edges
|
| 59 |
+
elif (mode == 'Grayscale'):
|
| 60 |
+
return gray_image
|
| 61 |
+
# If user want shadow from the grayscale as well
|
| 62 |
+
elif (mode == 'With Shadow'):
|
| 63 |
+
pencil_sketch_image = cv2.divide(
|
| 64 |
+
gray_image, inverted_blurred_image, scale=226.0)
|
| 65 |
+
# Chaning the image into 4-channels
|
| 66 |
+
pencil_sketch_image = cv2.cvtColor(
|
| 67 |
+
pencil_sketch_image, cv2.COLOR_GRAY2BGR)
|
| 68 |
+
# Giving a better outline
|
| 69 |
+
combined_image = cv2.bitwise_and(pencil_sketch_image, edges)
|
| 70 |
+
|
| 71 |
+
return combined_image
|
| 72 |
+
elif (mode == 'Negative'):
|
| 73 |
+
pencil_sketch_image = cv2.divide(
|
| 74 |
+
gray_image, inverted_blurred_image, scale=226.0)
|
| 75 |
+
# Giving a better outline then make a negative
|
| 76 |
+
combined_image = cv2.bitwise_not(pencil_sketch_image, edges)
|
| 77 |
+
|
| 78 |
+
return combined_image
|
| 79 |
+
except Exception as error:
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
class GradioInterface:
|
| 84 |
+
def __init__(self):
|
| 85 |
+
|
| 86 |
+
self.imageProcesser = ImageProcesser()
|
| 87 |
+
|
| 88 |
+
with gr.Blocks() as input_blocks:
|
| 89 |
+
with gr.Row():
|
| 90 |
+
imageInput = gr.Image(label="Original")
|
| 91 |
+
imageOutput = gr.Image(label='Processed')
|
| 92 |
+
btnGenerate = gr.Button(value='Generate')
|
| 93 |
+
with gr.Row():
|
| 94 |
+
sliderShadow = gr.Slider(minimum=1, maximum=27, value=9, step=2,
|
| 95 |
+
label="Shadow Strengthen", info="9 is for outline only, 21 is with shadows")
|
| 96 |
+
sliderOutline = gr.Slider(minimum=0, maximum=7, value=0, step=1,
|
| 97 |
+
label="Outline Simplify", info="")
|
| 98 |
+
with gr.Row():
|
| 99 |
+
sldierR = gr.Slider(label='R', minimum=0,
|
| 100 |
+
maximum=255, step=1, value=0)
|
| 101 |
+
sliderG = gr.Slider(label='G', minimum=0,
|
| 102 |
+
maximum=255, step=1, value=0)
|
| 103 |
+
sliderB = gr.Slider(label='B', minimum=0,
|
| 104 |
+
maximum=255, step=1, value=0)
|
| 105 |
+
# sliderA = gr.Slider(label='A', minimum=0,
|
| 106 |
+
# maximum=255, step=1, value=0)
|
| 107 |
+
radioMode = gr.Radio(["With Shadow", "Outline Only", "Negative", 'Grayscale'],
|
| 108 |
+
label="Mode", value='With Shadow')
|
| 109 |
+
|
| 110 |
+
# radioBackground = gr.Radio(['Keep Background', 'Remove Background'],
|
| 111 |
+
# value='Keep Background')
|
| 112 |
+
# button to start processing
|
| 113 |
+
btnGenerate.click(fn=self.imageProcesser.sketch_image,
|
| 114 |
+
inputs=[imageInput, sliderShadow, sliderOutline, sldierR, sliderG, sliderB, radioMode], outputs=imageOutput)
|
| 115 |
+
|
| 116 |
+
input_blocks.launch()
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
gradio_interface = GradioInterface()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python-opencv
|