Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,225 +1,14 @@
|
|
1 |
-
# import gradio as gr
|
2 |
-
# import cv2
|
3 |
-
# import numpy as np
|
4 |
-
# import onnxruntime as ort
|
5 |
-
|
6 |
-
# # Load the ONNX model using onnxruntime
|
7 |
-
# onnx_model_path = "Model_IV.onnx" # Update with your ONNX model path
|
8 |
-
# session = ort.InferenceSession(onnx_model_path)
|
9 |
-
|
10 |
-
# # Function to perform object detection with the ONNX model
|
11 |
-
# def detect_objects(frame, confidence_threshold=0.5):
|
12 |
-
# # Convert the frame from BGR (OpenCV) to RGB
|
13 |
-
# image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
14 |
-
|
15 |
-
# # Preprocessing: Resize and normalize the image
|
16 |
-
# # Assuming YOLO model input is 640x640, update according to your model's input size
|
17 |
-
# input_size = (640, 640)
|
18 |
-
# image_resized = cv2.resize(image, input_size)
|
19 |
-
# image_normalized = image_resized / 255.0 # Normalize to [0, 1]
|
20 |
-
# image_input = np.transpose(image_normalized, (2, 0, 1)) # Change to CHW format
|
21 |
-
# image_input = np.expand_dims(image_input, axis=0).astype(np.float32) # Add batch dimension
|
22 |
-
|
23 |
-
# # Perform inference
|
24 |
-
# inputs = {session.get_inputs()[0].name: image_input}
|
25 |
-
# outputs = session.run(None, inputs)
|
26 |
-
|
27 |
-
# # # Assuming YOLO model outputs are in the form of [boxes, confidences, class_probs]
|
28 |
-
# # boxes, confidences, class_probs = outputs
|
29 |
-
|
30 |
-
# # # Post-processing: Filter boxes by confidence threshold
|
31 |
-
# # detections = []
|
32 |
-
# # for i, confidence in enumerate(confidences[0]):
|
33 |
-
# # if confidence >= confidence_threshold:
|
34 |
-
# # x1, y1, x2, y2 = boxes[0][i]
|
35 |
-
# # class_id = np.argmax(class_probs[0][i]) # Get class with highest probability
|
36 |
-
# # detections.append((x1, y1, x2, y2, confidence, class_id))
|
37 |
-
|
38 |
-
# # # Draw bounding boxes and labels on the image
|
39 |
-
# # for (x1, y1, x2, y2, confidence, class_id) in detections:
|
40 |
-
# # color = (0, 255, 0) # Green color for bounding boxes
|
41 |
-
# # cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
|
42 |
-
# # label = f"Class {class_id}: {confidence:.2f}"
|
43 |
-
# # cv2.putText(image, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
44 |
-
|
45 |
-
# # # Convert the image back to BGR for displaying in Gradio
|
46 |
-
# # image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
47 |
-
|
48 |
-
# return outputs
|
49 |
-
|
50 |
-
# # Gradio interface to use the webcam for real-time object detection
|
51 |
-
# # Added a slider for the confidence threshold
|
52 |
-
# iface = gr.Interface(fn=detect_objects,
|
53 |
-
# #inputs=[
|
54 |
-
# # gr.Video(sources="webcam", type="numpy"), # Webcam input
|
55 |
-
# inputs = gr.Image(sources=["webcam"], type="numpy"),
|
56 |
-
# # gr.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Confidence Threshold") # Confidence slider
|
57 |
-
# # ],
|
58 |
-
# outputs="image") # Show output image with bounding boxes
|
59 |
-
|
60 |
-
# iface.launch()
|
61 |
-
###
|
62 |
-
# import gradio as gr
|
63 |
-
# import cv2
|
64 |
-
# from huggingface_hub import hf_hub_download
|
65 |
-
# from gradio_webrtc import WebRTC
|
66 |
-
# from twilio.rest import Client
|
67 |
-
# import os
|
68 |
-
# from inference import YOLOv8
|
69 |
-
|
70 |
-
# model_file = hf_hub_download(
|
71 |
-
# repo_id="aje6/ASL-Fingerspelling-Detection", filename="onnx/Model_IV.onnx"
|
72 |
-
# )
|
73 |
-
|
74 |
-
# model = YOLOv8(model_file)
|
75 |
-
|
76 |
-
# account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
|
77 |
-
# auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
|
78 |
-
|
79 |
-
# if account_sid and auth_token:
|
80 |
-
# client = Client(account_sid, auth_token)
|
81 |
-
|
82 |
-
# token = client.tokens.create()
|
83 |
-
|
84 |
-
# rtc_configuration = {
|
85 |
-
# "iceServers": token.ice_servers,
|
86 |
-
# "iceTransportPolicy": "relay",
|
87 |
-
# }
|
88 |
-
# else:
|
89 |
-
# rtc_configuration = None
|
90 |
-
|
91 |
-
|
92 |
-
# def detection(image, conf_threshold=0.3):
|
93 |
-
# image = cv2.resize(image, (model.input_width, model.input_height))
|
94 |
-
# new_image = model.detect_objects(image, conf_threshold)
|
95 |
-
# return cv2.resize(new_image, (500, 500))
|
96 |
-
|
97 |
-
|
98 |
-
# css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
99 |
-
# .my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
|
100 |
-
|
101 |
-
|
102 |
-
# with gr.Blocks(css=css) as demo:
|
103 |
-
# gr.HTML(
|
104 |
-
# """
|
105 |
-
# <h1 style='text-align: center'>
|
106 |
-
# YOLOv10 Webcam Stream (Powered by WebRTC ⚡️)
|
107 |
-
# </h1>
|
108 |
-
# """
|
109 |
-
# )
|
110 |
-
# gr.HTML(
|
111 |
-
# """
|
112 |
-
# <h3 style='text-align: center'>
|
113 |
-
# <a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
|
114 |
-
# </h3>
|
115 |
-
# """
|
116 |
-
# )
|
117 |
-
# with gr.Column(elem_classes=["my-column"]):
|
118 |
-
# with gr.Group(elem_classes=["my-group"]):
|
119 |
-
# image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
120 |
-
# conf_threshold = gr.Slider(
|
121 |
-
# label="Confidence Threshold",
|
122 |
-
# minimum=0.0,
|
123 |
-
# maximum=1.0,
|
124 |
-
# step=0.05,
|
125 |
-
# value=0.30,
|
126 |
-
# )
|
127 |
-
|
128 |
-
# image.stream(
|
129 |
-
# fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
|
130 |
-
# )
|
131 |
-
|
132 |
-
# if __name__ == "__main__":
|
133 |
-
# demo.launch()
|
134 |
-
|
135 |
-
# import gradio as gr
|
136 |
-
# import numpy as np
|
137 |
-
# import cv2
|
138 |
-
# from ultralytics import YOLO
|
139 |
-
|
140 |
-
# model = YOLO('Model_IV.pt')
|
141 |
-
|
142 |
-
# def transform_cv2(frame, transform):
|
143 |
-
# if transform == "cartoon":
|
144 |
-
# # prepare color
|
145 |
-
# img_color = cv2.pyrDown(cv2.pyrDown(frame))
|
146 |
-
# for _ in range(6):
|
147 |
-
# img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
|
148 |
-
# img_color = cv2.pyrUp(cv2.pyrUp(img_color))
|
149 |
-
|
150 |
-
# # prepare edges
|
151 |
-
# img_edges = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
152 |
-
# img_edges = cv2.adaptiveThreshold(
|
153 |
-
# cv2.medianBlur(img_edges, 7),
|
154 |
-
# 255,
|
155 |
-
# cv2.ADAPTIVE_THRESH_MEAN_C,
|
156 |
-
# cv2.THRESH_BINARY,
|
157 |
-
# 9,
|
158 |
-
# 2,
|
159 |
-
# )
|
160 |
-
# img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
|
161 |
-
# # combine color and edges
|
162 |
-
# img = cv2.bitwise_and(img_color, img_edges)
|
163 |
-
# return img
|
164 |
-
# elif transform == "edges":
|
165 |
-
# # perform edge detection
|
166 |
-
# img = cv2.cvtColor(cv2.Canny(frame, 100, 200), cv2.COLOR_GRAY2BGR)
|
167 |
-
# return img
|
168 |
-
# else:
|
169 |
-
# return np.flipud(frame)
|
170 |
-
|
171 |
-
# with gr.Blocks() as demo:
|
172 |
-
# with gr.Row():
|
173 |
-
# with gr.Column():
|
174 |
-
# transform = gr.Dropdown(choices=["cartoon", "edges", "flip"],
|
175 |
-
# value="flip", label="Transformation")
|
176 |
-
# input_img = gr.Image(sources=["webcam"], type="numpy")
|
177 |
-
# with gr.Column():
|
178 |
-
# output_img = gr.Image(streaming=True)
|
179 |
-
# dep = input_img.stream(transform_cv2, [input_img, transform], [output_img],
|
180 |
-
# time_limit=30, stream_every=0.1, concurrency_limit=30)
|
181 |
-
|
182 |
-
# if __name__ == "__main__":
|
183 |
-
# demo.launch()
|
184 |
-
|
185 |
-
###
|
186 |
-
|
187 |
-
# import gradio as gr
|
188 |
-
# import torch
|
189 |
-
# import cv2
|
190 |
-
|
191 |
-
# # Load the YOLOv8 model
|
192 |
-
# model = torch.hub.load('ultralytics/yolov8', 'yolov8s', trust_repo=True)
|
193 |
-
# model.load_state_dict(torch.load('Model_IV'))
|
194 |
-
|
195 |
-
# def inference(img):
|
196 |
-
# results = model(img)
|
197 |
-
# annotated_img = results.render()[0]
|
198 |
-
# return annotated_img
|
199 |
-
|
200 |
-
# iface = gr.Interface(fn=inference, inputs="webcam", outputs="image")
|
201 |
-
# iface.launch()
|
202 |
-
|
203 |
import gradio as gr
|
204 |
import torch
|
205 |
from PIL import Image
|
206 |
import torchvision.transforms as T
|
207 |
from ultralytics import YOLO
|
208 |
-
# import onnxruntime as ort
|
209 |
import cv2
|
210 |
import numpy as np
|
211 |
|
212 |
-
# Load
|
213 |
-
|
214 |
model = YOLO("Model_IV.pt")
|
215 |
-
# model = torch.load("Model_IV.pt")
|
216 |
-
# model.eval()
|
217 |
checkpoint = torch.load("Model_IV.pt")
|
218 |
-
# model.load_state_dict(checkpoint) # Load the saved weights
|
219 |
-
# model.eval() # Set the model to evaluation mode
|
220 |
-
|
221 |
-
# Load the onnx model
|
222 |
-
# model = ort.InferenceSession("Model_IV.onnx")
|
223 |
|
224 |
# Define preprocessing
|
225 |
transform = T.Compose([
|
@@ -239,83 +28,6 @@ def predict(image):
|
|
239 |
results = model(image)
|
240 |
annotated_img = results[0].plot()
|
241 |
return annotated_img
|
242 |
-
|
243 |
-
# # Preprocess the image
|
244 |
-
|
245 |
-
# # Get name and shape of the model's inputs
|
246 |
-
# input_name = model.get_inputs()[0].name
|
247 |
-
# input_shape = model.get_inputs()[0].shape
|
248 |
-
|
249 |
-
# # Resize the image to the model's input shape
|
250 |
-
# image = cv2.resize(image, (input_shape[2], input_shape[3]))
|
251 |
-
|
252 |
-
# original_image_shape = image.shape
|
253 |
-
# print("Original image shape:", original_image_shape)
|
254 |
-
|
255 |
-
# # Reshape the image to match the model's input shape
|
256 |
-
# image = image.reshape(3, 640, 640)
|
257 |
-
|
258 |
-
# # Normalize output image using ImageNet-style normalization
|
259 |
-
# mean = [0.485, 0.456, 0.406]
|
260 |
-
# std = [0.229, 0.224, 0.225]
|
261 |
-
# mean = np.expand_dims(mean, axis=(1,2))
|
262 |
-
# std = np.expand_dims(std, axis=(1,2))
|
263 |
-
# image = (image / 255.0 - mean)/std
|
264 |
-
|
265 |
-
# # Convert the image to a numpy array and add a batch dimension
|
266 |
-
# if len(input_shape) == 4 and input_shape[0] == 1:
|
267 |
-
# image = np.expand_dims(image, axis=0)
|
268 |
-
# image = image.astype(np.float32)
|
269 |
-
|
270 |
-
# print("Input image shape:", image.shape)
|
271 |
-
|
272 |
-
# # Make prediction
|
273 |
-
# output = model.run(None, {input_name: image})
|
274 |
-
|
275 |
-
# # print("Output shape:", output.shape)
|
276 |
-
|
277 |
-
# # print("type output:", type(output))
|
278 |
-
# # print(output)
|
279 |
-
|
280 |
-
# # Postprocess output image
|
281 |
-
|
282 |
-
# annotated_img = output[0]
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
# # annotated_img = (output[0] / 255.0 - mean)/std
|
287 |
-
# # annotated_img = classes[output[0][0].argmax(0)]
|
288 |
-
|
289 |
-
# print("Annotated image type before normalization:", type(annotated_img))
|
290 |
-
# # print("Annotated image before normalization:", annotated_img)
|
291 |
-
# print("Min value of image before normalization:", np.min(annotated_img))
|
292 |
-
# print("Max value of image before normalization:", np.max(annotated_img))
|
293 |
-
|
294 |
-
# # # Normalize output image using ImageNet-style normalization (again)
|
295 |
-
# # annotated_img = (annotated_img / 255.0 - mean)/std
|
296 |
-
|
297 |
-
# # Normalize output image using Min-Max normalization
|
298 |
-
# min_val = np.min(annotated_img)
|
299 |
-
# max_val = np.max(annotated_img)
|
300 |
-
# annotated_img = (annotated_img - min_val) / (max_val - min_val)
|
301 |
-
|
302 |
-
# print("Min value of image after normalization:", np.min(annotated_img))
|
303 |
-
# print("Max value of image after normalization:", np.max(annotated_img))
|
304 |
-
# print("annotated_img type after normalization:", type(annotated_img))
|
305 |
-
# # print("annotated_img shape after normalization:", annotated_img.shape)
|
306 |
-
|
307 |
-
# # Reshape the image to match the PIL Image input shape
|
308 |
-
# print("annotated_img shape before reshape:", annotated_img.shape)
|
309 |
-
# annotated_img = annotated_img.reshape(original_image_shape)
|
310 |
-
# print("annotated_img shape after reshape:", annotated_img.shape)
|
311 |
-
|
312 |
-
# # Convert to PIL Image
|
313 |
-
# annotated_img = Image.fromarray(annotated_img)
|
314 |
-
|
315 |
-
# print("PIL Image type:", type(annotated_img))
|
316 |
-
# # print("PIL Image shape:", annotated_img.shape)
|
317 |
-
|
318 |
-
# return annotated_img
|
319 |
|
320 |
# Gradio interface
|
321 |
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import torchvision.transforms as T
|
5 |
from ultralytics import YOLO
|
|
|
6 |
import cv2
|
7 |
import numpy as np
|
8 |
|
9 |
+
# Load the PT model
|
|
|
10 |
model = YOLO("Model_IV.pt")
|
|
|
|
|
11 |
checkpoint = torch.load("Model_IV.pt")
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Define preprocessing
|
14 |
transform = T.Compose([
|
|
|
28 |
results = model(image)
|
29 |
annotated_img = results[0].plot()
|
30 |
return annotated_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Gradio interface
|
33 |
demo = gr.Interface(
|