当前位置:实例文章 » 其他实例» [文章]230726作业

230726作业

发布人:shili8 发布时间:2025-03-14 13:34 阅读次数:0

**230726作业**

**题目描述**

本次作业要求设计并实现一个基于Python的图像处理系统,能够自动识别和分类不同类型的图像。系统将使用深度学习算法来完成图像分类任务。

**系统需求**

1. **图像输入**: 系统需要能够接收用户上传的图像文件(支持jpg、png等格式)。
2. **图像预处理**: 系统需要对接收到的图像进行预处理,包括尺寸调整和数据标准化。
3. **图像分类**: 系统需要使用深度学习算法来对图像进行分类,识别出图像的类型(例如动物、植物、建筑等)。
4. **结果输出**: 系统需要将分类结果输出给用户,包括图像分类结果和相应的准确率。

**系统设计**

###1. 图像输入模块

import osfrom PIL import Imageclass ImageInput:
 def __init__(self):
 self.image_path = None def upload_image(self, image_path):
 """
上传图像文件 :param image_path: 图像文件路径 :return: None """
 if not os.path.exists(image_path):
 print("Error: Image file does not exist.")
 return False self.image_path = image_path return True def get_image(self):
 """
 获取图像对象 :return: Pillow Image对象 """
 return Image.open(self.image_path)


###2. 图像预处理模块
import numpy as npfrom PIL import Imageclass ImagePreprocess:
 def __init__(self, image):
 self.image = image def resize_image(self, size):
 """
 调整图像尺寸 :param size: 尺寸(宽度x高度)
 :return: None """
 self.image.thumbnail(size)

 def normalize_image(self):
 """
 数据标准化 :return: None """
 image_array = np.array(self.image)
 normalized_array = (image_array - np.min(image_array)) / (np.max(image_array) - np.min(image_array))
 return normalized_array


###3. 图像分类模块
import torchfrom torchvision import models, transformsclass ImageClassifier:
 def __init__(self):
 self.model = None def load_model(self, model_name):
 """
 加载模型 :param model_name: 模型名称(例如resnet18)
 :return: None """
 if model_name == "resnet18":
 self.model = models.resnet18(pretrained=True)
 elif model_name == "vgg16":
 self.model = models.vgg16(pretrained=True)

 def classify_image(self, image_array):
 """
 对图像进行分类 :param image_array: 图像数组 :return: 分类结果(类别名称和准确率)
 """
 # 将图像数组转换为PyTorch tensor input_tensor = torch.tensor(image_array)
 input_tensor = input_tensor.unsqueeze(0)

 # 运行模型预测 output = self.model(input_tensor)

 # 获取分类结果 _, predicted = torch.max(output,1)

 # 计算准确率 accuracy = (predicted ==0).float().mean()

 return predicted.item(), accuracy.item()


###4. 结果输出模块
class ResultOutput:
 def __init__(self):
 pass def output_result(self, classification_result):
 """
 输出分类结果 :param classification_result: 分类结果(类别名称和准确率)
 :return: None """
 print("Classification Result:")
 print(f"Class Name: {classification_result[0]}")
 print(f"Accuracy: {classification_result[1]:.2f}")


**测试代码**

if __name__ == "__main__":
 #上传图像文件 image_input = ImageInput()
 image_input.upload_image("test_image.jpg")

 # 预处理图像 image_preprocess = ImagePreprocess(image_input.get_image())
 image_preprocess.resize_image((224,224))
 normalized_array = image_preprocess.normalize_image()

 # 分类图像 image_classifier = ImageClassifier()
 image_classifier.load_model("resnet18")
 classification_result = image_classifier.classify_image(normalized_array)

 # 输出分类结果 result_output = ResultOutput()
 result_output.output_result(classification_result)


**注释**

* 本系统设计使用Python语言,并且基于Pillow和PyTorch库。
* 系统分为四个模块:图像输入、预处理、分类和输出。
* 每个模块都有相应的类和方法,用于完成特定的任务。
* 测试代码示例了如何使用系统进行图像分类。

**参考**

* Pillow库文档:< />* PyTorch库文档:< />* ResNet18模型论文:<

相关标签:
其他信息

其他资源

Top