23B708T commited on
Commit
d20ef10
1 Parent(s): 4d16fe3

Upload 6 files

Browse files
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Tfoddemo
3
+ emoji: 🐢
4
+ colorFrom: indigo
5
+ colorTo: red
6
+ python_version: 3.8
7
+ sdk: gradio
8
+ sdk_version: 4.0.2
9
+ app_file: app.py
10
+ pinned: false
11
+ license: apache-2.0
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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://nyp-aicourse.s3-ap-southeast-1.amazonaws.com/pretrained-models/balloon_model.tar.gz")
39
+ tarfile.open("balloon_model.tar.gz").extractall()
40
+ model_dir = 'saved_model'
41
+ detection_model = tf.saved_model.load(str(model_dir))
42
+ return detection_model
43
+
44
+ # samples_folder = 'test_samples
45
+ # image_path = 'test_samples/sample_balloon.jpeg
46
+ #
47
+
48
+ def predict(pilimg):
49
+
50
+ image_np = pil_image_as_numpy_array(pilimg)
51
+ return predict2(image_np)
52
+
53
+ def predict2(image_np):
54
+
55
+ results = detection_model(image_np)
56
+
57
+ # different object detection models have additional results
58
+ result = {key:value.numpy() for key,value in results.items()}
59
+
60
+ label_id_offset = 0
61
+ image_np_with_detections = image_np.copy()
62
+
63
+ viz_utils.visualize_boxes_and_labels_on_image_array(
64
+ image_np_with_detections[0],
65
+ result['detection_boxes'][0],
66
+ (result['detection_classes'][0] + label_id_offset).astype(int),
67
+ result['detection_scores'][0],
68
+ category_index,
69
+ use_normalized_coordinates=True,
70
+ max_boxes_to_draw=200,
71
+ min_score_thresh=.60,
72
+ agnostic_mode=False,
73
+ line_thickness=2)
74
+
75
+ result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
76
+
77
+ return result_pil_img
78
+
79
+
80
+ REPO_ID = "khengkok/mkktfodmodel"
81
+ detection_model = load_model()
82
+ # pil_image = Image.open(image_path)
83
+ # image_arr = pil_image_as_numpy_array(pil_image)
84
+
85
+ # predicted_img = predict(image_arr)
86
+ # predicted_img.save('predicted.jpg')
87
+
88
+ gr.Interface(fn=predict,
89
+ inputs=gr.Image(type="pil"),
90
+ outputs=gr.Image(type="pil")
91
+ ).launch(share=True)
data/label_map.pbtxt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ item {
2
+ id: 1
3
+ name: 'balloon'
4
+ }
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #tf2-tensorflow-object-detection-api
2
+ tf-models-research-object-detection
3
+ matplotlib
4
+ wget
5
+ Pillow==9.5
6
+ huggingface_hub
test_samples/sample_balloon.jpeg ADDED