-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_web.py
More file actions
92 lines (70 loc) · 2.44 KB
/
utils_web.py
File metadata and controls
92 lines (70 loc) · 2.44 KB
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
import os
import cv2
import pandas as pd
import streamlit as st
from PIL import Image
from QtFusion.path import abs_path
def save_uploaded_file(uploaded_file):
"""
保存上传的文件到服务器上。
Args:
uploaded_file (UploadedFile): 通过Streamlit上传的文件。
Returns:
str: 保存文件的完整路径,如果没有文件上传则返回 None。
当用户上传文件时,将该文件保存到服务器的指定目录中。
"""
# 检查是否有文件上传
if uploaded_file is not None:
base_path = "tempDir" # 定义文件保存的基本路径
# 如果路径不存在,创建这个路径
if not os.path.exists(base_path):
os.makedirs(base_path)
# 获取文件的完整路径
file_path = os.path.join(base_path, uploaded_file.name)
# 以二进制写模式打开文件
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer()) # 写入文件
return file_path # 返回文件路径
return None # 如果没有文件上传,返回 None
def concat_results(result, location, confidence, time):
"""
显示检测结果。
Args:
result (str): 检测结果。
location (str): 检测位置。
confidence (str): 置信度。
time (str): 检测用时。
"""
# 创建一个包含这些信息的 DataFrame
result_data = {
"识别结果": [result],
"位置": [location],
"置信度": [confidence],
"用时": [time]
}
results_df = pd.DataFrame(result_data)
return results_df
def load_default_image():
"""
加载默认图片。
Returns:
Image: 返回默认图片对象。
"""
ini_image = abs_path("icon/ini-image.png")
return Image.open(ini_image)
def get_camera_names():
"""
获取可用摄像头名称列表。
Returns:
list: 返回包含“未启用摄像头”和可用摄像头索引号的列表。
"""
camera_names = ["未启用摄像头", "0"]
max_test_cameras = 3 # 定义要测试的最大摄像头数量,可以根据需要调整
for i in range(max_test_cameras):
cap = cv2.VideoCapture(i, cv2.CAP_DSHOW)
if cap.isOpened() and str(i) not in camera_names:
camera_names.append(str(i))
cap.release()
if len(camera_names) == 1:
st.write("未找到可用的摄像头")
return camera_names