0% found this document useful (0 votes)
25 views13 pages

Yolo Tensorflow

The document outlines a Python implementation of a YOLO (You Only Look Once) object detection system using TensorFlow and OpenCV. It defines a Detector class that initializes a YOLO network, processes images for detection, and draws results on images or video frames. The main function sets up argument parsing for weights and directories, and runs detection from both camera and image files.

Uploaded by

jamyt6384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views13 pages

Yolo Tensorflow

The document outlines a Python implementation of a YOLO (You Only Look Once) object detection system using TensorFlow and OpenCV. It defines a Detector class that initializes a YOLO network, processes images for detection, and draws results on images or video frames. The main function sets up argument parsing for weights and directories, and runs detection from both camera and image files.

Uploaded by

jamyt6384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import os

import cv2

import argparse

import numpy as np

import tensorflow as tf

import [Link] as cfg

from yolo.yolo_net import YOLONet

from [Link] import Timer

:class Detector(object)

:def __init__(self, net, weight_file)

[Link] = net

self.weights_file = weight_file

[Link] = [Link]
self.num_class = len([Link])

self.image_size = cfg.IMAGE_SIZE

self.cell_size = cfg.CELL_SIZE

self.boxes_per_cell = cfg.BOXES_PER_CELL

[Link] = [Link]

self.iou_threshold = cfg.IOU_THRESHOLD

self.boundary1 = self.cell_size * self.cell_size * self.num_class

\+ self.boundary2 = self.boundary1

