Spaces:
Sleeping
Sleeping
Upload Passport_image_using_AI.py
Browse files- pages/Passport_image_using_AI.py +167 -0
pages/Passport_image_using_AI.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
from PIL import Image,ImageColor
|
4 |
+
import io
|
5 |
+
import numpy as np
|
6 |
+
from passport_img import merge_all,predict_on_image,overlay,add_name_dob,align_crop_image
|
7 |
+
|
8 |
+
|
9 |
+
def grayscale_image(image):
|
10 |
+
# Convert the image to grayscale
|
11 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
12 |
+
return gray
|
13 |
+
st.title('📷Passport Photo Maker📷')
|
14 |
+
st.title('🤖AI Enabled🤖')
|
15 |
+
st.write("Made with ❤️ by Mainak")
|
16 |
+
def main():
|
17 |
+
# st.title("Grayscale Image Converter")
|
18 |
+
|
19 |
+
# File uploader
|
20 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
21 |
+
|
22 |
+
|
23 |
+
if uploaded_file is not None:
|
24 |
+
addname = st.checkbox('Select if you want to add name and Date of Birth')
|
25 |
+
aicrop = st.checkbox('Select if you want to crop or change background using AI')
|
26 |
+
size = st.selectbox('Select Size',('3X4','3.5X4.5'))
|
27 |
+
size = int(''.join(i for i in size if not i in ['X','.']))
|
28 |
+
if size==3545:
|
29 |
+
num_img = st.slider('Number of Image', 0, 36, 6)
|
30 |
+
elif size==34:
|
31 |
+
num_img = st.slider('Number of Image', 0, 49, 7)
|
32 |
+
if addname and not aicrop:
|
33 |
+
col1,col2 = st.columns(2)
|
34 |
+
with col1:
|
35 |
+
# st.text('Enter Name')
|
36 |
+
name = st.text_input('Enter Name',value="First_name Last_name",key = 1)
|
37 |
+
with col2:
|
38 |
+
# st.text('Enter Date of Birth')
|
39 |
+
dob = st.text_input('Enter Date of Birth',value="01/01/0001",key = 2)
|
40 |
+
|
41 |
+
if st.button('make'):
|
42 |
+
with st.spinner('Ruko jara sabar karo....'):
|
43 |
+
image = Image.open(uploaded_file)
|
44 |
+
img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
45 |
+
img_array = add_name_dob(img_array,name,dob)
|
46 |
+
gray_image = merge_all(num_img,img_array,size)
|
47 |
+
gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)
|
48 |
+
|
49 |
+
pil_image = Image.fromarray(gray_image)
|
50 |
+
st.image(pil_image, caption='Passport Image', use_column_width=True)
|
51 |
+
|
52 |
+
# Create a download link for the grayscale image
|
53 |
+
buffered = io.BytesIO()
|
54 |
+
pil_image.save(buffered, format="PNG")
|
55 |
+
download_btn = st.download_button(
|
56 |
+
label='Download Image',
|
57 |
+
data=buffered.getvalue(),
|
58 |
+
file_name='passport_image.png',
|
59 |
+
mime='image/png'
|
60 |
+
)
|
61 |
+
elif addname and aicrop:
|
62 |
+
color = st.color_picker('Pick A Color for Background', '#00f900')
|
63 |
+
color = ImageColor.getcolor(color, "RGB")
|
64 |
+
|
65 |
+
col1,col2 = st.columns(2)
|
66 |
+
with col1:
|
67 |
+
# st.text('Enter Name')
|
68 |
+
name = st.text_input('Enter Name',value="First_name Last_name",key = 1)
|
69 |
+
with col2:
|
70 |
+
# st.text('Enter Date of Birth')
|
71 |
+
dob = st.text_input('Enter DOB',value="01/01/0001",key = 2)
|
72 |
+
|
73 |
+
if st.button('make'):
|
74 |
+
with st.spinner('Ruko jara sabar karo....'):
|
75 |
+
image = Image.open(uploaded_file)
|
76 |
+
img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
77 |
+
img = align_crop_image(img_array)
|
78 |
+
if img is not None:
|
79 |
+
# predict by YOLOv8
|
80 |
+
if predict_on_image(img, conf=0.6) is not None:
|
81 |
+
boxes, masks, cls, probs = predict_on_image(img, conf=0.6)
|
82 |
+
# overlay masks on original image
|
83 |
+
image_with_masks = np.copy(img)
|
84 |
+
for mask_i in masks:
|
85 |
+
image_with_masks = overlay(image_with_masks, mask_i, color=color, alpha=1)
|
86 |
+
else:
|
87 |
+
image_with_masks=img
|
88 |
+
img_array = add_name_dob(image_with_masks,name,dob)
|
89 |
+
gray_image = merge_all(num_img,img_array,size)
|
90 |
+
gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)
|
91 |
+
|
92 |
+
pil_image = Image.fromarray(gray_image)
|
93 |
+
st.image(pil_image, caption='Passport Image', use_column_width=True)
|
94 |
+
|
95 |
+
# Create a download link for the grayscale image
|
96 |
+
buffered = io.BytesIO()
|
97 |
+
pil_image.save(buffered, format="PNG")
|
98 |
+
download_btn = st.download_button(
|
99 |
+
label='Download Image',
|
100 |
+
data=buffered.getvalue(),
|
101 |
+
file_name='passport_image.png',
|
102 |
+
mime='image/png'
|
103 |
+
)
|
104 |
+
else:
|
105 |
+
st.warning('Try with another image')
|
106 |
+
elif aicrop and not addname:
|
107 |
+
|
108 |
+
color = st.color_picker('Pick A Color for Background', '#00f900')
|
109 |
+
color = ImageColor.getcolor(color, "RGB")
|
110 |
+
if st.button('make'):
|
111 |
+
with st.spinner('Ruko jara sabar karo....'):
|
112 |
+
image = Image.open(uploaded_file)
|
113 |
+
img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
114 |
+
img = align_crop_image(img_array)
|
115 |
+
if img is not None:
|
116 |
+
# predict by YOLOv8
|
117 |
+
if predict_on_image(img, conf=0.6) is not None:
|
118 |
+
boxes, masks, cls, probs = predict_on_image(img, conf=0.6)
|
119 |
+
# overlay masks on original image
|
120 |
+
image_with_masks = np.copy(img)
|
121 |
+
for mask_i in masks:
|
122 |
+
image_with_masks = overlay(image_with_masks, mask_i, color=color, alpha=1)
|
123 |
+
else:
|
124 |
+
image_with_masks=img
|
125 |
+
# img_array = add_name_dob(image_with_masks,name,dob)
|
126 |
+
gray_image = merge_all(num_img,image_with_masks,size)
|
127 |
+
gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)
|
128 |
+
|
129 |
+
pil_image = Image.fromarray(gray_image)
|
130 |
+
st.image(pil_image, caption='Passport Image', use_column_width=True)
|
131 |
+
|
132 |
+
# Create a download link for the grayscale image
|
133 |
+
buffered = io.BytesIO()
|
134 |
+
pil_image.save(buffered, format="PNG")
|
135 |
+
download_btn = st.download_button(
|
136 |
+
label='Download Image',
|
137 |
+
data=buffered.getvalue(),
|
138 |
+
file_name='passport_image.png',
|
139 |
+
mime='image/png'
|
140 |
+
)
|
141 |
+
else:
|
142 |
+
st.error('Try with another image')
|
143 |
+
|
144 |
+
else:
|
145 |
+
if st.button('make'):
|
146 |
+
with st.spinner('Ruko jara sabar karo....'):
|
147 |
+
image = Image.open(uploaded_file)
|
148 |
+
img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
149 |
+
gray_image = merge_all(num_img,img_array,size)
|
150 |
+
gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)
|
151 |
+
|
152 |
+
pil_image = Image.fromarray(gray_image)
|
153 |
+
st.image(pil_image, caption='Passport Image', use_column_width=True)
|
154 |
+
|
155 |
+
# Create a download link for the grayscale image
|
156 |
+
buffered = io.BytesIO()
|
157 |
+
pil_image.save(buffered, format="PNG")
|
158 |
+
download_btn = st.download_button(
|
159 |
+
label='Download Image',
|
160 |
+
data=buffered.getvalue(),
|
161 |
+
file_name='passport_image.png',
|
162 |
+
mime='image/png'
|
163 |
+
)
|
164 |
+
|
165 |
+
|
166 |
+
if __name__ == '__main__':
|
167 |
+
main()
|