| |
| |
| |
|
|
| from __future__ import absolute_import, print_function, division |
| import requests |
| import cv2 |
| import base64 |
| import numpy as np |
|
|
| |
| |
| |
| def img2b64(img): |
| retval, buffer = cv2.imencode('.bmp', img) |
| pic_str = base64.b64encode(buffer).decode() |
| return pic_str |
|
|
| |
| |
| |
| def b642img(pic_str): |
| img_data = base64.b64decode(pic_str) |
| nparr = np.frombuffer(img_data, np.uint8) |
| img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
| return img_np |
|
|
| |
| |
| |
| def post_files(): |
| path = 'vis_output/my_workspace.JPG' |
| img = cv2.imread(path) |
| if img is None: |
| print(f"Failed to read image at {path}") |
| return |
|
|
| pic_str = img2b64(img) |
| data = { |
| 'img': pic_str, |
| 'prompt': 'Please segment the affordance map of mug in this image.' |
| } |
|
|
| |
| r = requests.post('http://localhost:3200/img_mask', json=data) |
|
|
| if r.status_code == 200: |
| print('Success. Received response from server.') |
| result = r.json() |
| result_b64 = result.get('img', None) |
|
|
| if result_b64: |
| result_img = b642img(result_b64) |
| save_path = 'affordance_mask_result.jpg' |
| cv2.imwrite(save_path, result_img) |
| print(f"Result saved to {save_path}") |
| else: |
| print("No image returned in the response.") |
| else: |
| print(f"Request failed with status code {r.status_code}") |
|
|
| |
| |
| |
| if __name__ == '__main__': |
| post_files() |
|
|
|
|