Deeksha Shetty commited on
Commit
61869eb
·
1 Parent(s): aa59079

update app.py

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