Ahad00/bp
0
1import streamlit as st2import cv23import numpy as np4from inference_sdk import InferenceHTTPClient5import tempfile6import os7 8# Initialize the Inference client9CLIENT = InferenceHTTPClient(10 api_url="https://detect.roboflow.com",11 api_key="6GhKXVQ9VNX5tLW3pyUw"12)13 14model_id = "student-attention-tracking/1"15 16st.title("Student Attention Tracking")17 18# Use Streamlit's camera input19stframe = st.empty() # Placeholder for video feed20 21# Capture video from the webcam22cap = cv2.VideoCapture(0)23 24while True:25 ret, frame = cap.read()26 27 if not ret:28 break29 30 # Convert frame to OpenCV format31 # Save frame to a temporary file32 with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:33 temp_filename = temp_file.name34 cv2.imwrite(temp_filename, frame)35 36 # Run inference37 result = CLIENT.infer(temp_filename, model_id=model_id)38 os.remove(temp_filename) # Delete temp file after use39 40 # Draw predictions41 for obj in result["predictions"]:42 x, y, w, h = int(obj["x"]), int(obj["y"]), int(obj["width"]), int(obj["height"])43 label = obj["class"]44 confidence = obj["confidence"]45 46 color = (0, 255, 0) if label == "Attentive" else (0, 0, 255)47 cv2.rectangle(frame, (x - w // 2, y - h // 2), (x + w // 2, y + h // 2), color, 2)48 cv2.putText(frame, f"{label} ({confidence:.2f})", (x - w // 2, y - h // 2 - 10),49 cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)50 51 # Convert frame to RGB for Streamlit52 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)53 54 # Display updated frame in Streamlit55 stframe.image(frame, channels="RGB", use_column_width=True)56 57# Release the camera after the loop ends58cap.release()59 