self.cell_size * self.cell_size * self.boxes_per_cell

)([Link] = [Link]

[Link](tf.global_variables_initializer())

print('Restoring weights from: ' + self.weights_file)

)([Link] = [Link]

[Link]([Link], self.weights_file)
:def draw_result(self, img, 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)

[Link](img, (x - w, y - h), (x + w, y + h), (0, 255, 0), 2)

,[Link](img, (x - w, y - h - 20)

)1- ,)125, 125, 125( ,)x + w, y - h(

lineType = cv2.LINE_AA if cv2.__version__ > '3' else cv2.CV_AA

([Link]

,img, result[i][0] + ' : %.2f' % result[i][5]

,cv2.FONT_HERSHEY_SIMPLEX, 0.5 ,)x - w + 5, y - h - 7(

)lineType ,1 ,)0 ,0 ,0(

:def detect(self, img)

img_h, img_w, _ = [Link]


inputs = [Link](img, (self.image_size, self.image_size))

inputs = [Link](inputs, cv2.COLOR_BGR2RGB).astype(np.float32)

inputs = (inputs / 255.0) * 2.0 - 1.0

inputs = [Link](inputs, (1, self.image_size, self.image_size, 3))

result = self.detect_from_cvmat(inputs)[0]

:for i in range(len(result))

result[i][1] *= (1.0 * img_w / self.image_size)

result[i][2] *= (1.0 * img_h / self.image_size)

result[i][3] *= (1.0 * img_w / self.image_size)

result[i][4] *= (1.0 * img_h / self.image_size)

return result

:def detect_from_cvmat(self, inputs)

,net_output = [Link]([Link]
)feed_dict={[Link]: inputs}

][ = results

:for i in range(net_output.shape[0])

[Link](self.interpret_output(net_output[i]))

return results

:def interpret_output(self, output)

,probs = [Link]((self.cell_size, self.cell_size

))self.boxes_per_cell, self.num_class

(class_probs = [Link]

,output[0:self.boundary1]

))self.cell_size, self.cell_size, self.num_class(

(scales = [Link]

,output[self.boundary1:self.boundary2]

))self.cell_size, self.cell_size, self.boxes_per_cell(

(boxes = [Link]
,output[self.boundary2:]

))self.cell_size, self.cell_size, self.boxes_per_cell, 4(

(offset = [Link]

)self.cell_size * self.boxes_per_cell * ][Link](self.cell_size)[

(offset = [Link]

([Link]

,offset

,)]self.boxes_per_cell, self.cell_size, self.cell_size[

))0 ,2 ,1(

boxes[:, :, :, 0] += offset

boxes[:, :, :, 1] += [Link](offset, (1, 0, 2))

boxes[:, :, :, :2] = 1.0 * boxes[:, :, :, 0:2] / self.cell_size

boxes[:, :, :, 2:] = [Link](boxes[:, :, :, 2:])

boxes *= self.image_size
:for i in range(self.boxes_per_cell)

:for j in range(self.num_class)

(probs[:, :, i, j] = [Link]

)class_probs[:, :, j], scales[:, :, i]

filter_mat_probs = [Link](probs >= [Link], dtype='bool')

filter_mat_boxes = [Link](filter_mat_probs)

,boxes_filtered = boxes[filter_mat_boxes[0]

]filter_mat_boxes[1], filter_mat_boxes[2]

probs_filtered = probs[filter_mat_probs]

(classes_num_filtered = [Link]

[)filter_mat_probs, axis=3

]filter_mat_boxes[0], filter_mat_boxes[1], filter_mat_boxes[2]

argsort = [Link]([Link](probs_filtered))[::-1]

boxes_filtered = boxes_filtered[argsort]

probs_filtered = probs_filtered[argsort]
classes_num_filtered = classes_num_filtered[argsort]

:for i in range(len(boxes_filtered))

:if probs_filtered[i] == 0

continue

:for j in range(i + 1, len(boxes_filtered))

:if [Link](boxes_filtered[i], boxes_filtered[j]) > self.iou_threshold

probs_filtered[j] = 0.0

filter_iou = [Link](probs_filtered > 0.0, dtype='bool')

boxes_filtered = boxes_filtered[filter_iou]

probs_filtered = probs_filtered[filter_iou]

classes_num_filtered = classes_num_filtered[filter_iou]

][ = result

:for i in range(len(boxes_filtered))

([Link]
,[Link][classes_num_filtered[i]][

,boxes_filtered[i][0]

,boxes_filtered[i][1]

,boxes_filtered[i][2]

,boxes_filtered[i][3]

)]probs_filtered[i]

return result

:def iou(self, box1, box2)

\ - tb = min(box1[0] + 0.5 * box1[2], box2[0] + 0.5 * box2[2])

max(box1[0] - 0.5 * box1[2], box2[0] - 0.5 * box2[2])

\ - lr = min(box1[1] + 0.5 * box1[3], box2[1] + 0.5 * box2[3])

max(box1[1] - 0.5 * box1[3], box2[1] - 0.5 * box2[3])

inter = 0 if tb < 0 or lr < 0 else tb * lr

return inter / (box1[2] * box1[3] + box2[2] * box2[3] - inter)


:def camera_detector(self, cap, wait=10)

)(detect_timer = Timer

)(ret, _ = [Link]

:while ret

)(ret, frame = [Link]

)(detect_timer.tic

result = [Link](frame)

)(detect_timer.toc

(print('Average detecting time: {:.3f}s'.format

))detect_timer.average_time

self.draw_result(frame, result)

[Link]('Camera', frame)

[Link](wait)

)(ret, frame = [Link]


:def image_detector(self, imname, wait=0)

)(detect_timer = Timer

image = [Link](imname)

)(detect_timer.tic

result = [Link](image)

)(detect_timer.toc

(print('Average detecting time: {:.3f}s'.format

))detect_timer.average_time

self.draw_result(image, result)

[Link]('Image', image)

[Link](wait)

:)(def main
)(parser = [Link]

parser.add_argument('--weights', default="YOLO_small.ckpt", type=str)

parser.add_argument('--weight_dir', default='weights', type=str)

parser.add_argument('--data_dir', default="data", type=str)

parser.add_argument('--gpu', default='', type=str)

)(args = parser.parse_args

[Link]['CUDA_VISIBLE_DEVICES'] = [Link]

yolo = YOLONet(False)

weight_file = [Link](args.data_dir, args.weight_dir, [Link])

detector = Detector(yolo, weight_file)

detect from camera #

cap = [Link](-1) #

detector.camera_detector(cap) #
detect from image file #

'imname = 'test/[Link]

detector.image_detector(imname)

:'__if __name__ == '__main

)(main

You might also like