Advertisement
Mochinov

Untitled

Mar 24th, 2022
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. import cv2
  2. from yolo import Yolo
  3.  
  4. import numpy as np
  5.  
  6. colors = [255, 255, 255]
  7.  
  8. target_class_id = 0
  9.  
  10.  
  11. net = Yolo()
  12.  
  13. def delete_people(
  14.         boxes_result_video,
  15.         frame_number,
  16.         human_id,
  17.         frame,
  18.     ):
  19.     """
  20.    
  21.    """
  22.     x, y, w, h = boxes_result_video[frame_number][human_id]
  23.     frame[y-10:h+10, x-10:w+10] = median[y-10:h+10, x-10:w+10]
  24.     return frame
  25.  
  26. def nastroika(name_video):
  27.     """
  28.    
  29.    """
  30.     frame = 15 # Что за параметр ? почему 15
  31.     video1 = cv2.VideoCapture(name_video)
  32.     length = int(video1.get(cv2.CAP_PROP_FRAME_COUNT))
  33.     frames = []
  34.     while frame < length / 10:
  35.  
  36.         success, first_frame = video1.read() # не используемая success переменная ? зачем
  37.  
  38.         video1.set(cv2.CAP_PROP_POS_FRAMES, frame)
  39.         gray = cv2.cvtColor(
  40.             first_frame,
  41.             cv2.IMREAD_COLOR
  42.         )
  43.         frames.append(gray)
  44.         frame += 25 # что за 25 ? и почему 25 ?
  45.  
  46.     video1.release()
  47.     median = np.median(frames, axis=0).astype(dtype=np.uint8)
  48.     return median
  49.  
  50. def draw(path_to_video, boxes_result_video):
  51.     """
  52.    
  53.    """
  54.     video = cv2.VideoCapture(path_to_video)
  55.     success = True
  56.     while success:
  57.         success, frame = video.read()
  58.         for i in boxes_result_video:
  59.             for i1 in i:
  60.                 x, y, w, h = i[i1]
  61.                 cv2.ellipse(frame, (int(x + w/2), y + h), (int(h/10), int(w/1.2)), 90, 210, 510, 255)#Рисовка эллипсов
  62.             cv2.imshow('', frame)
  63.             cv2.waitKey(0)
  64.  
  65. def detect(frame, target_class_id):
  66.     """
  67.    
  68.    """
  69.     # Для чего нужно вызывать метод в методе из метода ? почему нельзя сделать статическим метод и обращаться к нему ? зачем cтолько вызовов ?
  70.     data, idxs, index = net.detect(frame, target_class_id)
  71.     return data, idxs, index
  72.  
  73. def media_analysis(media_path: str) -> list:
  74.     """
  75.    
  76.    """
  77.     video = cv2.VideoCapture(media_path)
  78.     detection_data = []
  79.     success = True
  80.     while success:
  81.         success, frame = video.read()
  82.         temporary_array = {}
  83.         bboxes, indices, idxs = detect(frame, target_class_id) # Для чего тут idxs? почему bboxes? bb
  84.         for i in indices:
  85.             # i = i[0]
  86.             box = bboxes[i]
  87.             x = round(box[0])
  88.             y = round(box[1])
  89.             w = round(box[2])
  90.             h = round(box[3])
  91.             temporary_array[i] = [x, y, w, h]
  92.  
  93.         detection_data.append(temporary_array)
  94.  
  95.         i = delete_people(
  96.             detection_data,
  97.             0, 3,
  98.             frame,
  99.         ) # Что тут происходит ? он записывается в переменную i, а зачем ?
  100.  
  101.     return detection_data
  102.  
  103. def image_analysis(media_path: str) -> list:
  104.     """
  105.    
  106.    """
  107.     detection_data = {}
  108.     frame = cv2.imread(media_path, cv2.IMREAD_COLOR)
  109.  
  110.     bboxes, indices, _ = detect(frame, target_class_id)
  111.     for index in indices:
  112.         box = bboxes[index]
  113.         detection_data[index] = [round(box[0]), round(box[1]), round(box[2]), round(box[3])]
  114.  
  115.  
  116.     for index in detection_data:
  117.         x1, y1, x2, y2 = detection_data[index]
  118.         cv2.rectangle(frame, (x1, y1), (x1+x2, y1+y2), 255)
  119.         cv2.ellipse(frame, (int(x1 + x2/2), y1 + y2), (int(y2/10), int(x2/1.2)), 90, 210, 510, 255)
  120.  
  121.     cv2.imshow('r.jpg', frame)
  122.     cv2.waitKey(0)
  123.  
  124.     return detection_data
  125.  
  126. path = 'j.jpeg'
  127.  
  128.  
  129.  
  130. # median = nastroika(path)
  131. print(image_analysis(path))
  132.  
  133. # 1) Объяснить как работает видео обработка, на статичной и не статичной камере
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement