akhaliq HF Staff commited on
Commit
81f3097
·
1 Parent(s): 4b6d935

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import math
3
+ import matplotlib.pyplot as plt
4
+ import onnxruntime as rt
5
+ import cv2
6
+ import json
7
+ import gradio as gr
8
+ from huggingface_hub import hf_hub_download
9
+ import onnxruntime as rt
10
+
11
+ modele = hf_hub_download(repo_id="onnx/EfficientNet-Lite4", filename="efficientnet-lite4-11.onnx")
12
+ # load the labels text file
13
+ labels = json.load(open("labels_map.txt", "r"))
14
+
15
+ # set image file dimensions to 224x224 by resizing and cropping image from center
16
+ def pre_process_edgetpu(img, dims):
17
+ output_height, output_width, _ = dims
18
+ img = resize_with_aspectratio(img, output_height, output_width, inter_pol=cv2.INTER_LINEAR)
19
+ img = center_crop(img, output_height, output_width)
20
+ img = np.asarray(img, dtype='float32')
21
+ # converts jpg pixel value from [0 - 255] to float array [-1.0 - 1.0]
22
+ img -= [127.0, 127.0, 127.0]
23
+ img /= [128.0, 128.0, 128.0]
24
+ return img
25
+
26
+ # resize the image with a proportional scale
27
+ def resize_with_aspectratio(img, out_height, out_width, scale=87.5, inter_pol=cv2.INTER_LINEAR):
28
+ height, width, _ = img.shape
29
+ new_height = int(100. * out_height / scale)
30
+ new_width = int(100. * out_width / scale)
31
+ if height > width:
32
+ w = new_width
33
+ h = int(new_height * height / width)
34
+ else:
35
+ h = new_height
36
+ w = int(new_width * width / height)
37
+ img = cv2.resize(img, (w, h), interpolation=inter_pol)
38
+ return img
39
+
40
+ # crop the image around the center based on given height and width
41
+ def center_crop(img, out_height, out_width):
42
+ height, width, _ = img.shape
43
+ left = int((width - out_width) / 2)
44
+ right = int((width + out_width) / 2)
45
+ top = int((height - out_height) / 2)
46
+ bottom = int((height + out_height) / 2)
47
+ img = img[top:bottom, left:right]
48
+ return img
49
+
50
+
51
+ sess = rt.InferenceSession(modele)
52
+
53
+ def inference(img):
54
+ img = cv2.imread(img)
55
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
56
+
57
+ img = pre_process_edgetpu(img, (224, 224, 3))
58
+
59
+ img_batch = np.expand_dims(img, axis=0)
60
+
61
+ results = sess.run(["Softmax:0"], {"images:0": img_batch})[0]
62
+ result = reversed(results[0].argsort()[-5:])
63
+ resultdic = {}
64
+ for r in result:
65
+ resultdic[labels[str(r)]] = float(results[0][r])
66
+ return resultdic
67
+
68
+ title="EfficientNet-Lite4"
69
+ description="EfficientNet-Lite 4 is the largest variant and most accurate of the set of EfficientNet-Lite model. It is an integer-only quantized model that produces the highest accuracy of all of the EfficientNet models. It achieves 80.4% ImageNet top-1 accuracy, while still running in real-time (e.g. 30ms/image) on a Pixel 4 CPU."
70
+ examples=[['catonnx.jpg']]
71
+ gr.Interface(inference,gr.inputs.Image(type="filepath"),"label",title=title,description=description,examples=examples).launch()