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()