akhaliq HF staff commited on
Commit
8f5cd7f
1 Parent(s): 841b41c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import numpy as np
3
+ import torch
4
+ from torchvision import transforms, models
5
+ from onnx import numpy_helper
6
+ import os
7
+ import onnxruntime as rt
8
+ from matplotlib.colors import hsv_to_rgb
9
+ import cv2
10
+ import gradio as gr
11
+
12
+ import matplotlib.pyplot as plt
13
+ import matplotlib.patches as patches
14
+
15
+ import pycocotools.mask as mask_util
16
+
17
+ def preprocess(image):
18
+ # Resize
19
+ ratio = 800.0 / min(image.size[0], image.size[1])
20
+ image = image.resize((int(ratio * image.size[0]), int(ratio * image.size[1])), Image.BILINEAR)
21
+
22
+ # Convert to BGR
23
+ image = np.array(image)[:, :, [2, 1, 0]].astype('float32')
24
+
25
+ # HWC -> CHW
26
+ image = np.transpose(image, [2, 0, 1])
27
+
28
+ # Normalize
29
+ mean_vec = np.array([102.9801, 115.9465, 122.7717])
30
+ for i in range(image.shape[0]):
31
+ image[i, :, :] = image[i, :, :] - mean_vec[i]
32
+
33
+ # Pad to be divisible of 32
34
+ import math
35
+ padded_h = int(math.ceil(image.shape[1] / 32) * 32)
36
+ padded_w = int(math.ceil(image.shape[2] / 32) * 32)
37
+
38
+ padded_image = np.zeros((3, padded_h, padded_w), dtype=np.float32)
39
+ padded_image[:, :image.shape[1], :image.shape[2]] = image
40
+ image = padded_image
41
+
42
+ return image
43
+
44
+
45
+
46
+ # Start from ORT 1.10, ORT requires explicitly setting the providers parameter if you want to use execution providers
47
+ # other than the default CPU provider (as opposed to the previous behavior of providers getting set/registered by default
48
+ # based on the build flags) when instantiating InferenceSession.
49
+ # For example, if NVIDIA GPU is available and ORT Python package is built with CUDA, then call API as following:
50
+ # onnxruntime.InferenceSession(path/to/model, providers=['CUDAExecutionProvider'])
51
+ os.system("wget https://github.com/AK391/models/raw/main/vision/object_detection_segmentation/mask-rcnn/model/MaskRCNN-10.onnx")
52
+ sess = rt.InferenceSession("MaskRCNN-10.onnx")
53
+
54
+ outputs = sess.get_outputs()
55
+
56
+
57
+ classes = [line.rstrip('\n') for line in open('coco_classes.txt')]
58
+
59
+
60
+ def display_objdetect_image(image, boxes, labels, scores, masks, score_threshold=0.7):
61
+ # Resize boxes
62
+ ratio = 800.0 / min(image.size[0], image.size[1])
63
+ boxes /= ratio
64
+
65
+ _, ax = plt.subplots(1, figsize=(12,9))
66
+
67
+ image = np.array(image)
68
+
69
+ for mask, box, label, score in zip(masks, boxes, labels, scores):
70
+ # Showing boxes with score > 0.7
71
+ if score <= score_threshold:
72
+ continue
73
+
74
+ # Finding contour based on mask
75
+ mask = mask[0, :, :, None]
76
+ int_box = [int(i) for i in box]
77
+ mask = cv2.resize(mask, (int_box[2]-int_box[0]+1, int_box[3]-int_box[1]+1))
78
+ mask = mask > 0.5
79
+ im_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
80
+ x_0 = max(int_box[0], 0)
81
+ x_1 = min(int_box[2] + 1, image.shape[1])
82
+ y_0 = max(int_box[1], 0)
83
+ y_1 = min(int_box[3] + 1, image.shape[0])
84
+ mask_y_0 = max(y_0 - box[1], 0)
85
+ mask_y_1 = mask_y_0 + y_1 - y_0
86
+ mask_x_0 = max(x_0 - box[0], 0)
87
+ mask_x_1 = mask_x_0 + x_1 - x_0
88
+ im_mask[y_0:y_1, x_0:x_1] = mask[
89
+ mask_y_0 : mask_y_1, mask_x_0 : mask_x_1
90
+ ]
91
+ im_mask = im_mask[:, :, None]
92
+
93
+ # OpenCV version 4.x
94
+ contours, hierarchy = cv2.findContours(
95
+ im_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
96
+ )
97
+
98
+ image = cv2.drawContours(image, contours, -1, 25, 3)
99
+
100
+ rect = patches.Rectangle((box[0], box[1]), box[2] - box[0], box[3] - box[1], linewidth=1, edgecolor='b', facecolor='none')
101
+ ax.annotate(classes[label] + ':' + str(np.round(score, 2)), (box[0], box[1]), color='w', fontsize=12)
102
+ ax.add_patch(rect)
103
+
104
+ ax.imshow(image)
105
+ plt.axis('off')
106
+ plt.savefig('out.png', bbox_inches='tight')
107
+
108
+
109
+ def inference(img):
110
+ input_image = Image.open(img)
111
+ orig_tensor = np.asarray(input_image)
112
+ input_tensor = preprocess(input_image)
113
+
114
+ output_names = list(map(lambda output: output.name, outputs))
115
+ input_name = sess.get_inputs()[0].name
116
+
117
+ boxes, labels, scores, masks = sess.run(output_names, {input_name: input_tensor})
118
+ display_objdetect_image(input_image, boxes, labels, scores, masks)
119
+ return 'out.png'
120
+
121
+ title="Mask R-CNN"
122
+ description="This model is a real-time neural network for object instance segmentation that detects 80 different classes."
123
+ examples=[["examplemask-rcnn.jpeg"]]
124
+ gr.Interface(inference,gr.inputs.Image(type="filepath"),gr.outputs.Image(type="file"),title=title,description=description,examples=examples).launch(enable_queue=True)