AnkitGaur2811 commited on
Commit
b6d32cc
1 Parent(s): 2109f21

Created app.py

Browse files
Files changed (1) hide show
  1. app.py +191 -0
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Image Conversion App .ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1GA79rJr1_J5bHa7sWxF1GtDqqaTznVia
8
+
9
+ # ***Image Conversion from Simple image***
10
+ > To
11
+ 1. Edge Image
12
+ 2. Pencile sketch
13
+ 3. Painting
14
+ 4. Cartoon image
15
+
16
+ ###Step 1:- Import All libraries
17
+ Namely
18
+ - Numpy (For storing Image)
19
+ - Matplotlib (For Display image)
20
+ - OpenCV (for Converting the image)
21
+ """
22
+
23
+ # installing all imp libs to the machine
24
+ #!pip install opencv_contrib_python
25
+
26
+ import cv2
27
+ import numpy as np
28
+ import matplotlib.pyplot as plt
29
+ import gradio as gr
30
+
31
+ """###Step 2:- Import and show the image
32
+ for this we will be using opencv and matplotlib
33
+
34
+ """
35
+
36
+ def read_image(Image):
37
+ '''
38
+ This Function is made to take image input from the user
39
+ Input: Path of the Image
40
+ Output: Image in RGB format
41
+ '''
42
+ img = cv2.imread(Image)
43
+ img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # As opencv read the image as BGR so we have to convert it to RGB
44
+ plt.imshow(img)
45
+ plt.show()
46
+ return img
47
+
48
+ filename ="/content/to.jpeg"
49
+ Image= read_image(filename)
50
+
51
+ """### Step 3:- Converting the Image to respective types
52
+ Making diffrent Function for each work
53
+
54
+ ####4. Cartoon Image
55
+ """
56
+
57
+ # create Edge
58
+ def edge_mask(Image, line_size, Blur_value):
59
+ grey_img=cv2.cvtColor(Image,cv2.COLOR_RGB2GRAY)
60
+ grey_blur= cv2.medianBlur(grey_img, Blur_value)
61
+ edge_image= cv2.adaptiveThreshold(grey_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, line_size, Blur_value)
62
+ return edge_image
63
+ # line_size,Blur_value=7,7
64
+ # edges= edge_mask(Image,line_size,Blur_value)
65
+ # plt.imshow(edges, cmap="binary")
66
+ # plt.show()
67
+
68
+ #reduce Colour Palet
69
+ def colour_quantization(Image, k):
70
+ data=np.float32(Image).reshape(-1,3)
71
+ critria= (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.01)
72
+ ret, label, center= cv2.kmeans(data,k, None, critria, 10, cv2.KMEANS_RANDOM_CENTERS)
73
+ centers= np.uint8(center)
74
+ result=centers[label.flatten()]
75
+ result=result.reshape(Image.shape)
76
+ return result
77
+ # Paint= colour_quantization(Image,10)
78
+ # plt.imshow(Paint)
79
+ # plt.show()
80
+ # cv2.imwrite("final1.jpg",Paint)
81
+
82
+ def cartoon(Paint,edges):
83
+ Blurred_img= cv2.bilateralFilter(Paint, d=8 ,sigmaColor=200, sigmaSpace=200)
84
+ Final=cv2.bitwise_and(Blurred_img,Blurred_img,mask=edges)
85
+ # plt.imshow(Final)
86
+ # plt.show()
87
+ # Final= cv2.cvtColor(Final, cv2.COLOR_RGB2BGR)
88
+ # cv2.imwrite("Final.jpg", Final)
89
+ return Final
90
+ # cartoon()
91
+
92
+ def Main_cartoon(Image,Line_size,Blur_value,Color_count):
93
+ edge_mask_img= edge_mask(Image, Line_size, Blur_value)
94
+ Paint_img=colour_quantization(Image, Color_count)
95
+ cartoon_img= cartoon(Paint_img,edge_mask_img)
96
+ # cartoon_img=cv2.cvtColor(cartoon_img, cv2.COLOR_RGB2BGR)
97
+ return cartoon_img
98
+
99
+ x=Main_cartoon(Image,7,7,10)
100
+
101
+ """####3. Painting"""
102
+
103
+ def Painting(Image, Colour_count):
104
+ Painting=colour_quantization(Image,Colour_count)
105
+ # Painting=cv2.cvtColor(Painting, cv2.COLOR_RGB2BGR)
106
+ return Painting
107
+
108
+ y=Painting(Image,10)
109
+
110
+ """####2.Pencil Sketch"""
111
+
112
+ def Pencil_sketch(Image):
113
+ grey_img=cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)
114
+ invert_img=cv2.bitwise_not(grey_img)
115
+ blur_img=cv2.GaussianBlur(invert_img, (111,111),0)
116
+ invblur_img=cv2.bitwise_not(blur_img)
117
+ sketch_img=cv2.divide(grey_img,invblur_img, scale=256.0)
118
+ return sketch_img
119
+
120
+ z=Pencil_sketch(Image)
121
+
122
+ """####1.Edge Sketch"""
123
+
124
+ def Edge_sketch(Image):
125
+ edge_img = cv2.Canny(Image,100,200)
126
+ edge_img = cv2.cvtColor(edge_img,cv2.COLOR_BGR2RGB)
127
+ return edge_img
128
+
129
+ t=Edge_sketch(Image)
130
+
131
+ """###Step 4:- Testing"""
132
+
133
+ plt.imshow(x)
134
+ plt.show()
135
+ cv2.imwrite("Final.jpg", x)
136
+
137
+ plt.imshow(y)
138
+ plt.show()
139
+ cv2.imwrite("final1.jpg",y)
140
+
141
+ plt.imshow(z)
142
+ plt.show()
143
+ cv2.imwrite("final2.jpg",z)
144
+
145
+ plt.imshow(t)
146
+ plt.show()
147
+ cv2.imwrite("final3.jpg",t)
148
+
149
+ """### Step 5:- Gradio app and driver code
150
+
151
+ #### Driver Code
152
+ """
153
+
154
+ def Sketch_app(Image,Type,Color_count,Line_size,Blur_value):
155
+ if Type == "Cartoon":
156
+ Result = Main_cartoon(Image,Line_size,Blur_value,Color_count)
157
+ elif Type == "Painting":
158
+ Result = Painting(Image,Color_count)
159
+ elif Type == "Pencil sketch":
160
+ Result = Pencil_sketch(Image)
161
+ else:
162
+ Result = None
163
+ return Result
164
+
165
+ """#### Gradio app
166
+
167
+ ##### Installing and Importing Gradio
168
+ As Gradio is a 3rd party library we have to install it in Our Run time before Executing
169
+ """
170
+
171
+ #!pip install gradio
172
+
173
+
174
+
175
+ Image_conversion = gr.Interface(
176
+ fn = Sketch_app,
177
+ inputs=[
178
+ gr.Image( tool="select",label="Image to Convert", show_label=True),
179
+ gr.Dropdown( choices = ["Pencil sketch","Painting","Cartoon"],label="Type to convert", show_label=True),
180
+ gr.Slider( minimum=5, maximum=20, value =10, step= 1,label="Number of colour to be used in photo ( use only in case of painting and cartoon)", show_label=True),
181
+ gr.Slider( minimum=5, maximum=10, value =7, step= 1,label="Blurr effect to be used in photo ( use only in case of cartoon)", show_label=True),
182
+ gr.Slider( minimum=5, maximum=10, value =7, step= 1,label="Thickness of edges to be used in photo ( use only in case of cartoon)", show_label=True)],
183
+ outputs= "image",
184
+ tittle = " Image Conversion App",
185
+ description = """This is an image conversion app we take a regular photo and convert it into Cartoon, Painting, Pencil sketch. it is purely python based used
186
+ Gradio(for interfaceing), OpenCV (For image conversion), Numpy(for storing the image), Matplotlib (for displaying the image) """,
187
+ theme = "dark"
188
+ )
189
+
190
+ Image_conversion.launch()
191
+