What is this Yolo class for? How can I access this codes? What is the alternative way for replacing these codes?
class Yolo():
'''
Packed yolo Netwrok from cv2
'''
def __init__(self):
# get model configuration and weight
model_configuration = 'yolov3.cfg'
model_weight = 'yolov3.weights'
# define classes
self.classes = None
class_file = 'coco.names'
with open(class_file, 'rt') as file:
self.classes = file.read().rstrip('n').split('n')
net = cv2.dnn.readNetFromDarknet(
model_configuration, model_weight)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
self.yolo = net
self.cf_th = CF_THRESHOLD
self.nms_th = NMS_THRESHOLD
self.resolution = INPUT_RESOLUTION
print('Model Initialization Done!')
def detect(self, frame):
'''
The yolo function which is provided by opencv
Args:
frames(np.array): input picture for object detection
Returns:
ret(np.array): all possible boxes with dim = (N, classes+5)
'''
blob = cv2.dnn.blobFromImage(np.float32(frame), 1/255, self.resolution,
[0, 0, 0], 1, crop=False)
self.yolo.setInput(blob)
layers_names = self.yolo.getLayerNames()
output_layer =
[layers_names[i[0] - 1] for i in self.yolo.getUnconnectedOutLayers()]
outputs = self.yolo.forward(output_layer)
ret = np.zeros((1, len(self.classes)+5))
for out in outputs:
ret = np.concatenate((ret, out), axis=0)
return ret
def draw_bbox(self, frame, class_id, conf, left, top, right, bottom):
'''
Drew a Bounding Box
Args:
frame(np.array): the base image for painting box on
class_id(int) : id of the object
conf(float) : confidential score for the object
left(int) : the left pixel for the box
top(int) : the top pixel for the box
right(int) : the right pixel for the box
bottom(int) : the bottom pixel for the box
Return:
frame(np.array): the image with bounding box on it
'''
# Draw a bounding box.
cv2.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
label = '%.2f' % conf
# Get the label for the class name and its confidence
if self.classes:
assert(class_id < len(self.classes))
label = '%s:%s' % (self.classes[class_id], label)
#Display the label at the top of the bounding box
label_size, base_line = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 2, 1)
top = max(top, label_size[1])
cv2.rectangle(frame,
(left, top - round(1.5*label_size[1])),
(left + round(label_size[0]), top + base_line),
(255, 255, 255), cv2.FILLED)
cv2.putText(frame, label, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 2)
return frame
What is this Yolo class for? How can I access this codes? What is the alternative way for replacing these codes?
How can I access this codes? What is the alternative way for replacing these codes?
New contributor
Ahmet Selim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.