Spaces:
Sleeping
Sleeping
Wang
commited on
Commit
•
c0930aa
1
Parent(s):
4e2d045
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,142 @@
|
|
1 |
-
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
from six import BytesIO
|
4 |
from PIL import Image
|
@@ -6,11 +144,9 @@ 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
|
|
|
14 |
|
15 |
# Install TensorFlow within the Hugging Face environment
|
16 |
os.system('pip install tensorflow')
|
@@ -18,19 +154,15 @@ os.system('pip install tensorflow')
|
|
18 |
# Now you can import TensorFlow
|
19 |
import tensorflow as tf
|
20 |
|
21 |
-
|
22 |
PATH_TO_LABELS = 'data/label_map.pbtxt'
|
23 |
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
|
24 |
|
25 |
def pil_image_as_numpy_array(pilimg):
|
26 |
-
|
27 |
img_array = tf.keras.utils.img_to_array(pilimg)
|
28 |
img_array = np.expand_dims(img_array, axis=0)
|
29 |
return img_array
|
30 |
|
31 |
def load_image_into_numpy_array(path):
|
32 |
-
|
33 |
-
image = None
|
34 |
image_data = tf.io.gfile.GFile(path, 'rb').read()
|
35 |
image = Image.open(BytesIO(image_data))
|
36 |
return pil_image_as_numpy_array(image)
|
@@ -41,28 +173,15 @@ def load_model():
|
|
41 |
detection_model = tf.saved_model.load(saved_model_dir)
|
42 |
return detection_model
|
43 |
|
44 |
-
def load_model2():
|
45 |
-
wget.download("https://nyp-aicourse.s3-ap-southeast-1.amazonaws.com/pretrained-models/balloon_model.tar.gz")
|
46 |
-
tarfile.open("balloon_model.tar.gz").extractall()
|
47 |
-
model_dir = 'saved_model'
|
48 |
-
detection_model = tf.saved_model.load(str(model_dir))
|
49 |
-
return detection_model
|
50 |
-
|
51 |
-
# samples_folder = 'test_samples
|
52 |
-
# image_path = 'test_samples/sample_balloon.jpeg
|
53 |
-
#
|
54 |
-
|
55 |
def predict(pilimg):
|
56 |
-
|
57 |
image_np = pil_image_as_numpy_array(pilimg)
|
58 |
-
return
|
59 |
-
|
60 |
-
def predict2(image_np):
|
61 |
|
|
|
62 |
results = detection_model(image_np)
|
63 |
|
64 |
-
#
|
65 |
-
result = {key:value.numpy() for key,value in results.items()}
|
66 |
|
67 |
label_id_offset = 0
|
68 |
image_np_with_detections = image_np.copy()
|
@@ -75,16 +194,14 @@ def predict2(image_np):
|
|
75 |
category_index,
|
76 |
use_normalized_coordinates=True,
|
77 |
max_boxes_to_draw=200,
|
78 |
-
min_score_thresh
|
79 |
agnostic_mode=False,
|
80 |
-
line_thickness=2
|
|
|
81 |
|
82 |
result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
|
83 |
-
|
84 |
return result_pil_img
|
85 |
|
86 |
-
import cv2
|
87 |
-
|
88 |
def predict_video(video_path):
|
89 |
cap = cv2.VideoCapture(video_path)
|
90 |
frame_width = int(cap.get(3))
|
@@ -102,7 +219,7 @@ def predict_video(video_path):
|
|
102 |
pil_image = Image.fromarray(frame)
|
103 |
|
104 |
# Perform object detection on the frame
|
105 |
-
result_pil_img =
|
106 |
|
107 |
# Convert the result back to a NumPy array
|
108 |
result_np_img = tf.keras.utils.img_to_array(result_pil_img)
|
@@ -116,24 +233,21 @@ def predict_video(video_path):
|
|
116 |
|
117 |
return "output.avi"
|
118 |
|
119 |
-
|
120 |
-
|
121 |
REPO_ID = "Louisw3399/burgerorfriesdetector"
|
122 |
detection_model = load_model()
|
123 |
-
# pil_image = Image.open(image_path)
|
124 |
-
# image_arr = pil_image_as_numpy_array(pil_image)
|
125 |
-
|
126 |
-
# predicted_img = predict(image_arr)
|
127 |
-
# predicted_img.save('predicted.jpg')
|
128 |
|
129 |
-
gr.Interface(
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
133 |
|
134 |
gr.Interface(
|
135 |
fn=predict_video,
|
136 |
inputs=gr.Video(type="file", label="Upload a video"),
|
137 |
-
outputs=gr.Video(type="file", label="Download the processed video")
|
|
|
138 |
).launch(share=True)
|
139 |
|
|
|
|
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 |
+
# # Install TensorFlow within the Hugging Face environment
|
16 |
+
# os.system('pip install tensorflow')
|
17 |
+
|
18 |
+
# # Now you can import TensorFlow
|
19 |
+
# import tensorflow as tf
|
20 |
+
|
21 |
+
|
22 |
+
# PATH_TO_LABELS = 'data/label_map.pbtxt'
|
23 |
+
# category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
|
24 |
+
|
25 |
+
# def pil_image_as_numpy_array(pilimg):
|
26 |
+
|
27 |
+
# img_array = tf.keras.utils.img_to_array(pilimg)
|
28 |
+
# img_array = np.expand_dims(img_array, axis=0)
|
29 |
+
# return img_array
|
30 |
+
|
31 |
+
# def load_image_into_numpy_array(path):
|
32 |
+
|
33 |
+
# image = None
|
34 |
+
# image_data = tf.io.gfile.GFile(path, 'rb').read()
|
35 |
+
# image = Image.open(BytesIO(image_data))
|
36 |
+
# return pil_image_as_numpy_array(image)
|
37 |
+
|
38 |
+
# def load_model():
|
39 |
+
# download_dir = snapshot_download(REPO_ID)
|
40 |
+
# saved_model_dir = os.path.join(download_dir, "saved_model")
|
41 |
+
# detection_model = tf.saved_model.load(saved_model_dir)
|
42 |
+
# return detection_model
|
43 |
+
|
44 |
+
# def load_model2():
|
45 |
+
# wget.download("https://nyp-aicourse.s3-ap-southeast-1.amazonaws.com/pretrained-models/balloon_model.tar.gz")
|
46 |
+
# tarfile.open("balloon_model.tar.gz").extractall()
|
47 |
+
# model_dir = 'saved_model'
|
48 |
+
# detection_model = tf.saved_model.load(str(model_dir))
|
49 |
+
# return detection_model
|
50 |
+
|
51 |
+
# # samples_folder = 'test_samples
|
52 |
+
# # image_path = 'test_samples/sample_balloon.jpeg
|
53 |
+
# #
|
54 |
+
|
55 |
+
# def predict(pilimg):
|
56 |
+
|
57 |
+
# image_np = pil_image_as_numpy_array(pilimg)
|
58 |
+
# return predict2(image_np)
|
59 |
+
|
60 |
+
# def predict2(image_np):
|
61 |
+
|
62 |
+
# results = detection_model(image_np)
|
63 |
+
|
64 |
+
# # different object detection models have additional results
|
65 |
+
# result = {key:value.numpy() for key,value in results.items()}
|
66 |
+
|
67 |
+
# label_id_offset = 0
|
68 |
+
# image_np_with_detections = image_np.copy()
|
69 |
+
|
70 |
+
# viz_utils.visualize_boxes_and_labels_on_image_array(
|
71 |
+
# image_np_with_detections[0],
|
72 |
+
# result['detection_boxes'][0],
|
73 |
+
# (result['detection_classes'][0] + label_id_offset).astype(int),
|
74 |
+
# result['detection_scores'][0],
|
75 |
+
# category_index,
|
76 |
+
# use_normalized_coordinates=True,
|
77 |
+
# max_boxes_to_draw=200,
|
78 |
+
# min_score_thresh=.60,
|
79 |
+
# agnostic_mode=False,
|
80 |
+
# line_thickness=2)
|
81 |
+
|
82 |
+
# result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
|
83 |
+
|
84 |
+
# return result_pil_img
|
85 |
+
|
86 |
+
# import cv2
|
87 |
+
|
88 |
+
# def predict_video(video_path):
|
89 |
+
# cap = cv2.VideoCapture(video_path)
|
90 |
+
# frame_width = int(cap.get(3))
|
91 |
+
# frame_height = int(cap.get(4))
|
92 |
+
|
93 |
+
# # Define the codec and create a video writer object
|
94 |
+
# out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width, frame_height))
|
95 |
+
|
96 |
+
# while cap.isOpened():
|
97 |
+
# ret, frame = cap.read()
|
98 |
+
# if not ret:
|
99 |
+
# break
|
100 |
+
|
101 |
+
# # Convert the frame to PIL image
|
102 |
+
# pil_image = Image.fromarray(frame)
|
103 |
+
|
104 |
+
# # Perform object detection on the frame
|
105 |
+
# result_pil_img = predict(pil_image)
|
106 |
+
|
107 |
+
# # Convert the result back to a NumPy array
|
108 |
+
# result_np_img = tf.keras.utils.img_to_array(result_pil_img)
|
109 |
+
|
110 |
+
# # Write the frame with detected objects to the video output
|
111 |
+
# out.write(result_np_img.astype('uint8'))
|
112 |
+
|
113 |
+
# # Release the video capture and writer objects
|
114 |
+
# cap.release()
|
115 |
+
# out.release()
|
116 |
+
|
117 |
+
# return "output.avi"
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
# REPO_ID = "Louisw3399/burgerorfriesdetector"
|
122 |
+
# detection_model = load_model()
|
123 |
+
# # pil_image = Image.open(image_path)
|
124 |
+
# # image_arr = pil_image_as_numpy_array(pil_image)
|
125 |
+
|
126 |
+
# # predicted_img = predict(image_arr)
|
127 |
+
# # predicted_img.save('predicted.jpg')
|
128 |
+
|
129 |
+
# gr.Interface(fn=predict,
|
130 |
+
# inputs=gr.Image(type="pil"),
|
131 |
+
# outputs=gr.Image(type="pil")
|
132 |
+
# ).launch(share=True)
|
133 |
+
|
134 |
+
# gr.Interface(
|
135 |
+
# fn=predict_video,
|
136 |
+
# inputs=gr.Video(type="file", label="Upload a video"),
|
137 |
+
# outputs=gr.Video(type="file", label="Download the processed video")
|
138 |
+
# ).launch(share=True)
|
139 |
+
|
140 |
import numpy as np
|
141 |
from six import BytesIO
|
142 |
from PIL import Image
|
|
|
144 |
from object_detection.utils import label_map_util
|
145 |
from object_detection.utils import visualization_utils as viz_utils
|
146 |
from object_detection.utils import ops as utils_op
|
|
|
|
|
|
|
147 |
from huggingface_hub import snapshot_download
|
148 |
+
import gradio as gr
|
149 |
+
import cv2
|
150 |
|
151 |
# Install TensorFlow within the Hugging Face environment
|
152 |
os.system('pip install tensorflow')
|
|
|
154 |
# Now you can import TensorFlow
|
155 |
import tensorflow as tf
|
156 |
|
|
|
157 |
PATH_TO_LABELS = 'data/label_map.pbtxt'
|
158 |
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
|
159 |
|
160 |
def pil_image_as_numpy_array(pilimg):
|
|
|
161 |
img_array = tf.keras.utils.img_to_array(pilimg)
|
162 |
img_array = np.expand_dims(img_array, axis=0)
|
163 |
return img_array
|
164 |
|
165 |
def load_image_into_numpy_array(path):
|
|
|
|
|
166 |
image_data = tf.io.gfile.GFile(path, 'rb').read()
|
167 |
image = Image.open(BytesIO(image_data))
|
168 |
return pil_image_as_numpy_array(image)
|
|
|
173 |
detection_model = tf.saved_model.load(saved_model_dir)
|
174 |
return detection_model
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
def predict(pilimg):
|
|
|
177 |
image_np = pil_image_as_numpy_array(pilimg)
|
178 |
+
return predict_objects(image_np)
|
|
|
|
|
179 |
|
180 |
+
def predict_objects(image_np):
|
181 |
results = detection_model(image_np)
|
182 |
|
183 |
+
# Different object detection models may have additional results
|
184 |
+
result = {key: value.numpy() for key, value in results.items()}
|
185 |
|
186 |
label_id_offset = 0
|
187 |
image_np_with_detections = image_np.copy()
|
|
|
194 |
category_index,
|
195 |
use_normalized_coordinates=True,
|
196 |
max_boxes_to_draw=200,
|
197 |
+
min_score_thresh=0.60,
|
198 |
agnostic_mode=False,
|
199 |
+
line_thickness=2
|
200 |
+
)
|
201 |
|
202 |
result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
|
|
|
203 |
return result_pil_img
|
204 |
|
|
|
|
|
205 |
def predict_video(video_path):
|
206 |
cap = cv2.VideoCapture(video_path)
|
207 |
frame_width = int(cap.get(3))
|
|
|
219 |
pil_image = Image.fromarray(frame)
|
220 |
|
221 |
# Perform object detection on the frame
|
222 |
+
result_pil_img = predict_objects(frame)
|
223 |
|
224 |
# Convert the result back to a NumPy array
|
225 |
result_np_img = tf.keras.utils.img_to_array(result_pil_img)
|
|
|
233 |
|
234 |
return "output.avi"
|
235 |
|
|
|
|
|
236 |
REPO_ID = "Louisw3399/burgerorfriesdetector"
|
237 |
detection_model = load_model()
|
|
|
|
|
|
|
|
|
|
|
238 |
|
239 |
+
gr.Interface(
|
240 |
+
fn=predict,
|
241 |
+
inputs=gr.Image(type="pil"),
|
242 |
+
outputs=gr.Image(type="pil"),
|
243 |
+
label="Image Object Detection"
|
244 |
+
).launch(share=True)
|
245 |
|
246 |
gr.Interface(
|
247 |
fn=predict_video,
|
248 |
inputs=gr.Video(type="file", label="Upload a video"),
|
249 |
+
outputs=gr.Video(type="file", label="Download the processed video"),
|
250 |
+
label="Video Object Detection"
|
251 |
).launch(share=True)
|
252 |
|
253 |
+
|