mirror of
https://github.com/NanjingForestryUniversity/supermachine-tobacco.git
synced 2025-11-08 22:33:54 +00:00
添加多线程接收类
This commit is contained in:
parent
4b56560cc8
commit
f415a480a6
@ -90,6 +90,7 @@
|
||||

|
||||
|
||||
在新的镜头下,数据的分布和原本的数据已经发生了巨大的变化,根本就不是同一种东西,这就是为什么分类结果会出错了。
|
||||
这种情况的出现也和拍摄矫正不到位有关,具体细节询问周超。
|
||||
|
||||
## 预测过程的后处理(异色问题)
|
||||
|
||||
|
||||
111
transmit.py
Normal file
111
transmit.py
Normal file
@ -0,0 +1,111 @@
|
||||
import os
|
||||
import threading
|
||||
from queue import Queue
|
||||
import numpy as np
|
||||
from config import Config
|
||||
|
||||
|
||||
class Receiver(object):
|
||||
def __init__(self):
|
||||
self.output = None
|
||||
|
||||
def set_source(self, *args, **kwargs):
|
||||
"""
|
||||
用于设置数据的来源,每个receiver仅允许有单个来源
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def set_output(self, output: Queue):
|
||||
"""
|
||||
设置单个输出源
|
||||
:param output:
|
||||
:return:
|
||||
"""
|
||||
self.output = output
|
||||
|
||||
def start(self, *args, **kwargs):
|
||||
"""
|
||||
启动接收线程或进程
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def stop(self, *args, **kwargs):
|
||||
"""
|
||||
停止接收线程或进程
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class FifoReceiver(Receiver):
|
||||
def __init__(self, fifo_path: str, output: Queue, read_max_num: int):
|
||||
super().__init__()
|
||||
self._input_fifo_path = None
|
||||
self._output_queue = None
|
||||
self._max_len = read_max_num
|
||||
|
||||
self.set_source(fifo_path)
|
||||
self.set_output(output)
|
||||
self._need_stop = threading.Event()
|
||||
self._need_stop.clear()
|
||||
|
||||
def set_source(self, fifo_path: str):
|
||||
if not os.access(fifo_path, os.F_OK):
|
||||
os.mkfifo(fifo_path, 0o777)
|
||||
self._input_fifo_path = fifo_path
|
||||
|
||||
def set_output(self, output: Queue):
|
||||
self._output_queue = output
|
||||
|
||||
def start(self, post_process_func=None, name='fifo_receiver'):
|
||||
x = threading.Thread(target=self._receive_thread_func,
|
||||
name=name, args=(post_process_func, ))
|
||||
x.start()
|
||||
|
||||
def stop(self):
|
||||
self._need_stop.set()
|
||||
|
||||
def _receive_thread_func(self, post_process_func=None):
|
||||
"""
|
||||
接收线程
|
||||
|
||||
:param post_process_func:
|
||||
:return:
|
||||
"""
|
||||
while not self._need_stop.is_set():
|
||||
input_fifo = os.open(self._input_fifo_path, os.O_RDONLY)
|
||||
data = os.read(input_fifo, self._max_len)
|
||||
if post_process_func is not None:
|
||||
data = post_process_func(data)
|
||||
self._output_queue.put(data)
|
||||
os.close(input_fifo)
|
||||
self._need_stop.clear()
|
||||
|
||||
@staticmethod
|
||||
def spec_data_post_process(data):
|
||||
if len(data) < 3:
|
||||
threshold = int(float(data))
|
||||
print("[INFO] Get Spec threshold: ", threshold)
|
||||
return threshold
|
||||
else:
|
||||
spec_img = np.frombuffer(data, dtype=np.float32).\
|
||||
reshape((Config.nRows, Config.nBands, -1)).transpose(0, 2, 1)
|
||||
return spec_img
|
||||
|
||||
@staticmethod
|
||||
def rgb_data_post_process(data):
|
||||
if len(data) < 3:
|
||||
threshold = int(float(data))
|
||||
print("[INFO] Get RGB threshold: ", threshold)
|
||||
return threshold
|
||||
else:
|
||||
rgb_img = np.frombuffer(data, dtype=np.uint8).reshape((Config.nRgbRows, Config.nRgbCols, -1))
|
||||
return rgb_img
|
||||
Loading…
Reference in New Issue
Block a user