Advertisement
Mochinov

Untitled

Mar 24th, 2022
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4.  
  5. class Yolo:
  6.     """
  7.    
  8.    """
  9.     def __init__(
  10.         self,
  11.         weights = "yolov3.weights",
  12.         config = "yolov3.cfg",
  13.         names = "yolov3.txt",
  14.         conf_thresh = 0.7,
  15.         nms_thresh = 0.2,
  16.     ) -> None:
  17.         """
  18.        
  19.        """
  20.         self.ct = conf_thresh
  21.         self.nmst = nms_thresh
  22.         self.net = cv2.dnn.readNet(weights, config)
  23.         self.classes = [0] # Зачем она тут ?
  24.         layer_names = self.net.getLayerNames()
  25.         self.output_layers = [
  26.             layer_names[i-1] for i in self.net.getUnconnectedOutLayers()
  27.         ]
  28.  
  29.     def detect(self, img, target_id) -> tuple:
  30.         """
  31.        
  32.        """
  33.  
  34.         b, c, ids, idxs = self.get_detection_data(img, target_id)
  35.  
  36.         return b, idxs, ids
  37.  
  38.     def get_detection_data(self, img, target_id) -> tuple:
  39.         """
  40.        
  41.        """
  42.  
  43.         layer_outputs = self.get_information(img)
  44.         height, width = img.shape[:2]
  45.         b, c, ids, idxs = self.thresh(layer_outputs, width, height, target_id)
  46.        
  47.         return b, c, ids, idxs
  48.  
  49.     def get_information(self, img):
  50.         """
  51.        
  52.        """
  53.  
  54.         blob = cv2.dnn.blobFromImage(
  55.             img, 1 / 255.0,
  56.             (416, 416),
  57.             swapRB=True,
  58.             crop=False
  59.         )
  60.         self.net.setInput(blob)
  61.         layer_outputs = self.net.forward(self.output_layers)
  62.  
  63.         return layer_outputs
  64.  
  65.     def thresh(self, layer_outputs, width, height, target_id):
  66.         """
  67.        
  68.        """
  69.  
  70.         boxes = []
  71.         confidences = []
  72.         class_ids = []
  73.         for output in layer_outputs:
  74.             for detection in output:
  75.                 scores = detection[5:]
  76.                 class_id = np.argmax(scores)
  77.                 confidence = scores[class_id]
  78.                 if confidence > self.ct and class_id == target_id:
  79.                     box = detection[0:4] * np.array([width, height, width, height])
  80.                     (cx, cy, w, h) = box.astype('int')
  81.                     tx = int(cx - (w / 2))
  82.                     ty = int(cy - (h / 2))
  83.                     boxes.append([tx, ty, int(w), int(h)])
  84.                     confidences.append(float(confidence))
  85.                     class_ids.append(class_id)
  86.         idxs = cv2.dnn.NMSBoxes(boxes, confidences, self.ct, self.nmst)
  87.  
  88.         return boxes, confidences, class_ids, idxs
  89.  
  90.  
  91. # 1) names, зачем он нужен ?
  92. # 2) Что происходит в конструкторе ? (Что там инициализируется)
  93. # 3) Для чего target_class_id ?
  94. # 4) Зачем colors?
  95. # 5) Что возвращает ? get_information
  96. # 6) Почему именно такие цифры почему не другие ? blob = cv2.dnn.blobFromImage(img, 1 / 255.0, (416, 416), swapRB=True, crop=False)
  97. # 7) Что означает b, c, ids, idxs ? каждая расшифровка
  98. # 8) Что это self.net.setInput(blob)?
  99. # 9) Как работает каждый метод и что происходит
  100. # 10) Зачем здесь scores = detection[5:]? почему именно 5?
  101. # 11) Зачем нужна c?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement