Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import os
- class Yolo:
- """
- """
- def __init__(
- self,
- weights = "yolov3.weights",
- config = "yolov3.cfg",
- names = "yolov3.txt",
- conf_thresh = 0.7,
- nms_thresh = 0.2,
- ) -> None:
- """
- """
- self.ct = conf_thresh
- self.nmst = nms_thresh
- self.net = cv2.dnn.readNet(weights, config)
- self.classes = [0] # Зачем она тут ?
- layer_names = self.net.getLayerNames()
- self.output_layers = [
- layer_names[i-1] for i in self.net.getUnconnectedOutLayers()
- ]
- def detect(self, img, target_id) -> tuple:
- """
- """
- b, c, ids, idxs = self.get_detection_data(img, target_id)
- return b, idxs, ids
- def get_detection_data(self, img, target_id) -> tuple:
- """
- """
- layer_outputs = self.get_information(img)
- height, width = img.shape[:2]
- b, c, ids, idxs = self.thresh(layer_outputs, width, height, target_id)
- return b, c, ids, idxs
- def get_information(self, img):
- """
- """
- blob = cv2.dnn.blobFromImage(
- img, 1 / 255.0,
- (416, 416),
- swapRB=True,
- crop=False
- )
- self.net.setInput(blob)
- layer_outputs = self.net.forward(self.output_layers)
- return layer_outputs
- def thresh(self, layer_outputs, width, height, target_id):
- """
- """
- boxes = []
- confidences = []
- class_ids = []
- for output in layer_outputs:
- for detection in output:
- scores = detection[5:]
- class_id = np.argmax(scores)
- confidence = scores[class_id]
- if confidence > self.ct and class_id == target_id:
- box = detection[0:4] * np.array([width, height, width, height])
- (cx, cy, w, h) = box.astype('int')
- tx = int(cx - (w / 2))
- ty = int(cy - (h / 2))
- boxes.append([tx, ty, int(w), int(h)])
- confidences.append(float(confidence))
- class_ids.append(class_id)
- idxs = cv2.dnn.NMSBoxes(boxes, confidences, self.ct, self.nmst)
- return boxes, confidences, class_ids, idxs
- # 1) names, зачем он нужен ?
- # 2) Что происходит в конструкторе ? (Что там инициализируется)
- # 3) Для чего target_class_id ?
- # 4) Зачем colors?
- # 5) Что возвращает ? get_information
- # 6) Почему именно такие цифры почему не другие ? blob = cv2.dnn.blobFromImage(img, 1 / 255.0, (416, 416), swapRB=True, crop=False)
- # 7) Что означает b, c, ids, idxs ? каждая расшифровка
- # 8) Что это self.net.setInput(blob)?
- # 9) Как работает каждый метод и что происходит
- # 10) Зачем здесь scores = detection[5:]? почему именно 5?
- # 11) Зачем нужна c?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement