mirror of
https://github.com/NanjingForestryUniversity/supermachine-wood.git
synced 2025-11-08 18:23:54 +00:00
完成了复杂通讯和简单通讯的代码(未测试),等端大佬补充qt的测试程序
This commit is contained in:
parent
e4351408b4
commit
0b291735e9
21
classifer.py
21
classifer.py
@ -508,6 +508,7 @@ class WoodClass(object):
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
x_data, y_data, img_names = self.get_train_data(data_dir, plot_2d=plot_2d)
|
x_data, y_data, img_names = self.get_train_data(data_dir, plot_2d=plot_2d)
|
||||||
|
x_data = x_data[:, [0, 1, 2]]
|
||||||
kmeans = KMeans(n_clusters=3, random_state=0).fit(x_data)
|
kmeans = KMeans(n_clusters=3, random_state=0).fit(x_data)
|
||||||
|
|
||||||
# 获取聚类后的数据
|
# 获取聚类后的数据
|
||||||
@ -541,11 +542,20 @@ class WoodClass(object):
|
|||||||
:return: 调整后的数据
|
:return: 调整后的数据
|
||||||
'''
|
'''
|
||||||
sorted_idx = sorted(range(len(img_names)), key=lambda x: int(img_names[x][3:-4]))
|
sorted_idx = sorted(range(len(img_names)), key=lambda x: int(img_names[x][3:-4]))
|
||||||
x_data = x_data[sorted_idx, [0, 1, 2]]
|
x_data = x_data[sorted_idx]
|
||||||
y_data = y_data[sorted_idx]
|
y_data = y_data[sorted_idx]
|
||||||
labels = labels[sorted_idx]
|
labels = labels[sorted_idx]
|
||||||
img_names = img_names[sorted_idx]
|
img_names = [img_names[i] for i in sorted_idx]
|
||||||
return x_data, y_data, labels, img_names
|
mapping = {0: 's', 1: 'z', 2: 'q'}
|
||||||
|
y_data = [mapping[i] for i in y_data]
|
||||||
|
labels = [mapping[i] for i in labels]
|
||||||
|
data = []
|
||||||
|
for i in range(len(img_names)):
|
||||||
|
x_tmp = np.round(x_data[i, :]).astype(np.int)
|
||||||
|
x_tmp = np.char.zfill(x_tmp.astype(np.str), 3)
|
||||||
|
x_tmp = "".join(x_tmp)
|
||||||
|
data.append(x_tmp + y_data[i] + labels[i])
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -559,9 +569,10 @@ if __name__ == '__main__':
|
|||||||
# wood.correct()
|
# wood.correct()
|
||||||
# wood.load()
|
# wood.load()
|
||||||
# fit 相应的文件夹
|
# fit 相应的文件夹
|
||||||
settings.model_path = str(ROOT_DIR / 'models' / wood.fit_pictures(data_path=data_path))
|
# settings.model_path = str(ROOT_DIR / 'models' / wood.fit_pictures(data_path=data_path))
|
||||||
|
|
||||||
wood.get_kmeans_data(data_path, plot_2d=True)
|
x_data, y_data, labels, img_names = wood.get_kmeans_data(data_path, plot_2d=False)
|
||||||
|
send_data = wood.data_adjustments(x_data, y_data, labels, img_names)
|
||||||
|
|
||||||
# 测试单张图片的预测,predict_mode=True表示导入本地的model, False为现场训练的
|
# 测试单张图片的预测,predict_mode=True表示导入本地的model, False为现场训练的
|
||||||
pic = cv2.imread(r"data/318/dark/rgb89.png")
|
pic = cv2.imread(r"data/318/dark/rgb89.png")
|
||||||
|
|||||||
@ -45,8 +45,13 @@ def process_cmd(cmd: str, data: any, connected_sock: socket.socket, detector: Wo
|
|||||||
settings.model_path = data
|
settings.model_path = data
|
||||||
detector.load(path=settings.model_path)
|
detector.load(path=settings.model_path)
|
||||||
response = simple_sock(connected_sock, cmd_type=cmd)
|
response = simple_sock(connected_sock, cmd_type=cmd)
|
||||||
elif cmd == 'DT':
|
elif cmd == 'KM':
|
||||||
pass
|
x_data, y_data, labels, img_names = detector.get_kmeans_data(data, plot_2d=False)
|
||||||
|
result = detector.data_adjustments(x_data, y_data, labels, img_names)
|
||||||
|
result = ','.join([str(x) for x in result])
|
||||||
|
response = simple_sock(connected_sock, cmd_type=cmd, result=result)
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logging.error(f'错误指令,指令为{cmd}')
|
logging.error(f'错误指令,指令为{cmd}')
|
||||||
response = False
|
response = False
|
||||||
|
|||||||
24
utils.py
24
utils.py
@ -278,7 +278,7 @@ def ack_sock(send_sock: socket.socket, cmd_type: str) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def done_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool:
|
def done_sock(send_sock: socket.socket, cmd_type: str, result) -> bool:
|
||||||
'''
|
'''
|
||||||
发送任务完成指令
|
发送任务完成指令
|
||||||
:param cmd_type:指令类型
|
:param cmd_type:指令类型
|
||||||
@ -292,8 +292,17 @@ def done_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool
|
|||||||
logging.error('结果在这种指令里很没必要')
|
logging.error('结果在这种指令里很没必要')
|
||||||
result = b'\xff'
|
result = b'\xff'
|
||||||
elif cmd_type == 'IM':
|
elif cmd_type == 'IM':
|
||||||
result = result.to_bytes(1, "big")
|
if result == 0:
|
||||||
msg = b'\xaa\x00\x00\x00\x05' + (' D' + cmd_type).upper().encode('ascii') + result + b'\xff\xff\xbb'
|
result = b'S'
|
||||||
|
elif result == 1:
|
||||||
|
result = b'Z'
|
||||||
|
elif result == 2:
|
||||||
|
result = b'Q'
|
||||||
|
elif cmd_type == 'KM':
|
||||||
|
result.encode('ascii')
|
||||||
|
length = len(result) + 4
|
||||||
|
length = length.to_bytes(4, byteorder='big')
|
||||||
|
msg = b'\xaa' +length + (' D' + cmd_type).upper().encode('ascii') + result + b'\xff\xff\xbb'
|
||||||
try:
|
try:
|
||||||
send_sock.send(msg)
|
send_sock.send(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -302,7 +311,7 @@ def done_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool:
|
def simple_sock(send_sock: socket.socket, cmd_type: str, result) -> bool:
|
||||||
'''
|
'''
|
||||||
发送任务完成指令
|
发送任务完成指令
|
||||||
:param cmd_type:指令类型
|
:param cmd_type:指令类型
|
||||||
@ -322,6 +331,12 @@ def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bo
|
|||||||
msg = b'A'
|
msg = b'A'
|
||||||
elif cmd_type == 'MD':
|
elif cmd_type == 'MD':
|
||||||
msg = b'D'
|
msg = b'D'
|
||||||
|
elif cmd_type == 'KM':
|
||||||
|
msg = b'K'
|
||||||
|
result = result.encode('ascii')
|
||||||
|
result = b',' + result
|
||||||
|
length = len(result)
|
||||||
|
msg = msg + length.to_bytes(1, 'big') + result
|
||||||
try:
|
try:
|
||||||
send_sock.send(msg)
|
send_sock.send(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -331,7 +346,6 @@ def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bo
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
log = Logger(is_to_file=True)
|
log = Logger(is_to_file=True)
|
||||||
log.log("nihao")
|
log.log("nihao")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user