Juned24 commited on
Commit
a531e37
·
verified ·
1 Parent(s): 0265ff3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -47
app.py CHANGED
@@ -4,34 +4,56 @@ import streamlit as st
4
  import cv2
5
  from vidgear.gears import CamGear
6
 
7
- # Function untuk plot bounding boxes pada frames
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def plot_boxes(frame, model):
9
  results = model.predict(frame)
10
- annotator = Annotator(frame)
11
  for r in results:
12
  boxes = r.boxes
13
  for box in boxes:
14
- b = box.xyxy[0] # Box coordinates
15
- c = box.cls # Class ID
16
- annotator.box_label(b, model.names[int(c)])
17
- return annotator.result()
18
 
19
- # Function to process and display video
20
- def process_video(source, model, options=None):
21
- if source == 'Webcam':
22
- cap = cv2.VideoCapture(0) # webcam
23
- else: # YouTube source
24
- cap = CamGear(source=source, stream_mode=True, logging=True, **options).start()
25
 
26
  FRAME_WINDOW = st.empty()
27
 
28
  while True:
29
- if source == 'Webcam':
30
- ret, frame = cap.read()
31
  if not ret:
32
  break
33
- else: # YouTube source
34
- frame = cap.read()
35
  if frame is None:
36
  break
37
 
@@ -39,34 +61,5 @@ def process_video(source, model, options=None):
39
  frame = plot_boxes(frame, model)
40
  FRAME_WINDOW.image(frame)
41
 
42
- if source == 'Webcam':
43
- cap.release()
44
-
45
- # Load the YOLOv9 model
46
- model = YOLO('yolov9c.pt')
47
-
48
- # Streamlit UI setup
49
- st.set_page_config(page_title="Ruang Belajar", layout="wide", initial_sidebar_state="expanded")
50
-
51
- st.title('Ruang Belajar Deployment YoloV9')
52
-
53
- # Sidebar
54
- with st.sidebar:
55
- video_source = st.radio('Pilih video source', ['Webcam', 'YouTube'])
56
- youtube_link = ""
57
- if video_source == 'YouTube':
58
- youtube_link = st.text_input('YouTube video link', '')
59
-
60
- # Set desired quality
61
- options = {"STREAM_RESOLUTION": "720p"}
62
-
63
- # Process video
64
- if st.button('Start'):
65
- if video_source == 'YouTube' and youtube_link:
66
- process_video(youtube_link, model, options)
67
- elif video_source == 'Webcam':
68
- process_video('Webcam', model)
69
-
70
- # Note on closing resources
71
- st.sidebar.markdown("### Note")
72
- st.sidebar.markdown("Tutup tab browser lain yang tidak anda gunakan untuk mendapat hasil webcam yang baik saat menggunakan sumber video lokal.")
 
4
  import cv2
5
  from vidgear.gears import CamGear
6
 
7
+ # Membuat Streamlit UI
8
+ st.set_page_config(page_title="Ruang Belajar", layout="wide", initial_sidebar_state="expanded")
9
+
10
+ st.title('Ruang Belajar Deployment YoloV9')
11
+
12
+ # Membuat Sidebar
13
+ with st.sidebar:
14
+ video = st.radio('Pilih data video', ['Webcam', 'YouTube'])
15
+ link_youtube = ""
16
+ if video == 'YouTube':
17
+ link_youtube = st.text_input('Masukan Link Youtube', '')
18
+
19
+ # Process video
20
+ if st.button('Start'):
21
+ if data_video == 'YouTube' and youtube_link:
22
+ process_video(youtube_link, model)
23
+ elif data_video == 'Webcam':
24
+ process_video('Webcam', model)
25
+
26
+ # Load Model YOLOv9
27
+ model = YOLO('yolov9c.pt')
28
+
29
+ # Membuat Function untuk plot bounding boxes pada frames
30
  def plot_boxes(frame, model):
31
  results = model.predict(frame)
32
+ annotasi = Annotator(frame)
33
  for r in results:
34
  boxes = r.boxes
35
  for box in boxes:
36
+ b = box.xyxy[0] # Box coordinat
37
+ c = box.cls # ID Class
38
+ annotasi.box_label(b, model.names[int(c)])
39
+ return annotasi.result()
40
 
41
+ # Membuat Function untuk process dan display video
42
+ def process_video(data, model):
43
+ if data == 'Webcam':
44
+ camera = cv2.VideoCapture(0) # kode webcam
45
+ else: # YouTube link
46
+ camera = CamGear(data=data, stream_mode=True, logging=True).start()
47
 
48
  FRAME_WINDOW = st.empty()
49
 
50
  while True:
51
+ if data == 'Webcam':
52
+ ret, frame = camera.read()
53
  if not ret:
54
  break
55
+ else: # YouTube link
56
+ frame = camera.read()
57
  if frame is None:
58
  break
59
 
 
61
  frame = plot_boxes(frame, model)
62
  FRAME_WINDOW.image(frame)
63
 
64
+ if data == 'Webcam':
65
+ camera.release()