While True not executing in streamlit

Hello, I’m trying to host my application using streamlit but it’s not recognising the arguments in while True loop,

my code snippet is as follows,

import streamlit as st
import cv2
from cvzone.FaceMeshModule import FaceMeshDetector
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase, RTCConfiguration, VideoProcessorBase, WebRtcMode

RTC_CONFIGURATION = RTCConfiguration(
    {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})

face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

class VideoTransformer(VideoTransformerBase):
    def transform(self, frame):
        img = frame.to_ndarray(format='bgr24')

        faces = face_cascade.detectMultiScale(
            cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 1.1, 3)

        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)

        detector = FaceMeshDetector(maxFaces=1)
        idList = [22, 23, 24, 26, 110, 157, 158, 159, 160, 161, 130, 243]
        ratioList = []
        blinkCounter = 0
        counter = 0
        color = (255, 255, 0)

        while True:

            if img.get(cv2.img_PROP_POS_FRAMES) == img.get(cv2.img_PROP_FRAME_COUNT):
                img.set(cv2.img_PROP_POS_FRAMES, 0)

            img, faces = detector.findFaceMesh(img, draw=False)

            if faces:
                face = faces[0]
                for id in idList:
                    cv2.circle(img, face[id], 5, color, cv2.FILLED)

                leftUp = face[159]
                leftDown = face[23]
                leftLeft = face[130]
                leftRight = face[243]
                lenghtVer, _ = detector.findDistance(leftUp, leftDown)
                lenghtHor, _ = detector.findDistance(leftLeft, leftRight)

                cv2.line(img, leftUp, leftDown, (0, 200, 0), 3)
                cv2.line(img, leftLeft, leftRight, (0, 200, 0), 3)

                ratio = int((lenghtVer / lenghtHor) * 100)
                ratioList.append(ratio)
                if len(ratioList) > 3:
                    ratioList.pop(0)
                ratioAvg = sum(ratioList) / len(ratioList)

                if ratioAvg < 35 and counter == 0:
                    blinkCounter += 1
                    color = (0, 200, 0)
                    counter = 1
                if counter != 0:
                    counter += 1
                    if counter > 10:
                        counter = 0
                        color = (255, 255, 0)

                cv2.putText(img, "Blink Count: {}".format(blinkCounter), (10, 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

                img = cv2.resize(img, (640, 360))

            else:
                img = cv2.resize(img, (640, 360))
            if cv2.waitKey(1) & 0xff == ord('q'):
                break
        return img


def main():
    st.title('Blinking App')
    webrtc_streamer(key="example", mode=WebRtcMode.SENDRECV, rtc_configuration=RTC_CONFIGURATION,
                        video_processor_factory=VideoTransformer)


if __name__ == "__main__":
    main()

Hi Rishi,

Welcome to the forum! :balloon:

Can you please tell us the error you’re getting?

Thanks,
Charly

Hey,
All the code inside the while True loop is not executing in the web app. Everything opens fine and the webcam is initiated but the computer vision code which is in the while true loop doesn’t run in the streamlit webapp.

Thanks for coming back so promptly.

Let us have a look, we should come back with suggestions shortly :slight_smile:

Best wishes,
Charly

Sure, Thanks

It looks like you put the infinite loop in the callback so the callback does not finish and return the new frame.

2 Likes

Thanks for stepping in, Yuichiro! :raised_hands:

Thanks for the reply, The code requires the condition to be true to return the output. Do you have any alternative way in mind? If yes, could you please share the code. Would help a lot, Thanks