yuragoithf commited on
Commit
c88379a
1 Parent(s): 77203cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -27
app.py CHANGED
@@ -1,36 +1,67 @@
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()
 
 
 
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)