jonathanagustin commited on
Commit
e42b13d
1 Parent(s): eed2323
Files changed (2) hide show
  1. app.py +12 -22
  2. requirements.txt +2 -1
app.py CHANGED
@@ -52,7 +52,7 @@ try:
52
  import numpy as np
53
  import streamlink
54
  from PIL import Image
55
- from ultralytics import YOLO
56
  except ImportError:
57
  install_requirements()
58
  import cv2
@@ -61,7 +61,7 @@ except ImportError:
61
  import numpy as np
62
  import streamlink
63
  from PIL import Image
64
- from ultralytics import YOLO
65
 
66
 
67
  logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
@@ -252,16 +252,7 @@ class LiveYouTubeObjectDetector:
252
  def __init__(self):
253
  """Initializes the LiveYouTubeObjectDetector with YOLO model and UI components."""
254
  logging.getLogger().setLevel(logging.DEBUG)
255
- model_url = "https://huggingface.co/aai521-group6/yolov8x-coco/resolve/main/yolov8x-coco.pt?download=true"
256
- local_model_path = "yolov8x-coco.pt"
257
- response = requests.get(model_url)
258
- if response.status_code == 200:
259
- with open(local_model_path, "wb") as f:
260
- f.write(response.content)
261
- print("Model downloaded successfully.")
262
- else:
263
- raise Exception(f"Failed to download model: Status code {response.status_code}")
264
- self.model = YOLO(local_model_path)
265
  self.streams = INITIAL_STREAMS
266
 
267
  # Gradio UI
@@ -327,16 +318,15 @@ class LiveYouTubeObjectDetector:
327
  :rtype: Tuple[Image.Image, List[Tuple[Tuple[int, int, int, int], str]]]
328
  """
329
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
330
- results = self.model(frame_rgb)
331
  annotations = []
332
- for result in results:
333
- for box in result.boxes:
334
- class_id = int(box.cls[0])
335
- class_name = result.names[class_id]
336
- # EXTRACT BOUNDING BOX AND CONVERT TO INTEGER
337
- x1, y1, x2, y2 = box.xyxy[0]
338
- bbox_coords = (int(x1), int(y1), int(x2), int(y2))
339
- annotations.append((bbox_coords, class_name))
340
 
341
  return Image.fromarray(frame_rgb), annotations
342
 
@@ -416,7 +406,7 @@ class LiveYouTubeObjectDetector:
416
  def detect_objects_from_url(url):
417
  return self.detect_objects(url)
418
 
419
- return app.queue().launch(show_api=False)
420
 
421
 
422
  if __name__ == "__main__":
 
52
  import numpy as np
53
  import streamlink
54
  from PIL import Image
55
+ from super_gradients.training import models
56
  except ImportError:
57
  install_requirements()
58
  import cv2
 
61
  import numpy as np
62
  import streamlink
63
  from PIL import Image
64
+ from super_gradients.training import models
65
 
66
 
67
  logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
 
252
  def __init__(self):
253
  """Initializes the LiveYouTubeObjectDetector with YOLO model and UI components."""
254
  logging.getLogger().setLevel(logging.DEBUG)
255
+ self.model = models.get("yolo_nas_l", pretrained_weights="coco")
 
 
 
 
 
 
 
 
 
256
  self.streams = INITIAL_STREAMS
257
 
258
  # Gradio UI
 
318
  :rtype: Tuple[Image.Image, List[Tuple[Tuple[int, int, int, int], str]]]
319
  """
320
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
321
+ predictions = self.model.predict(frame_rgb)
322
  annotations = []
323
+ result = predictions._images_prediction_lst[0]
324
+
325
+ for bbox, label in zip(result.prediction.bboxes_xyxy, result.prediction.labels):
326
+ x1, y1, x2, y2 = bbox
327
+ class_name = result.class_names[int(label)]
328
+ bbox_coords = (int(x1), int(y1), int(x2), int(y2))
329
+ annotations.append((bbox_coords, class_name))
 
330
 
331
  return Image.fromarray(frame_rgb), annotations
332
 
 
406
  def detect_objects_from_url(url):
407
  return self.detect_objects(url)
408
 
409
+ return app.queue().launch(show_api=False, debug=True)
410
 
411
 
412
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -3,6 +3,7 @@ innertube
3
  opencv-python
4
  pillow
5
  streamlink
 
6
  tiktoken
7
  typing-extensions
8
- ultralytics
 
3
  opencv-python
4
  pillow
5
  streamlink
6
+ super-gradients
7
  tiktoken
8
  typing-extensions
9
+ ultralytics