图像识别是人工智能的一个重要领域,是指利用计算机视觉技术对图像进行处理、分析和理解,以达到识别图像中的对象、场景或特征的过程。
基于图像识别的概念,依据对于图像处理的方式不同延伸出很多其他的子任务,如下所示:
- 目标定位(Object Localization)
- 图像检测(Object Detection)
- 图像分类(Image Classification)
- 零样本目标检测(Zero-shot image classification)
图像识别的应用场景
- 安防监控:可以用于监控摄像头捕捉到的视频流,识别出人脸、车辆、行人等。
- 智能交通:可以应用于智能交通系统中,监测交通流量、识别车牌、监测违规行为等。
- 智能家居:在智能家居领域,图像识别可以用于人体检测、姿势识别、物体识别等。
- 自动驾驶:图像识别也是自动驾驶技术的关键技术之一,通过识别道路标志、行人、车辆等,帮助汽车实现自主导航和安全驾驶。
实战案例
本章使用 transformers==4.39.3,pillow==10.3.0,可参考版本进行安装。
实战将采用 HuggingFace 提供的 transformers 库去调用预训练模型,Transformer 库提供了易于使用的 API,可以轻松地加载、使用和微调预训练模型。预测结果将使用 pillow 库绘画出图像识别识别的结果。
HuggingFace 的 transformers 库提供了方便的接口来加载、运行和微调各种预训练的 transformers 模型,安装方式如下:
- pip install transformers
- pip install pillow
pipeline() 是使用预训练模型进行推理的最简便、高效的方式,适用于多种任务跨模型的应用场景。利用 pipeline(),可轻松调用不同模型。代码示例如下所示:
def test_pipeline(): # 情感分析 classifier = pipeline(task="sentiment-analysis") # 给出三个文本内容 results = classifier( ["It's a great ride around the island", "The weather is bad", "He sent an email"]) # 打印出结果 for i in results: print(f"label: {i['label']}, with score: {round(i['score'], 4)}")目标检测
目标检测是在图像中确定并标记出物体位置和类别的任务,通常以边界框的形式呈现,通常是在已知的类别中检测目标。
具体实现是模型会接收带有标签的数据,模型会返回图像中检测到的目标物体的位置和类别信息。
以下是代码示例:
def test_object_detector(): # 打开一张图片 with open("./breakfast.png", mode='rb') as f: image = Image.open(f) # 初始化一个 pipeline 对象 object_detector = pipeline(task='object-detection', revision='main') # 传入图像 predictions = object_detector(image) # 将预测结果输出 print(json.dumps(predictions, indent=2, ensure_ascii=False)) # 使用 ImageDraw 创建一个可在图像上绘制形状和文本的对象 draw = ImageDraw.Draw(image) # 遍历检测到每个对象的预测结果 从预测结果去除边界框、标签和置信度得分信息 for prediction in predictions: box = prediction["box"] label = prediction["label"] score = prediction["score"] xmin, ymin, xmax, ymax = box.values() # 使用绘图对象,在图像上绘制由边界框定义的矩形框,用红色描边,线宽为1。 draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1) # 在边界框的左上角绘制文本,显示对象标签和置信度得分,文本颜色为白色,字号为16。 # Win 使用Arial字体 :ImageFont.truetype('arial.ttf') # Mac可用 PingFang:ImageFont.truetype('PingFang') draw.text((xmin, ymin), f"{label}:{round(score, 2)}", fill="white", font=ImageFont.truetype('arial.ttf', size=16)) # 展示绘制结果 image.show() # 保存图片 image.save('./breakfast_object.png')零样本目标检测
零样本目标检测是利用模型再训练阶段学到的通用特征,在没有事先见过的类别的情况下,在图像中检测和识别目标物体。
具体实现是将图像传递给零样本目标检测模型,并且给出一组候选标签(candidate_labels),模型会尝试识别图像中是否存在标签对应的物体。
以下是代码示例:
def test_object_detect(): with open('breakfast.png', mode='rb') as f: image = Image.open(f) # 初始化一个 pipeline 对象 object_detector = pipeline( task="zero-shot-object-detection", revision='main' ) # 传入图像,并且给出标签 predictions = object_detector(image, candidate_labels=['milk']) print(json.dumps(predictions, indent=2, ensure_ascii=False)) # 可视化预测 draw = ImageDraw.Draw(image) # 遍历检测到的每个对象的预测结果。 for prediction in predictions: # 从每个预测结果中获取边界框(box)、标签(label)和置信度得分(score)等信息。 box = prediction["box"] label = prediction["label"] score = prediction["score"] xmin, ymin, xmax, ymax = box.values() # 使用绘图对象,在图像上绘制由边界框定义的矩形框,用红色描边,线宽为1。 draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1) # 在边界框的左上角绘制文本,显示对象标签和置信度得分,文本颜色为白色,字号为16。 # Win 使用Arial字体 :ImageFont.truetype('arial.ttf') # Mac可用 PingFang:ImageFont.truetype('PingFang') draw.text((xmin, ymin), f"{label}: {round(score, 2)}", fill="white", font=ImageFont.truetype('arial.ttf', size=16)) image.show() image.save('breakfast_zero_milk.png')总结
- 了解图像识别应用场景。
- 了解图像检测用法。
- 了解零样本识别用法。
|