I am trying to make an streamlit app which will detect objects via webcam
This is the code
import cv2
import streamlit as st
import numpy as np
from streamlit_webrtc import VideoProcessorBase, webrtc_streamer, VideoTransformerBase
from ultralytics import YOLO
import cv2
import cvzone
import math
model = YOLO("../YOLO_WEIGHTS/yolov8n.pt")
class_names = [
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck",
"boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
"bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra",
"giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis",
"snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard",
"surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon",
"bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog",
"pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table",
"toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave",
"oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
"teddy bear", "hair drier", "toothbrush"
]
# Create a VideoProcessor to handle webcam feed processing
class WebcamProcessor(VideoProcessorBase):
def __init__(self):
self.frame_out = None
def recv(self, img: np.ndarray) -> np.ndarray:
# You can perform any additional processing here if needed
results = model(img, stream=True)
for r in results:
boxes = r.boxes
for box in boxes:
# printing the bounding box
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
print(x1, y1, x2, y2)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
# printing the class name and the confidence score
conf = math.ceil((box.conf[0] * 100)) / 100
cls = int(box.cls[0]) # converting the float to int so that the class name can be called
cvzone.putTextRect(img, f'{class_names[cls]} {conf} ', (max(0, x1), max(35, y1)), scale=1, thickness=1)
return img
# Create a Streamlit app
def main():
st.title("Webcam Stream with Streamlit")
# Start webcam streaming using streamlit-webrtc
webrtc_ctx = webrtc_streamer(
key="example",
video_transformer_factory=WebcamProcessor,
async_transform=True,
)
if not webrtc_ctx.video_transformer:
st.warning("No video source found. Please allow webcam access.")
return
st.write("Webcam Feed:")
st.image(webrtc_ctx.video_transformer.frame_out, channels="BGR", use_column_width=True)
if __name__ == "__main__":
main()
I have tried using cv2.imshow() feature to display the webcam but for some reason, streamlit does not support it.
By using the code above, I am only able to see the webcam and no detections at all.