【指针检测项目】yolo的输出作为fasterrcnn的输入,程序的整合

本文介绍了一种将Yolo和Faster RCNN两种目标检测算法进行整合的方法,通过使用Python和TensorFlow实现,展示了如何加载模型、预处理图像、执行检测并可视化结果。特别关注了Yolo的detect_image函数与Pillow库的交互,以及如何避免AttributeError错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整合后的代码如下

import sys
import argparse
from yolo import YOLO, detect_video
from PIL import Image
import os
import numpy as np
import six.moves.urllib as urllib
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
sys.path.append("..")
from utils import label_map_util
from utils import visualization_utils as vis_util
import cv2
from timeit import default_timer as timer
PATH_TO_CKPT = 'D:\\Faster_RCNN_\\output'+'\\frozen_inference_graph.pb'
PATH_TO_LABELS = 'D:/Faster_RCNN_/dataset/pascal_label_map.pbtxt'
NUM_CLASSES = 3
detection_graph = tf.Graph()
with detection_graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
print(category_index)
'''
def load_image_into_numpy_array(image): 
  #image = Image.merge('RGB',(image,image,image))
  img = np.array(image)
  #print(image.size)
  return img
'''

IMAGE_SIZE = (12, 8)

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        while 1:
   # for image_path in os.listdir('test_images_voc2'):
       # for image_path in os.listdir('./test_pointer'):
            image = Image.open('D:\\models-master\\models-master\\research\\object_detection2\\test_pointer1\\test.jpg' )
            r_image = YOLO().detect_image(image)
            pilimg = Image.fromarray(r_image)
            pilimg.save('D:\\models-master\\models-master\\research\\object_detection2\\result_pointer1\\result.jpg')
            print('success')
            start = timer()
            s_image = Image.open('D:\\models-master\\models-master\\research\\object_detection2\\result_pointer1\\result.jpg')
            image_np = np.array(s_image)
            #image_np = np.array(r_image)
            #image_np_expanded = np.expand_dims(image_np, axis=0)
            image_np_expanded = np.expand_dims(image_np, axis=0)
            #image233 = np.concatenate(image_np_expanded, axis=-1)

            print(s_image.size)
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            scores = detection_graph.get_tensor_by_name('detection_scores:0')
            classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')
            (boxes, scores, classes, num_detections) = sess.run(
                [boxes, scores, classes, num_detections],
                feed_dict={image_tensor: image_np_expanded})
            end = timer()
            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np, np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8)
            (r, g, b)=cv2.split(image_np)
            image_np=cv2.merge([b,g,r])

            print(end - start)
           # cv2.imwrite('./result_images_voc2'+'/'+image_path,image_np)
            cv2.imwrite('D:\\models-master\\models-master\\research\\object_detection2\\result_pointer1\\result1.jpg', image_np)

重点是这句话pilimg = Image.fromarray(r_image),必须要这句话不然会报错AttributeError: ‘numpy.ndarray’ object has no attribute 'save’原因是下面的yolo代码的最后 image1 = cv2.cvtColor(np.array(image), cv2.COLOR_GRAY2BGR)是opencv下的图片为numpy格式,而上述代码是pillow下的,所以要做一下转换。
同理 image1 = cv2.cvtColor(np.array(image), cv2.COLOR_GRAY2BGR)中的第一个参数若是image也会报错,要从pillow格式下转换为opencv的numpy格式。
yolo里的detect_image(self, image)代码如下

def detect_image(self, image):
    start = timer()
if self.model_image_size != (None, None):
    assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
    assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
    boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
else:
    new_image_size = (image.width - (image.width % 32),
                      image.height - (image.height % 32))
    boxed_image = letterbox_image(image, new_image_size)
image_data = np.array(boxed_image, dtype='float32')

#print(image_data.shape)
image_data /= 255.
image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

out_boxes, out_scores, out_classes = self.sess.run(
    [self.boxes, self.scores, self.classes],
    feed_dict={
        self.yolo_model.input: image_data,
        self.input_image_shape: [image.size[1], image.size[0]],
        K.learning_phase(): 0
    })

