[Python] Deployed a ML model that uses realtime camera feed. But getting error in it

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    This is the error I am getting. OpenCV and streamlit is used in accessing the camera. The website was working fine on localhost. It is accessing the camera then. I was expecting it to get access to camera of the computer on which site is run. when even deployed on a website. The website is deployed on site http://gesturespeak.streamlit.app by streamlit cloud on github repo. What changes should I do in my code? Is there some other way I can deploy a ML mode that uses real time camera feed?

    This is the error msg.

    [12:26:40] Python dependencies were installed from /mount/src/asl/requirements.txt using uv.

    Check if streamlit is installed

    Streamlit is already installed

    [12:26:42] Processed dependencies!

    INFO: [2025-03-22 12:28:12.771991] Created TensorFlow Lite XNNPACK delegate for CPU.[2025-03-22 12:28:12.772305]

    WARNING: All log messages before absl::InitializeLog() is called are written to STDERR

    W0000 00:00:1742646492.805719 2389 inference_feedback_manager.cc:114] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.

    W0000 00:00:1742646492.833830 2389 inference_feedback_manager.cc:114] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.

    [ WARN:0@10.837] global cap_v4l.cpp:913 open VIDEOIO(V4L2:/dev/video0): can't open camera by index

    [ERROR:0@10.837] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

    W0000 00:00:1742646497.008393 2407 inference_feedback_manager.cc:114] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.

    W0000 00:00:1742646497.039679 2407 inference_feedback_manager.cc:114] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.

    This is the code by which model is being deployed.

    import streamlit as st
    import pickle
    import cv2
    import mediapipe as mp
    import numpy as np
    import time

    def calculate_angle(v1, v2):
    v1 = np.array(v1)
    v2 = np.array(v2)
    cosine_angle = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
    angle = np.arccos(np.clip(cosine_angle, -1.0, 1.0))
    return np.degrees(angle)

    # Load trained model
    with open('./model.p', 'rb') as f:
    model_dict = pickle.load(f)

    model = model_dict['model']

    # Constants
    offset = 20

    # Mediapipe Hands
    mp_hands = mp.solutions.hands
    hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.6)

    # Labels
    labels_dict = {
    0:'space', 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J',
    11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S',
    20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z', 27: 'delete'
    }


    # Streamlit UI
    st.title("GestureSpeak: ASL Hand Gesture Recognition")
    st.write("Show an ASL hand sign to recognize it. Press **Stop Webcam** to quit.")

    # Webcam state
    if "run_webcam" not in st.session_state:
    st.session_state.run_webcam = False

    # Button Controls
    col1, col2 = st.columns(2)
    with col1:
    if st.button("Start Webcam"):
    st.session_state.run_webcam = True
    with col2:
    if st.button("Stop Webcam"):
    st.session_state.run_webcam = False

    # Video and output display
    stframe = st.empty()
    prediction_text = st.empty()
    result_text = st.empty()

    # Track previous prediction and time
    previous_prediction = None
    start_time = None
    recognized_text = ""

    if st.session_state.run_webcam:
    cap = cv2.VideoCapture(0)

    while st.session_state.run_webcam:
    ret, frame = cap.read()
    if not ret:
    st.error("Failed to capture image.")
    break

    imgRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = hands.process(imgRGB)

    predicted_character = "None"

    if results.multi_hand_landmarks:
    hand_landmarks = results.multi_hand_landmarks[0]
    h, w, _ = frame.shape
    x_min, y_min, x_max, y_max = w, h, 0, 0

    for landmark in hand_landmarks.landmark:
    x, y = int(landmark.x * w), int(landmark.y * h)
    x_min, y_min = min(x_min, x), min(y_min, y)
    x_max, y_max = max(x_max, x), max(y_max, y)

    x_min, y_min = max(0, x_min - offset), max(0, y_min - offset)
    x_max, y_max = min(w, x_max + offset), min(h, y_max + offset)

    data_aux = []
    x_, y_, z_ = [], [], []
    landmarks = []

    for i in range(21):
    x = hand_landmarks.landmark.x
    y = hand_landmarks.landmark.y
    z = hand_landmarks.landmark.z

    x_.append(x)
    y_.append(y)
    z_.append(z)
    landmarks.append((x, y, z))

    min_x, min_y, min_z = min(x_), min(y_), min(z_)
    for i in range(21):
    data_aux.append(x_/min_x)
    data_aux.append(y_/ min_y)
    data_aux.append(z_/ min_z)

    finger_joints = [(4, 1, 8, 5), (8, 5, 12, 9), (12, 9, 16, 13), (16, 13, 20, 17)]
    for joint in finger_joints:
    v1 = np.subtract(landmarks[joint[0]], landmarks[joint[1]])
    v2 = np.subtract(landmarks[joint[2]], landmarks[joint[3]])
    data_aux.append(calculate_angle(v1, v2))

    data_aux = np.array(data_aux).reshape(1, -1)

    if data_aux.shape[1] == 67:
    prediction = model.predict(data_aux)
    # predicted_index = int(prediction[0])
    # predicted_character = labels_dict[predicted_index]

    predicted_character = prediction[0]
    # Track and print if character is stable for 2 seconds
    if predicted_character == previous_prediction:
    if time.time() - start_time >= 2:
    if(predicted_character == 'space'):
    recognized_text+=' '
    else:
    if(predicted_character == 'delete'):
    recognized_text = recognized_text[:-1]
    else:
    recognized_text += predicted_character
    previous_prediction = None
    else:
    previous_prediction = predicted_character
    start_time = time.time()
    else:
    print("Incorrect number of features")

    cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
    cv2.putText(frame, predicted_character, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)

    stframe.image(frame, channels="BGR")
    prediction_text.subheader(f"Predicted Character: **{predicted_character}**")
    result_text.subheader(f"Recognized Text: **{recognized_text}**")

    cap.release()
    cv2.destroyAllWindows()

    Continue reading...

Compartilhe esta Página