File size: 7,973 Bytes
51d7677
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
import streamlit as st
import cv2
from PIL import Image,ImageColor
import io
import numpy as np
from passport_img import merge_all,predict_on_image,overlay,add_name_dob,align_crop_image


def grayscale_image(image):
    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return gray
st.title('📷Passport Photo Maker📷')
st.title('🤖AI Enabled🤖')
st.write("Made with ❤️ by Mainak")
def main():
    # st.title("Grayscale Image Converter")

    # File uploader
    uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
    

    if uploaded_file is not None:
        addname = st.checkbox('Select if you want to add name and Date of Birth')
        aicrop = st.checkbox('Select if you want to crop or change background using AI')
        size = st.selectbox('Select Size',('3X4','3.5X4.5'))
        size = int(''.join(i for i in size if not i in ['X','.']))
        if size==3545:
            num_img = st.slider('Number of Image', 0, 36, 6)
        elif size==34:
            num_img = st.slider('Number of Image', 0, 49, 7)
        if addname and not aicrop:
            col1,col2 = st.columns(2)
            with col1:  
                # st.text('Enter Name')
                name = st.text_input('Enter Name',value="First_name Last_name",key = 1)
            with col2:
                # st.text('Enter Date of Birth')
                dob = st.text_input('Enter Date of Birth',value="01/01/0001",key = 2)
           
            if st.button('make'):
                with st.spinner('Ruko jara sabar karo....'):
                    image = Image.open(uploaded_file)
                    img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
                    img_array = add_name_dob(img_array,name,dob)
                    gray_image = merge_all(num_img,img_array,size)
                    gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)

                    pil_image = Image.fromarray(gray_image)
                    st.image(pil_image, caption='Passport Image', use_column_width=True)

                    # Create a download link for the grayscale image
                    buffered = io.BytesIO()
                    pil_image.save(buffered, format="PNG")
                    download_btn = st.download_button(
                        label='Download Image',
                        data=buffered.getvalue(),
                        file_name='passport_image.png',
                        mime='image/png'
                    )
        elif addname and aicrop:
            color = st.color_picker('Pick A Color for Background', '#00f900')
            color = ImageColor.getcolor(color, "RGB")

            col1,col2 = st.columns(2)
            with col1:  
                # st.text('Enter Name')
                name = st.text_input('Enter Name',value="First_name Last_name",key = 1)
            with col2:
                # st.text('Enter Date of Birth')
                dob = st.text_input('Enter DOB',value="01/01/0001",key = 2)

            if st.button('make'):
                with st.spinner('Ruko jara sabar karo....'):
                    image = Image.open(uploaded_file)
                    img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
                    img = align_crop_image(img_array)
                    if img is not None:
                        # predict by YOLOv8
                        if predict_on_image(img, conf=0.6) is not None:
                            boxes, masks, cls, probs = predict_on_image(img, conf=0.6)
                            # overlay masks on original image
                            image_with_masks = np.copy(img)
                            for mask_i in masks:
                                image_with_masks = overlay(image_with_masks, mask_i, color=color, alpha=1)
                        else:
                            image_with_masks=img
                        img_array = add_name_dob(image_with_masks,name,dob)
                        gray_image = merge_all(num_img,img_array,size)
                        gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)

                        pil_image = Image.fromarray(gray_image)
                        st.image(pil_image, caption='Passport Image', use_column_width=True)

                        # Create a download link for the grayscale image
                        buffered = io.BytesIO()
                        pil_image.save(buffered, format="PNG")
                        download_btn = st.download_button(
                            label='Download Image',
                            data=buffered.getvalue(),
                            file_name='passport_image.png',
                            mime='image/png'
                        )
                    else:
                        st.warning('Try with another image')
        elif aicrop and not addname:
            
            color = st.color_picker('Pick A Color for Background', '#00f900')
            color = ImageColor.getcolor(color, "RGB")
            if st.button('make'):
                with st.spinner('Ruko jara sabar karo....'):
                    image = Image.open(uploaded_file)
                    img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
                    img = align_crop_image(img_array)
                    if img is not None:
                        # predict by YOLOv8
                        if predict_on_image(img, conf=0.6) is not None:
                            boxes, masks, cls, probs = predict_on_image(img, conf=0.6)
                            # overlay masks on original image
                            image_with_masks = np.copy(img)
                            for mask_i in masks:
                                image_with_masks = overlay(image_with_masks, mask_i, color=color, alpha=1)
                        else:
                            image_with_masks=img
                        # img_array = add_name_dob(image_with_masks,name,dob)
                        gray_image = merge_all(num_img,image_with_masks,size)
                        gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)

                        pil_image = Image.fromarray(gray_image)
                        st.image(pil_image, caption='Passport Image', use_column_width=True)

                        # Create a download link for the grayscale image
                        buffered = io.BytesIO()
                        pil_image.save(buffered, format="PNG")
                        download_btn = st.download_button(
                            label='Download Image',
                            data=buffered.getvalue(),
                            file_name='passport_image.png',
                            mime='image/png'
                        )
                    else:
                        st.error('Try with another image')

        else:
            if st.button('make'):
                with st.spinner('Ruko jara sabar karo....'):
                    image = Image.open(uploaded_file)
                    img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
                    gray_image = merge_all(num_img,img_array,size)
                    gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)

                    pil_image = Image.fromarray(gray_image)
                    st.image(pil_image, caption='Passport Image', use_column_width=True)

                    # Create a download link for the grayscale image
                    buffered = io.BytesIO()
                    pil_image.save(buffered, format="PNG")
                    download_btn = st.download_button(
                        label='Download Image',
                        data=buffered.getvalue(),
                        file_name='passport_image.png',
                        mime='image/png'
                    )
        

if __name__ == '__main__':
    main()