databases01.py
2.81 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
# 导入pymysql模块
import os
import random
import time
import pymysql
import requests
# 连接database
conn = pymysql.connect(
host="192.168.5.1",
port=3307,
user="user_yangll",
password="vGxw9jWg",
database="glsx_car_service",
charset="utf8",
)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的SQL语句
SQL = """
select sof.*,so.vin_no from sys_order_file sof LEFT JOIN sys_order so ON so.id = sof.order_id
WHERE so.order_source = 'pazl' AND sof.file_name = '三码合一';
"""
# 执行SQL语句
cursor.execute(SQL)
# 获取查询结果
result = cursor.fetchall()
def generate_random_filename():
timestamp = int(time.time()) # 获取当前时间戳
random_number = random.randint(1000, 9999) # 生成一个随机数
filename = f"file_{timestamp}_{random_number}" # 创建文件名
return filename
def get_extension(filename):
parts = filename.split(".")
extension = parts[-1]
return extension
def get_project_root():
# 获取当前脚本文件的路径
current_file = os.path.abspath(__file__)
# 获取当前项目的根目录
project_root = os.path.dirname(os.path.dirname(current_file))
print(project_root)
return project_root
# 处理查询结果
# 生成训练数据集
for index, row in enumerate(result[0:5000]):
print(row)
filename = generate_random_filename()
network_img_path = row[4]
print("network_img_path", network_img_path)
extension = get_extension(network_img_path)
complete_filename = f"{filename}.{extension}"
try:
response = requests.get(network_img_path)
except Exception as e:
response = requests.get(network_img_path)
path = os.path.join(
get_project_root(), "Identify_vinandsn", "train", "threecode", complete_filename
)
with open(path, "wb") as file:
file.write(response.content)
# urllib.request.urlretrieve(network_img_path, path)
time.sleep(0.001)
print("File downloaded successfully.")
# 生成测试数据集
for index, row in enumerate(result[-5000:]):
print(row)
filename = generate_random_filename()
network_img_path = row[4]
print("network_img_path", network_img_path)
extension = get_extension(network_img_path)
complete_filename = f"{filename}.{extension}"
try:
response = requests.get(network_img_path)
except Exception as e:
response = requests.get(network_img_path)
path = os.path.join(
get_project_root(), "Identify_vinandsn", "validation", "threecode", complete_filename
)
with open(path, "wb") as file:
file.write(response.content)
# urllib.request.urlretrieve(network_img_path, path)
time.sleep(0.001)
print("File downloaded successfully.")
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()