• 大小: 302KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: 其他
  • 标签: yolov1  tensorflow  代码  

资源简介

yolov1的tensorflow实现,我对其做了完整的注释,针对于入门的同学,下载即可运行

资源截图

代码片段和文件信息

import os
import cv2
import argparse
import numpy as np
import tensorflow as tf
import yolo.config as cfg
from yolo.yolo_net import YOLONet
from utils.timer import Timer

#检测的类Detector
class Detector(object):

    def __init__(self net weight_file):   # Yolo网络
        self.net = net    #网络
        self.weights_file = weight_file  #保留模型的文件

        self.classes = cfg.CLASSES   # PASCAL VOC数据集的20个类别
        self.num_class = len(self.classes)  #20
        self.image_size = cfg.IMAGE_SIZE    #image_size 448
        self.cell_size = cfg.CELL_SIZE      #cell_size 7
        self.boxes_per_cell = cfg.BOXES_PER_CELL  #每一个cell预测的框 2
        self.threshold = cfg.THRESHOLD    #0.2
        self.iou_threshold = cfg.IOU_THRESHOLD  #iou阈值 0.5
        self.boundary1 = self.cell_size * self.cell_size * self.num_class  #7*7*20
        self.boundary2 = self.boundary1 +\
            self.cell_size * self.cell_size * self.boxes_per_cell    #7*7*20 + 7*7*2

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())   #tensorflow中初始化全局变量

        print(‘Restoring weights from: ‘ + self.weights_file)
        self.saver = tf.train.Saver()
        self.saver.restore(self.sess self.weights_file)  #从模型文件中恢复会话

    def draw_result(self img result):   #输出结果
        print(“hell“)
        print(len(result))
        for i in range(len(result)):
            x = int(result[i][1])
            y = int(result[i][2])
            w = int(result[i][3] / 2)
            h = int(result[i][4] / 2)
            cv2.rectangle(img (x - w y - h) (x + w y + h) (0 255 0) 2)
            cv2.rectangle(img (x - w y - h - 20)
                          (x + w y - h) (125 125 125) -1)
            lineType = cv2.LINE_AA if cv2.__version__ > ‘3‘ else cv2.CV_AA
            cv2.putText(
                img result[i][0] + ‘ : %.2f‘ % result[i][5]
                (x - w + 5 y - h - 7) cv2.FONT_HERSHEY_SIMPLEX 0.5
                (0 0 0) 1 lineType)

    def detect(self img):  #检测
        img_h img_w _ = img.shape
        inputs = cv2.resize(img (self.image_size self.image_size))   #resize大小
        inputs = cv2.cvtColor(inputs cv2.COLOR_BGR2RGB).astype(np.float32)
        inputs = (inputs / 255.0) * 2.0 - 1.0        #对图片处理一下
        inputs = np.reshape(inputs (1 self.image_size self.image_size 3))  #reshape一下, [batch_size image_size image_size 3]

        result = self.detect_from_cvmat(inputs)[0]  #返回已经是真实的坐标了

        print(“输出result1:“ result[0][1])
        print(“输出result2:“ result[0][2])
        print(“输出result3:“ result[0][3])
        print(“输出result4:“ result[0][4])

        for i in range(len(result)):
            result[i][1] *= (1.0 * img_w / self.image_size)  #x_center是真实的坐标了
            result[i][2] *= (1.0 * img_h / self.image_size)  #y_center
            result[i][3] *= (1.0 * img_w / self.image_size)  #width
            result[i][4] *= (1.0 * img_h 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-07 15:59  yolov1_tensorflow\
     文件          12  2018-02-28 20:44  yolov1_tensorflow\.gitignore
     文件        1067  2018-02-28 20:44  yolov1_tensorflow\LICENSE
     文件         709  2018-02-28 20:44  yolov1_tensorflow\README.md
     文件         306  2018-02-28 20:44  yolov1_tensorflow\download_data.sh
     目录           0  2018-08-07 15:59  yolov1_tensorflow\test\
     文件      100945  2007-01-10 02:29  yolov1_tensorflow\test\000021.jpg
     文件       58495  2018-02-28 20:44  yolov1_tensorflow\test\cat.jpg
     文件      113880  2018-02-28 20:44  yolov1_tensorflow\test\person.jpg
     文件       10536  2018-08-07 11:43  yolov1_tensorflow\test.py
     文件        7834  2018-08-01 18:33  yolov1_tensorflow\train.py
     目录           0  2018-08-07 15:59  yolov1_tensorflow\utils\
     文件           0  2018-02-28 20:44  yolov1_tensorflow\utils\__init__.py
     目录           0  2018-08-07 15:59  yolov1_tensorflow\utils\__pycache__\
     文件         170  2018-07-29 09:36  yolov1_tensorflow\utils\__pycache__\__init__.cpython-35.pyc
     文件         144  2018-07-30 20:33  yolov1_tensorflow\utils\__pycache__\__init__.cpython-36.pyc
     文件        5477  2018-07-29 09:36  yolov1_tensorflow\utils\__pycache__\pascal_voc.cpython-35.pyc
     文件        4708  2018-07-30 20:34  yolov1_tensorflow\utils\__pycache__\pascal_voc.cpython-36.pyc
     文件        1368  2018-07-29 09:36  yolov1_tensorflow\utils\__pycache__\timer.cpython-35.pyc
     文件        1241  2018-07-30 20:33  yolov1_tensorflow\utils\__pycache__\timer.cpython-36.pyc
     文件        8452  2018-08-01 19:55  yolov1_tensorflow\utils\pascal_voc.py
     文件        1125  2018-02-28 20:44  yolov1_tensorflow\utils\timer.py
     目录           0  2018-08-07 15:59  yolov1_tensorflow\yolo\
     文件           0  2018-02-28 20:44  yolov1_tensorflow\yolo\__init__.py
     目录           0  2018-08-07 15:59  yolov1_tensorflow\yolo\__pycache__\
     文件         169  2018-07-29 09:35  yolov1_tensorflow\yolo\__pycache__\__init__.cpython-35.pyc
     文件         143  2018-07-30 20:33  yolov1_tensorflow\yolo\__pycache__\__init__.cpython-36.pyc
     文件        1189  2018-07-29 09:35  yolov1_tensorflow\yolo\__pycache__\config.cpython-35.pyc
     文件        1063  2018-07-30 20:33  yolov1_tensorflow\yolo\__pycache__\config.cpython-36.pyc
     文件        8070  2018-07-29 09:35  yolov1_tensorflow\yolo\__pycache__\yolo_net.cpython-35.pyc
     文件        7036  2018-08-06 16:13  yolov1_tensorflow\yolo\__pycache__\yolo_net.cpython-36.pyc
............此处省略3个文件信息

评论

共有 条评论