File size: 9,443 Bytes
bac55b4
6810bed
bac55b4
 
 
8004cfc
bac55b4
 
 
014abd7
bac55b4
 
 
 
 
5d3564f
bac55b4
 
 
 
 
 
 
 
 
8004cfc
 
 
 
 
 
 
014abd7
bac55b4
014abd7
 
 
bac55b4
 
 
 
 
 
 
 
 
0ab59c6
bac55b4
 
6810bed
bac55b4
6810bed
bac55b4
014abd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bac55b4
6810bed
bac55b4
 
 
6810bed
bac55b4
014abd7
bac55b4
014abd7
bac55b4
 
 
5d3564f
 
 
 
 
 
 
 
 
bac55b4
 
 
 
 
 
7fee005
bac55b4
7fee005
bac55b4
8004cfc
7fee005
 
8004cfc
 
014abd7
 
8004cfc
014abd7
8004cfc
014abd7
8004cfc
014abd7
8004cfc
bac55b4
 
 
014abd7
bac55b4
 
 
014abd7
 
 
bac55b4
 
 
b8a5709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
014abd7
b8a5709
 
 
014abd7
 
 
b8a5709
 
 
 
5d3564f
 
 
 
 
 
 
 
 
bac55b4
 
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 gradio as gr
from PIL import Image as PIL_Image # Renaming to avoid conflict with Image from gltflib
from io import BytesIO
import json
import os
import glob

import src.constants as constants

from src.front_card_frame import create_card_frame
from src.front_card_image import create_card_image
from src.front_card_image_historic_site import create_historic_site_card_image
from src.picture_box_model import create_picture_box_model
from src.extracted_objects_model import create_extracted_objects_model
from src.card_model import create_card_model
from src.manhole_model import create_manhole_model

language = os.environ.get('LANGUAGE', 'en')
print(f'LANGUAGE: {language}')

with open(f'src/display_message_{language}.json', 'r', encoding='utf-8') as f:
    display_message_dict = json.load(f)
    model_type_dict = display_message_dict['model_type_dict']
    color_dict = display_message_dict['color_dict']

default_card_design_image_dict = {value: PIL_Image.open(constants.back_card_img_dict[key]).convert('RGB') for (key, value) in model_type_dict.items()}
def update_card_image(card_design_upload, card_design_dropdown, update_type):
    if update_type == 'Upload':
        return card_design_upload['background']
    else:
        return default_card_design_image_dict[card_design_dropdown]

def create_3dmodel(model_no, title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image, card_design_preview_image):

    picture_img_bytearray = BytesIO()
    picture_image['background'].save(picture_img_bytearray, "JPEG", quality=95)
    picture_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data.

    option_dict = {
        'タイトル': title,
        '色': color,
        'マーク': mark,
        '史跡種類': historic_site_type,
        '訪問難度': difficulty,
        '説明文': description,
        '厚み': '有' if is_thick else '無',
        'language': language
    }

    # Consider implementing model_no categorization rules or creating a factory class
    if model_no not in ['A', 'B']:
        # Create the card image (front side)
        if model_no == '1':
            front_img_bytearray = create_historic_site_card_image(picture_img_bytearray, option_dict)
            
            # Retrieve the card image (back side)
            back_path = constants.back_card_img_dict[model_no]
            back_img = PIL_Image.open(back_path)
            back_img_bytearray = BytesIO()
            back_img.convert('RGB').save(back_img_bytearray, "JPEG", quality=95)
            back_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data

        else:            
            back_img_bytearray = BytesIO()
            card_design_preview_image.save(back_img_bytearray, "JPEG", quality=95)
            back_img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data.    
            card_frame_bytearray = create_card_frame(back_img_bytearray)
            front_img_bytearray = create_card_image(card_frame_bytearray, picture_img_bytearray, option_dict)

        # Create a 3D model of the card (return value is the path of the created model)
        model_path = create_card_model(front_img_bytearray, back_img_bytearray, option_dict)
        
    else:
        # Create a 3D model of the card (return value is the path of the created model)
        if model_no == 'A':
            model_path = create_picture_box_model(picture_img_bytearray)
        if model_no == 'B':
            model_path = create_extracted_objects_model(picture_img_bytearray)
    
    return model_path

def create_manhole(image):
    img_bytearray = BytesIO()
    image['background'].save(img_bytearray, "JPEG", quality=95)
    img_bytearray.seek(0) # Seek to the beginning of the image, otherwise it results in empty data.
    
    model_path = create_manhole_model(img_bytearray)
    
    return model_path

