weijiang2024
commited on
Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +21 -0
- Dockerfile +0 -0
- README.md +33 -8
- __init__.py +4 -0
- app.py +104 -0
- common/db.py +17 -0
- config/S3/S3.yaml +1 -0
- config/keywords/keywords.yaml +11 -0
- config/model/yolov8-seg.yaml +46 -0
- config/weight/yolov8n-seg.pt +3 -0
- data/exp1.in/sample.1.png +0 -0
- data/exp2.in/20240511-163941.png +3 -0
- data/exp2.in/20240511-164141.png +3 -0
- data/exp2.in/20240511-164142.png +3 -0
- data/exp2.in/20240511-164149.png +3 -0
- data/exp2.in/20240511-164156.png +3 -0
- data/exp2.in/20240511-164202.png +3 -0
- data/exp2.in/20240511-164209.png +3 -0
- data/exp2.in/20240511-164223.png +3 -0
- data/exp2.in/20240511-164254.png +3 -0
- data/exp2.in/20240511-164304.png +3 -0
- data/exp2.in/20240511-164310.png +3 -0
- data/exp2.in/20240511-164314.png +3 -0
- data/exp3.in/origin/20240722-175852.091-1.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-2.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-3.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-4.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-5.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-6.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-7.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-8.jpg +3 -0
- data/exp3.in/origin/20240722-175852.091-9.jpg +3 -0
- data/temp/temp.png +0 -0
- eval_fashion_person.py +68 -0
- eval_model.py +48 -0
- name_entity_recognition.py +33 -0
- person_registration.py +320 -0
- requirements.txt +0 -0
- segment.py +61 -0
- speech_to_txt.py +22 -0
- utils/__init__.py +7 -0
- utils/__pycache__/__init__.cpython-311.pyc +0 -0
- utils/__pycache__/load_input.cpython-311.pyc +0 -0
- utils/__pycache__/operate_s3.cpython-311.pyc +0 -0
- utils/__pycache__/utils.cpython-311.pyc +0 -0
- utils/app.py +60 -0
- utils/load_input.py +69 -0
- utils/operate_csv.py +55 -0
- utils/operate_json.py +74 -0
- utils/operate_s3.py +87 -0
.gitattributes
CHANGED
@@ -33,3 +33,24 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
data/exp2.in/20240511-163941.png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
data/exp2.in/20240511-164141.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
data/exp2.in/20240511-164142.png filter=lfs diff=lfs merge=lfs -text
|
39 |
+
data/exp2.in/20240511-164149.png filter=lfs diff=lfs merge=lfs -text
|
40 |
+
data/exp2.in/20240511-164156.png filter=lfs diff=lfs merge=lfs -text
|
41 |
+
data/exp2.in/20240511-164202.png filter=lfs diff=lfs merge=lfs -text
|
42 |
+
data/exp2.in/20240511-164209.png filter=lfs diff=lfs merge=lfs -text
|
43 |
+
data/exp2.in/20240511-164223.png filter=lfs diff=lfs merge=lfs -text
|
44 |
+
data/exp2.in/20240511-164254.png filter=lfs diff=lfs merge=lfs -text
|
45 |
+
data/exp2.in/20240511-164304.png filter=lfs diff=lfs merge=lfs -text
|
46 |
+
data/exp2.in/20240511-164310.png filter=lfs diff=lfs merge=lfs -text
|
47 |
+
data/exp2.in/20240511-164314.png filter=lfs diff=lfs merge=lfs -text
|
48 |
+
data/exp3.in/origin/20240722-175852.091-1.jpg filter=lfs diff=lfs merge=lfs -text
|
49 |
+
data/exp3.in/origin/20240722-175852.091-2.jpg filter=lfs diff=lfs merge=lfs -text
|
50 |
+
data/exp3.in/origin/20240722-175852.091-3.jpg filter=lfs diff=lfs merge=lfs -text
|
51 |
+
data/exp3.in/origin/20240722-175852.091-4.jpg filter=lfs diff=lfs merge=lfs -text
|
52 |
+
data/exp3.in/origin/20240722-175852.091-5.jpg filter=lfs diff=lfs merge=lfs -text
|
53 |
+
data/exp3.in/origin/20240722-175852.091-6.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
+
data/exp3.in/origin/20240722-175852.091-7.jpg filter=lfs diff=lfs merge=lfs -text
|
55 |
+
data/exp3.in/origin/20240722-175852.091-8.jpg filter=lfs diff=lfs merge=lfs -text
|
56 |
+
data/exp3.in/origin/20240722-175852.091-9.jpg filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
File without changes
|
README.md
CHANGED
@@ -1,12 +1,37 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: 😻
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: green
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.40.0
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Suanfamama_Cognitive_Computational_Fashion
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 4.29.0
|
6 |
---
|
7 |
+
## 基于广州北京路商圈来往时尚用户及趋势被动发现模块
|
8 |
+
### 目标
|
9 |
+
*
|
10 |
+
### 模型训练 & 预测
|
11 |
+
* 模型训练狭义:准备数据集 通过正向传播 及 反向传播 及 梯度下降等核心DL算法 输出一个经权重迭代后的神经网络
|
12 |
+
* 模型训练广义:
|
13 |
+
* 基模型的灵活使用
|
14 |
+
* AIGC知识库的不断更新 及 RAG算法的调优
|
15 |
+
* 价值及安全对齐 如时尚
|
16 |
+
* 模型训练:每天都有增量的黄金标准,每天都根据专家咨询意见(如小玲买手)整理知识库 调节智能体各部分设定 以达到复制人脑部分功能的目标
|
17 |
+
* 模型预测:专家给出一个分数及文本原因 机器智能体也给出一个分数及文本原因 不断优化 不断拟合 total loss value 不断下降
|
18 |
+
|
19 |
+
### 技术架构
|
20 |
+
* 1.0
|
21 |
+
* 直接控制摄像头并拍照
|
22 |
+
* 传给分析模块
|
23 |
+
* 触发被动用户注册及时尚分数评估
|
24 |
+
* 完成最终入库逻辑
|
25 |
+
* 1.1
|
26 |
+
* 实时视频流成为单独 微服务
|
27 |
+
* 截取图片 后续逻辑不变
|
28 |
+
* 1.2
|
29 |
+
* 实时视频流成为单独 微服务
|
30 |
+
* 向外提供一个API接口 输入实时视频流
|
31 |
+
* 计算:完整的入库
|
32 |
+
* 输出:用json返回运行结果
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
|
|
__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import algs.alg0.eval_fashion_person
|
2 |
+
import algs.alg0.person_registration
|
3 |
+
import algs.alg0.segment
|
4 |
+
import algs.alg0.utils
|
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
#from algs.alg0.utils.utils import *
|
4 |
+
#from algs.alg0.eval_fashion_person import *
|
5 |
+
#from algs.alg0.name_entity_recognition import *
|
6 |
+
#from algs.alg0.utils.operate_json import *
|
7 |
+
|
8 |
+
from utils.utils import *
|
9 |
+
from eval_fashion_person import *
|
10 |
+
from name_entity_recognition import *
|
11 |
+
from utils.operate_json import *
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
def eval_image(input_image, progress=gr.Progress()):
|
16 |
+
if input_image is None:
|
17 |
+
raise gr.Error("未上传图片,请先上传图片")
|
18 |
+
progress((0, 3), desc="上传图片中")
|
19 |
+
img_name = get_one_name()
|
20 |
+
input_image = output_to_binary(input_image)
|
21 |
+
save_tx(file_data=input_image,
|
22 |
+
file_name=img_name)
|
23 |
+
progress((1, 3), desc="评估分数中")
|
24 |
+
data = eval_fashion_person_tx_url(img_name)
|
25 |
+
progress((2, 3), desc="识别类型中")
|
26 |
+
reason = data['fashion_eval_reason']
|
27 |
+
score = data['fashion_score_predict']
|
28 |
+
score = ("未识别出" if score == -1 else score)
|
29 |
+
data = {}
|
30 |
+
while not data:
|
31 |
+
text = name_entity_recognition(reason)
|
32 |
+
data = text_to_json(text)
|
33 |
+
keys_to_check = [
|
34 |
+
'type',
|
35 |
+
'upper_garment',
|
36 |
+
'lower_garment',
|
37 |
+
'headgear',
|
38 |
+
'sock',
|
39 |
+
'shoe',
|
40 |
+
'accessory',
|
41 |
+
'backpack',
|
42 |
+
'action',
|
43 |
+
'countenance'
|
44 |
+
]
|
45 |
+
data = {k: ("未识别出" if data.get(k) is None else data[k]) for k in keys_to_check}
|
46 |
+
data = {k: ("未识别出" if v == "None" else v) for k, v in data.items()}
|
47 |
+
result = [reason,
|
48 |
+
score,
|
49 |
+
data['type'],
|
50 |
+
data['upper_garment'],
|
51 |
+
data['lower_garment'],
|
52 |
+
data['headgear'],
|
53 |
+
data['sock'],
|
54 |
+
data['shoe'],
|
55 |
+
data['accessory'],
|
56 |
+
data['backpack'],
|
57 |
+
data['action'],
|
58 |
+
data['countenance']]
|
59 |
+
return result
|
60 |
+
|
61 |
+
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column():
|
65 |
+
Image_input = gr.Image(label="请上传图片",
|
66 |
+
height=640,
|
67 |
+
width=640,
|
68 |
+
show_share_button=False)
|
69 |
+
with gr.Row():
|
70 |
+
run_button = gr.Button(value="开始评估")
|
71 |
+
score = gr.Textbox(label="评分")
|
72 |
+
with gr.Column():
|
73 |
+
with gr.Group():
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column():
|
76 |
+
type = gr.Textbox(label="人物类型")
|
77 |
+
headgear = gr.Textbox(label="帽子类型")
|
78 |
+
shoe = gr.Textbox(label="鞋子类型")
|
79 |
+
backpack = gr.Textbox(label="背包类型")
|
80 |
+
countenance = gr.Textbox(label="表情类型")
|
81 |
+
with gr.Column():
|
82 |
+
upper_garment = gr.Textbox(label="上衣类型")
|
83 |
+
lower_garment = gr.Textbox(label="下衣类型")
|
84 |
+
sock = gr.Textbox(label="袜子类型")
|
85 |
+
accessory = gr.Textbox(label="配饰类型")
|
86 |
+
action = gr.Textbox(label="动作类型")
|
87 |
+
text_output = gr.Textbox(label="时尚买手小玲评估结果")
|
88 |
+
|
89 |
+
run_button.click(eval_image,
|
90 |
+
inputs=Image_input,
|
91 |
+
outputs=[text_output,
|
92 |
+
score,
|
93 |
+
type,
|
94 |
+
upper_garment,
|
95 |
+
lower_garment,
|
96 |
+
headgear,
|
97 |
+
sock,
|
98 |
+
shoe,
|
99 |
+
accessory,
|
100 |
+
backpack,
|
101 |
+
action,
|
102 |
+
countenance])
|
103 |
+
|
104 |
+
demo.queue().launch()
|
common/db.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from docutils.frontend import read_config_file
|
2 |
+
from fastapi import Depends
|
3 |
+
from mysql.connector import connect
|
4 |
+
import json
|
5 |
+
|
6 |
+
def get_db_connection():
|
7 |
+
config = read_config_file('config.json')
|
8 |
+
db_config = config['database']
|
9 |
+
connection = connect(
|
10 |
+
host=db_config['host'],
|
11 |
+
user=db_config['user'],
|
12 |
+
password=db_config['password'],
|
13 |
+
database=db_config['database']
|
14 |
+
)
|
15 |
+
# 开启自动提交模式
|
16 |
+
connection.autocommit = True
|
17 |
+
return connection
|
config/S3/S3.yaml
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
config/keywords/keywords.yaml
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
类型: ["男", "女", "儿童", "群体"]
|
2 |
+
上衣: ["男", "女", "儿童", "群体"]
|
3 |
+
下衣: ["男", "女", "儿童", "群体"]
|
4 |
+
帽子: ["男", "女", "儿童", "群体"]
|
5 |
+
袜子: ["男", "女", "儿童", "群体"]
|
6 |
+
鞋子: ["男", "女", "儿童", "群体"]
|
7 |
+
配饰: ["男", "女", "儿童", "群体"]
|
8 |
+
背包: ["男", "女", "儿童", "群体"]
|
9 |
+
场景: ["男", "女", "儿童", "群体"]
|
10 |
+
动作: ["男", "女", "儿童", "群体"]
|
11 |
+
表情: ["男", "女", "儿童", "群体"]
|
config/model/yolov8-seg.yaml
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
2 |
+
# YOLOv8-seg instance segmentation model. For Usage examples see https://docs.ultralytics.com/tasks/segment
|
3 |
+
|
4 |
+
# Parameters
|
5 |
+
nc: 80 # number of classes
|
6 |
+
scales: # model compound scaling constants, i.e. 'model=yolov8n-seg.yaml' will call yolov8-seg.yaml with scale 'n'
|
7 |
+
# [depth, width, max_channels]
|
8 |
+
n: [0.33, 0.25, 1024]
|
9 |
+
s: [0.33, 0.50, 1024]
|
10 |
+
m: [0.67, 0.75, 768]
|
11 |
+
l: [1.00, 1.00, 512]
|
12 |
+
x: [1.00, 1.25, 512]
|
13 |
+
|
14 |
+
# YOLOv8.0n backbone
|
15 |
+
backbone:
|
16 |
+
# [from, repeats, module, args]
|
17 |
+
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
|
18 |
+
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
|
19 |
+
- [-1, 3, C2f, [128, True]]
|
20 |
+
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
|
21 |
+
- [-1, 6, C2f, [256, True]]
|
22 |
+
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
|
23 |
+
- [-1, 6, C2f, [512, True]]
|
24 |
+
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
|
25 |
+
- [-1, 3, C2f, [1024, True]]
|
26 |
+
- [-1, 1, SPPF, [1024, 5]] # 9
|
27 |
+
|
28 |
+
# YOLOv8.0n head
|
29 |
+
head:
|
30 |
+
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
|
31 |
+
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
|
32 |
+
- [-1, 3, C2f, [512]] # 12
|
33 |
+
|
34 |
+
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
|
35 |
+
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
|
36 |
+
- [-1, 3, C2f, [256]] # 15 (P3/8-small)
|
37 |
+
|
38 |
+
- [-1, 1, Conv, [256, 3, 2]]
|
39 |
+
- [[-1, 12], 1, Concat, [1]] # cat head P4
|
40 |
+
- [-1, 3, C2f, [512]] # 18 (P4/16-medium)
|
41 |
+
|
42 |
+
- [-1, 1, Conv, [512, 3, 2]]
|
43 |
+
- [[-1, 9], 1, Concat, [1]] # cat head P5
|
44 |
+
- [-1, 3, C2f, [1024]] # 21 (P5/32-large)
|
45 |
+
|
46 |
+
- [[15, 18, 21], 1, Segment, [nc, 32, 256]] # Segment(P3, P4, P5)
|
config/weight/yolov8n-seg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d39e867b2c3a5dbc1aa764411544b475cb14727bf6af1ec46c238f8bb1351ab9
|
3 |
+
size 7054355
|
data/exp1.in/sample.1.png
ADDED
data/exp2.in/20240511-163941.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164141.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164142.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164149.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164156.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164202.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164209.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164223.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164254.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164304.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164310.png
ADDED
Git LFS Details
|
data/exp2.in/20240511-164314.png
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-1.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-2.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-3.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-4.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-5.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-6.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-7.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-8.jpg
ADDED
Git LFS Details
|
data/exp3.in/origin/20240722-175852.091-9.jpg
ADDED
Git LFS Details
|
data/temp/temp.png
ADDED
eval_fashion_person.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
|
4 |
+
from algs.alg0.utils.operate_tx import *
|
5 |
+
from algs.alg0.utils.utils import *
|
6 |
+
|
7 |
+
|
8 |
+
# AI评估人物图像(URL)
|
9 |
+
def eval_fashion_person(img_url):
|
10 |
+
url = os.environ["LINKAI_HOST"]
|
11 |
+
headers = {
|
12 |
+
"Content-Type": "application/json",
|
13 |
+
"Authorization": "Bearer " + os.environ["LINKAI_KEY"]
|
14 |
+
}
|
15 |
+
body = {
|
16 |
+
"app_code": os.environ["LINKAI_CODE_2"],
|
17 |
+
"messages": [
|
18 |
+
{
|
19 |
+
"role": "user",
|
20 |
+
"content": [
|
21 |
+
{
|
22 |
+
"type": "text",
|
23 |
+
"text": "识别图片中的人物,并根据知识库的知识必须给出评分和评分原因,其中,评分需要严格谨慎一点且评分原因必须包含所参考的知识的详细内容"
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"type": "image_url",
|
27 |
+
"image_url": {
|
28 |
+
"url": img_url
|
29 |
+
}
|
30 |
+
}
|
31 |
+
]
|
32 |
+
}
|
33 |
+
]
|
34 |
+
}
|
35 |
+
res = requests.post(url, json=body, headers=headers)
|
36 |
+
if res.status_code == 200:
|
37 |
+
reply_text = res.json().get("choices")[0]['message']['content']
|
38 |
+
return reply_text
|
39 |
+
else:
|
40 |
+
error = res.json().get("error")
|
41 |
+
print(f"请求异常, 错误码={res.status_code}, 错误类型={error.get('type')}, 错误信息={error.get('message')}")
|
42 |
+
|
43 |
+
|
44 |
+
# 从腾讯云获取图片url并评分
|
45 |
+
def eval_fashion_person_tx_url(img_name):
|
46 |
+
person_name = img_name.split(".")[0]
|
47 |
+
img_url = generate_tx_presigned_url(img_name)
|
48 |
+
score, conclusion = get_score_conclusion(img_url)
|
49 |
+
result = {"username": person_name,
|
50 |
+
"avatar_url": img_url,
|
51 |
+
"fashion_score_predict": score,
|
52 |
+
"fashion_eval_reason": conclusion
|
53 |
+
}
|
54 |
+
return result
|
55 |
+
|
56 |
+
|
57 |
+
# 获取分数和评语
|
58 |
+
def get_score_conclusion(img_url):
|
59 |
+
conclusion = eval_fashion_person(img_url)
|
60 |
+
score = extract_first_number(conclusion)
|
61 |
+
return score, conclusion
|
62 |
+
|
63 |
+
|
64 |
+
if __name__ == '__main__':
|
65 |
+
img_url = ''
|
66 |
+
img_name = ''
|
67 |
+
print(eval_fashion_person(img_url))
|
68 |
+
print(eval_fashion_person_tx_url(img_name))
|
eval_model.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
from algs.alg0.utils.operate_csv import *
|
4 |
+
from algs.alg0.utils.utils import *
|
5 |
+
from algs.alg0.utils.app import *
|
6 |
+
|
7 |
+
|
8 |
+
# 从csv评估模型调优结果并将数据存入log
|
9 |
+
def eval_model_csv(csv_file_path='./data/temp/temp.csv'):
|
10 |
+
scores_predict = get_field_values(csv_file_path, "fashion_score_predict")
|
11 |
+
scores_true = get_field_values(csv_file_path, "fashion_score_true")
|
12 |
+
result = calculate_loss(scores_predict, scores_true)
|
13 |
+
data = count_model_csv(csv_file_path)
|
14 |
+
result.update(data)
|
15 |
+
result['date'] = datetime.now().strftime("%Y-%m-%d")
|
16 |
+
result['source'] = csv_file_path
|
17 |
+
append_to_csv(result, './data/suanfamama-fashion-guangzhou-dataset/log.csv')
|
18 |
+
|
19 |
+
|
20 |
+
# 从数据库评估模型调优结果并将数据存入log
|
21 |
+
def eval_model():
|
22 |
+
data = app_get_user()
|
23 |
+
scores_predict = [row["fashion_score_predict"] for row in data if "fashion_score_predict" in row]
|
24 |
+
scores_true = [row["fashion_score_true"] for row in data if "fashion_score_true" in row]
|
25 |
+
result = calculate_loss(scores_predict, scores_true)
|
26 |
+
result['date'] = datetime.now().strftime("%Y-%m-%d")
|
27 |
+
result['source'] = 'users'
|
28 |
+
append_to_csv(result, './data/suanfamama-fashion-guangzhou-dataset/log.csv')
|
29 |
+
|
30 |
+
|
31 |
+
# 计算需要统计的字段
|
32 |
+
def count_model_csv(csv_file_path='./data/temp/temp.csv'):
|
33 |
+
field_names = ['type', 'upper_garment', 'lower_garment', 'headgear',
|
34 |
+
'sock', 'shoe', 'accessory', 'backpack', 'scene',
|
35 |
+
'action', 'countenance', 'base_model']
|
36 |
+
results = {}
|
37 |
+
for field_name in field_names:
|
38 |
+
data = get_field_values(csv_file_path, field_name)
|
39 |
+
result = count_words_in_strings(data)
|
40 |
+
results[field_name] = result
|
41 |
+
return results
|
42 |
+
|
43 |
+
if __name__ == '__main__':
|
44 |
+
# eval_model_csv() # 从csv评估模型调优结果
|
45 |
+
# eval_model # 从数据库评估模型调优结果
|
46 |
+
print(datetime.now())
|
47 |
+
eval_model_csv("./data/suanfamama-fashion-guangzhou-dataset/20240731.csv")
|
48 |
+
print(datetime.now())
|
name_entity_recognition.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
import requests
|
3 |
+
from algs.alg0.utils.operate_json import *
|
4 |
+
|
5 |
+
|
6 |
+
# 命名实体识别
|
7 |
+
def name_entity_recognition(text,
|
8 |
+
model="qwen2:7b",
|
9 |
+
url="http://localhost:11434/api/chat"):
|
10 |
+
data = {
|
11 |
+
"model": model,
|
12 |
+
"messages": [
|
13 |
+
{
|
14 |
+
"role": "user",
|
15 |
+
"content": "从接下来的文本必须按照类型:内容的格式必须输出所提取人物类型、上衣类型、下衣类型、帽子类型、袜子类型、鞋子类型、背包类型、配饰类型、动作类型、表情类型,注意若未提及则内容输出为None,人物类型只有男性、女性、儿童、群体、None"
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"role": "user",
|
19 |
+
"content": text
|
20 |
+
}
|
21 |
+
],
|
22 |
+
"stream": False,
|
23 |
+
"temperature": 0.3
|
24 |
+
}
|
25 |
+
responce = requests.post(url, json=data)
|
26 |
+
reply = responce.json()['message']['content']
|
27 |
+
return reply
|
28 |
+
|
29 |
+
|
30 |
+
if __name__ == '__main__':
|
31 |
+
text = '这位小朋友身着白色T恤搭配棕色短裤,搭配白色袜子和灰色运动鞋,头戴黄色帽子,肩上挎着黑色包,手里还拿着一把伞,看起来准备好应对任何天气了呢!但是,这位小朋友的鞋子和袜子并不太搭配,灰色运动鞋和白色袜子有点不协调哦,稍微有点扣分。总体来说,搭配清爽舒适,颜色搭配也比较和谐,符合儿童时尚的要求。根据知识库的规则,考虑到服装设计、面料、搭配的角度,以及人物属性,这位小朋友的穿着可以给予7分。'
|
32 |
+
data = name_entity_recognition(text)
|
33 |
+
print(text_to_json(data))
|
person_registration.py
ADDED
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
from fastapi import FastAPI
|
3 |
+
import cv2
|
4 |
+
|
5 |
+
from algs.alg0.utils.load_input import *
|
6 |
+
from algs.alg0.utils.operate_csv import *
|
7 |
+
from algs.alg0.utils.operate_json import *
|
8 |
+
from algs.alg0.utils.operate_s3 import *
|
9 |
+
from algs.alg0.utils.utils import *
|
10 |
+
from algs.alg0.utils.operate_tx import *
|
11 |
+
from algs.alg0.segment import *
|
12 |
+
from algs.alg0.eval_fashion_person import *
|
13 |
+
from algs.alg0.utils.app import *
|
14 |
+
from algs.alg0.name_entity_recognition import *
|
15 |
+
|
16 |
+
app = FastAPI()
|
17 |
+
|
18 |
+
|
19 |
+
# 从模型结果中注册用户
|
20 |
+
def register_user(out,
|
21 |
+
is_csv=False,
|
22 |
+
is_app=False):
|
23 |
+
for box in out[0].boxes:
|
24 |
+
# 根据框截取人物图像
|
25 |
+
person_img = get_one_person(box.xyxy, out[0].orig_img)
|
26 |
+
|
27 |
+
# 获取图片名称
|
28 |
+
img_name = get_one_name()
|
29 |
+
|
30 |
+
# 将图片放到腾讯云
|
31 |
+
save_tx(file_data=person_img,
|
32 |
+
file_name=img_name)
|
33 |
+
|
34 |
+
# 获取评分
|
35 |
+
data = eval_fashion_person_tx_url(img_name)
|
36 |
+
|
37 |
+
# 进行被动用户注册
|
38 |
+
if is_csv:
|
39 |
+
append_to_csv(data)
|
40 |
+
if is_app:
|
41 |
+
app_register_user(data)
|
42 |
+
print(f'人物{data["username"]}注册完成时间{datetime.now()}')
|
43 |
+
|
44 |
+
|
45 |
+
# 使用摄像头发现行人并注册
|
46 |
+
def person_register_camera(camera_num=0,
|
47 |
+
is_csv=False,
|
48 |
+
is_app=False):
|
49 |
+
# 加载摄像头
|
50 |
+
cap = load_camera(camera_num)
|
51 |
+
_, img = cap.read()
|
52 |
+
|
53 |
+
# 调用模型推理
|
54 |
+
out = segmentation(img)
|
55 |
+
|
56 |
+
# 注册用户
|
57 |
+
register_user(out, is_csv, is_app)
|
58 |
+
|
59 |
+
|
60 |
+
# 使用视频流发现行人并注册
|
61 |
+
def person_register_frames(capture_frames,
|
62 |
+
is_csv=False,
|
63 |
+
is_app=False):
|
64 |
+
# 从实时视频流截取一帧
|
65 |
+
img = next(capture_frames)
|
66 |
+
|
67 |
+
# 调用模型推理
|
68 |
+
out = segmentation(img)
|
69 |
+
|
70 |
+
# 获取图片名称
|
71 |
+
img_name = get_one_name()
|
72 |
+
|
73 |
+
# 保存原始图片
|
74 |
+
save_tx(file_data=out[0].orig_img,
|
75 |
+
file_name=img_name,
|
76 |
+
bucket_name='fashion-imgs-1254150807')
|
77 |
+
|
78 |
+
# 保存画框图片
|
79 |
+
perd_out = out[0].plot()
|
80 |
+
person_name = img_name.split('.')[0]
|
81 |
+
pred_img_name = f'{person_name}_prediction.png'
|
82 |
+
save_tx(file_data=perd_out,
|
83 |
+
file_name=pred_img_name,
|
84 |
+
bucket_name='fashion-imgs-1254150807')
|
85 |
+
|
86 |
+
# 注册用户
|
87 |
+
register_user(out, is_csv, is_app)
|
88 |
+
|
89 |
+
|
90 |
+
# 从图片路径发现行人并注册
|
91 |
+
def person_register_imgpath(imgs_path="./data/suanfamama-fashion-guangzhou-dataset/20240722",
|
92 |
+
is_csv=False,
|
93 |
+
is_app=False):
|
94 |
+
# 从图片路径加载图片
|
95 |
+
for i, filename in enumerate(os.listdir(imgs_path)):
|
96 |
+
# 检查是否已该处理过图片
|
97 |
+
if check_image_name_in_tx(filename):
|
98 |
+
continue
|
99 |
+
|
100 |
+
# 从图片路径读取图片
|
101 |
+
img = cv2.imread(os.path.join(imgs_path, filename))
|
102 |
+
|
103 |
+
# 保存原始图片
|
104 |
+
save_tx(file_data=img,
|
105 |
+
file_name=filename,
|
106 |
+
bucket_name='fashion-imgs-1254150807')
|
107 |
+
|
108 |
+
# 调用模型推理
|
109 |
+
out = segmentation(img)
|
110 |
+
|
111 |
+
# 保存画框图片
|
112 |
+
perd_out = out[0].plot()
|
113 |
+
person_name = filename.split('.')[0]
|
114 |
+
pred_img_name = f'{person_name}_prediction.png'
|
115 |
+
save_tx(file_data=perd_out,
|
116 |
+
file_name=pred_img_name,
|
117 |
+
bucket_name='fashion-imgs-1254150807')
|
118 |
+
|
119 |
+
# 注册用户
|
120 |
+
register_user(out, is_csv, is_app)
|
121 |
+
|
122 |
+
|
123 |
+
# 从S3下载图片后发现行人并注册(停止维护)
|
124 |
+
def person_register_s3(bucket_name='fashion-guangzhou-dataset',
|
125 |
+
is_csv=False,
|
126 |
+
is_app=False):
|
127 |
+
# 暂存图片地址
|
128 |
+
file_path = './data/temp/temp.png'
|
129 |
+
|
130 |
+
# 创建S3资源对象
|
131 |
+
from algs.alg0.utils.operate_s3 import config_S3
|
132 |
+
s3 = config_S3()
|
133 |
+
|
134 |
+
# 获取桶对象
|
135 |
+
bucket = s3.Bucket(bucket_name)
|
136 |
+
|
137 |
+
# 从S3桶加载图片
|
138 |
+
for i, obj in enumerate(bucket.objects.all()):
|
139 |
+
|
140 |
+
# 检查是否已经注册
|
141 |
+
from algs.alg0.utils.operate_s3 import check_image_name_in_s3
|
142 |
+
if check_image_name_in_s3(obj.key):
|
143 |
+
continue
|
144 |
+
print(f'本图片开始时间{datetime.now()}')
|
145 |
+
|
146 |
+
# 用图片名字获取图片
|
147 |
+
from algs.alg0.utils.operate_s3 import take_img_S3
|
148 |
+
take_img_S3(obj.key)
|
149 |
+
img = cv2.imread('data/suanfamama-fashion-guangzhou-dataset/temp.jpeg', cv2.IMREAD_COLOR)
|
150 |
+
print(f'读取图片完成时间{datetime.now()}')
|
151 |
+
|
152 |
+
# 调用模型推理
|
153 |
+
out = segmentation(img)
|
154 |
+
|
155 |
+
# 保存带框图片
|
156 |
+
pro_out = out[0].plot()
|
157 |
+
# cv2.imwrite(f'./data/exp3.in/result/{filename}.png', pro_out)
|
158 |
+
save_img_as_png(pro_out, file_path)
|
159 |
+
image_name = obj.key.split('.')[0]
|
160 |
+
metadata = {'Content-Type': 'image/png'}
|
161 |
+
bucket.upload_file(file_path, f'{image_name}.png', ExtraArgs={'ContentType': 'image/png'})
|
162 |
+
print(f'保存图片完成时间{datetime.now()}')
|
163 |
+
|
164 |
+
for box in out[0].boxes:
|
165 |
+
# 根据框截取人物图像
|
166 |
+
person_img = get_one_person(box.xyxy, out[0].orig_img)
|
167 |
+
|
168 |
+
# 将图片存为PNG文件
|
169 |
+
save_img_as_png(person_img, file_path)
|
170 |
+
|
171 |
+
# 获取图片名称
|
172 |
+
img_name, person_name = get_one_name()
|
173 |
+
|
174 |
+
# 将图片放到S3
|
175 |
+
from algs.alg0.utils.operate_s3 import save_S3
|
176 |
+
save_S3(img_name)
|
177 |
+
|
178 |
+
# 从S3取出图片URL
|
179 |
+
from algs.alg0.utils.operate_s3 import take_url_S3
|
180 |
+
img_url = take_url_S3(img_name)
|
181 |
+
|
182 |
+
# 获取图片的评分和评价
|
183 |
+
score, conclusion = get_score_conclusion(img_url)
|
184 |
+
|
185 |
+
# 将数据追加写入csv
|
186 |
+
data = [image_name, person_name, img_url, score, conclusion]
|
187 |
+
append_to_csv(data, csv_file_path="./data/temp/temp.csv")
|
188 |
+
print(f'保存单个人物图片完成时间{datetime.now()}')
|
189 |
+
|
190 |
+
# 进行被动用户注册
|
191 |
+
result = {
|
192 |
+
"username": person_name,
|
193 |
+
"avatar_url": img_url,
|
194 |
+
"fashion_score_true": -1,
|
195 |
+
"fashion_score_predict": score,
|
196 |
+
"fashion_eval_reason": conclusion
|
197 |
+
}
|
198 |
+
register_user(result)
|
199 |
+
print(f'单个人物注册完成时间{datetime.now()}')
|
200 |
+
|
201 |
+
|
202 |
+
# 跟据标签分割人物并注册
|
203 |
+
def person_register_label(imgs_path='./data/suanfamama-fashion-guangzhou-dataset/20240730',
|
204 |
+
label_path='./data/suanfamama-fashion-guangzhou-dataset/20240730_label',
|
205 |
+
is_csv=False,
|
206 |
+
is_app=False):
|
207 |
+
# 从标签路径加载标签并读取相应图片
|
208 |
+
for i, filename in enumerate(os.listdir(label_path)):
|
209 |
+
# 从标签获取标签值和box以及图片路径
|
210 |
+
print(f'本标签{filename}开始时间{datetime.now()}')
|
211 |
+
img_path, boxes = read_json_label(os.path.join(label_path, filename))
|
212 |
+
|
213 |
+
for box in boxes:
|
214 |
+
# 读取原始图片
|
215 |
+
img_path = img_path.split('\\')[-1]
|
216 |
+
orig_img = cv2.imread(os.path.join(imgs_path, img_path),
|
217 |
+
cv2.IMREAD_COLOR)
|
218 |
+
|
219 |
+
# 根据框截取人物图像
|
220 |
+
xyxy = box['xyxy']
|
221 |
+
person_img = orig_img[int(xyxy[1]):int(xyxy[3]), int(xyxy[0]):int(xyxy[2]), ::-1]
|
222 |
+
person_img = output_to_binary(person_img)
|
223 |
+
|
224 |
+
# 获取图片名称
|
225 |
+
img_name = get_one_name()
|
226 |
+
|
227 |
+
# 将图片放到腾讯云
|
228 |
+
save_tx(file_data=person_img,
|
229 |
+
file_name=img_name)
|
230 |
+
|
231 |
+
# 获取评分
|
232 |
+
data = eval_fashion_person_tx_url(img_name)
|
233 |
+
|
234 |
+
# 进行被动用户注册
|
235 |
+
data["fashion_score_true"] = box['label'],
|
236 |
+
|
237 |
+
if is_csv:
|
238 |
+
append_to_csv(data)
|
239 |
+
if is_app:
|
240 |
+
app_register_user(data)
|
241 |
+
print(f'人物{data["username"]}注册完成时间{datetime.now()}')
|
242 |
+
|
243 |
+
|
244 |
+
# 从CSV文件中读取人物信息并注册
|
245 |
+
def person_register_csv(csv_file_path='./data/temp/temp.csv'):
|
246 |
+
data = read_csv_to_dicts(csv_file_path)
|
247 |
+
for i, t in enumerate(data):
|
248 |
+
app_register_user(t)
|
249 |
+
print(f'单人注册完成时间{datetime.now()}')
|
250 |
+
|
251 |
+
|
252 |
+
# 更新数据库中每个人的评分
|
253 |
+
def update_user():
|
254 |
+
# 获取数据库中被动注册的用户数据
|
255 |
+
data_list = app_get_user()
|
256 |
+
for person in data_list:
|
257 |
+
# 获取评分
|
258 |
+
person_name = person[0]
|
259 |
+
img_name = person_name + '.png'
|
260 |
+
data = eval_fashion_person_tx_url(img_name)
|
261 |
+
|
262 |
+
# 进行被动用户更新
|
263 |
+
result = {
|
264 |
+
"gender": data[4]
|
265 |
+
}
|
266 |
+
app_register_user(result, url="http://localhost:8000/users/updataRegByCamera")
|
267 |
+
print(f'人物{data["username"]}更新完成时间{datetime.now()}')
|
268 |
+
|
269 |
+
|
270 |
+
# 更新csv中每个人的评分
|
271 |
+
def update_user_csv(csv_file_path='./data/temp/temp.csv'):
|
272 |
+
# 创建一个临时文件来保存更新后的数据
|
273 |
+
temp_file_path = csv_file_path + '.tmp'
|
274 |
+
|
275 |
+
# 获取CSV中的数据
|
276 |
+
result = []
|
277 |
+
data_list = read_csv_to_dicts(csv_file_path)
|
278 |
+
for row in data_list:
|
279 |
+
img_name = row["username"] + '.png'
|
280 |
+
data = eval_fashion_person_tx_url(img_name)
|
281 |
+
result.append(data)
|
282 |
+
print(f'人物{data["username"]}更新完成时间{datetime.now()}')
|
283 |
+
dict_to_csv(result, temp_file_path)
|
284 |
+
|
285 |
+
# 替换原始文件
|
286 |
+
os.replace(temp_file_path, csv_file_path)
|
287 |
+
|
288 |
+
|
289 |
+
# 从数据库获取用户到csv
|
290 |
+
def export_user_to_csv(csv_file_path='./data/temp/temp.csv'):
|
291 |
+
users = app_get_user()
|
292 |
+
for user in users:
|
293 |
+
json = {}
|
294 |
+
if check_in_csv(user['username'], csv_file_path, 'username'):
|
295 |
+
continue
|
296 |
+
while not json:
|
297 |
+
data = name_entity_recognition(user['fashion_eval_reason'])
|
298 |
+
json = text_to_json(data)
|
299 |
+
user.update(json)
|
300 |
+
user['base_model'] = 'LINKAI-3.5'
|
301 |
+
user['scene'] = '沙面'
|
302 |
+
append_to_csv(user, csv_file_path)
|
303 |
+
print(f"{user['username']}获取完成时间:{datetime.now()}")
|
304 |
+
|
305 |
+
|
306 |
+
if __name__ == '__main__':
|
307 |
+
# register_user() # 从模型结果中注册用户
|
308 |
+
# person_register_camera() # 使用摄像头发现行人并注册
|
309 |
+
# person_register_frames(capture_frames()) # 使用视频流发现行人并注册
|
310 |
+
# person_register_imgpath() # 从图片路径发现行人并注册
|
311 |
+
# person_register_s3() # 从S3下载图片后发现行人并注册(停止维护)
|
312 |
+
# person_register_label() # 跟据标签分割人物并注册
|
313 |
+
# person_register_csv() # 从CSV注册进数据库
|
314 |
+
# update_user() # 更新数据库中的被动用户数据
|
315 |
+
# update_user_csv() # 更新CSV中的被动用户数据
|
316 |
+
# export_user_to_csv() # 从数据库获取被动用户信息
|
317 |
+
|
318 |
+
print(datetime.now())
|
319 |
+
export_user_to_csv(csv_file_path='./data/suanfamama-fashion-guangzhou-dataset/20240731.csv')
|
320 |
+
print(datetime.now())
|
requirements.txt
ADDED
Binary file (4.49 kB). View file
|
|
segment.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from algs.alg0.utils.load_input import *
|
2 |
+
|
3 |
+
|
4 |
+
# 使用YOLO进行语义分割
|
5 |
+
def segmentation(input,
|
6 |
+
img_size=(1920, 1088),
|
7 |
+
model_path='./config/weight/yolov8n-seg.pt'):
|
8 |
+
# 加载YOLOv8语义分割模型(最小参数量)
|
9 |
+
model = load_model(model_path)
|
10 |
+
|
11 |
+
# 加载模型到图形计算设备
|
12 |
+
model = load_device(model)
|
13 |
+
|
14 |
+
# 模型推理
|
15 |
+
results = model(input, imgsz=img_size, classes=0)
|
16 |
+
|
17 |
+
return results
|
18 |
+
|
19 |
+
|
20 |
+
# 循环读取摄像头推理
|
21 |
+
def reuse_camera_segmentation(camera_num=0,
|
22 |
+
box=True):
|
23 |
+
# 循环读取摄像头推理
|
24 |
+
while True:
|
25 |
+
|
26 |
+
# 加载摄像头
|
27 |
+
cap = load_camera(camera_num)
|
28 |
+
|
29 |
+
# 读取摄像头内容
|
30 |
+
_, input = cap.read()
|
31 |
+
|
32 |
+
out = segmentation(input)
|
33 |
+
|
34 |
+
# 显示结果(带框/不带框)
|
35 |
+
if box:
|
36 |
+
out = out[0].plot()
|
37 |
+
else:
|
38 |
+
out = out[0].plot(boxes=False) # 不显示预测框
|
39 |
+
|
40 |
+
# 显示结果
|
41 |
+
cv2.imshow('frame', out)
|
42 |
+
|
43 |
+
# 等待退出
|
44 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
45 |
+
break
|
46 |
+
|
47 |
+
# 关闭摄像头
|
48 |
+
cap.release()
|
49 |
+
|
50 |
+
# 关闭窗口
|
51 |
+
cv2.destroyAllWindows()
|
52 |
+
|
53 |
+
|
54 |
+
if __name__ == '__main__':
|
55 |
+
# 单次展示语义分割
|
56 |
+
# cap = load_camera(1)
|
57 |
+
# _, input = cap.read()
|
58 |
+
# out = segmentation(input)
|
59 |
+
|
60 |
+
# 展示实时语义分割
|
61 |
+
reuse_camera_segmentation(camera_num=1)
|
speech_to_txt.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import whisper
|
2 |
+
import os
|
3 |
+
|
4 |
+
model = whisper.load_model("base")
|
5 |
+
|
6 |
+
# load audio and pad/trim it to fit 30 seconds
|
7 |
+
audio = whisper.load_audio("./data/ling-voice/0730/073001.m4a")
|
8 |
+
audio = whisper.pad_or_trim(audio)
|
9 |
+
|
10 |
+
# make log-Mel spectrogram and move to the same device as the model
|
11 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
12 |
+
|
13 |
+
# detect the spoken language
|
14 |
+
_, probs = model.detect_language(mel)
|
15 |
+
print(f"Detected language: {max(probs, key=probs.get)}")
|
16 |
+
|
17 |
+
# decode the audio
|
18 |
+
options = whisper.DecodingOptions()
|
19 |
+
result = whisper.decode(model, mel, options)
|
20 |
+
|
21 |
+
# print the recognized text
|
22 |
+
print(result.text)
|
utils/__init__.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .operate_s3 import *
|
2 |
+
from .load_input import *
|
3 |
+
from .utils import *
|
4 |
+
from .operate_csv import *
|
5 |
+
from .operate_json import *
|
6 |
+
from .operate_tx import *
|
7 |
+
from .app import *
|
utils/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (409 Bytes). View file
|
|
utils/__pycache__/load_input.cpython-311.pyc
ADDED
Binary file (2.75 kB). View file
|
|
utils/__pycache__/operate_s3.cpython-311.pyc
ADDED
Binary file (2.87 kB). View file
|
|
utils/__pycache__/utils.cpython-311.pyc
ADDED
Binary file (4.33 kB). View file
|
|
utils/app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
|
4 |
+
|
5 |
+
# 注册/更新进数据库
|
6 |
+
def app_register_user(data, url="http://localhost:8000/users/regByCamera"):
|
7 |
+
# 发起POST请求
|
8 |
+
api_key = os.environ["Mama_API_Key"] # 获取token
|
9 |
+
response = requests.post(url,
|
10 |
+
json=data,
|
11 |
+
headers={"Content-Type": "application/json",
|
12 |
+
"mama_api_key": api_key})
|
13 |
+
|
14 |
+
# 获取JSON响应
|
15 |
+
if response.status_code == 200:
|
16 |
+
# 检查响应是否为空
|
17 |
+
if response.text.strip():
|
18 |
+
try:
|
19 |
+
json_response = response.json()
|
20 |
+
print(json_response)
|
21 |
+
except ValueError as e:
|
22 |
+
print(f"解析JSON响应失败: {e}")
|
23 |
+
return []
|
24 |
+
else:
|
25 |
+
print("回应为空")
|
26 |
+
return []
|
27 |
+
else:
|
28 |
+
print(f"回应失败状态码:{response.status_code}")
|
29 |
+
return []
|
30 |
+
|
31 |
+
|
32 |
+
# 从数据库获取被动用户数据
|
33 |
+
def app_get_user(url="http://localhost:8000/users/getUserInfo"):
|
34 |
+
# 发起GET请求
|
35 |
+
api_key = os.environ["Mama_API_Key"] # 获取token
|
36 |
+
response = requests.get(url,
|
37 |
+
headers={"Content-Type": "application/json",
|
38 |
+
"mama_api_key": api_key})
|
39 |
+
# 获取响应内容
|
40 |
+
if response.status_code == 200:
|
41 |
+
# 检查响应是否为空
|
42 |
+
if response.text.strip():
|
43 |
+
try:
|
44 |
+
data_list = response.json()
|
45 |
+
return data_list
|
46 |
+
except ValueError as e:
|
47 |
+
print(f"解析JSON响应失败: {e}")
|
48 |
+
return []
|
49 |
+
else:
|
50 |
+
print("回应为空")
|
51 |
+
return []
|
52 |
+
else:
|
53 |
+
print(f"回应失败状态码:{response.status_code}")
|
54 |
+
return []
|
55 |
+
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
# app_register_user() # 注册/更新数据库
|
59 |
+
# app_get_user() # 从数据库获取被动用户数据
|
60 |
+
print(app_get_user())
|
utils/load_input.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
from ultralytics import YOLO
|
3 |
+
import torch
|
4 |
+
|
5 |
+
|
6 |
+
# 读取摄像头(返回摄像头)
|
7 |
+
def load_camera(num="http://192.168.1.3:8080",
|
8 |
+
width=1920,
|
9 |
+
height=1080):
|
10 |
+
cap = cv2.VideoCapture(num)
|
11 |
+
|
12 |
+
# 设置摄像头参数
|
13 |
+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
|
14 |
+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
|
15 |
+
|
16 |
+
if cap.isOpened():
|
17 |
+
return cap
|
18 |
+
else:
|
19 |
+
return None
|
20 |
+
|
21 |
+
|
22 |
+
# 读取视频
|
23 |
+
def load_video(video_path):
|
24 |
+
video = cv2.VideoCapture(video_path)
|
25 |
+
return video
|
26 |
+
|
27 |
+
|
28 |
+
# 加载模型
|
29 |
+
def load_model(model_path,
|
30 |
+
yaml_path=None,
|
31 |
+
task='segment'):
|
32 |
+
# 加载(改动过结构)的模型
|
33 |
+
if yaml_path:
|
34 |
+
model = YOLO(yaml_path, task=task).load(model_path)
|
35 |
+
else:
|
36 |
+
model = YOLO(model_path, task=task)
|
37 |
+
return model
|
38 |
+
|
39 |
+
|
40 |
+
# 加载模型到图形计算设备
|
41 |
+
def load_device(model):
|
42 |
+
# 获取图形计算设备信息
|
43 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
44 |
+
|
45 |
+
# 将模型切换到图形计算设备上
|
46 |
+
model.to(device)
|
47 |
+
|
48 |
+
return model
|
49 |
+
|
50 |
+
|
51 |
+
# 将摄像头设置成生成器
|
52 |
+
def capture_frames(num=0):
|
53 |
+
cap = load_camera(num)
|
54 |
+
while True:
|
55 |
+
ret, frame = cap.read()
|
56 |
+
if not ret:
|
57 |
+
break
|
58 |
+
yield frame
|
59 |
+
cap.release()
|
60 |
+
|
61 |
+
|
62 |
+
# 使用生成器形成视频流
|
63 |
+
def generate_capture_frames():
|
64 |
+
for frame in capture_frames():
|
65 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
66 |
+
frame = buffer.tobytes()
|
67 |
+
yield (b'--frame\r\n'
|
68 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
69 |
+
|
utils/operate_csv.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
import os
|
3 |
+
|
4 |
+
|
5 |
+
# 续写CSV
|
6 |
+
def append_to_csv(data,
|
7 |
+
csv_file_path="../data/temp/temp.csv"):
|
8 |
+
# 检查文件是否存在及是否为空
|
9 |
+
if not os.path.exists(csv_file_path) or os.stat(csv_file_path).st_size == 0:
|
10 |
+
# 文件不存在或为空,需要写入表头
|
11 |
+
fieldnames = list(data.keys())
|
12 |
+
with open(csv_file_path, 'w', newline='', encoding='utf-8') as file:
|
13 |
+
writer = csv.DictWriter(file, fieldnames=fieldnames)
|
14 |
+
writer.writeheader()
|
15 |
+
with open(csv_file_path, 'r', newline='', encoding='utf-8') as file:
|
16 |
+
reader = csv.DictReader(file)
|
17 |
+
fieldnames = reader.fieldnames
|
18 |
+
with open(csv_file_path, 'a', newline='', encoding='utf-8') as file:
|
19 |
+
writer = csv.DictWriter(file, fieldnames=fieldnames)
|
20 |
+
writer.writerow(data)
|
21 |
+
|
22 |
+
|
23 |
+
# 读取CSV文件并返回字典
|
24 |
+
def read_csv_to_dicts(filename='../data/temp/temp.csv'):
|
25 |
+
data = []
|
26 |
+
with open(filename, mode='r', encoding='utf-8') as file:
|
27 |
+
reader = csv.DictReader(file)
|
28 |
+
for row in reader:
|
29 |
+
data.append(row)
|
30 |
+
return data
|
31 |
+
|
32 |
+
|
33 |
+
# 将字典列表写入CSV文件
|
34 |
+
def dict_to_csv(data, csv_file_path='./data/temp/temp.csv'):
|
35 |
+
with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file:
|
36 |
+
writer = csv.DictWriter(file, fieldnames=list(data[0].keys()))
|
37 |
+
writer.writeheader()
|
38 |
+
writer.writerows(data)
|
39 |
+
|
40 |
+
|
41 |
+
# 从CSV中读取指定字段的所有值
|
42 |
+
def get_field_values(file_path, field_name):
|
43 |
+
if not os.path.exists(file_path):
|
44 |
+
return None
|
45 |
+
data = read_csv_to_dicts(file_path)
|
46 |
+
values = [row[field_name] for row in data if field_name in row]
|
47 |
+
return values
|
48 |
+
|
49 |
+
|
50 |
+
# 检查值是否存在于CSV
|
51 |
+
def check_in_csv(data, file_path, field_name):
|
52 |
+
datas = get_field_values(file_path, field_name)
|
53 |
+
if datas == None:
|
54 |
+
return False
|
55 |
+
return True if data in datas else False
|
utils/operate_json.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
from ..name_entity_recognition import *
|
5 |
+
from algs.alg0.utils.utils import *
|
6 |
+
|
7 |
+
|
8 |
+
# 将二维数组转化为JSON字符串
|
9 |
+
def data_to_json(data_matrix):
|
10 |
+
results = []
|
11 |
+
for row in data_matrix:
|
12 |
+
result = {"person_name": row[0],
|
13 |
+
"img_url": row[1],
|
14 |
+
"score": row[2],
|
15 |
+
"conclusion": row[3]
|
16 |
+
}
|
17 |
+
results.append(result)
|
18 |
+
return json.dumps({"results": results}, ensure_ascii=False, indent=4)
|
19 |
+
|
20 |
+
|
21 |
+
# 读取labeljson返回所需参数
|
22 |
+
def read_json_label(json_file_path='./data/suanfamama-fashion-guangzhou-dataset/20240722_label/20240722-193429-6.json'):
|
23 |
+
# 检查json路径
|
24 |
+
if not os.path.exists(json_file_path):
|
25 |
+
print(f" {json_file_path}JSON不存在")
|
26 |
+
return None
|
27 |
+
|
28 |
+
# 读取JSON数据
|
29 |
+
with open(json_file_path, 'r', encoding='utf-8') as file:
|
30 |
+
data = json.loads(file.read())
|
31 |
+
|
32 |
+
# 提取图片路径
|
33 |
+
image_path = data['imagePath']
|
34 |
+
|
35 |
+
# 初始化标签名及对应边界框坐标列表
|
36 |
+
labels_with_boxes = []
|
37 |
+
|
38 |
+
# 遍历所有形状(shapes)以获取标签和坐标
|
39 |
+
for shape in data['shapes']:
|
40 |
+
label = shape['label']
|
41 |
+
xyxy = [shape['points'][0][0], shape['points'][0][1], shape['points'][1][0], shape['points'][1][1]]
|
42 |
+
labels_with_boxes.append({'label': label, 'xyxy': xyxy})
|
43 |
+
|
44 |
+
return image_path, labels_with_boxes
|
45 |
+
|
46 |
+
|
47 |
+
# 将字符串转换为json
|
48 |
+
def text_to_json(text):
|
49 |
+
# 将中文字段转为英文
|
50 |
+
transform = {'人物类型': 'type',
|
51 |
+
'上衣类型': 'upper_garment',
|
52 |
+
'下衣类型': 'lower_garment',
|
53 |
+
'帽子类型': 'headgear',
|
54 |
+
'袜子类型': 'sock',
|
55 |
+
'鞋子类型': 'shoe',
|
56 |
+
'配饰类型': 'accessory',
|
57 |
+
'背包类型': 'backpack',
|
58 |
+
'动作类型': 'action',
|
59 |
+
'表情类型': 'countenance'
|
60 |
+
}
|
61 |
+
|
62 |
+
lines = text.split('\n')
|
63 |
+
data = {}
|
64 |
+
for line in lines:
|
65 |
+
parts = line.strip().split(':')
|
66 |
+
if len(parts) < 2:
|
67 |
+
continue
|
68 |
+
key = transform.get(parts[0].strip())
|
69 |
+
value = parts[1].strip()
|
70 |
+
if contains_any_keyword(value, ['未', '无']):
|
71 |
+
value = "None"
|
72 |
+
if key:
|
73 |
+
data[key] = value
|
74 |
+
return data
|
utils/operate_s3.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import boto3
|
2 |
+
import yaml
|
3 |
+
|
4 |
+
|
5 |
+
# 上传PNG图片到S3
|
6 |
+
def save_S3(file_name,
|
7 |
+
s3_name='fashion-imgs'):
|
8 |
+
# 设置暂缓存图片地址
|
9 |
+
file_path = '../data/temp/temp.png'
|
10 |
+
|
11 |
+
# 创建S3资源对象
|
12 |
+
s3 = config_S3()
|
13 |
+
|
14 |
+
# 获取桶对象
|
15 |
+
bucket = s3.Bucket(s3_name)
|
16 |
+
|
17 |
+
# 设置元数据格式(避免获取URL时变成下载)
|
18 |
+
metadata = {'Content-Type': 'image/png'}
|
19 |
+
|
20 |
+
# 上传文件
|
21 |
+
bucket.upload_file(file_path, file_name, ExtraArgs={'ContentType': 'image/png'})
|
22 |
+
|
23 |
+
|
24 |
+
# 从S3取出图片URL
|
25 |
+
def take_url_S3(img_name='person1.png',
|
26 |
+
s3_name='fashion-imgs'):
|
27 |
+
# 创建S3资源对象
|
28 |
+
s3 = config_S3()
|
29 |
+
|
30 |
+
# 获取对象的URL
|
31 |
+
img_url = s3.meta.client.generate_presigned_url(
|
32 |
+
'get_object',
|
33 |
+
Params={
|
34 |
+
'Bucket': s3_name,
|
35 |
+
'Key': img_name
|
36 |
+
}
|
37 |
+
)
|
38 |
+
|
39 |
+
return img_url
|
40 |
+
|
41 |
+
|
42 |
+
# 从S3下载图片
|
43 |
+
def take_img_S3(s3_file_key='20240722-191721-5.jpeg',
|
44 |
+
local_file_path='../data/temp/temp.jpeg',
|
45 |
+
bucket_name='fashion-guangzhou-dataset'):
|
46 |
+
# 创建S3资源对象
|
47 |
+
s3 = config_S3()
|
48 |
+
|
49 |
+
# 从指定桶下载指定图片
|
50 |
+
s3.meta.client.download_file(bucket_name, s3_file_key, local_file_path)
|
51 |
+
|
52 |
+
|
53 |
+
# 配置S3
|
54 |
+
def config_S3(yaml_path='../config/S3/S3.yaml'):
|
55 |
+
# 读取S3的配置
|
56 |
+
f = open(yaml_path, 'r', encoding='utf-8')
|
57 |
+
res = yaml.load(f, Loader=yaml.FullLoader)
|
58 |
+
|
59 |
+
# 配置S3的访问信息
|
60 |
+
access_key = res['access_key']
|
61 |
+
secret_key = res['secret_key']
|
62 |
+
|
63 |
+
# 创建S3资源对象
|
64 |
+
s3 = boto3.resource('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
|
65 |
+
|
66 |
+
return s3
|
67 |
+
|
68 |
+
|
69 |
+
# 检查S3存储桶中是否存在指定名称的图片(忽略文件扩展名)
|
70 |
+
def check_image_name_in_s3(image_name,
|
71 |
+
bucket_name='fashion-guangzhou-dataset'):
|
72 |
+
# 创建S3资源对象
|
73 |
+
s3 = config_S3()
|
74 |
+
|
75 |
+
# 获取桶对象
|
76 |
+
bucket = s3.Bucket(bucket_name)
|
77 |
+
|
78 |
+
# 列出Bucket中的所有对象
|
79 |
+
objects = bucket.objects.all()
|
80 |
+
|
81 |
+
# 检查每个对象的键(key),看是否与image_name匹配(忽略后缀)
|
82 |
+
for obj in objects:
|
83 |
+
# 去除键中的文件扩展名后比较
|
84 |
+
if obj.key.split('.')[:-1] == image_name.split('.'):
|
85 |
+
return True
|
86 |
+
|
87 |
+
return False
|