Deployed app cannot access camera using cv2.VideoCapture

import streamlit as st
import cv2
from ultralytics import YOLO
import time

Load the YOLOv8 model

model = YOLO(ā€œbest.ptā€)

def perform_inference(frame):

results = model.predict(frame, conf=0.25, iou=0.1, half=True, retina_masks=True, agnostic_nms=True)
annotated_frame = results[0].plot()

return annotated_frame

def main():
st.title(ā€˜Real-Time Hand Detectionā€™)

cap = cv2.VideoCapture(0)

if not cap.isOpened():
    st.error("Cannot access the camera.")

    return

stframe = st.empty()
st_info = st.empty()

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        #frame = cv2.resize(frame, (480,640))
        start_time = time.time()
        annotated_frame = perform_inference(frame)
        stframe.image(annotated_frame, channels='BGR', use_column_width=True)
        end_time = time.time()
        inference_time = end_time - start_time
        #st_info.text("Inference Time: {:.2f} ms".format(inference_time))
        st_info.markdown(
            f'<div style="background-color: #F1C40F; color: #000033; padding: 10px; font-size: 22px;">Inference Time: {inference_time:.2f} ms</div>',
            unsafe_allow_html=True
        )

if name == ā€œmainā€:
main()

i have deployed app but it showing me error of ā€˜Cannot access the camera.ā€™ from cap is not opening. I have tried some ways but still could not get the solution.

it is working good on local host but not working on deployed app,

please someone help me in that.

thank you very much in advance

It because the opencv module is trying to access serverā€™s camera instead of yours
try with this st.camera_input - Streamlit Docs
you can access camera via browser like this

thanks but i want to access videocam for live video from webcam or mobielcamā€¦

May be check this out
this might help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.