| import os |
| from PIL import Image |
| from PIL import ImageFile |
| import requests |
| import base64 |
| from io import BytesIO |
|
|
| clip_base = "CLIP(Base)" |
| description = "本项目为CLIP模型的DEMO,可用于图文检索和图像、文本的表征提取,应用于搜索、推荐等应用场景。" |
|
|
| yes = "是" |
| no = "否" |
|
|
| |
| server_ip = os.environ.get("CLIP_SERVER_IP", "127.0.0.1") |
|
|
| clip_service_url_d = { |
| clip_base: f'http://{server_ip}/knn-service', |
| } |
|
|
|
|
| def pil_base64(image, img_format="JPEG"): |
| Image.MAX_IMAGE_PIXELS = 1000000000 |
| ImageFile.LOAD_TRUNCATED_IMAGES = True |
| img_buffer = BytesIO() |
| image.save(img_buffer, format=img_format) |
| byte_data = img_buffer.getvalue() |
| base64_str = base64.b64encode(byte_data) |
| return base64_str.decode("utf-8") |
|
|
|
|
| def url2img(img_url, thumbnail=yes): |
| try: |
| print(img_url, thumbnail) |
| |
| path = img_url.split("9.22.26.31")[1] |
| image = Image.open(path).convert("RGB") |
| max_ = max(image.size) |
| if max_ > 224 and thumbnail == yes: |
| ratio = max_ // 224 |
| image.thumbnail(size=(image.width // ratio, image.height // ratio)) |
| return image |
| except Exception as e: |
| print(e) |
|
|