Couldn’t display the output in the browser, but always output window appears in idle

I am running a object detection analysis and I need the output to be displayed in the browser instead of idle I couldn’t identify the issue I am still a novice

I am running a object detection analysis (It needs to be displayed to the user so they can define dynamic ROI themselves )so that I need the output to be displayed in the browser instead of idle. I have a button called start analysis When i click the button the video uploaded by the user is moved to the flask backend and i need the result to be displayed in the browser instead of idle.The output should be in real time.

Here is the code

    # Global variables to store ROI coordinates
    roi_start = None
    roi_end = None

    # Function to handle mouse events for selecting ROI
    def mouse_callback(event, x, y, flags, param):
        nonlocal roi_start, roi_end

        if event == cv2.EVENT_LBUTTONDOWN:
            # Start selecting ROI
            roi_start = (x, y)
        elif event == cv2.EVENT_LBUTTONUP:
            # End selecting ROI
            roi_end = (x, y)
            cv2.rectangle(param, roi_start, roi_end, (0, 255, 0), 2)
            cv2.imshow("Image", param)

    # Function to check if a point is inside a rectangle
    def point_inside_rect(x, y, rect):
        x1, y1, x2, y2 = rect
        return x1 <= x <= x2 and y1 <= y <= y2

    cap = cv2.VideoCapture(video_file)

    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))

    model = YOLO("../YOLO-Weights/yolov8l.pt")
    classNames = ["person"]

    # Define the limit for number of persons in ROI
    LIMIT_PERSONS = 7

    # Create a window to display the video
    cv2.namedWindow("Image")

    # Set mouse callback function
    cv2.setMouseCallback("Image", mouse_callback)

    while True:
        success, img = cap.read()

        if roi_start and roi_end:
            # Draw the ROI rectangle
            cv2.rectangle(img, roi_start, roi_end, (0, 255, 0), 2)

            # Get ROI coordinates
            x1 = min(roi_start[0], roi_end[0])
            y1 = min(roi_start[1], roi_end[1])
            x2 = max(roi_start[0], roi_end[0])
            y2 = max(roi_start[1], roi_end[1])

            # Define the ROI rectangle
            roi_rect = (x1, y1, x2, y2)

            # Doing detections using YOLOv8 frame by frame
            results = model(img)

            # Count the number of people within the ROI
            people_in_roi = 0

            for r in results:
                boxes = r.boxes
                for box in boxes:
                    x1, y1, x2, y2 = box.xyxy[0]
                    x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)

                    # Check if the center of the bounding box is inside the ROI
                    center_x = (x1 + x2) // 2
                    center_y = (y1 + y2) // 2
                    if point_inside_rect(center_x, center_y, roi_rect):
                        people_in_roi += 1

                    cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)

                    conf = math.ceil((box.conf[0] * 100) / 100)
                    cls = int(box.cls[0])
                    if cls < len(classNames):  # Ensure class index is within range
                        class_name = classNames[cls]
                        label = f'{class_name}{conf}'
                        t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
                        c2 = x1 + t_size[0], y1 - t_size[1] - 3
                        cv2.rectangle(img, (x1, y1), c2, [255, 0, 255], -1, cv2.LINE_AA)  # filled
                        cv2.putText(img, label, (x1, y1 - 2), 0, 1, [255, 255, 255], thickness=1, lineType=cv2.LINE_AA)

            # Display the number of people within the ROI
            cv2.putText(img, f'People in ROI: {people_in_roi}', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

            # Check if the number of people exceeds the limit
            if people_in_roi > LIMIT_PERSONS:
                # Display message
                cv2.putText(img, 'Please move to other counters', (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

            # Yield the processed frame as a byte array
            yield (b'--framern'
                   b'Content-Type: image/jpegrnrn'
                   + cv2.imencode('.jpg', img)[1].tobytes()
                   + b'rn')

        # Display the frame
        cv2.imshow("Image", img)

        # Check for user input to exit the loop (if necessary)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release the video capture object and close windows
    cap.release()
    cv2.destroyAllWindows()

@app.route('/save-file', methods=['POST'])
def save_file():
    if 'file' not in request.files:
        return redirect(request.url)

    file = request.files['file']

    if file.filename == '':
        return redirect(request.url)

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file_path = os.path.join(UPLOAD_FOLDER, filename)
        file.save(file_path)
        
        # Create a Flask response object with the frame generator
        return Response(process_video(file_path), mimetype='multipart/x-mixed-replace; boundary=frame')

    return redirect(request.url)```

New contributor

Dragodec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật