yolo的一个汽车识别项目记录

本文详细介绍了一种基于深度学习的图像识别与处理流程,包括图片预处理、模型加载、预测及结果可视化等关键步骤,展示了如何使用YOLOv2-tiny模型进行目标检测。

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

得到图片的路径:输入为图片文件夹,返回值为路径列表

def get_image_path(dir):
    paths = []
    for file in os.listdir(dir):
        file_path = os.path.join(dir, file)
        paths.append(file_path)
    return paths

定义个:
1、导入argparse模块

2、创建解析器对象ArgumentParser,可以添加参数。

description:描述程序

parser=argparse.ArgumentParser(description="This is a example program ")

add_help:默认是True,可以设置False禁用

3、add_argument()方法,用来指定程序需要接受的命令参数
add_argument()常用的参数:

dest:如果提供dest,例如dest=“a”,那么可以通过args.a访问该参数

default:设置参数的默认值

action:参数出发的动作

store:保存参数,默认

store_const:保存一个被定义为参数规格一部分的值(常量),而不是一个来自参数解析而来的值。

store_ture/store_false:保存相应的布尔值

append:将值保存在一个列表中。

append_const:将一个定义在参数规格中的值(常量)保存在一个列表中。

count:参数出现的次数

parser.add_argument("-v", “–verbosity”, action=“count”, default=0, help=“increase output verbosity”)

version:打印程序版本信息

type:把从命令行输入的结果转成设置的类型

choice:允许的参数值

parser.add_argument("-v", “–verbosity”, type=int, choices=[0, 1, 2], help=“increase output verbosity”)

help:参数命令的介绍

parser = argparse.ArgumentParser()
    parser.add_argument('--dir_path', default='test_images', type=str)
    parser.add_argument('--out_path', default='out_images', type=str)
    parser.add_argument('--model_file', default='model/yolov2-tiny-voc.h5', type=str)
    args = parser.parse_args()

得到路径

paths = utils.get_image_path(args.dir_path)

读取图片 进行resize 存到李彪

images = []
for path in paths:
        image = cv2.imread(path)
        resized = cv2.resize(image, (416, 416))
        images.append(resized)
       

之后可进行图像处理:滤波之类的
image_processed = []
归一化

def preprocess_image(resized):
    out_image = resized/255.
    return out_image

批量处理

 for image in images:
        image_processed.append(utils.preprocess_image(image))

然后加载模型:

model = load_model(args.model_file)
    predictions = model.predict(np.array(image_processed))

画出框框:

    for i in range(predictions.shape[0]):
        boxes = utils.process_predictions(predictions[i],probs_threshold=0.3,iou_threshold=0.2)
        out_image = utils.draw_boxes(images[i],boxes)
        cv2.imwrite('%s/out%s.jpg'%(args.out_path,i), out_image)

画框子函数有两个 一个生成boxs一个画出boxs:

def draw_boxes(image,boxes):
    for i in range(len(boxes)):
        color = colors[boxes[i].clas]
        best_class_name = classes[boxes[i].clas]

        image = cv2.rectangle(image, (boxes[i].x1, boxes[i].y1),
                                    (boxes[i].x2, boxes[i].y2),color)
#  坐标为 左上 右下 
        cv2.putText(
            image, best_class_name + ' : %.2f' % boxes[i].p_max,
            (int(boxes[i].x1 + 5), int(boxes[i].y1 - 7)), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
            color, 1)

    return image
def process_predictions(prediction, n_grid=13, n_class=20, n_box=5, probs_threshold=0.3, iou_threshold=0.3):
    prediction = np.reshape(prediction, (n_grid, n_grid, n_box, 5+n_class))
    boxes = []
    for row in range(n_grid):
        for col in range(n_grid):
            for b in range(n_box):
                tx, ty, tw, th, tc = prediction[row, col, b, :5]
                box = Box()
                box.w = np.exp(tw) * anchors[2 * b + 1] * 32.0
                box.h = np.exp(th) * anchors[2 * b + 1] * 32.0
                c_probs = softmax(prediction[row, col, b, 5:])
                box.clas = np.argmax(c_probs)
                box.p_max = np.max(c_probs) * sigmoid(tc)

                center_x = (float(col) + sigmoid(tx)) * 32.0
                center_y = (float(row) + sigmoid(ty)) * 32.0
#                center_x = float(col+1)*32.0+tx
#                center_y = float(row+1)*32.0+ty 
#                box.x1 = int(center_x - (tw*10 / 2.))  #得到中心点坐标
#                box.x2 = int(center_x + (tw*10 / 2.))
#                box.y1 = int(center_y - (th*10 / 2.))
#                box.y2 = int(center_y + (th*10 / 2.))

#
                box.x1 = int(center_x - (box.w / 2.))  #得到中心点坐标
                box.x2 = int(center_x + (box.w / 2.))
                box.y1 = int(center_y - (box.h / 2.))
                box.y2 = int(center_y + (box.h / 2.))

                if box.p_max > probs_threshold:
                    boxes.append(box)

    boxes.sort(key=lambda b: b.p_max, reverse=True)

    filtered_boxes = non_maximal_suppression(boxes, iou_threshold)

    return filtered_boxes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值