-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketcher.py
166 lines (138 loc) · 5.33 KB
/
sketcher.py
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
import matplotlib.pyplot as plt
import streamlit as st
import cv2
from PIL import Image
import numpy as np
from io import StringIO
#import numpy as np
#import tensorflow as tf
#import tensorflow_hub as hub
#from collections.abc import Iterable
icon = Image.open("UV_icon1.png")
st.set_page_config(
page_title = "Sketcher",
page_icon = icon,
layout = "centered"
)
st.set_option('deprecation.showPyplotGlobalUse', False)
st.title("SKETCHER")
st.header("A program to convert any Image to various Sketchs and filters using Python")
st.subheader("AVAILABLE FILTERS")
st.markdown("- Pencil Sketch")
st.markdown("- Grayscale")
st.markdown("- Watercolor")
st.markdown("- Negative")
st.markdown("- Canny edge dynamic detection")
st.markdown("- Cartoon")
def fun(image1,image2):
while True:
plt.figure(figsize=(14,8))
plt.subplot(1,2,1)
plt.title("original image",size = 18)
plt.imshow(image1)
plt.axis('off')
plt.subplot(1,2,2)
plt.title("sketch",size=18)
image2 = cv2.cvtColor(image2,cv2.COLOR_BGR2RGB)
plt.imshow(image2)
plt.axis('off')
ax = plt.show()
st.pyplot(ax)
break
def gray(image):
gray_img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
return gray_img
def watercol(image):
water_img = cv2.stylization(image,sigma_s = 60, sigma_r = 0.3)#0.2 or 0.3
return water_img
def negative(image):
neg_img = 255 - image
return neg_img
def sketch(image):
grey = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
invert_img = cv2.bitwise_not(grey)
#appliying gaussian blur
blur_img = cv2.GaussianBlur(invert_img,(111,111),0)
inblur_img = cv2.bitwise_not(blur_img)
sketch = cv2.divide(grey,inblur_img,scale = 255.0)
final_img = cv2.cvtColor(sketch,cv2.COLOR_BGR2RGB)
return final_img
def sobel_edge(image):
img_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blur_img = cv2.GaussianBlur(img_gray,(3,3), 0)
sobelx = cv2.Sobel(src=blur_img, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge Detection on the X axis
sobely = cv2.Sobel(src=blur_img, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge Detection on the Y axis
sobelxy = cv2.Sobel(src=blur_img, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X and Y Sobel Edge Detection
return sobelxy
def canny_edge(image,threshold1, threshold2):
img_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blur_img = cv2.GaussianBlur(img_gray,(3,3), 0, 0)
edges = cv2.Canny(image=blur_img, threshold1=threshold1, threshold2=threshold2)
return edges
def cartoon(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_blur = cv2.medianBlur(gray, 5)
edges = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 7, 7)
k = 7
data = np.float32(image).reshape((-1, 3))
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.001)
ret, label, center = cv2.kmeans(data, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
result = center[label.flatten()]
result = result.reshape(image.shape)
return result
# Uploading the File to the Page
#image2 = None
option = st.selectbox('Sketch the image into :',('Pencil sketch','Gray','Watercolor','Negative','Canny Edge','Cartoon', 'Custom Stylizer'))
uploadFile = st.file_uploader(label="Upload your image below", type=['jpg', 'png'])
# Checking the Format of the page
if uploadFile is not None:
im = Image.open(uploadFile)
image1 = np.array(im)
st.subheader("Original Image")
st.image(image1)
st.subheader("Converted Image")
if option == 'Gray':
out_img = gray(image1)
elif option == 'Pencil sketch':
out_img = sketch(image1)
elif option == 'Watercolor':
out_img = watercol(image1)
elif option == 'Negative':
out_img = negative(image1)
elif option == 'Cartoon':
out_img = cartoon(image1)
elif option == 'Canny Edge':
values = st.slider("Select the Range of Thresholds",0,255,(25,75))
st.text(values)
threshold1, threshold2 = values
out_img = canny_edge(image1, threshold1, threshold2)
#elif option == 'Custom Stylizer':
#out_img = style_upload(image1)
if out_img is not None:
st.image(out_img)
#st.subheader("Comparison")
#fun(image1,out_img)
st.text("")
st.success('Successfully converted the Image', icon = "✅")
st.text("")
st.text("")
feedback = st.slider('Rate this Project', 0,5,1)
st.write(f"I'm giving {feedback} star for this project !")
st.text("Thank you for visiting this project..Hope it will be useful for you")
for j in range(0,4):
st.text("")
st.subheader("Wanna join / become MEMBER of our society UV...")
st.write("Follow the LINK [UV](https://theuvofearth.wixsite.com/stage1)")
for i in range(0,5):
st.text("")
linkedin = st.button('LinkedIn')
github = st.button('GitHub')
if linkedin:
webbrowser.open("https://www.linkedin.com/in/naveen-kumar-s-921990210/")
if github:
webbrowser.open("https://github.com/0EnIgma1")
#res = pg.confirm('Do you want to download the sketch ?')
#if res == "OK":
#cv2.imwrite('sketch.png',sketch)
#print("Image downloaded successfully !")