人工智能学习81-Yolo预测类

人工智能学习81-Yolo预测类—快手视频
人工智能学习82-Yolo预测类—快手视频

YoLo预测类

Yolo预测类加载预测模型,提供三个预测方法,一是预测图片的方法detect_image(),二是计算每秒帧数的方法get_FPS(),三是预测热成像的方法detect_heatmap()。

Yolo.py类

import colorsys
import os
import time

import numpy as np
from keras import backend as K
from PIL import ImageDraw, ImageFont

from yolo_model import get_yolo_model
from utils import (cvtColor, get_anchors, get_classes, preprocess_input,
                         resize_image, show_config)
from utils_bbox import DecodeBox

class YOLO(object):
    _defaults = {
   
   
        "model_path": '../model_data/yolo_weights.h5',  # 原来是:yolo_weights.h5 , best_epoch_weights.h5
        "classes_path": '../model_data/coco_classes.txt',  # 原来是:coco_classes.txt , voc_classes.txt
        "anchors_path": '../model_data/yolo_anchors.txt',
        "anchors_mask": [[6, 7, 8], [3, 4, 5], [0, 1, 2]],
        "input_shape": [416, 416],
        "confidence": 0.5,
        "nms_iou": 0.3,
        "max_boxes": 100,
        "letterbox_image": False,
    }

    @classmethod
    def get_defaults(cls, n):
        if n in cls._defaults:
            return cls._defaults[n]
        else:
            return "Unrecognized attribute name '" + n + "'"

    def __init__(self, **kwargs):
        self.__dict__.update(self._defaults)
        for name, value in kwargs.items():
            setattr(self, name, value)
            self._defaults[name] = value
            print("yolo.py __init__ name={},value={}".format(name, value))

        self.class_names, self.num_classes = get_classes(self.classes_path)
        self.anchors, self.num_anchors = get_anchors(self.anchors_path)  # shape(9,2)

        hsv_tuples = [(x / self.num_classes, 1., 1.) for x in range(self.num_classes)]  # (0.01,1.0,1.0)第一个元素为小数
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors))
        self.input_image_shape = K.placeholder(shape=(2,))  # 输入图片大小替位符
        self.sess = K.get_session()  # 使用Tensorflow-1.13.0
        self.boxes, self.scores, self.classes = self.generate()
        show_config(**self._defaults)

    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'

        self.yolo_model = get_yolo_model([None, None, 3], self.anchors_mask, self.num_classes)
        self.yolo_model.load_weights(self.model_path)
        print('装入模型:{} model, anchors, and classes loaded.'.format(model_path))
        boxes, scores, classes = DecodeBox(
            self.yolo_model.output,  # 模型定义的输出,是nets\yolo.py中yolo_body函数中的张量[P5, P4, P3]
            self.anchors,  # 先验框 [116,90],[156,198],[373,326] [30,61],[62,45],[59,119] [10,13],[16,30],[33,23]
            self.num_classes,  # 目标分类
            self.input_image_shape,  # 实际输入图片大小
            self.input_shape,  # 处理图片大小[416, 416]
            anchor_mask=self.anchors_mask,  # [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
            max_boxes=self.max_boxes,
            confidence=self.confidence,
            nms_iou=self.nms_iou,
            letterbox_image=self.letterbox_image
        )
        return boxes, scores, classes

    def detect_image(self, image, crop=False, count=False):
        image = cvtColor(image)
        image_data = resize_image(image, (self.input_shape[1], self.input_shape[0]), self.letterbox_image)
        image_data = np.expand_dims(preprocess_input(np.array(image_data, dtype='float32')), 0)
        print("K.learning_phase()={}".format(K.learning_phase()))
        print("self.yolo_model.input={}".format(self.yolo_model.input))
        print("self.input_image_shape={}".format(self.input_image_shape))

        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='../model_data/simhei.ttf',
                                  size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = int(max((image.size[0] + image.size[1]) // np.mean(self.input_shape), 1))
        if count:
            print("top_label:", out_classes)
            classes_nums = np.zeros([self.num_classes])
            for i in range(self.num_classes):
                num = np.sum(out_classes == i)
                if num > 0:
                    print(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值