Commit
·
2f9a8a8
1
Parent(s):
c88379a
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,67 @@
|
|
| 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 |
-
import io
|
| 39 |
-
import requests
|
| 40 |
-
from PIL import Image
|
| 41 |
-
import torch
|
| 42 |
-
import numpy
|
| 43 |
|
| 44 |
-
from transformers import DetrFeatureExtractor, DetrForSegmentation
|
| 45 |
-
from transformers.models.detr.feature_extraction_detr import rgb_to_id
|
| 46 |
|
| 47 |
-
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 48 |
-
image = Image.open(requests.get(url, stream=True).raw)
|
| 49 |
|
| 50 |
-
feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50-panoptic")
|
| 51 |
-
model = DetrForSegmentation.from_pretrained("facebook/detr-resnet-50-panoptic")
|
| 52 |
|
| 53 |
-
# prepare image for the model
|
| 54 |
-
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 55 |
|
| 56 |
-
# forward pass
|
| 57 |
-
outputs = model(**inputs)
|
| 58 |
|
| 59 |
-
# use the `post_process_panoptic` method of `DetrFeatureExtractor` to convert to COCO format
|
| 60 |
-
processed_sizes = torch.as_tensor(inputs["pixel_values"].shape[-2:]).unsqueeze(0)
|
| 61 |
-
result = feature_extractor.post_process_panoptic(outputs, processed_sizes)[0]
|
| 62 |
|
| 63 |
-
# the segmentation is stored in a special-format png
|
| 64 |
-
panoptic_seg = Image.open(io.BytesIO(result["png_string"]))
|
| 65 |
-
panoptic_seg = numpy.array(panoptic_seg, dtype=numpy.uint8)
|
| 66 |
-
# retrieve the ids corresponding to each mask
|
| 67 |
-
panoptic_seg_id = rgb_to_id(panoptic_seg)
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os, io
|
| 3 |
+
import gradio as gr
|
| 4 |
+
# from PIL import Image
|
| 5 |
|
| 6 |
+
# API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-panoptic"
|
| 7 |
|
| 8 |
+
SECRET_TOKEN = os.getenv("SECRET_TOKEN")
|
| 9 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-dc5-panoptic"
|
| 10 |
+
headers = {"Authorization": f'Bearer {SECRET_TOKEN}'}
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
+
def image_classifier(inp):
|
| 15 |
+
return {'cat': 0.3, 'dog': 0.7}
|
| 16 |
|
| 17 |
+
def query(filename):
|
| 18 |
+
with open(filename, "rb") as f:
|
| 19 |
+
data = f.read()
|
| 20 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
| 21 |
+
return response.json()
|
| 22 |
|
| 23 |
+
def rb(img):
|
| 24 |
+
# initialiaze io to_bytes converter
|
| 25 |
+
img_byte_arr = io.BytesIO()
|
| 26 |
+
# define quality of saved array
|
| 27 |
+
img.save(img_byte_arr, format='JPEG', subsampling=0, quality=100)
|
| 28 |
+
# converts image array to bytesarray
|
| 29 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 30 |
+
response = requests.post(API_URL, headers=headers, data=img_byte_arr)
|
| 31 |
+
return response.json()
|
| 32 |
|
| 33 |
|
| 34 |
+
inputs = gr.inputs.Image(type="pil", label="Upload an image")
|
| 35 |
+
demo = gr.Interface(fn=rb, inputs=inputs, outputs="json")
|
| 36 |
+
demo.launch()
|
| 37 |
|
| 38 |
+
# import io
|
| 39 |
+
# import requests
|
| 40 |
+
# from PIL import Image
|
| 41 |
+
# import torch
|
| 42 |
+
# import numpy
|
| 43 |
|
| 44 |
+
# from transformers import DetrFeatureExtractor, DetrForSegmentation
|
| 45 |
+
# from transformers.models.detr.feature_extraction_detr import rgb_to_id
|
| 46 |
|
| 47 |
+
# url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 48 |
+
# image = Image.open(requests.get(url, stream=True).raw)
|
| 49 |
|
| 50 |
+
# feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50-panoptic")
|
| 51 |
+
# model = DetrForSegmentation.from_pretrained("facebook/detr-resnet-50-panoptic")
|
| 52 |
|
| 53 |
+
# # prepare image for the model
|
| 54 |
+
# inputs = feature_extractor(images=image, return_tensors="pt")
|
| 55 |
|
| 56 |
+
# # forward pass
|
| 57 |
+
# outputs = model(**inputs)
|
| 58 |
|
| 59 |
+
# # use the `post_process_panoptic` method of `DetrFeatureExtractor` to convert to COCO format
|
| 60 |
+
# processed_sizes = torch.as_tensor(inputs["pixel_values"].shape[-2:]).unsqueeze(0)
|
| 61 |
+
# result = feature_extractor.post_process_panoptic(outputs, processed_sizes)[0]
|
| 62 |
|
| 63 |
+
# # the segmentation is stored in a special-format png
|
| 64 |
+
# panoptic_seg = Image.open(io.BytesIO(result["png_string"]))
|
| 65 |
+
# panoptic_seg = numpy.array(panoptic_seg, dtype=numpy.uint8)
|
| 66 |
+
# # retrieve the ids corresponding to each mask
|
| 67 |
+
# panoptic_seg_id = rgb_to_id(panoptic_seg)
|