File size: 2,317 Bytes
2f9a8a8 c88379a 2f9a8a8 c88379a 2f9a8a8 c88379a 7d1537c 2f9a8a8 8db3578 2f9a8a8 c88379a 2f9a8a8 c88379a 36238a7 2f9a8a8 c88379a 2f9a8a8 d532c55 2f9a8a8 7d1537c 2f9a8a8 8db3578 2f9a8a8 7d1537c 2f9a8a8 79948e0 2f9a8a8 cb7cbf4 2f9a8a8 fd3835f 2f9a8a8 |
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 |
import requests
import os, io
import gradio as gr
# from PIL import Image
# API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-panoptic"
SECRET_TOKEN = os.getenv("SECRET_TOKEN")
API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-dc5-panoptic"
headers = {"Authorization": f'Bearer {SECRET_TOKEN}'}
def image_classifier(inp):
return {'cat': 0.3, 'dog': 0.7}
def query(filename):
with open(filename, "rb") as f:
data = f.read()
response = requests.post(API_URL, headers=headers, data=data)
return response.json()
def rb(img):
# initialiaze io to_bytes converter
img_byte_arr = io.BytesIO()
# define quality of saved array
img.save(img_byte_arr, format='JPEG', subsampling=0, quality=100)
# converts image array to bytesarray
img_byte_arr = img_byte_arr.getvalue()
response = requests.post(API_URL, headers=headers, data=img_byte_arr)
return response.json()
inputs = gr.components.Image(type="pil", label="Upload an image")
demo = gr.Interface(fn=rb, inputs=inputs, outputs="json")
demo.launch()
# import io
# import requests
# from PIL import Image
# import torch
# import numpy
# from transformers import DetrFeatureExtractor, DetrForSegmentation
# from transformers.models.detr.feature_extraction_detr import rgb_to_id
# url = "http://images.cocodataset.org/val2017/000000039769.jpg"
# image = Image.open(requests.get(url, stream=True).raw)
# feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50-panoptic")
# model = DetrForSegmentation.from_pretrained("facebook/detr-resnet-50-panoptic")
# # prepare image for the model
# inputs = feature_extractor(images=image, return_tensors="pt")
# # forward pass
# outputs = model(**inputs)
# # use the `post_process_panoptic` method of `DetrFeatureExtractor` to convert to COCO format
# processed_sizes = torch.as_tensor(inputs["pixel_values"].shape[-2:]).unsqueeze(0)
# result = feature_extractor.post_process_panoptic(outputs, processed_sizes)[0]
# # the segmentation is stored in a special-format png
# panoptic_seg = Image.open(io.BytesIO(result["png_string"]))
# panoptic_seg = numpy.array(panoptic_seg, dtype=numpy.uint8)
# # retrieve the ids corresponding to each mask
# panoptic_seg_id = rgb_to_id(panoptic_seg)
|