File size: 4,382 Bytes
117b368
 
 
 
 
88e28b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117b368
 
 
 
 
 
88e28b1
 
117b368
 
 
 
 
88e28b1
 
117b368
 
88e28b1
 
117b368
 
88e28b1
 
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88e28b1
 
117b368
 
 
 
 
 
88e28b1
 
117b368
 
 
 
88e28b1
 
 
 
 
117b368
88e28b1
 
 
 
 
 
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88e28b1
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
import gradio as gr

from eval_fashion_person import *
from name_entity_recognition import *
from utils.operate_json import *
import logging

# Create a logger
logger = logging.getLogger(__name__)

# Set the logging level
logger.setLevel(logging.DEBUG)

# Create a file handler
file_handler = logging.FileHandler('my_app.log')

# Create a formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Add the formatter to the handler
file_handler.setFormatter(formatter)

# Add the handler to the logger
logger.addHandler(file_handler)


def eval_image(input_image, progress=gr.Progress()):
    if input_image is None:
        raise gr.Error("未上传图片,请先上传图片")
    progress((0, 3), desc="上传图片中")
    logger.debug("上传图片中")

    img_name = get_one_name()
    input_image = output_to_binary(input_image)
    save_tx(file_data=input_image,
            file_name=img_name)
    progress((1, 3), desc="评估分数中")
    logger.debug("评估分数中")

    data = eval_fashion_person_tx_url(img_name)
    progress((2, 3), desc="识别类型中")
    logger.debug("识别类型中")

    reason = data['fashion_eval_reason']
    score = data['fashion_score_predict']
    logger.debug(data, reason, score)

    score = ("未识别出" if score == -1 else score)
    data = {}
    while not data:
        text = name_entity_recognition(reason)
        data = text_to_json(text)
    keys_to_check = [
        'type',
        'upper_garment',
        'lower_garment',
        'headgear',
        'sock',
        'shoe',
        'accessory',
        'backpack',
        'action',
        'countenance'
    ]
    data = {k: ("未识别出" if data.get(k) is None else data[k]) for k in keys_to_check}
    data = {k: ("未识别出" if v == "None" else v) for k, v in data.items()}
    result = [reason,
              score,
              data['type'],
              data['upper_garment'],
              data['lower_garment'],
              data['headgear'],
              data['sock'],
              data['shoe'],
              data['accessory'],
              data['backpack'],
              data['action'],
              data['countenance']]
    return result


with gr.Blocks() as demo:
    demo.title = "时尚买手小玲智能体"  # Add title
    demo.description = "上传一张人物图片,让时尚买手小玲为你评估穿搭!"  # Add description
    with gr.Row():
        with gr.Column():
            Image_input = gr.Image(label="请上传图片",
                                   height=640,
                                   show_share_button=False)
            with gr.Row():
                run_button = gr.Button(value="智能时尚评估")
                score = gr.Textbox(label="认知计算时尚总得分")
        with gr.Column():
            with gr.Group():
                with gr.Row():
                    with gr.Column():
                        type = gr.Textbox(label="认知-人物类型")
                        headgear = gr.Textbox(label="认知-帽子类型")
                        shoe = gr.Textbox(label="认知-鞋子类型")
                        backpack = gr.Textbox(label="认知-背包类型")
                        countenance = gr.Textbox(label="认知-表情类型")
                    with gr.Column():
                        upper_garment = gr.Textbox(label="认知-上衣类型")
                        lower_garment = gr.Textbox(label="认知-下衣类型")
                        sock = gr.Textbox(label="认知-袜子类型")
                        accessory = gr.Textbox(label="认知-配饰类型")
                        action = gr.Textbox(label="认知-动作类型")
            text_output = gr.Textbox(label="小玲时尚穿搭建议")

    run_button.click(eval_image,
                     inputs=Image_input,
                     outputs=[text_output,
                              score,
                              type,
                              upper_garment,
                              lower_garment,
                              headgear,
                              sock,
                              shoe,
                              accessory,
                              backpack,
                              action,
                              countenance])

demo.queue().launch(share=True)