Variable outside video_frame_callback stay the same

Summary
Hello everyone, im a newbie to streamlit, im using streamlit-webrtc library at the moment. I’m following guide on github for real-time face recognition, everything work perfectly; beside that variable outside video_frame_callback paremeter in webrtc_streamer stay the same when the camera is running. Let me show you guys my code for the problem.
I think it is related to threading problem cause the app will not rerun after the calling video_frame_callback parameter

My code

Main

result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
df = pd.DataFrame(columns = ['Name', 'Score', 'Attendance time'])
leap = 0

st.title('Điểm danh', anchor = False)

left, right = st.columns(2, gap = 'large')

with right:
    webrtc_ctx = webrtc_streamer(
        key="object-detection",
        mode=WebRtcMode.SENDRECV,
        video_frame_callback=attendance,
        media_stream_constraints={"video": True, "audio": False},
        async_processing=True,
    )

with left:
    if st.checkbox("Show the predicted value", value = True):
        if webrtc_ctx.state.playing:
            dataframe = st.empty()
            while True and (leap % 5 == 0):
                result = result_queue.get()
                result_list = [{'Name': result.name, 'Score': result.score, 'Attendance time': result.time}]
                result_df = pd.DataFrame(result_list)
                df = pd.concat([df, result_df], ignore_index = True)
                dataframe.dataframe(df)
                print(leap)
                leap += 1

Attendance function

def attendance(frame: av.VideoFrame) -> av.VideoFrame:
    image = frame.to_ndarray(format = 'bgr24')
    image = imutils.resize(image, WINDOWS_WIDTH, WINDOWS_HEIGHT)

    bboxs, landmarks = bf.get_face_bbox(image)

    for i in range(len(bboxs)):
        # Block of code to get face recognition, return name and score
        
        detection = Detection(
            name = str(name),
            score = float(score)
            time = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') 
        )
        // some code to show recogition on screen
        result_queue.put(detection)

    return av.VideoFrame.from_ndarray(image, format = 'bgr24')

Expected behaviour

Leap variable increment correctly, and saving to dataframe per 5 leap

Unexpected behaviour

  • Leap variable did not increment, it stay the same at 0 when the camera is running. Everything work fine except that leap variable

Note

I’m not providing the full code because it is too long for reader, if you guys need I can provide in discussion. Many thanks if someone can help me solve this, i already desperate finding all the sources many day ago :smiling_face_with_tear:

The condition of your while loop becomes False after the first iteration. So it is incremented up to 1, and that is right before exiting the loop (after printing 0).

1 Like

oh you are right, i was just looking again that loop, many thanks

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