-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·111 lines (95 loc) · 3.92 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from streamlit_webrtc import webrtc_streamer,WebRtcMode
import requests
import streamlit as st
import pathlib
from datetime import datetime
from VideoProcessorGPU import VideoProcessor
import av
import minio
processor = VideoProcessor()
minioClient = minio.Minio(
"192.168.92.12:9000",
access_key="minioadmin",
secret_key="minioadmin",
secure=False
)
sidebar = st.sidebar
sidebar.title("课堂行为检测Demo")
checkConnectionButton = sidebar.button("检查连接")
courseName = sidebar.text_input("课程名称")
teacherName = sidebar.text_input("教师名称")
sidebar.title("录制控制")
col1,col2 = sidebar.columns(2)
with col1:
startSavingButton = col1.button("开始录制")
with col2:
stopSavingButton = col2.button("停止录制")
sidebar.title("上传控制")
uploadButton = sidebar.button("上传录像")
if startSavingButton:
if courseName:
if teacherName:
if not pathlib.Path(f"./recordings/{teacherName}/").exists():
pathlib.Path(f"./recordings/{teacherName}/").mkdir(parents=True,exist_ok=True)
if not pathlib.Path(f"./recordings/{teacherName}/{courseName}").exists():
pathlib.Path(f"./recordings/{teacherName}/{courseName}").mkdir(parents=True,exist_ok=True)
time = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
videoWriter = av.open(f"./recordings/{teacherName}/{courseName}/{time}.mp4","w")
videoOutputStream = videoWriter.add_stream("mpeg4",15)
def videoCallBack(frame:av.VideoFrame):
try:
frame = processor.processing(frame)
for packet in videoOutputStream.encode(frame):
videoWriter.mux(packet)
return frame
except Exception as e:
print(str(e))
if checkConnectionButton:
try:
response = requests.get("http://localhost:8000/v2/health/ready")
if response.status_code == 200:
sidebar.success("连接成功,推理服务已准备就绪!")
else:
sidebar.error("连接失败,推理服务存在异常")
except:
sidebar.error("连接失败,无法与推理服务建立连接")
st.title("实时推理画面")
webrtc_streamer(
key="example",
video_frame_callback=videoCallBack,
async_processing=True,
mode=WebRtcMode.SENDRECV,
media_stream_constraints={"video": True, "audio": False},
)
if stopSavingButton:
videoWriter.close()
sidebar.success("录制完成!")
if uploadButton:
if courseName:
# try:
# minioClient.make_bucket(courseName)
# except Exception as e:
# st.error("创建存储桶失败:"+str(e))
try:
files = pathlib.Path(f"./recordings/{teacherName}/{courseName}").glob("*.mp4")
with sidebar.status("正在上传...",expanded=True) as status:
for file in files:
# 检查文件名是不是今天的
if datetime.strptime(file.name.split(".")[0],"%Y-%m-%d-%H-%M-%S") > datetime.now():
continue
else:
try:
with open(file,"rb") as f:
minioClient.put_object(
bucket_name="videoupload",
object_name= teacherName+ "/" + courseName +"/"+file.name ,
data=f,
length=file.stat().st_size
)
except Exception as e:
status.update(label="上传失败!",state="error",expanded=False)
sidebar.error("上传失败:"+str(e))
st.write(f"上传完成:{file.name}")
status.update(label="上传完成!",state="complete",expanded=False)
except Exception as e:
sidebar.error("上传失败:"+str(e))