print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
            size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
thickness = (image.size[0] + image.size[1]) // 300

for i, c in reversed(list(enumerate(out_classes))):
    predicted_class = self.class_names[c]
    box = out_boxes[i]
    score = out_scores[i]

    label = '{} {:.2f}'.format(predicted_class, score)
    draw = ImageDraw.Draw(image)
    #print(image.size)
    label_size = draw.textsize(label, font)

    top, left, bottom, right = box
    top = max(0, np.floor(top + 0.5).astype('int32'))
    left = max(0, np.floor(left + 0.5).astype('int32'))
    bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
    right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
    print(label, (left, top), (right, bottom))

    if top - label_size[1] >= 0:
        text_origin = np.array([left, top - label_size[1]])
    else:
        text_origin = np.array([left, top + 1])

    # My kingdom for a good redistributable image drawing library.
    for i in range(thickness):
        draw.rectangle(
            [left + i, top + i, right - i, bottom - i],
            outline=255) #outline=self.colors[c]
    draw.rectangle(
        [tuple(text_origin), tuple(text_origin + label_size)],
        fill=255) #fill=self.colors[c]
    draw.text(text_origin, label, fill=0, font=font)# fill=255
    del draw
#img = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE)
#img = cv2.imread(image)
#cropped = img[bottom:top, left:right]
image = image.crop((left, top, right, bottom))
image1 = cv2.cvtColor(np.array(image), cv2.COLOR_GRAY2BGR)
#print(len(image.split(image)))
end = timer()
print(end - start)
return image1
#return cropped
### YOLO 和 Faster R-CNN 对比 #### 性能准确性 YOLO (You Only Look Once) 是一种单阶段检测器,能够一次性完成边界框预测和类别分类的任务。这种设计使得 YOLO 的速度非常快,在实时应用中有显著优势[^1]。 相比之下,Faster R-CNN 属于两阶段的目标检测框架。它先通过区域提议网络(Region Proposal Network, RPN)生成候选区域,再对这些区域进行精细化处理并最终确定目标位置与类别。这种方法虽然增加了计算复杂度,但在定位精度上通常优于 YOLO[^2]。 #### 速度对比 由于 YOLO 将整个过程简化为单一神经网络结构,因此其推理速度快得多,适合用于需要快速响应的应用场景中。而 Faster R-CNN 需要额外的时间来执行两个独立但相互关联的过程——首先是提取可能包含物体的兴趣区,其次是针对每个兴趣区内具体对象类型的识别,这导致整体运行时间较长[^3]。 #### 优点分析 - **YOLO** - 实现简单高效; - 可以端到端训练; - 推理速度快,适用于视频流等高帧率数据源中的即时反馈需求; - **Faster R-CNN** - 更高的检测精度特别是对于小型或密集排列的对象; - 能够更好地理解背景信息从而减少误报情况的发生; - 支持多尺度特征图谱融合技术进一步提升性能表现; #### 缺点总结 - **YOLO** - 定位不够精确尤其是面对重叠严重的小型物品时容易出现问题; - **Faster R-CNN** - 计算资源消耗较大难以满足某些低功耗设备的要求; - 架构相对复杂部署成本较高; #### 应用案例 - **YOLO**: 自动驾驶汽车环境感知系统、无人机航拍监控等领域要求极短延迟的情况下表现出色。 - **Faster R-CNN**: 行人跟踪、医学影像诊断以及任何其他重视高度准确性的场合更为适用。 ```python import torch from torchvision.models.detection import fasterrcnn_resnet50_fpn, yolo_v5s # 加载预训练模型 frcnn_model = fasterrcnn_resnet50_fpn(pretrained=True) yolo_model = yolo_v5s(pretrained=True) # 设置评估模式 frcnn_model.eval() yolo_model.eval() with torch.no_grad(): # 假设 input_tensor 已经准备好作为输入张量 frcnn_output = frcnn_model(input_tensor) yolo_output = yolo_model(input_tensor) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值