Merge remote-tracking branch 'originn/master'

This commit is contained in:
FEIJINTI 2022-08-23 23:15:46 +08:00
commit 75ee16a9ad
2 changed files with 32 additions and 15 deletions

View File

@ -38,7 +38,7 @@ class Config:
valve_merge_size = 2 # 每两个喷阀当中有任意一个出现杂质则认为都是杂质 valve_merge_size = 2 # 每两个喷阀当中有任意一个出现杂质则认为都是杂质
valve_horizontal_padding = 3 # 喷阀横向膨胀的尺寸,应该是奇数,3时表示左右各膨胀1 valve_horizontal_padding = 3 # 喷阀横向膨胀的尺寸,应该是奇数,3时表示左右各膨胀1
max_open_valve_limit = 25 # 最大同时开启喷阀限制,按照电流计算,当前的喷阀可以开启的喷阀 600W的电源 / 12V电源 = 50A, 一个阀门1A max_open_valve_limit = 25 # 最大同时开启喷阀限制,按照电流计算,当前的喷阀可以开启的喷阀 600W的电源 / 12V电源 = 50A, 一个阀门1A
max_time_spent = 200
# save part # save part
offset_vertical = 0 offset_vertical = 0

45
main.py
View File

@ -15,57 +15,65 @@ def main(only_spec=False, only_color=False, if_merge=False, interval_time=None,
single_spec=False, single_color=False): single_spec=False, single_color=False):
spec_detector = SpecDetector(blk_model_path=Config.blk_model_path, pixel_model_path=Config.pixel_model_path) spec_detector = SpecDetector(blk_model_path=Config.blk_model_path, pixel_model_path=Config.pixel_model_path)
rgb_detector = RgbDetector(tobacco_model_path=Config.rgb_tobacco_model_path, rgb_detector = RgbDetector(tobacco_model_path=Config.rgb_tobacco_model_path,
background_model_path=Config.rgb_background_model_path) background_model_path=Config.rgb_background_model_path,
ai_path=Config.ai_path)
_, _ = spec_detector.predict(np.ones((Config.nRows, Config.nCols, Config.nBands), dtype=float)*0.4),\ _, _ = spec_detector.predict(np.ones((Config.nRows, Config.nCols, Config.nBands), dtype=float)*0.4),\
rgb_detector.predict(np.ones((Config.nRgbRows, Config.nRgbCols, Config.nRgbBands), dtype=np.uint8)*40) rgb_detector.predict(np.ones((Config.nRgbRows, Config.nRgbCols, Config.nRgbBands), dtype=np.uint8)*40)
total_len = Config.nRows * Config.nCols * Config.nBands * 4 # float型变量, 4个字节 total_len = Config.nRows * Config.nCols * Config.nBands * 4 # float型变量, 4个字节
total_rgb = Config.nRgbRows * Config.nRgbCols * Config.nRgbBands * 1 # int型变量 total_rgb = Config.nRgbRows * Config.nRgbCols * Config.nRgbBands * 1 # int型变量
if not single_color: if not single_color:
logging.info("create color fifo")
if not os.access(img_fifo_path, os.F_OK): if not os.access(img_fifo_path, os.F_OK):
os.mkfifo(img_fifo_path, 0o777) os.mkfifo(img_fifo_path, 0o777)
if not os.access(mask_fifo_path, os.F_OK): if not os.access(mask_fifo_path, os.F_OK):
os.mkfifo(mask_fifo_path, 0o777) os.mkfifo(mask_fifo_path, 0o777)
if not single_spec: if not single_spec:
logging.info("create rgb fifo")
if not os.access(rgb_fifo_path, os.F_OK): if not os.access(rgb_fifo_path, os.F_OK):
os.mkfifo(rgb_fifo_path, 0o777) os.mkfifo(rgb_fifo_path, 0o777)
if not os.access(rgb_mask_fifo_path, os.F_OK): if not os.access(rgb_mask_fifo_path, os.F_OK):
os.mkfifo(rgb_mask_fifo_path, 0o777) os.mkfifo(rgb_mask_fifo_path, 0o777)
logging.info(f"请注意!正在以调试模式运行程序,输出的信息可能较多。") logging.info(f"请注意!正在以调试模式运行程序,输出的信息可能较多。")
# specially designed for Miaow.
if (interval_time is not None) and (delay_repeat_time is not None): if (interval_time is not None) and (delay_repeat_time is not None):
interval_time = float(interval_time) / 1000.0 interval_time = float(interval_time) / 1000.0
delay_repeat_time = int(delay_repeat_time) delay_repeat_time = int(delay_repeat_time)
logging.warning(f'Delay {interval_time*1000:.2f}ms will be added per {delay_repeat_time} frames') logging.warning(f'Delay {interval_time*1000:.2f}ms will be added per {delay_repeat_time} frames')
delay_repeat_time_count = 0 delay_repeat_time_count = 0
while True: while True:
if not single_color: img_data, rgb_data = None, None
if single_spec:
fd_img = os.open(img_fifo_path, os.O_RDONLY) fd_img = os.open(img_fifo_path, os.O_RDONLY)
# spec data read # spec data read
data = os.read(fd_img, total_len) data_total = os.read(fd_img, total_len)
if len(data) < 3: if len(data_total) < 3:
try: try:
threshold = int(float(data)) threshold = int(float(data_total))
Config.spec_size_threshold = threshold Config.spec_size_threshold = threshold
logging.info(f'[INFO] Get spec threshold: {threshold}') logging.info(f'[INFO] Get spec threshold: {threshold}')
except Exception as e: except Exception as e:
logging.error( logging.error(
f'毁灭性错误:收到长度小于3却无法转化为整数spec_size_threshold的网络报文报文内容为 {data},' f'毁灭性错误:收到长度小于3却无法转化为整数spec_size_threshold的网络报文报文内容为 {data_total},'
f' 错误为 {e}.') f' 错误为 {e}.')
if single_spec:
continue
else: else:
data_total = data data_total = data_total
os.close(fd_img) os.close(fd_img)
try: try:
img_data = np.frombuffer(data_total, dtype=np.float32).reshape((Config.nRows, Config.nBands, -1)) \ img_data = np.frombuffer(data_total, dtype=np.float32).reshape((Config.nRows, Config.nBands, -1)) \
.transpose(0, 2, 1) .transpose(0, 2, 1)
print(f"get image_shape {img_data.shape}")
except Exception as e: except Exception as e:
logging.error(f'毁灭性错误!收到的光谱数据长度为{len(data_total)}无法转化成指定的形状 {e}') logging.error(f'毁灭性错误!收到的光谱数据长度为{len(data_total)}无法转化成指定的形状 {e}')
if not single_spec: if single_color:
fd_rgb = os.open(rgb_fifo_path, os.O_RDONLY) fd_rgb = os.open(rgb_fifo_path, os.O_RDONLY)
# rgb data read # rgb data read
rgb_data = os.read(fd_rgb, total_rgb) rgb_data_total = os.read(fd_rgb, total_rgb)
if len(rgb_data) < 3: if len(rgb_data_total) < 3:
try: try:
rgb_threshold = int(float(rgb_data)) rgb_threshold = int(float(rgb_data_total))
Config.rgb_size_threshold = rgb_threshold Config.rgb_size_threshold = rgb_threshold
logging.info(f'Get rgb threshold: {rgb_threshold}') logging.info(f'Get rgb threshold: {rgb_threshold}')
except Exception as e: except Exception as e:
@ -73,23 +81,29 @@ def main(only_spec=False, only_color=False, if_merge=False, interval_time=None,
f' 错误为 {e}.') f' 错误为 {e}.')
continue continue
else: else:
rgb_data_total = rgb_data rgb_data_total = rgb_data_total
os.close(fd_rgb) os.close(fd_rgb)
try: try:
rgb_data = np.frombuffer(rgb_data_total, dtype=np.uint8).reshape((Config.nRgbRows, Config.nRgbCols, -1)) rgb_data = np.frombuffer(rgb_data_total, dtype=np.uint8).reshape((Config.nRgbRows, Config.nRgbCols, -1))
print(f"get rgb_data shape {rgb_data.shape}")
except Exception as e: except Exception as e:
logging.error(f'毁灭性错误!收到的rgb数据长度为{len(rgb_data)}无法转化成指定形状 {e}') logging.error(f'毁灭性错误!收到的rgb数据长度为{len(rgb_data_total)}无法转化成指定形状 {e}')
# 识别 read # 识别 read
since = time.time() since = time.time()
# predict # predict
if single_spec or single_color: if single_spec or single_color:
print('start predict')
if single_spec: if single_spec:
print('spec predict', img_data.shape)
mask_spec = spec_detector.predict(img_data).astype(np.uint8) mask_spec = spec_detector.predict(img_data).astype(np.uint8)
masks = [mask_spec, ] masks = [mask_spec, ]
print('spectral mask shape:', masks[0].shape)
else: else:
print('rgb predict', rgb_data.shape)
mask_rgb = rgb_detector.predict(rgb_data).astype(np.uint8) mask_rgb = rgb_detector.predict(rgb_data).astype(np.uint8)
masks = [mask_rgb, ] masks = [mask_rgb, ]
print("rgb mask shape: ", masks[0].shape)
else: else:
if only_spec: if only_spec:
# 光谱识别 # 光谱识别
@ -126,8 +140,10 @@ def main(only_spec=False, only_color=False, if_merge=False, interval_time=None,
else: else:
output_fifos = [mask_fifo_path, rgb_mask_fifo_path] output_fifos = [mask_fifo_path, rgb_mask_fifo_path]
for fifo, mask in zip(output_fifos, masks): for fifo, mask in zip(output_fifos, masks):
print("open fifo")
fd_mask = os.open(fifo, os.O_WRONLY) fd_mask = os.open(fifo, os.O_WRONLY)
os.write(fd_mask, mask.tobytes()) os.write(fd_mask, mask.tobytes())
print("close fifo")
os.close(fd_mask) os.close(fd_mask)
time_spent = (time.time() - since) * 1000 time_spent = (time.time() - since) * 1000
predict_by = 'spec' if single_spec else 'rgb' if single_color else 'spec+rgb' predict_by = 'spec' if single_spec else 'rgb' if single_color else 'spec+rgb'
@ -161,4 +177,5 @@ if __name__ == '__main__':
console_handler.setLevel(logging.DEBUG if args.d else logging.WARNING) console_handler.setLevel(logging.DEBUG if args.d else logging.WARNING)
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[file_handler, console_handler], level=logging.DEBUG) handlers=[file_handler, console_handler], level=logging.DEBUG)
main(only_spec=args.os, only_color=args.oc, if_merge=args.m, interval_time=args.dt, delay_repeat_time=args.df) main(only_spec=args.os, only_color=args.oc, if_merge=args.m, interval_time=args.dt, delay_repeat_time=args.df,
single_spec=args.ss, single_color=args.sc)