with gr.Blocks() as demo:

    gr.Markdown(display_message_dict['header'])
    
    with gr.Tab(display_message_dict['tab_label_card_general']):
        with gr.Row():
            
            with gr.Column():
                
                title = gr.Textbox(label=display_message_dict['label_title'], placeholder=display_message_dict['placeholder_title'])
                
                gr.Markdown(display_message_dict['card_general_description'])
                
                with gr.Row():
                    with gr.Column():
                            card_design_dropdown = gr.Dropdown(list(default_card_design_image_dict.keys()), label=display_message_dict['label_card_design_sample'])                           
                            card_design_upload = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="868:1213", label=display_message_dict['label_card_design_upload'])
                    with gr.Column():                        
                        card_design_preview_image = gr.Image(label=display_message_dict['label_card_design_preview'], type="pil")
                    card_design_upload.change(fn=lambda card_design_upload, card_design_dropdown: update_card_image(card_design_upload, card_design_dropdown, 'Upload'), 
                                              inputs=[card_design_upload, card_design_dropdown], outputs=card_design_preview_image)
                    card_design_dropdown.change(fn=lambda card_design_upload, card_design_dropdown: update_card_image(card_design_upload, card_design_dropdown, 'Drop'), 
                                                inputs=[card_design_upload, card_design_dropdown], outputs=card_design_preview_image)

            with gr.Column():
                description = gr.Textbox(lines=2, label=display_message_dict['label_description'], placeholder=display_message_dict['placeholder_description'])
                is_thick = gr.Checkbox(label=display_message_dict['label_is_thick'], value=False, info=display_message_dict['info_is_thick'])
                picture_image = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="1:1", label=display_message_dict['label_image'])

        button = gr.Button(display_message_dict['label_button'])
        button.click(
                fn=lambda  title, description, is_thick, picture_image, card_design_preview_image:
                    create_3dmodel(None, title, None, None, None, None, description, is_thick, picture_image, card_design_preview_image),
            inputs=[title, description, is_thick, picture_image, card_design_preview_image],
            outputs=[gr.Model3D(camera_position=(90, 90, 5))]
        )
    
    with gr.Tab(display_message_dict['tab_label_historic_site_card']):
        with gr.Row():
            with gr.Column():
                title = gr.Textbox(label=display_message_dict['label_title'], placeholder=display_message_dict['placeholder_title'])
                color = gr.Radio([(color_dict[key], key) for key in color_dict], value=list(color_dict)[0], label=display_message_dict['label_color'])
                
                # Mark(Designation Type) is only visible for Japanese(Because there are no Designation Type Images for other languages).
                # In other languages, mark is only 'No Designation'(No Designation Type Image).
                is_mark_visible = True if language == 'ja' else False
                mark = gr.Radio(display_message_dict['mark_list'], visible=is_mark_visible,
                    value=list(display_message_dict['mark_list'])[len(display_message_dict['mark_list'])-1], label=display_message_dict['label_mark'])
                
                historic_site_type = gr.Textbox(label=display_message_dict["label_historic_site_type"], placeholder=display_message_dict["placeholder_historic_site_type"])
            with gr.Column():
                difficulty = gr.Slider(1, 5, 3, step=1, label=display_message_dict['label_difficulty'])
                description = gr.Textbox(lines=2, label=display_message_dict['label_description'], placeholder=display_message_dict['placeholder_description'])
                is_thick = gr.Checkbox(label=display_message_dict['label_is_thick'], value=False, info=display_message_dict['info_is_thick'])
                picture_image = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", crop_size="1:1", label=display_message_dict['label_image'])

        button = gr.Button(display_message_dict['label_button'])
        button.click(
                fn=lambda title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image:
                    create_3dmodel('1', title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image, None),
            inputs=[title, color, mark, historic_site_type, difficulty, description, is_thick, picture_image],
            outputs=[gr.Model3D(camera_position=(90, 90, 5))]
        )

        gr.Markdown(display_message_dict['footer_historic_site_card'])
        
    with gr.Tab(display_message_dict["tab_label_manhole"]):
        image = gr.ImageEditor(image_mode='RGB', sources="upload", type="pil", label=display_message_dict['label_image'])
        button = gr.Button(display_message_dict['label_button'])
        button.click(
            create_manhole,
            inputs=[image],
            outputs=[gr.Model3D(camera_position=(90, 90, 5))]
        )

demo.launch()