|
|
|
import streamlit as st |
|
import cv2 |
|
import tempfile |
|
import torch |
|
import numpy as np |
|
from ultralytics import YOLO |
|
from PIL import Image |
|
from io import BytesIO |
|
import requests |
|
|
|
@st.cache_resource |
|
def load_yolo_model(): |
|
return YOLO("yolov8n.pt") |
|
|
|
def analyze_with_deepseek(text): |
|
prompt = f"请分析学生的以下行为并提供教学建议:{text}" |
|
return f"分析:学生可能在积极参与小组讨论。建议教师鼓励团队合作,提升学习主动性。" |
|
|
|
def process_video(uploaded_file, model): |
|
tfile = tempfile.NamedTemporaryFile(delete=False) |
|
tfile.write(uploaded_file.read()) |
|
cap = cv2.VideoCapture(tfile.name) |
|
|
|
frames = [] |
|
heatmap = np.zeros((480, 640)) |
|
behavior_summary = [] |
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
frame = cv2.resize(frame, (640, 480)) |
|
results = model(frame) |
|
boxes = results[0].boxes.xyxy.cpu().numpy() if results else [] |
|
|
|
for box in boxes: |
|
x1, y1, x2, y2 = map(int, box[:4]) |
|
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) |
|
cx, cy = int((x1+x2)/2), int((y1+y2)/2) |
|
heatmap[cy, cx] += 1 |
|
|
|
frames.append(frame) |
|
behavior_summary.append("检测到学生行为") |
|
|
|
cap.release() |
|
return frames, heatmap, behavior_summary |
|
|
|
def display_heatmap(heatmap): |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
fig, ax = plt.subplots() |
|
sns.heatmap(heatmap, cmap="YlOrRd", ax=ax) |
|
st.pyplot(fig) |
|
|
|
st.title("🎓 学生课堂行为自动识别与智能反馈系统") |
|
st.markdown("---") |
|
|
|
model = load_yolo_model() |
|
uploaded_file = st.file_uploader("请上传课堂视频(mp4格式)", type=["mp4"]) |
|
|
|
if uploaded_file: |
|
st.video(uploaded_file) |
|
with st.spinner("正在分析视频..."): |
|
frames, heatmap, behavior_summary = process_video(uploaded_file, model) |
|
|
|
st.success("分析完成!") |
|
st.subheader("📌 注意力热力图") |
|
display_heatmap(heatmap) |
|
|
|
st.subheader("📊 行为语义分析") |
|
for idx, summary in enumerate(behavior_summary[:3]): |
|
text_analysis = analyze_with_deepseek(summary) |
|
st.info(f"帧 {idx+1}: {text_analysis}") |
|
|
|
st.subheader("📄 教学优化建议(示例)") |
|
st.markdown("- 增加互动提问频次") |
|
st.markdown("- 鼓励小组讨论与合作") |
|
st.markdown("- 适当调整教学节奏,吸引注意力") |
|
|
|
st.download_button("📥 导出教学分析报告", data="报告内容示例...", file_name="teaching_report.txt") |
|
|