supermachine--tomato-passio.../20240627test4/to_leaf_test.py
TG 242bb9a71b feat:新增20240627test4为现场部署版本的测试版(包含传统方法、实例分割、目标检测、图像分类多个模型)
fix:修复在20240627test4中的classifier.py的analyze_tomato函数中white_defect的函数忘记传递两个阈值量的错误;修复analyze_tomato函数中的叶片实例分割存在的问题,解决由于变量污染引起的分割错误;
2024-07-21 22:17:46 +08:00

74 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
# @Time : 2024/7/7 下午4:33
# @Author : TG
# @File : totest.py
# @Software: PyCharm
import time
import cv2
import numpy as np
import os
import pandas as pd
from PIL import Image
import re
from to_seg import TOSEG
def tryint(s):
try:
return int(s)
except ValueError:
return s
def natural_sort_key(s):
return [tryint(c) for c in re.split('([0-9]+)', s)]
def main():
image_dir = r'D:\project\20240714Actual_deployed\testimg'
to = TOSEG() # 假设 TOSEG 是已定义好的类,可以处理图片分割
# 获取所有.bmp文件并进行自然排序
rgb_files = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith('.bmp')]
rgb_files.sort(key=natural_sort_key)
# 准备保存到 Excel 的数据
records = []
for idx, image_path in enumerate(rgb_files):
img = cv2.imread(image_path, cv2.IMREAD_COLOR)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
t = time.time()
result = to.toseg(img) # 假设 toseg 方法接受一个图片数组,并返回处理后的图片
e = time.time()
result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
process_time = e - t
print(f'{idx + 1}张图时间:{process_time}')
# 获取原始文件名并添加“mask”后缀
original_filename = os.path.splitext(os.path.basename(image_path))[0]
output_filename = f'{original_filename}_leaf.png'
cv2.imwrite(os.path.join(r'D:\project\20240714Actual_deployed\leaf',
output_filename), result)
# 添加记录到列表
records.append({
"Image Index": idx + 1,
"File Name": original_filename,
"Processing Time (s)": process_time
})
# cv2.imshow('result', result)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# 创建 DataFrame 并写入 Excel 文件
df = pd.DataFrame(records)
df.to_excel(r'D:\project\20240714Actual_deployed\leaf\leaf_processing_times.xlsx', index=False)
if __name__ == '__main__':
main()