Fix Enable Tracking Button and Optimize FPS in Streamlit Application (#14508)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Lakshantha Dissanayake 2024-07-17 15:14:16 -07:00 committed by GitHub
parent 0f865de198
commit ebf7dcf5a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -99,24 +99,26 @@ def inference():
stop_button = st.button("Stop") # Button to stop the inference stop_button = st.button("Stop") # Button to stop the inference
prev_time = 0
while videocapture.isOpened(): while videocapture.isOpened():
success, frame = videocapture.read() success, frame = videocapture.read()
if not success: if not success:
st.warning("Failed to read frame from webcam. Please make sure the webcam is connected properly.") st.warning("Failed to read frame from webcam. Please make sure the webcam is connected properly.")
break break
curr_time = time.time() prev_time = time.time()
fps = 1 / (curr_time - prev_time)
prev_time = curr_time
# Store model predictions # Store model predictions
if enable_trk: if enable_trk == "Yes":
results = model.track(frame, conf=conf, iou=iou, classes=selected_ind, persist=True) results = model.track(frame, conf=conf, iou=iou, classes=selected_ind, persist=True)
else: else:
results = model(frame, conf=conf, iou=iou, classes=selected_ind) results = model(frame, conf=conf, iou=iou, classes=selected_ind)
annotated_frame = results[0].plot() # Add annotations on frame annotated_frame = results[0].plot() # Add annotations on frame
# Calculate model FPS
curr_time = time.time()
fps = 1 / (curr_time - prev_time)
prev_time = curr_time
# display frame # display frame
org_frame.image(frame, channels="BGR") org_frame.image(frame, channels="BGR")
ann_frame.image(annotated_frame, channels="BGR") ann_frame.image(annotated_frame, channels="BGR")