aje6 commited on
Commit
ba3b687
·
verified ·
1 Parent(s): 7d0861c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -208
app.py CHANGED
@@ -1,205 +1,3 @@
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
@@ -208,11 +6,6 @@ from ultralytics import YOLO
208
 
209
  # Load your model
210
  model = YOLO("Model_IV.pt")
211
- # model = torch.load("Model_IV.pt")
212
- # model.eval()
213
- # checkpoint = torch.load("Model_IV.pt")
214
- # model.load_state_dict(checkpoint) # Load the saved weights
215
- # model.eval() # Set the model to evaluation mode
216
 
217
  # Define preprocessing
218
  transform = T.Compose([
@@ -239,7 +32,7 @@ def predict(image):
239
  # Gradio interface
240
  demo = gr.Interface(
241
  fn=predict,
242
- inputs=gr.Image(type="pil"), # Accepts image input
243
  outputs="image" # Customize based on your output format
244
  )
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  from PIL import Image
 
6
 
7
  # Load your model
8
  model = YOLO("Model_IV.pt")
 
 
 
 
 
9
 
10
  # Define preprocessing
11
  transform = T.Compose([
 
32
  # Gradio interface
33
  demo = gr.Interface(
34
  fn=predict,
35
+ inputs=gr.Image(type="webcam"), # Accepts image input
36
  outputs="image" # Customize based on your output format
37
  )
38