-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
178 lines (145 loc) · 7.4 KB
/
Copy pathmain.py
File metadata and controls
178 lines (145 loc) · 7.4 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
171
172
173
174
175
176
177
178
"""
main.py
运行整个项目完整的流程管线
Author: Zhu Jiahao
Date: 2025-07-14
"""
import os
from src.api.image_api import OpenCVImageClient
from src.api.qwen_api import QwenClient
from src.api.deepseek_api import DeepSeekClient
from src.api.writing_api import RobotWritingClient
from src.utils.utils import read_txt_file, format_text_to_json
from src.utils.config import __config__
from src.utils.logger import __logger__
def main():
"""
@TODO: Describe the whole pipeline
"""
pipeline_logger = __logger__.get_module_logger("pipeline")
# 初始化
path_config = __config__.get_path_config()
camera_config = __config__.get_camera_config()
qwen_config = __config__.get_api_config("qwen")
qwen_vl_config = __config__.get_api_config("qwen_vl")
deepseek_config = __config__.get_api_config("deepseek")
robot_config = __config__.get_robot_config()
assets_confog = __config__.get_assets_config()
# 文件路径
INPUT_IMAGE_PATH = path_config.get("input", {}).get("images") # 输入照片路径
OUTPUT_LOG_PATH = path_config.get("output", {}).get("logs") # 输出日志路径
# OUTPUT_UNIT_PATH = path_config.get("output", {}).get("units") # 单元分割结果输出路径
IMAGE_FILENAME = os.path.join(INPUT_IMAGE_PATH, "raw_image.jpg") # 原始图像文件
OCR_FILENAME = os.path.join(OUTPUT_LOG_PATH, "ocr_result.txt") # OCR结果文件
ANSWER_FILENAME = os.path.join(OUTPUT_LOG_PATH, "answer.txt") # AI答案文件
BOX_VIZ_IMAGE_FILENAME = os.path.join(OUTPUT_LOG_PATH, "box_viz_image.png") # 标注答题框的图片
PREVIEW_IMAGE_FILENAME = os.path.join(OUTPUT_LOG_PATH, "preview.png") # 预览图
TASK_FILENAME = os.path.join(OUTPUT_LOG_PATH, "task.json") # 任务编排
image_client = OpenCVImageClient(
camera_config.get("id")
)
robot_writer = RobotWritingClient(
robot_config.get("com_port"),
robot_config.get("baudrate"),
robot_config.get("z_up"),
robot_config.get("z_down"),
robot_config.get("speed_move"),
robot_config.get("speed_write"),
robot_config.get("origin_x"),
robot_config.get("origin_y"),
assets_confog.get("chinese_fonts")
)
qwen_client = QwenClient(
api_key=qwen_config.get("api_key"),
base_url=qwen_config.get("base_url"),
vl_model=qwen_vl_config.get("model"),
text_model=qwen_config.get("model")
)
deepseek_client = DeepSeekClient(
api_key=deepseek_config.get("api_key"),
base_url=deepseek_config.get("base_url"),
model=deepseek_config.get("model")
)
print("请选择操作类型: ")
print("[1] 直接书写")
print("[2] AI答题")
strategy = input()
if strategy == "1":
print("请输入想要书写的文本: ")
text = input()
format_text_to_json(text, TASK_FILENAME)
robot_writer.go_center()
tasks = robot_writer.load_writing_tasks(TASK_FILENAME)
for task in tasks:
robot_writer.write_text_line(
task.get("text"),
task.get("a4_x_mm"),
task.get("a4_y_mm"),
task.get("char_height_mm"),
task.get("char_spacing_ratio")
)
robot_writer.stand_by()
if strategy == "2":
pipeline_logger.info("=====================================================")
pipeline_logger.info("=== Start the Pipeline ===")
pipeline_logger.info("=====================================================")
pipeline_logger.info("")
# Step 1: 捕获图片
pipeline_logger.info("=====================================================")
pipeline_logger.info("=== Step1: Capture Image ===")
pipeline_logger.info("=====================================================")
pipeline_logger.info("")
image_client.capture_single_image(IMAGE_FILENAME) # 试卷实体 -> IMAGE
# Step 2: OCR生成文本
pipeline_logger.info("=====================================================")
pipeline_logger.info("=== Step2: OCR Image ===")
pipeline_logger.info("=====================================================")
pipeline_logger.info("")
qwen_client.ocr_image(IMAGE_FILENAME, OCR_FILENAME) # IMAGE -> OCR_TXT
# Step 3: 文本分割
# pipeline_logger.info("=====================================================")
# pipeline_logger.info("=== Step3: Text Split ===")
# pipeline_logger.info("=====================================================")
# pipeline_logger.info("")
# full_text = read_txt_file(OCR_FILENAME)
# print(qwen_client.text_split(full_text))
# Step 3: AI生成答案
pipeline_logger.info("=====================================================")
pipeline_logger.info("=== Step3: Answer Generation ===")
pipeline_logger.info("=====================================================")
pipeline_logger.info("")
# deepseek_client.answer_reasoning_question(OCR_FILENAME, ANSWER_FILENAME) # OCR_TXT -> ANSWER_TXT
deepseek_client.answer_translation_question(OCR_FILENAME, ANSWER_FILENAME)
# deepseek_client.answer_english_question(OCR_FILENAME, ANSWER_FILENAME)
# deepseek_client.answer_math_question(OCR_FILENAME, ANSWER_FILENAME)
# Step 4: 位置映射
pipeline_logger.info("=====================================================")
pipeline_logger.info("=== Step4: Position Mapping ===")
pipeline_logger.info("=====================================================")
pipeline_logger.info("")
answer = read_txt_file(ANSWER_FILENAME)
img, img_w, img_h, mm_per_pixel_x, mm_per_pixel_y, px_per_mm_y = image_client.load_image_and_get_scale(IMAGE_FILENAME)
box = image_client.detect_single_black_box(img, BOX_VIZ_IMAGE_FILENAME)
image_client.generate_writing_task(img, box, answer, mm_per_pixel_x, mm_per_pixel_y,
px_per_mm_y, PREVIEW_IMAGE_FILENAME, TASK_FILENAME) # ANSWER_TXT -> TASK_JSON
# Step 5: 机械臂书写
pipeline_logger.info("=====================================================")
pipeline_logger.info("=== Step5: Robot Writing ===")
pipeline_logger.info("=====================================================")
pipeline_logger.info("")
robot_writer.go_center()
tasks = robot_writer.load_writing_tasks(TASK_FILENAME)
for task in tasks:
robot_writer.write_text_line(
task.get("text"),
task.get("a4_x_mm"),
task.get("a4_y_mm"),
task.get("char_height_mm"),
task.get("char_spacing_ratio")
)
pipeline_logger.info("=====================================================")
pipeline_logger.info("=== Pipeline Finished ===")
pipeline_logger.info("=====================================================")
robot_writer.stand_by()
if __name__ == "__main__":
main()