-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathutility.py
More file actions
170 lines (137 loc) · 5.14 KB
/
utility.py
File metadata and controls
170 lines (137 loc) · 5.14 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 4 10:50:52 2025
@author: 18523
"""
import base64
import requests
import cv2
import time
import numpy as np
import json
import httpx
from config import APIConfig, RAGConfig,VideoConfig
def frames_to_base64(frames,fps,timestamps):
print(len(frames))
print(fps)
width = frames[0].shape[1]
height = frames[0].shape[0]
fourcc = cv2.VideoWriter_fourcc(*'avc1')
#filename = ":".join(timestamps).replace("-","")
#video_writer = cv2.VideoWriter(f'./{filename}.mp4', fourcc, fps, (width, height))
video_writer = cv2.VideoWriter('./video_warning/output.mp4', fourcc, fps, (width, height))
# 遍历所有帧,并将其写入视频文件
for frame in frames:
# 确保帧是正确的数据类型和形状
if frame.dtype != np.uint8:
frame = frame.astype(np.uint8)
if len(frame.shape) == 2:
# 如果帧是灰度的,转换为BGR
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
video_writer.write(frame)
# 释放VideoWriter对象
video_writer.release()
with open('./video_warning/output.mp4', 'rb') as video_file:
video_base64 = base64.b64encode(video_file.read()).decode('utf-8')
return video_base64
#强制抽取关键帧帧,每秒一帧率
async def video_chat_async_limit_frame(text, frames,timestamps,fps=20):
video_base64 = frames_to_base64(frames,fps,timestamps)
#url = "http://172.16.10.44:8085/v1/chat/completions"
url = APIConfig.QWEN_API_URL
headers = {
"Content-Type": "application/json",
"authorization": APIConfig.QWEN_API_KEY
}
model = APIConfig.QWEN_MODEL
data_image = []
frame_count = int(VideoConfig.BUFFER_DURATION)
for i in range(frame_count):
frame = frames[(len(frames)//frame_count)*i]
image_path = 'output_frame.jpg'
cv2.imwrite(image_path, frame)
with open(image_path,'rb') as file:
image_base64 = "data:image/jpeg;base64,"+ base64.b64encode(file.read()).decode('utf-8')
data_image.append(image_base64)
content = [{"type": "text", "text": text}] + [{"type": "image_url","image_url": {"url":i}} for i in data_image]
# 构建API请求的URL和Headers
# 构建请求体
data = {
"model": model, # 模型名称
"vl_high_resolution_images":False,
"messages": [
{
"role": "user",
"content": content,
}
],
}
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0)) as client:
response = await client.post(url, headers=headers, json=data)
response_data = response.json()
#print(response_data)
return response_data['choices'][0]['message']['content']
async def video_chat_async(text, frames, timestamps, fps=20):
video_base64 = frames_to_base64(frames, fps, timestamps)
url = APIConfig.QWEN_API_URL
headers = {
"Content-Type": "application/json",
"authorization": APIConfig.QWEN_API_KEY
}
model = APIConfig.QWEN_MODEL
data = {
"model": model,
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": text},
{
"type": "video_url",
"video_url": {
"url": f"data:video/mp4;base64,{video_base64}"
}
}
]
}
],
"stop_token_ids": [151645, 151643]
}
async with httpx.AsyncClient(timeout=httpx.Timeout(APIConfig.REQUEST_TIMEOUT)) as client:
response = await client.post(url, headers=headers, json=data)
response_data = response.json()
return response_data['choices'][0]['message']['content']
async def chat_request(message,stream=False):
url = APIConfig.MOONSHOT_API_URL
model = APIConfig.MOONSHOT_MODEL
messages =[{"role" : "user", "content" :message}]
headers = {
"content-Type" : "application/json",
"authorization" : APIConfig.MOONSHOT_API_KEY
}
data ={
"messages" : messages,
"model" : model,
"repetition_penalty" : APIConfig.REPETITION_PENALTY,
"temperature" : APIConfig.TEMPERATURE,
"top_p": APIConfig.TOP_P,
"top_k": APIConfig.TOP_K,
"stream" : stream
}
async with httpx.AsyncClient(timeout=httpx.Timeout(APIConfig.REQUEST_TIMEOUT)) as client:
response = await client.post(url, headers=headers, json=data)
response = response.json()
return response['choices'][0]['message']['content']
def insert_txt(docs,table_name):
#插入文本,同时向量化
url = RAGConfig.VECTOR_API_URL
"""docs = [
"Artificial intelligence was founded as an academic discipline in 1956.",
"The field of AI research was founded at a workshop held on the campus of Dartmouth College during the summer of 1956."
]"""
data = {
"docs": docs,
"table_name": table_name
}
response = requests.post(url, json=data)
return response.json()