brandonongsc commited on
Commit
fd9e3a3
1 Parent(s): 8eda296

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -4
app.py CHANGED
@@ -1,7 +1,97 @@
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import numpy as np
3
+ from six import BytesIO
4
+ from PIL import Image
5
+ import tensorflow as tf
6
+ from object_detection.utils import label_map_util
7
+ from object_detection.utils import visualization_utils as viz_utils
8
+ from object_detection.utils import ops as utils_op
9
+ import tarfile
10
+ import wget
11
  import gradio as gr
12
+ from huggingface_hub import snapshot_download
13
+ import os
14
 
15
+ PATH_TO_LABELS = 'data/label_map.pbtxt'
16
+ category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
17
 
18
+ def pil_image_as_numpy_array(pilimg):
19
+
20
+ img_array = tf.keras.utils.img_to_array(pilimg)
21
+ img_array = np.expand_dims(img_array, axis=0)
22
+ return img_array
23
+
24
+ def load_image_into_numpy_array(path):
25
+
26
+ image = None
27
+ image_data = tf.io.gfile.GFile(path, 'rb').read()
28
+ image = Image.open(BytesIO(image_data))
29
+ return pil_image_as_numpy_array(image)
30
+
31
+ def load_model():
32
+ download_dir = snapshot_download(REPO_ID)
33
+ saved_model_dir = os.path.join(download_dir, "saved_model")
34
+ detection_model = tf.saved_model.load(saved_model_dir)
35
+ return detection_model
36
+
37
+ def load_model2():
38
+ wget.download("https://drive.google.com/uc?export=download&id=1MasePsUVgjfWofmUhmMCvruOHuZMg7KG")
39
+ #tarfile.open("balloon_model.tar.gz").extractall()
40
+
41
+ import zipfile
42
+ with zipfile.ZipFile("masknomask_detect_model.zip", 'r') as zip_ref:
43
+ zip_ref.extractall("saved_model")
44
+
45
+
46
+ model_dir = 'saved_model'
47
+ detection_model = tf.saved_model.load(str(model_dir))
48
+ return detection_model
49
+
50
+ # samples_folder = 'test_samples
51
+ # image_path = 'test_samples/sample_balloon.jpeg
52
+ #
53
+
54
+ def predict(pilimg):
55
+
56
+ image_np = pil_image_as_numpy_array(pilimg)
57
+ return predict2(image_np)
58
+
59
+ def predict2(image_np):
60
+
61
+ results = detection_model(image_np)
62
+
63
+ # different object detection models have additional results
64
+ result = {key:value.numpy() for key,value in results.items()}
65
+
66
+ label_id_offset = 0
67
+ image_np_with_detections = image_np.copy()
68
+
69
+ viz_utils.visualize_boxes_and_labels_on_image_array(
70
+ image_np_with_detections[0],
71
+ result['detection_boxes'][0],
72
+ (result['detection_classes'][0] + label_id_offset).astype(int),
73
+ result['detection_scores'][0],
74
+ category_index,
75
+ use_normalized_coordinates=True,
76
+ max_boxes_to_draw=200,
77
+ min_score_thresh=.60,
78
+ agnostic_mode=False,
79
+ line_thickness=2)
80
+
81
+ result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
82
+
83
+ return result_pil_img
84
+
85
+
86
+ REPO_ID = "brandonongsc/masknomask_detect_model"
87
+ detection_model = load_model()
88
+ # pil_image = Image.open(image_path)
89
+ # image_arr = pil_image_as_numpy_array(pil_image)
90
+
91
+ # predicted_img = predict(image_arr)
92
+ # predicted_img.save('predicted.jpg')
93
+
94
+ gr.Interface(fn=predict,
95
+ inputs=gr.Image(type="pil"),
96
+ outputs=gr.Image(type="pil")
97
+ ).launch(share=True)