mirror of
https://github.com/NanjingForestryUniversity/supermachine-tobacco.git
synced 2025-11-08 14:23:55 +00:00
022.7.27 complete version
This commit is contained in:
parent
ea7243da29
commit
3d720d5c2b
8
config.py
Normal file → Executable file
8
config.py
Normal file → Executable file
@ -4,8 +4,8 @@ import numpy as np
|
|||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
# 文件相关参数
|
# 文件相关参数
|
||||||
nRows, nCols, nBands, spec_size_threshold = 256, 1024, 22, 3
|
nRows, nCols, nBands = 256, 1024, 22
|
||||||
nRgbRows, nRgbCols, nRgbBands, rgb_size_threshold = 1024, 4096, 3, 4
|
nRgbRows, nRgbCols, nRgbBands = 1024, 4096, 3
|
||||||
|
|
||||||
# 需要设置的谱段等参数
|
# 需要设置的谱段等参数
|
||||||
selected_bands = [127, 201, 202, 294]
|
selected_bands = [127, 201, 202, 294]
|
||||||
@ -21,9 +21,11 @@ class Config:
|
|||||||
blk_size = 4
|
blk_size = 4
|
||||||
pixel_model_path = r"./models/dt.p"
|
pixel_model_path = r"./models/dt.p"
|
||||||
blk_model_path = r"./models/rf_4x4_c22_20_sen8_8.model"
|
blk_model_path = r"./models/rf_4x4_c22_20_sen8_8.model"
|
||||||
|
spec_size_threshold = 3
|
||||||
|
|
||||||
# rgb模型参数
|
# rgb模型参数
|
||||||
rgb_tobacco_model_path = r"models/tobacco_dt_2022-07-26_15-57.model"
|
rgb_tobacco_model_path = r"models/tobacco_dt_2022-07-26_15-57.model"
|
||||||
rgb_background_model_path = r"models/background_dt_2022-07-27_08-11.model"
|
rgb_background_model_path = r"models/background_dt_2022-07-27_08-11.model"
|
||||||
threshold_low, threshold_high = 5, 255
|
threshold_low, threshold_high = 10, 230
|
||||||
threshold_s = 175
|
threshold_s = 175
|
||||||
|
rgb_size_threshold = 4
|
||||||
|
|||||||
36
main.py
Normal file → Executable file
36
main.py
Normal file → Executable file
@ -8,6 +8,9 @@ from models import RgbDetector, SpecDetector, ManualTree, AnonymousColorDetector
|
|||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
|
|
||||||
|
SAVE_IMG, SAVE_NUM = False, 30
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
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,
|
||||||
@ -20,39 +23,52 @@ def main():
|
|||||||
os.mkfifo(mask_fifo_path, 0o777)
|
os.mkfifo(mask_fifo_path, 0o777)
|
||||||
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 SAVE_IMG:
|
||||||
|
img_list = []
|
||||||
while True:
|
while True:
|
||||||
fd_img = os.open(img_fifo_path, os.O_RDONLY)
|
fd_img = os.open(img_fifo_path, os.O_RDONLY)
|
||||||
fd_rgb = os.open(rgb_fifo_path, os.O_RDONLY)
|
fd_rgb = os.open(rgb_fifo_path, os.O_RDONLY)
|
||||||
|
|
||||||
|
# spec data read
|
||||||
data = os.read(fd_img, total_len)
|
data = os.read(fd_img, total_len)
|
||||||
# 读取(开启一个管道)
|
|
||||||
if len(data) < 3:
|
if len(data) < 3:
|
||||||
threshold = int(float(data))
|
threshold = int(float(data))
|
||||||
Config.spec_size_threshold = threshold
|
Config.spec_size_threshold = threshold
|
||||||
print("[INFO] Get threshold: ", threshold)
|
print("[INFO] Get spec threshold: ", threshold)
|
||||||
continue
|
|
||||||
else:
|
else:
|
||||||
data_total = data
|
data_total = data
|
||||||
|
os.close(fd_img)
|
||||||
|
|
||||||
|
# rgb data read
|
||||||
rgb_data = os.read(fd_rgb, total_rgb)
|
rgb_data = os.read(fd_rgb, total_rgb)
|
||||||
if len(rgb_data) < 3:
|
if len(rgb_data) < 3:
|
||||||
rgb_threshold = int(float(rgb_data))
|
rgb_threshold = int(float(rgb_data))
|
||||||
Config.rgb_size_threshold = rgb_threshold
|
Config.rgb_size_threshold = rgb_threshold
|
||||||
print(rgb_threshold)
|
print("[INFO] Get rgb threshold", rgb_threshold)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
rgb_data_total = rgb_data
|
rgb_data_total = rgb_data
|
||||||
os.close(fd_img)
|
|
||||||
os.close(fd_rgb)
|
os.close(fd_rgb)
|
||||||
|
|
||||||
# 识别
|
# 识别
|
||||||
t1 = time.time()
|
t1 = time.time()
|
||||||
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)
|
||||||
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))
|
||||||
|
if SAVE_IMG:
|
||||||
|
SAVE_NUM -= 1
|
||||||
|
img_list.append((rgb_data, img_data))
|
||||||
|
if SAVE_NUM <= 0:
|
||||||
|
break
|
||||||
# 光谱识别
|
# 光谱识别
|
||||||
mask = spec_detector.predict(img_data)
|
mask = spec_detector.predict(img_data)
|
||||||
# rgb识别
|
# rgb识别
|
||||||
mask_rgb = rgb_detector.predict(rgb_data)
|
mask_rgb = rgb_detector.predict(rgb_data)
|
||||||
|
|
||||||
# 结果合并
|
# 结果合并
|
||||||
mask_result = (mask | mask_rgb).astype(np.uint8)
|
mask_result = (mask | mask_rgb).astype(np.uint8)
|
||||||
|
|
||||||
|
# mask_result = mask_rgb.astype(np.uint8)
|
||||||
mask_result = mask_result.repeat(Config.blk_size, axis=0).repeat(Config.blk_size, axis=1).astype(np.uint8)
|
mask_result = mask_result.repeat(Config.blk_size, axis=0).repeat(Config.blk_size, axis=1).astype(np.uint8)
|
||||||
t2 = time.time()
|
t2 = time.time()
|
||||||
print(f'rgb len = {len(rgb_data)}')
|
print(f'rgb len = {len(rgb_data)}')
|
||||||
@ -63,6 +79,12 @@ def main():
|
|||||||
os.close(fd_mask)
|
os.close(fd_mask)
|
||||||
t3 = time.time()
|
t3 = time.time()
|
||||||
print(f'total time is:{t3 - t1}')
|
print(f'total time is:{t3 - t1}')
|
||||||
|
for i, img in enumerate(img_list):
|
||||||
|
print(f"writing img {i}...")
|
||||||
|
cv2.imwrite(f"./{i}.png", img[0][..., ::-1])
|
||||||
|
np.save(f'./{i}.npy', img[1])
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def save_main():
|
def save_main():
|
||||||
@ -131,8 +153,8 @@ def save_main():
|
|||||||
.sum(axis=1)
|
.sum(axis=1)
|
||||||
mask[mask <= threshold] = 0
|
mask[mask <= threshold] = 0
|
||||||
mask[mask > threshold] = 1
|
mask[mask > threshold] = 1
|
||||||
# mask_result = (mask | mask_rgb).astype(np.uint8)
|
mask_result = (mask | mask_rgb).astype(np.uint8)
|
||||||
mask_result = mask_rgb
|
# mask_result = mask_rgb
|
||||||
mask_result = mask_result.repeat(Config.blk_size, axis=0).repeat(Config.blk_size, axis=1).astype(np.uint8)
|
mask_result = mask_result.repeat(Config.blk_size, axis=0).repeat(Config.blk_size, axis=1).astype(np.uint8)
|
||||||
t2 = time.time()
|
t2 = time.time()
|
||||||
print(f'rgb len = {len(rgb_data)}')
|
print(f'rgb len = {len(rgb_data)}')
|
||||||
|
|||||||
4
models.py
Normal file → Executable file
4
models.py
Normal file → Executable file
@ -17,7 +17,7 @@ from sklearn.model_selection import train_test_split
|
|||||||
from config import Config
|
from config import Config
|
||||||
from utils import lab_scatter, read_labeled_img, size_threshold
|
from utils import lab_scatter, read_labeled_img, size_threshold
|
||||||
|
|
||||||
deploy = False
|
deploy = True
|
||||||
if not deploy:
|
if not deploy:
|
||||||
print("Training env")
|
print("Training env")
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
@ -415,7 +415,7 @@ class SpecDetector(Detector):
|
|||||||
# 烟梗mask中将背景赋值为0,将烟梗赋值为2
|
# 烟梗mask中将背景赋值为0,将烟梗赋值为2
|
||||||
yellow_things[yellow_things] = tobacco
|
yellow_things[yellow_things] = tobacco
|
||||||
yellow_things = yellow_things + 0
|
yellow_things = yellow_things + 0
|
||||||
yellow_things = binary_dilation(yellow_things, iterations=iteration)
|
# yellow_things = binary_dilation(yellow_things, iterations=iteration)
|
||||||
yellow_things = yellow_things + 0
|
yellow_things = yellow_things + 0
|
||||||
yellow_things[yellow_things == 1] = 2
|
yellow_things[yellow_things == 1] = 2
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user