File size: 2,328 Bytes
25c9c68
33c5b8a
 
25c9c68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33c5b8a
 
 
 
 
 
25c9c68
 
6ca388e
 
 
 
25c9c68
6ca388e
25c9c68
6ca388e
25c9c68
33c5b8a
 
 
 
 
 
25c9c68
 
 
 
33c5b8a
 
25c9c68
 
 
 
33c5b8a
25c9c68
6ca388e
 
 
25c9c68
33c5b8a
6ca388e
25c9c68
 
 
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
import requests
from PIL import Image
import base64
import gradio as gr

def analyze_images(image1, image2, api_key, org_id):
    url = f"https://platform.bodygram.com/api/orgs/{org_id}/scans"

    headers = {
        "Authorization": api_key,
    }

    with open(image1, "rb") as img1_file:
        image1_b64 = base64.b64encode(img1_file.read()).decode('utf-8')

    with open(image2, "rb") as img2_file:
        image2_b64 = base64.b64encode(img2_file.read()).decode('utf-8')

    data = {
        "customScanId": "myFirstScan",
        "photoScan": {
            "age": 29,
            "weight": 54000,
            "height": 1640,
            "gender": "female",
            "frontPhoto": image1_b64,
            "rightPhoto": image2_b64,
        },
    }

    response = requests.post(url, headers=headers, json=data).json()

    if 'entry' not in response.keys():
        return f"Request failed: {response}"

    response = response['entry']
    print(response.keys())

    obj_path = 'avatar.obj'
    with open(obj_path, 'wb') as avatar_file:
        avatar_file.write(base64.b64decode(response['avatar']['data']))

    if response['status'] == 'success':
        return {key: value for key, value in response.items() if key != 'avatar'}, obj_path
    else:
        return f"Request failed: {response['text']}", ''


def assign_sample_images():
    image1 = Image.open('./front.jpg')
    image2 = Image.open('./right.jpg')
    return image1, image2

with gr.Blocks() as demo:
    gr.Markdown("## 画像解析アプリ")
    
    with gr.Row():
        image1 = gr.Image(type="filepath", label="前からの写真", format='jpeg')
        image2 = gr.Image(type="filepath", label="右側からの写真", format='jpeg')
    
    api_key = gr.Textbox(label="APIキーを入力")
    org_id = gr.Textbox(label="組織IDを入力")
    
    sample_button = gr.Button("サンプル画像")
    analyze_button = gr.Button("解析", variant='primary')
    with gr.Row():
        avatar_model = gr.Model3D(label="アバター")
        result = gr.TextArea(label="解析結果")
    
    sample_button.click(assign_sample_images, inputs=[], outputs=[image1, image2])
    analyze_button.click(analyze_images, inputs=[image1, image2, api_key, org_id], outputs=[result, avatar_model])

if __name__ == '__main__':
    demo.launch()