-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
86 lines (62 loc) · 3.14 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Written by: Rahmad Sadli
Website : https://machinelearningspace.com
I finally made this program simple and readable
Hopefully, this program will help some beginners like me to understand better object detection.
If you want to redistribute it, just keep the author's name.
In oder to execute this program, you need to install TensorFlow 2.0 and opencv 4.x
For more details about how this program works. I explained well about it, just click the link below:
https://machinelearningspace.com/the-beginners-guide-to-implementing-yolo-v3-in-tensorflow-2-0-part-1/
Credit to:
Ayoosh Kathuria who shared his great work using pytorch, really appreaciated it.
https://blog.paperspace.com/how-to-implement-a-yolo-object-detector-in-pytorch/
"""
import tensorflow as tf
import numpy as np
import cv2
import time
def resize_image(inputs, modelsize):
inputs= tf.image.resize(inputs, modelsize)
return inputs
def output_boxes(inputs,model_size, max_output_size, max_output_size_per_class,
iou_threshold, confidence_threshold):
center_x, center_y, width, height, confidence, classes = \
tf.split(inputs, [1, 1, 1, 1, 1, -1], axis=-1)
top_left_x = center_x - width / 2.0
top_left_y = center_y - height / 2.0
bottom_right_x = center_x + width / 2.0
bottom_right_y = center_y + height / 2.0
inputs = tf.concat([top_left_x, top_left_y, bottom_right_x,
bottom_right_y, confidence, classes], axis=-1)
boxes_dicts = non_max_suppression(inputs, model_size, max_output_size,
max_output_size_per_class, iou_threshold, confidence_threshold)
return boxes_dicts
def draw_outputs(img, boxes, objectness, classes, nums, class_names):
boxes, objectness, classes, nums = boxes[0], objectness[0], classes[0], nums[0]
boxes=np.array(boxes)
for i in range(nums):
x1y1 = tuple((boxes[i,0:2] * [img.shape[1],img.shape[0]]).astype(np.int32))
x2y2 = tuple((boxes[i,2:4] * [img.shape[1],img.shape[0]]).astype(np.int32))
img = cv2.rectangle(img, (x1y1), (x2y2), (255,0,0), 2)
img = cv2.putText(img, '{} {:.4f}'.format(
class_names[int(classes[i])], objectness[i]),
(x1y1), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2)
return img
def load_class_names(file_name):
with open(file_name, 'r') as f:
class_names = f.read().splitlines()
return class_names
def non_max_suppression(inputs, model_size, max_output_size,
max_output_size_per_class, iou_threshold, confidence_threshold):
bbox, confs, class_probs = tf.split(inputs, [4, 1, -1], axis=-1)
bbox=bbox/model_size[0]
scores = confs * class_probs
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(bbox, (tf.shape(bbox)[0], -1, 1, 4)),
scores=tf.reshape(scores, (tf.shape(scores)[0], -1, tf.shape(scores)[-1])),
max_output_size_per_class=max_output_size_per_class,
max_total_size=max_output_size,
iou_threshold=iou_threshold,
score_threshold=confidence_threshold
)
return boxes, scores, classes, valid_detections