├── adb └── scrcpy_adb.py ├── game ├── __init__.py ├── game_action.py └── game_control.py ├── img_collection.py ├── main.py ├── ncnn.bin ├── ncnn.param └── utils └── yolov5.py /adb/scrcpy_adb.py: -------------------------------------------------------------------------------- 1 | from adbutils import adb 2 | import scrcpy 3 | import cv2 as cv 4 | import time 5 | from utils.yolov5 import YoloV5s 6 | 7 | 8 | class ScrcpyADB: 9 | def __init__(self): 10 | devices = adb.device_list() 11 | client = scrcpy.Client(device=devices[0]) 12 | # You can also pass an ADBClient instance to it 13 | adb.connect("127.0.0.1:5555") 14 | print(devices, client) 15 | client.add_listener(scrcpy.EVENT_FRAME, self.on_frame) 16 | client.start(threaded=True) 17 | self.client = client 18 | self.yolo = YoloV5s(target_size=640, 19 | prob_threshold=0.25, 20 | nms_threshold=0.45, 21 | num_threads=4, 22 | use_gpu=True) 23 | self.last_screen = None 24 | 25 | def on_frame(self, frame: cv.Mat): 26 | if frame is not None: 27 | self.last_screen = frame 28 | # try: 29 | # result = self.yolo(frame) 30 | # print(result) 31 | # for obj in result: 32 | # color = (0, 255, 0) 33 | # if obj.label == 0: 34 | # color = (255, 0, 0) 35 | # elif obj.label == 5: 36 | # color = (0, 0, 255) 37 | # 38 | # cv.rectangle(frame, 39 | # (int(obj.rect.x), int(obj.rect.y)), 40 | # (int(obj.rect.x + obj.rect.w), int(obj.rect.y + + obj.rect.h)), 41 | # color, 2 42 | # ) 43 | # print(obj) 44 | # 45 | # except Exception as e: 46 | # print(e) 47 | 48 | # cv.imshow('frame', frame) 49 | # cv.waitKey(1) 50 | 51 | def touch_start(self, x: int or float, y: int or float): 52 | self.client.control.touch(int(x), int(y), scrcpy.ACTION_DOWN) 53 | 54 | def touch_move(self, x: int or float, y: int or float): 55 | self.client.control.touch(int(x), int(y), scrcpy.ACTION_MOVE) 56 | 57 | def touch_end(self, x: int or float, y: int or float): 58 | self.client.control.touch(int(x), int(y), scrcpy.ACTION_UP) 59 | 60 | def tap(self, x: int or float, y: int or float): 61 | self.touch_start(x, y) 62 | time.sleep(0.01) 63 | self.touch_end(x, y) 64 | 65 | 66 | if __name__ == '__main__': 67 | sadb = ScrcpyADB() 68 | time.sleep(5) 69 | sadb.tap(1568 / 1.25, 166 / 1.25) 70 | time.sleep(999) 71 | -------------------------------------------------------------------------------- /game/__init__.py: -------------------------------------------------------------------------------- 1 | from .game_control import GameControl 2 | -------------------------------------------------------------------------------- /game/game_action.py: -------------------------------------------------------------------------------- 1 | from typing import Tuple 2 | 3 | from utils.yolov5 import YoloV5s 4 | from game_control import GameControl 5 | from adb.scrcpy_adb import ScrcpyADB 6 | import time 7 | import cv2 as cv 8 | from ncnn.utils.objects import Detect_Object 9 | import math 10 | import numpy as np 11 | 12 | 13 | def get_detect_obj_bottom(obj: Detect_Object) -> Tuple[int, int]: 14 | return int(obj.rect.x + obj.rect.w / 2), int(obj.rect.y + obj.rect.h) 15 | 16 | 17 | def distance_detect_object(a: Detect_Object, b: Detect_Object): 18 | return math.sqrt((a.rect.x - b.rect.x) ** 2 + (a.rect.y - b.rect.y) ** 2) 19 | 20 | 21 | def calc_angle(x1, y1, x2, y2): 22 | angle = math.atan2(y1 - y2, x1 - x2) 23 | return 180 - int(angle * 180 / math.pi) 24 | 25 | 26 | class GameAction: 27 | def __init__(self, ctrl: GameControl): 28 | self.ctrl = ctrl 29 | self.yolo = YoloV5s(target_size=640, 30 | prob_threshold=0.25, 31 | nms_threshold=0.45, 32 | num_threads=4, 33 | use_gpu=True) 34 | self.adb = self.ctrl.adb 35 | 36 | def mov_to_next_room(self): 37 | t = time.time() 38 | mov_start = False 39 | while True: 40 | time.sleep(0.1) 41 | screen = self.ctrl.adb.last_screen 42 | if screen is None: 43 | continue 44 | 45 | ada_image = cv.adaptiveThreshold(cv.cvtColor(screen, cv.COLOR_BGR2GRAY), 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 13, 3) 46 | cv.imshow('ada_image', ada_image) 47 | cv.waitKey(1) 48 | if np.sum(ada_image) == 0: 49 | print('过图成功') 50 | self.adb.touch_end(0, 0) 51 | return 52 | 53 | result = self.yolo(screen) 54 | for obj in result: 55 | color = (0, 255, 0) 56 | if obj.label == 1: 57 | color = (255, 0, 0) 58 | elif obj.label == 5: 59 | color = (0, 0, 255) 60 | cv.rectangle(screen, 61 | (int(obj.rect.x), int(obj.rect.y)), 62 | (int(obj.rect.x + obj.rect.w), int(obj.rect.y + + obj.rect.h)), 63 | color, 2 64 | ) 65 | # print(obj) 66 | 67 | hero = [x for x in result if x.label == 0.0] 68 | if len(hero) == 0: 69 | print('没有找到英雄') 70 | hero = None 71 | continue 72 | else: 73 | hero = hero[0] 74 | hx, hy = get_detect_obj_bottom(hero) 75 | cv.circle(screen, (hx, hy), 5, (0, 0, 125), 5) 76 | 77 | arrow = [x for x in result if x.label == 5] 78 | if len(arrow) == 0: 79 | continue 80 | min_distance_arrow = min(arrow, key=lambda a: distance_detect_object(hero, a)) 81 | 82 | ax, ay = get_detect_obj_bottom(min_distance_arrow) 83 | cv.circle(screen, (hx, hy), 5, (0, 255, 0), 5) 84 | cv.arrowedLine(screen, (hx, hy), (ax, ay), (255, 0, 0), 3) 85 | angle = calc_angle(hx, hy, ax, ay) 86 | sx, sy = self.ctrl.calc_mov_point(angle) 87 | 88 | if not mov_start: 89 | self.adb.touch_start(sx, sy) 90 | mov_start = True 91 | else: 92 | self.adb.touch_move(sx, sy) 93 | 94 | cv.imshow('screen', screen) 95 | cv.waitKey(1) 96 | 97 | 98 | if __name__ == '__main__': 99 | ctrl = GameControl(ScrcpyADB()) 100 | action = GameAction(ctrl) 101 | while True: 102 | action.mov_to_next_room() 103 | time.sleep(3) 104 | -------------------------------------------------------------------------------- /game/game_control.py: -------------------------------------------------------------------------------- 1 | import time 2 | from typing import Tuple 3 | 4 | from adb.scrcpy_adb import ScrcpyADB 5 | import math 6 | 7 | 8 | class GameControl: 9 | def __init__(self, adb: ScrcpyADB): 10 | self.adb = adb 11 | 12 | def calc_mov_point(self, angle: float) -> Tuple[int, int]: 13 | rx, ry = (205, 520) 14 | r = 100 15 | 16 | x = rx + r * math.cos(angle * math.pi / 180) 17 | y = ry - r * math.sin(angle * math.pi / 180) 18 | return int(x), int(y) 19 | 20 | def move(self, angle: float, t: float): 21 | # 计算轮盘x, y坐标 22 | x, y = self.calc_mov_point(angle) 23 | self.adb.touch_start(x, y) 24 | time.sleep(t) 25 | self.adb.touch_end(x, y) 26 | 27 | def attack(self, t: float = 0.01): 28 | x, y = (1142, 649) 29 | self.adb.touch_start(x, y) 30 | time.sleep(t) 31 | self.adb.touch_end(x, y) 32 | 33 | 34 | if __name__ == '__main__': 35 | ctl = GameControl(ScrcpyADB()) 36 | ctl.move(180, 3) 37 | time.sleep(0.3) 38 | ctl.attack() 39 | time.sleep(0.3) 40 | ctl.move(270, 5) 41 | time.sleep(0.3) 42 | ctl.attack(3) 43 | 44 | 45 | -------------------------------------------------------------------------------- /img_collection.py: -------------------------------------------------------------------------------- 1 | from adb.scrcpy_adb import ScrcpyADB 2 | import time 3 | import cv2 as cv 4 | 5 | # 1280 * 720 dpi 320 6 | if __name__ == '__main__': 7 | adb = ScrcpyADB() 8 | 9 | index = 56 10 | 11 | while True: 12 | index += 1 13 | time.sleep(2) 14 | screen = adb.last_screen 15 | cv.imwrite(f'img/{index}.png', screen) 16 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from ncnn.utils.objects import Detect_Object 2 | -------------------------------------------------------------------------------- /ncnn.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sr173/dnfm-yolo-tutorial/d3c6969cfd1882a1035a42ab3723152dd7edfcf5/ncnn.bin -------------------------------------------------------------------------------- /ncnn.param: -------------------------------------------------------------------------------- 1 | 7767517 2 | 173 197 3 | Input images 0 1 images 4 | Convolution /model.0/conv/Conv 1 1 images /model.0/conv/Conv_output_0 0=32 1=6 11=6 2=1 12=1 3=2 13=2 4=2 14=2 15=2 16=2 5=1 6=3456 5 | Swish /model.0/act/Mul 1 1 /model.0/conv/Conv_output_0 /model.0/act/Mul_output_0 6 | Convolution /model.1/conv/Conv 1 1 /model.0/act/Mul_output_0 /model.1/conv/Conv_output_0 0=64 1=3 11=3 2=1 12=1 3=2 13=2 4=1 14=1 15=1 16=1 5=1 6=18432 7 | Swish /model.1/act/Mul 1 1 /model.1/conv/Conv_output_0 /model.1/act/Mul_output_0 8 | Split splitncnn_0 1 2 /model.1/act/Mul_output_0 /model.1/act/Mul_output_0_splitncnn_0 /model.1/act/Mul_output_0_splitncnn_1 9 | Convolution /model.2/cv1/conv/Conv 1 1 /model.1/act/Mul_output_0_splitncnn_1 /model.2/cv1/conv/Conv_output_0 0=32 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=2048 10 | Swish /model.2/cv1/act/Mul 1 1 /model.2/cv1/conv/Conv_output_0 /model.2/cv1/act/Mul_output_0 11 | Split splitncnn_1 1 2 /model.2/cv1/act/Mul_output_0 /model.2/cv1/act/Mul_output_0_splitncnn_0 /model.2/cv1/act/Mul_output_0_splitncnn_1 12 | Convolution /model.2/m/m.0/cv1/conv/Conv 1 1 /model.2/cv1/act/Mul_output_0_splitncnn_1 /model.2/m/m.0/cv1/conv/Conv_output_0 0=32 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=1024 13 | Swish /model.2/m/m.0/cv1/act/Mul 1 1 /model.2/m/m.0/cv1/conv/Conv_output_0 /model.2/m/m.0/cv1/act/Mul_output_0 14 | Convolution /model.2/m/m.0/cv2/conv/Conv 1 1 /model.2/m/m.0/cv1/act/Mul_output_0 /model.2/m/m.0/cv2/conv/Conv_output_0 0=32 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=9216 15 | Swish /model.2/m/m.0/cv2/act/Mul 1 1 /model.2/m/m.0/cv2/conv/Conv_output_0 /model.2/m/m.0/cv2/act/Mul_output_0 16 | BinaryOp /model.2/m/m.0/Add 2 1 /model.2/cv1/act/Mul_output_0_splitncnn_0 /model.2/m/m.0/cv2/act/Mul_output_0 /model.2/m/m.0/Add_output_0 0=0 17 | Convolution /model.2/cv2/conv/Conv 1 1 /model.1/act/Mul_output_0_splitncnn_0 /model.2/cv2/conv/Conv_output_0 0=32 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=2048 18 | Swish /model.2/cv2/act/Mul 1 1 /model.2/cv2/conv/Conv_output_0 /model.2/cv2/act/Mul_output_0 19 | Concat /model.2/Concat 2 1 /model.2/m/m.0/Add_output_0 /model.2/cv2/act/Mul_output_0 /model.2/Concat_output_0 0=0 20 | Convolution /model.2/cv3/conv/Conv 1 1 /model.2/Concat_output_0 /model.2/cv3/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=4096 21 | Swish /model.2/cv3/act/Mul 1 1 /model.2/cv3/conv/Conv_output_0 /model.2/cv3/act/Mul_output_0 22 | Convolution /model.3/conv/Conv 1 1 /model.2/cv3/act/Mul_output_0 /model.3/conv/Conv_output_0 0=128 1=3 11=3 2=1 12=1 3=2 13=2 4=1 14=1 15=1 16=1 5=1 6=73728 23 | Swish /model.3/act/Mul 1 1 /model.3/conv/Conv_output_0 /model.3/act/Mul_output_0 24 | Split splitncnn_2 1 2 /model.3/act/Mul_output_0 /model.3/act/Mul_output_0_splitncnn_0 /model.3/act/Mul_output_0_splitncnn_1 25 | Convolution /model.4/cv1/conv/Conv 1 1 /model.3/act/Mul_output_0_splitncnn_1 /model.4/cv1/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=8192 26 | Swish /model.4/cv1/act/Mul 1 1 /model.4/cv1/conv/Conv_output_0 /model.4/cv1/act/Mul_output_0 27 | Split splitncnn_3 1 2 /model.4/cv1/act/Mul_output_0 /model.4/cv1/act/Mul_output_0_splitncnn_0 /model.4/cv1/act/Mul_output_0_splitncnn_1 28 | Convolution /model.4/m/m.0/cv1/conv/Conv 1 1 /model.4/cv1/act/Mul_output_0_splitncnn_1 /model.4/m/m.0/cv1/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=4096 29 | Swish /model.4/m/m.0/cv1/act/Mul 1 1 /model.4/m/m.0/cv1/conv/Conv_output_0 /model.4/m/m.0/cv1/act/Mul_output_0 30 | Convolution /model.4/m/m.0/cv2/conv/Conv 1 1 /model.4/m/m.0/cv1/act/Mul_output_0 /model.4/m/m.0/cv2/conv/Conv_output_0 0=64 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=36864 31 | Swish /model.4/m/m.0/cv2/act/Mul 1 1 /model.4/m/m.0/cv2/conv/Conv_output_0 /model.4/m/m.0/cv2/act/Mul_output_0 32 | BinaryOp /model.4/m/m.0/Add 2 1 /model.4/cv1/act/Mul_output_0_splitncnn_0 /model.4/m/m.0/cv2/act/Mul_output_0 /model.4/m/m.0/Add_output_0 0=0 33 | Split splitncnn_4 1 2 /model.4/m/m.0/Add_output_0 /model.4/m/m.0/Add_output_0_splitncnn_0 /model.4/m/m.0/Add_output_0_splitncnn_1 34 | Convolution /model.4/m/m.1/cv1/conv/Conv 1 1 /model.4/m/m.0/Add_output_0_splitncnn_1 /model.4/m/m.1/cv1/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=4096 35 | Swish /model.4/m/m.1/cv1/act/Mul 1 1 /model.4/m/m.1/cv1/conv/Conv_output_0 /model.4/m/m.1/cv1/act/Mul_output_0 36 | Convolution /model.4/m/m.1/cv2/conv/Conv 1 1 /model.4/m/m.1/cv1/act/Mul_output_0 /model.4/m/m.1/cv2/conv/Conv_output_0 0=64 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=36864 37 | Swish /model.4/m/m.1/cv2/act/Mul 1 1 /model.4/m/m.1/cv2/conv/Conv_output_0 /model.4/m/m.1/cv2/act/Mul_output_0 38 | BinaryOp /model.4/m/m.1/Add 2 1 /model.4/m/m.0/Add_output_0_splitncnn_0 /model.4/m/m.1/cv2/act/Mul_output_0 /model.4/m/m.1/Add_output_0 0=0 39 | Convolution /model.4/cv2/conv/Conv 1 1 /model.3/act/Mul_output_0_splitncnn_0 /model.4/cv2/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=8192 40 | Swish /model.4/cv2/act/Mul 1 1 /model.4/cv2/conv/Conv_output_0 /model.4/cv2/act/Mul_output_0 41 | Concat /model.4/Concat 2 1 /model.4/m/m.1/Add_output_0 /model.4/cv2/act/Mul_output_0 /model.4/Concat_output_0 0=0 42 | Convolution /model.4/cv3/conv/Conv 1 1 /model.4/Concat_output_0 /model.4/cv3/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 43 | Swish /model.4/cv3/act/Mul 1 1 /model.4/cv3/conv/Conv_output_0 /model.4/cv3/act/Mul_output_0 44 | Split splitncnn_5 1 2 /model.4/cv3/act/Mul_output_0 /model.4/cv3/act/Mul_output_0_splitncnn_0 /model.4/cv3/act/Mul_output_0_splitncnn_1 45 | Convolution /model.5/conv/Conv 1 1 /model.4/cv3/act/Mul_output_0_splitncnn_1 /model.5/conv/Conv_output_0 0=256 1=3 11=3 2=1 12=1 3=2 13=2 4=1 14=1 15=1 16=1 5=1 6=294912 46 | Swish /model.5/act/Mul 1 1 /model.5/conv/Conv_output_0 /model.5/act/Mul_output_0 47 | Split splitncnn_6 1 2 /model.5/act/Mul_output_0 /model.5/act/Mul_output_0_splitncnn_0 /model.5/act/Mul_output_0_splitncnn_1 48 | Convolution /model.6/cv1/conv/Conv 1 1 /model.5/act/Mul_output_0_splitncnn_1 /model.6/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=32768 49 | Swish /model.6/cv1/act/Mul 1 1 /model.6/cv1/conv/Conv_output_0 /model.6/cv1/act/Mul_output_0 50 | Split splitncnn_7 1 2 /model.6/cv1/act/Mul_output_0 /model.6/cv1/act/Mul_output_0_splitncnn_0 /model.6/cv1/act/Mul_output_0_splitncnn_1 51 | Convolution /model.6/m/m.0/cv1/conv/Conv 1 1 /model.6/cv1/act/Mul_output_0_splitncnn_1 /model.6/m/m.0/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 52 | Swish /model.6/m/m.0/cv1/act/Mul 1 1 /model.6/m/m.0/cv1/conv/Conv_output_0 /model.6/m/m.0/cv1/act/Mul_output_0 53 | Convolution /model.6/m/m.0/cv2/conv/Conv 1 1 /model.6/m/m.0/cv1/act/Mul_output_0 /model.6/m/m.0/cv2/conv/Conv_output_0 0=128 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=147456 54 | Swish /model.6/m/m.0/cv2/act/Mul 1 1 /model.6/m/m.0/cv2/conv/Conv_output_0 /model.6/m/m.0/cv2/act/Mul_output_0 55 | BinaryOp /model.6/m/m.0/Add 2 1 /model.6/cv1/act/Mul_output_0_splitncnn_0 /model.6/m/m.0/cv2/act/Mul_output_0 /model.6/m/m.0/Add_output_0 0=0 56 | Split splitncnn_8 1 2 /model.6/m/m.0/Add_output_0 /model.6/m/m.0/Add_output_0_splitncnn_0 /model.6/m/m.0/Add_output_0_splitncnn_1 57 | Convolution /model.6/m/m.1/cv1/conv/Conv 1 1 /model.6/m/m.0/Add_output_0_splitncnn_1 /model.6/m/m.1/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 58 | Swish /model.6/m/m.1/cv1/act/Mul 1 1 /model.6/m/m.1/cv1/conv/Conv_output_0 /model.6/m/m.1/cv1/act/Mul_output_0 59 | Convolution /model.6/m/m.1/cv2/conv/Conv 1 1 /model.6/m/m.1/cv1/act/Mul_output_0 /model.6/m/m.1/cv2/conv/Conv_output_0 0=128 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=147456 60 | Swish /model.6/m/m.1/cv2/act/Mul 1 1 /model.6/m/m.1/cv2/conv/Conv_output_0 /model.6/m/m.1/cv2/act/Mul_output_0 61 | BinaryOp /model.6/m/m.1/Add 2 1 /model.6/m/m.0/Add_output_0_splitncnn_0 /model.6/m/m.1/cv2/act/Mul_output_0 /model.6/m/m.1/Add_output_0 0=0 62 | Split splitncnn_9 1 2 /model.6/m/m.1/Add_output_0 /model.6/m/m.1/Add_output_0_splitncnn_0 /model.6/m/m.1/Add_output_0_splitncnn_1 63 | Convolution /model.6/m/m.2/cv1/conv/Conv 1 1 /model.6/m/m.1/Add_output_0_splitncnn_1 /model.6/m/m.2/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 64 | Swish /model.6/m/m.2/cv1/act/Mul 1 1 /model.6/m/m.2/cv1/conv/Conv_output_0 /model.6/m/m.2/cv1/act/Mul_output_0 65 | Convolution /model.6/m/m.2/cv2/conv/Conv 1 1 /model.6/m/m.2/cv1/act/Mul_output_0 /model.6/m/m.2/cv2/conv/Conv_output_0 0=128 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=147456 66 | Swish /model.6/m/m.2/cv2/act/Mul 1 1 /model.6/m/m.2/cv2/conv/Conv_output_0 /model.6/m/m.2/cv2/act/Mul_output_0 67 | BinaryOp /model.6/m/m.2/Add 2 1 /model.6/m/m.1/Add_output_0_splitncnn_0 /model.6/m/m.2/cv2/act/Mul_output_0 /model.6/m/m.2/Add_output_0 0=0 68 | Convolution /model.6/cv2/conv/Conv 1 1 /model.5/act/Mul_output_0_splitncnn_0 /model.6/cv2/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=32768 69 | Swish /model.6/cv2/act/Mul 1 1 /model.6/cv2/conv/Conv_output_0 /model.6/cv2/act/Mul_output_0 70 | Concat /model.6/Concat 2 1 /model.6/m/m.2/Add_output_0 /model.6/cv2/act/Mul_output_0 /model.6/Concat_output_0 0=0 71 | Convolution /model.6/cv3/conv/Conv 1 1 /model.6/Concat_output_0 /model.6/cv3/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=65536 72 | Swish /model.6/cv3/act/Mul 1 1 /model.6/cv3/conv/Conv_output_0 /model.6/cv3/act/Mul_output_0 73 | Split splitncnn_10 1 2 /model.6/cv3/act/Mul_output_0 /model.6/cv3/act/Mul_output_0_splitncnn_0 /model.6/cv3/act/Mul_output_0_splitncnn_1 74 | Convolution /model.7/conv/Conv 1 1 /model.6/cv3/act/Mul_output_0_splitncnn_1 /model.7/conv/Conv_output_0 0=512 1=3 11=3 2=1 12=1 3=2 13=2 4=1 14=1 15=1 16=1 5=1 6=1179648 75 | Swish /model.7/act/Mul 1 1 /model.7/conv/Conv_output_0 /model.7/act/Mul_output_0 76 | Split splitncnn_11 1 2 /model.7/act/Mul_output_0 /model.7/act/Mul_output_0_splitncnn_0 /model.7/act/Mul_output_0_splitncnn_1 77 | Convolution /model.8/cv1/conv/Conv 1 1 /model.7/act/Mul_output_0_splitncnn_1 /model.8/cv1/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=131072 78 | Swish /model.8/cv1/act/Mul 1 1 /model.8/cv1/conv/Conv_output_0 /model.8/cv1/act/Mul_output_0 79 | Split splitncnn_12 1 2 /model.8/cv1/act/Mul_output_0 /model.8/cv1/act/Mul_output_0_splitncnn_0 /model.8/cv1/act/Mul_output_0_splitncnn_1 80 | Convolution /model.8/m/m.0/cv1/conv/Conv 1 1 /model.8/cv1/act/Mul_output_0_splitncnn_1 /model.8/m/m.0/cv1/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=65536 81 | Swish /model.8/m/m.0/cv1/act/Mul 1 1 /model.8/m/m.0/cv1/conv/Conv_output_0 /model.8/m/m.0/cv1/act/Mul_output_0 82 | Convolution /model.8/m/m.0/cv2/conv/Conv 1 1 /model.8/m/m.0/cv1/act/Mul_output_0 /model.8/m/m.0/cv2/conv/Conv_output_0 0=256 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=589824 83 | Swish /model.8/m/m.0/cv2/act/Mul 1 1 /model.8/m/m.0/cv2/conv/Conv_output_0 /model.8/m/m.0/cv2/act/Mul_output_0 84 | BinaryOp /model.8/m/m.0/Add 2 1 /model.8/cv1/act/Mul_output_0_splitncnn_0 /model.8/m/m.0/cv2/act/Mul_output_0 /model.8/m/m.0/Add_output_0 0=0 85 | Convolution /model.8/cv2/conv/Conv 1 1 /model.7/act/Mul_output_0_splitncnn_0 /model.8/cv2/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=131072 86 | Swish /model.8/cv2/act/Mul 1 1 /model.8/cv2/conv/Conv_output_0 /model.8/cv2/act/Mul_output_0 87 | Concat /model.8/Concat 2 1 /model.8/m/m.0/Add_output_0 /model.8/cv2/act/Mul_output_0 /model.8/Concat_output_0 0=0 88 | Convolution /model.8/cv3/conv/Conv 1 1 /model.8/Concat_output_0 /model.8/cv3/conv/Conv_output_0 0=512 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=262144 89 | Swish /model.8/cv3/act/Mul 1 1 /model.8/cv3/conv/Conv_output_0 /model.8/cv3/act/Mul_output_0 90 | Convolution /model.9/cv1/conv/Conv 1 1 /model.8/cv3/act/Mul_output_0 /model.9/cv1/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=131072 91 | Swish /model.9/cv1/act/Mul 1 1 /model.9/cv1/conv/Conv_output_0 /model.9/cv1/act/Mul_output_0 92 | Split splitncnn_13 1 2 /model.9/cv1/act/Mul_output_0 /model.9/cv1/act/Mul_output_0_splitncnn_0 /model.9/cv1/act/Mul_output_0_splitncnn_1 93 | Pooling /model.9/m/MaxPool 1 1 /model.9/cv1/act/Mul_output_0_splitncnn_1 /model.9/m/MaxPool_output_0 0=0 1=5 11=5 2=1 12=1 3=2 13=2 14=2 15=2 5=1 94 | Split splitncnn_14 1 2 /model.9/m/MaxPool_output_0 /model.9/m/MaxPool_output_0_splitncnn_0 /model.9/m/MaxPool_output_0_splitncnn_1 95 | Pooling /model.9/m_1/MaxPool 1 1 /model.9/m/MaxPool_output_0_splitncnn_1 /model.9/m_1/MaxPool_output_0 0=0 1=5 11=5 2=1 12=1 3=2 13=2 14=2 15=2 5=1 96 | Split splitncnn_15 1 2 /model.9/m_1/MaxPool_output_0 /model.9/m_1/MaxPool_output_0_splitncnn_0 /model.9/m_1/MaxPool_output_0_splitncnn_1 97 | Pooling /model.9/m_2/MaxPool 1 1 /model.9/m_1/MaxPool_output_0_splitncnn_1 /model.9/m_2/MaxPool_output_0 0=0 1=5 11=5 2=1 12=1 3=2 13=2 14=2 15=2 5=1 98 | Concat /model.9/Concat 4 1 /model.9/cv1/act/Mul_output_0_splitncnn_0 /model.9/m/MaxPool_output_0_splitncnn_0 /model.9/m_1/MaxPool_output_0_splitncnn_0 /model.9/m_2/MaxPool_output_0 /model.9/Concat_output_0 0=0 99 | Convolution /model.9/cv2/conv/Conv 1 1 /model.9/Concat_output_0 /model.9/cv2/conv/Conv_output_0 0=512 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=524288 100 | Swish /model.9/cv2/act/Mul 1 1 /model.9/cv2/conv/Conv_output_0 /model.9/cv2/act/Mul_output_0 101 | Convolution /model.10/conv/Conv 1 1 /model.9/cv2/act/Mul_output_0 /model.10/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=131072 102 | Swish /model.10/act/Mul 1 1 /model.10/conv/Conv_output_0 /model.10/act/Mul_output_0 103 | Split splitncnn_16 1 2 /model.10/act/Mul_output_0 /model.10/act/Mul_output_0_splitncnn_0 /model.10/act/Mul_output_0_splitncnn_1 104 | Interp /model.11/Resize 1 1 /model.10/act/Mul_output_0_splitncnn_1 /model.11/Resize_output_0 0=1 1=2.000000e+00 2=2.000000e+00 3=0 4=0 6=0 105 | Concat /model.12/Concat 2 1 /model.11/Resize_output_0 /model.6/cv3/act/Mul_output_0_splitncnn_0 /model.12/Concat_output_0 0=0 106 | Split splitncnn_17 1 2 /model.12/Concat_output_0 /model.12/Concat_output_0_splitncnn_0 /model.12/Concat_output_0_splitncnn_1 107 | Convolution /model.13/cv1/conv/Conv 1 1 /model.12/Concat_output_0_splitncnn_1 /model.13/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=65536 108 | Swish /model.13/cv1/act/Mul 1 1 /model.13/cv1/conv/Conv_output_0 /model.13/cv1/act/Mul_output_0 109 | Convolution /model.13/m/m.0/cv1/conv/Conv 1 1 /model.13/cv1/act/Mul_output_0 /model.13/m/m.0/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 110 | Swish /model.13/m/m.0/cv1/act/Mul 1 1 /model.13/m/m.0/cv1/conv/Conv_output_0 /model.13/m/m.0/cv1/act/Mul_output_0 111 | Convolution /model.13/m/m.0/cv2/conv/Conv 1 1 /model.13/m/m.0/cv1/act/Mul_output_0 /model.13/m/m.0/cv2/conv/Conv_output_0 0=128 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=147456 112 | Swish /model.13/m/m.0/cv2/act/Mul 1 1 /model.13/m/m.0/cv2/conv/Conv_output_0 /model.13/m/m.0/cv2/act/Mul_output_0 113 | Convolution /model.13/cv2/conv/Conv 1 1 /model.12/Concat_output_0_splitncnn_0 /model.13/cv2/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=65536 114 | Swish /model.13/cv2/act/Mul 1 1 /model.13/cv2/conv/Conv_output_0 /model.13/cv2/act/Mul_output_0 115 | Concat /model.13/Concat 2 1 /model.13/m/m.0/cv2/act/Mul_output_0 /model.13/cv2/act/Mul_output_0 /model.13/Concat_output_0 0=0 116 | Convolution /model.13/cv3/conv/Conv 1 1 /model.13/Concat_output_0 /model.13/cv3/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=65536 117 | Swish /model.13/cv3/act/Mul 1 1 /model.13/cv3/conv/Conv_output_0 /model.13/cv3/act/Mul_output_0 118 | Convolution /model.14/conv/Conv 1 1 /model.13/cv3/act/Mul_output_0 /model.14/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=32768 119 | Swish /model.14/act/Mul 1 1 /model.14/conv/Conv_output_0 /model.14/act/Mul_output_0 120 | Split splitncnn_18 1 2 /model.14/act/Mul_output_0 /model.14/act/Mul_output_0_splitncnn_0 /model.14/act/Mul_output_0_splitncnn_1 121 | Interp /model.15/Resize 1 1 /model.14/act/Mul_output_0_splitncnn_1 /model.15/Resize_output_0 0=1 1=2.000000e+00 2=2.000000e+00 3=0 4=0 6=0 122 | Concat /model.16/Concat 2 1 /model.15/Resize_output_0 /model.4/cv3/act/Mul_output_0_splitncnn_0 /model.16/Concat_output_0 0=0 123 | Split splitncnn_19 1 2 /model.16/Concat_output_0 /model.16/Concat_output_0_splitncnn_0 /model.16/Concat_output_0_splitncnn_1 124 | Convolution /model.17/cv1/conv/Conv 1 1 /model.16/Concat_output_0_splitncnn_1 /model.17/cv1/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 125 | Swish /model.17/cv1/act/Mul 1 1 /model.17/cv1/conv/Conv_output_0 /model.17/cv1/act/Mul_output_0 126 | Convolution /model.17/m/m.0/cv1/conv/Conv 1 1 /model.17/cv1/act/Mul_output_0 /model.17/m/m.0/cv1/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=4096 127 | Swish /model.17/m/m.0/cv1/act/Mul 1 1 /model.17/m/m.0/cv1/conv/Conv_output_0 /model.17/m/m.0/cv1/act/Mul_output_0 128 | Convolution /model.17/m/m.0/cv2/conv/Conv 1 1 /model.17/m/m.0/cv1/act/Mul_output_0 /model.17/m/m.0/cv2/conv/Conv_output_0 0=64 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=36864 129 | Swish /model.17/m/m.0/cv2/act/Mul 1 1 /model.17/m/m.0/cv2/conv/Conv_output_0 /model.17/m/m.0/cv2/act/Mul_output_0 130 | Convolution /model.17/cv2/conv/Conv 1 1 /model.16/Concat_output_0_splitncnn_0 /model.17/cv2/conv/Conv_output_0 0=64 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 131 | Swish /model.17/cv2/act/Mul 1 1 /model.17/cv2/conv/Conv_output_0 /model.17/cv2/act/Mul_output_0 132 | Concat /model.17/Concat 2 1 /model.17/m/m.0/cv2/act/Mul_output_0 /model.17/cv2/act/Mul_output_0 /model.17/Concat_output_0 0=0 133 | Convolution /model.17/cv3/conv/Conv 1 1 /model.17/Concat_output_0 /model.17/cv3/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 134 | Swish /model.17/cv3/act/Mul 1 1 /model.17/cv3/conv/Conv_output_0 /model.17/cv3/act/Mul_output_0 135 | Split splitncnn_20 1 2 /model.17/cv3/act/Mul_output_0 /model.17/cv3/act/Mul_output_0_splitncnn_0 /model.17/cv3/act/Mul_output_0_splitncnn_1 136 | Convolution /model.18/conv/Conv 1 1 /model.17/cv3/act/Mul_output_0_splitncnn_1 /model.18/conv/Conv_output_0 0=128 1=3 11=3 2=1 12=1 3=2 13=2 4=1 14=1 15=1 16=1 5=1 6=147456 137 | Swish /model.18/act/Mul 1 1 /model.18/conv/Conv_output_0 /model.18/act/Mul_output_0 138 | Concat /model.19/Concat 2 1 /model.18/act/Mul_output_0 /model.14/act/Mul_output_0_splitncnn_0 /model.19/Concat_output_0 0=0 139 | Split splitncnn_21 1 2 /model.19/Concat_output_0 /model.19/Concat_output_0_splitncnn_0 /model.19/Concat_output_0_splitncnn_1 140 | Convolution /model.20/cv1/conv/Conv 1 1 /model.19/Concat_output_0_splitncnn_1 /model.20/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=32768 141 | Swish /model.20/cv1/act/Mul 1 1 /model.20/cv1/conv/Conv_output_0 /model.20/cv1/act/Mul_output_0 142 | Convolution /model.20/m/m.0/cv1/conv/Conv 1 1 /model.20/cv1/act/Mul_output_0 /model.20/m/m.0/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=16384 143 | Swish /model.20/m/m.0/cv1/act/Mul 1 1 /model.20/m/m.0/cv1/conv/Conv_output_0 /model.20/m/m.0/cv1/act/Mul_output_0 144 | Convolution /model.20/m/m.0/cv2/conv/Conv 1 1 /model.20/m/m.0/cv1/act/Mul_output_0 /model.20/m/m.0/cv2/conv/Conv_output_0 0=128 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=147456 145 | Swish /model.20/m/m.0/cv2/act/Mul 1 1 /model.20/m/m.0/cv2/conv/Conv_output_0 /model.20/m/m.0/cv2/act/Mul_output_0 146 | Convolution /model.20/cv2/conv/Conv 1 1 /model.19/Concat_output_0_splitncnn_0 /model.20/cv2/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=32768 147 | Swish /model.20/cv2/act/Mul 1 1 /model.20/cv2/conv/Conv_output_0 /model.20/cv2/act/Mul_output_0 148 | Concat /model.20/Concat 2 1 /model.20/m/m.0/cv2/act/Mul_output_0 /model.20/cv2/act/Mul_output_0 /model.20/Concat_output_0 0=0 149 | Convolution /model.20/cv3/conv/Conv 1 1 /model.20/Concat_output_0 /model.20/cv3/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=65536 150 | Swish /model.20/cv3/act/Mul 1 1 /model.20/cv3/conv/Conv_output_0 /model.20/cv3/act/Mul_output_0 151 | Split splitncnn_22 1 2 /model.20/cv3/act/Mul_output_0 /model.20/cv3/act/Mul_output_0_splitncnn_0 /model.20/cv3/act/Mul_output_0_splitncnn_1 152 | Convolution /model.21/conv/Conv 1 1 /model.20/cv3/act/Mul_output_0_splitncnn_1 /model.21/conv/Conv_output_0 0=256 1=3 11=3 2=1 12=1 3=2 13=2 4=1 14=1 15=1 16=1 5=1 6=589824 153 | Swish /model.21/act/Mul 1 1 /model.21/conv/Conv_output_0 /model.21/act/Mul_output_0 154 | Concat /model.22/Concat 2 1 /model.21/act/Mul_output_0 /model.10/act/Mul_output_0_splitncnn_0 /model.22/Concat_output_0 0=0 155 | Split splitncnn_23 1 2 /model.22/Concat_output_0 /model.22/Concat_output_0_splitncnn_0 /model.22/Concat_output_0_splitncnn_1 156 | Convolution /model.23/cv1/conv/Conv 1 1 /model.22/Concat_output_0_splitncnn_1 /model.23/cv1/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=131072 157 | Swish /model.23/cv1/act/Mul 1 1 /model.23/cv1/conv/Conv_output_0 /model.23/cv1/act/Mul_output_0 158 | Convolution /model.23/m/m.0/cv1/conv/Conv 1 1 /model.23/cv1/act/Mul_output_0 /model.23/m/m.0/cv1/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=65536 159 | Swish /model.23/m/m.0/cv1/act/Mul 1 1 /model.23/m/m.0/cv1/conv/Conv_output_0 /model.23/m/m.0/cv1/act/Mul_output_0 160 | Convolution /model.23/m/m.0/cv2/conv/Conv 1 1 /model.23/m/m.0/cv1/act/Mul_output_0 /model.23/m/m.0/cv2/conv/Conv_output_0 0=256 1=3 11=3 2=1 12=1 3=1 13=1 4=1 14=1 15=1 16=1 5=1 6=589824 161 | Swish /model.23/m/m.0/cv2/act/Mul 1 1 /model.23/m/m.0/cv2/conv/Conv_output_0 /model.23/m/m.0/cv2/act/Mul_output_0 162 | Convolution /model.23/cv2/conv/Conv 1 1 /model.22/Concat_output_0_splitncnn_0 /model.23/cv2/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=131072 163 | Swish /model.23/cv2/act/Mul 1 1 /model.23/cv2/conv/Conv_output_0 /model.23/cv2/act/Mul_output_0 164 | Concat /model.23/Concat 2 1 /model.23/m/m.0/cv2/act/Mul_output_0 /model.23/cv2/act/Mul_output_0 /model.23/Concat_output_0 0=0 165 | Convolution /model.23/cv3/conv/Conv 1 1 /model.23/Concat_output_0 /model.23/cv3/conv/Conv_output_0 0=512 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=262144 166 | Swish /model.23/cv3/act/Mul 1 1 /model.23/cv3/conv/Conv_output_0 /model.23/cv3/act/Mul_output_0 167 | Convolution /model.24/m.0/Conv 1 1 /model.17/cv3/act/Mul_output_0_splitncnn_0 /model.24/m.0/Conv_output_0 0=39 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=4992 168 | Reshape /model.24/Reshape 1 1 /model.24/m.0/Conv_output_0 /model.24/Reshape_output_0 0=-1 1=13 2=3 169 | Permute /model.24/Transpose 1 1 /model.24/Reshape_output_0 output 0=1 170 | Convolution /model.24/m.1/Conv 1 1 /model.20/cv3/act/Mul_output_0_splitncnn_0 /model.24/m.1/Conv_output_0 0=39 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=9984 171 | Reshape /model.24/Reshape_1 1 1 /model.24/m.1/Conv_output_0 /model.24/Reshape_1_output_0 0=-1 1=13 2=3 172 | Permute /model.24/Transpose_1 1 1 /model.24/Reshape_1_output_0 354 0=1 173 | Convolution /model.24/m.2/Conv 1 1 /model.23/cv3/act/Mul_output_0 /model.24/m.2/Conv_output_0 0=39 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=19968 174 | Reshape /model.24/Reshape_2 1 1 /model.24/m.2/Conv_output_0 /model.24/Reshape_2_output_0 0=-1 1=13 2=3 175 | Permute /model.24/Transpose_2 1 1 /model.24/Reshape_2_output_0 366 0=1 176 | -------------------------------------------------------------------------------- /utils/yolov5.py: -------------------------------------------------------------------------------- 1 | # Tencent is pleased to support the open source community by making ncnn available. 2 | # 3 | # Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. 4 | # 5 | # Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 6 | # in compliance with the License. You may obtain a copy of the License at 7 | # 8 | # https://opensource.org/licenses/BSD-3-Clause 9 | # 10 | # Unless required by applicable law or agreed to in writing, software distributed 11 | # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | # specific language governing permissions and limitations under the License. 14 | 15 | import time 16 | import numpy as np 17 | import ncnn 18 | from ncnn.model_zoo.model_store import get_model_file 19 | from ncnn.utils.objects import Detect_Object 20 | from ncnn.utils.functional import * 21 | 22 | 23 | class YoloV5Focus(ncnn.Layer): 24 | yolov5FocusLayers = [] 25 | 26 | def __init__(self): 27 | ncnn.Layer.__init__(self) 28 | self.one_blob_only = True 29 | 30 | self.yolov5FocusLayers.append(self) 31 | 32 | def forward(self, bottom_blob, top_blob, opt): 33 | x = np.array(bottom_blob) 34 | x = np.concatenate( 35 | [ 36 | x[..., ::2, ::2], 37 | x[..., 1::2, ::2], 38 | x[..., ::2, 1::2], 39 | x[..., 1::2, 1::2], 40 | ] 41 | ) 42 | 43 | top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator) 44 | if top_blob.empty(): 45 | return -100 46 | 47 | return 0 48 | 49 | 50 | def YoloV5Focus_layer_creator(): 51 | return YoloV5Focus() 52 | 53 | 54 | def YoloV5Focus_layer_destroyer(layer): 55 | for i in range(len(YoloV5Focus.yolov5FocusLayers)): 56 | if YoloV5Focus.yolov5FocusLayers[i] == layer: 57 | del YoloV5Focus.yolov5FocusLayers[i] 58 | break 59 | 60 | 61 | class YoloV5s: 62 | def __init__( 63 | self, 64 | target_size=640, 65 | prob_threshold=0.25, 66 | nms_threshold=0.45, 67 | num_threads=1, 68 | use_gpu=False, 69 | ): 70 | self.target_size = target_size 71 | self.prob_threshold = prob_threshold 72 | self.nms_threshold = nms_threshold 73 | self.num_threads = num_threads 74 | self.use_gpu = use_gpu 75 | 76 | self.mean_vals = [] 77 | self.norm_vals = [1 / 255.0, 1 / 255.0, 1 / 255.0] 78 | 79 | self.net = ncnn.Net() 80 | self.net.opt.use_vulkan_compute = self.use_gpu 81 | self.net.opt.num_threads = self.num_threads 82 | 83 | self.net.register_custom_layer( 84 | "YoloV5Focus", YoloV5Focus_layer_creator, YoloV5Focus_layer_destroyer 85 | ) 86 | 87 | # original pretrained model from https://github.com/ultralytics/yolov5 88 | # the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models 89 | self.net.load_param('ncnn.param') 90 | self.net.load_model("ncnn.bin") 91 | 92 | self.grid = [make_grid(10, 6), make_grid(20, 12), make_grid(40, 24)] 93 | self.stride = np.array([32, 16, 8]) 94 | self.anchor_grid = np.array( 95 | [ 96 | [116, 90, 156, 198, 373, 326], 97 | [30, 61, 62, 45, 59, 119], 98 | [10, 13, 16, 30, 33, 23], 99 | ] 100 | ).reshape((3, 1, 3, 1, 1, 2)) 101 | 102 | self.class_names = [ 103 | "Hero", 104 | "Monster", 105 | "npc", 106 | "Gate", 107 | "Item", 108 | "Mark", 109 | "Monster_Fake", 110 | "Switch", 111 | ] 112 | 113 | def __del__(self): 114 | self.net = None 115 | 116 | def __call__(self, img): 117 | img_w = img.shape[1] 118 | img_h = img.shape[0] 119 | 120 | w = img_w 121 | h = img_h 122 | scale = 1.0 123 | if w > h: 124 | scale = float(self.target_size) / w 125 | w = self.target_size 126 | h = int(h * scale) 127 | else: 128 | scale = float(self.target_size) / h 129 | h = self.target_size 130 | w = int(w * scale) 131 | 132 | mat_in = ncnn.Mat.from_pixels_resize( 133 | img, ncnn.Mat.PixelType.PIXEL_BGR2RGB, img_w, img_h, w, h 134 | ) 135 | # pad to target_size rectangle 136 | # yolov5/utils/datasets.py letterbox 137 | wpad = (w + 31) // 32 * 32 - w 138 | hpad = (h + 31) // 32 * 32 - h 139 | mat_in_pad = ncnn.copy_make_border( 140 | mat_in, 141 | hpad // 2, 142 | hpad - hpad // 2, 143 | wpad // 2, 144 | wpad - wpad // 2, 145 | ncnn.BorderType.BORDER_CONSTANT, 146 | 114.0, 147 | ) 148 | 149 | mat_in_pad.substract_mean_normalize(self.mean_vals, self.norm_vals) 150 | 151 | ex = self.net.create_extractor() 152 | ex.input("images", mat_in_pad) 153 | 154 | # 改动部分 Permute 155 | # anchor setting from yolov5/models/yolov5s.yaml 156 | ret1, mat_out1 = ex.extract("output") # stride 8 157 | ret2, mat_out2 = ex.extract("354") # stride 16 158 | ret3, mat_out3 = ex.extract("366") # stride 32 159 | 160 | pred = [np.array(mat_out3), np.array(mat_out2), np.array(mat_out1)] 161 | z = [] 162 | for i in range(len(pred)): 163 | num_grid = pred[i].shape[1] 164 | if mat_in_pad.w > mat_in_pad.h: 165 | num_grid_x = mat_in_pad.w // self.stride[i] 166 | num_grid_y = num_grid // num_grid_x 167 | else: 168 | num_grid_y = mat_in_pad.h // self.stride[i] 169 | num_grid_x = num_grid // num_grid_y 170 | if ( 171 | self.grid[i].shape[0] != num_grid_x 172 | or self.grid[i].shape[1] != num_grid_y 173 | ): 174 | self.grid[i] = make_grid(num_grid_x, num_grid_y) 175 | 176 | y = sigmoid(pred[i]) 177 | y = y.reshape(pred[i].shape[0], num_grid_y, num_grid_x, pred[i].shape[2]) 178 | y[..., 0:2] = (y[..., 0:2] * 2.0 - 0.5 + self.grid[i]) * self.stride[ 179 | i 180 | ] # xy 181 | y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh 182 | z.append(y.reshape(1, -1, y.shape[-1])) 183 | pred = np.concatenate(z, 1) 184 | 185 | result = self.non_max_suppression( 186 | pred, self.prob_threshold, self.nms_threshold 187 | )[0] 188 | 189 | if result is None: 190 | return [] 191 | 192 | objects = [ 193 | Detect_Object( 194 | obj[5], 195 | obj[4], 196 | (obj[0] - (wpad / 2)) / scale, 197 | (obj[1] - (hpad / 2)) / scale, 198 | (obj[2] - obj[0]) / scale, 199 | (obj[3] - obj[1]) / scale, 200 | ) 201 | for obj in result 202 | ] 203 | 204 | return objects 205 | 206 | def non_max_suppression( 207 | self, 208 | prediction, 209 | conf_thres=0.1, 210 | iou_thres=0.6, 211 | merge=False, 212 | classes=None, 213 | agnostic=False, 214 | ): 215 | """Performs Non-Maximum Suppression (NMS) on inference results 216 | 217 | Returns: 218 | detections with shape: nx6 (x1, y1, x2, y2, conf, cls) 219 | """ 220 | nc = prediction[0].shape[1] - 5 # number of classes 221 | xc = prediction[..., 4] > conf_thres # candidates 222 | 223 | # Settings 224 | min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height 225 | max_det = 300 # maximum number of detections per image 226 | time_limit = 10.0 # seconds to quit after 227 | redundant = True # require redundant detections 228 | multi_label = nc > 1 # multiple labels per box (adds 0.5ms/img) 229 | 230 | t = time.time() 231 | output = [None] * prediction.shape[0] 232 | for xi, x in enumerate(prediction): # image index, image inference 233 | # Apply constraints 234 | # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height 235 | x = x[xc[xi]] # confidence 236 | 237 | # If none remain process next image 238 | if not x.shape[0]: 239 | continue 240 | 241 | # Compute conf 242 | x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf 243 | 244 | # Box (center x, center y, width, height) to (x1, y1, x2, y2) 245 | box = xywh2xyxy(x[:, :4]) 246 | 247 | # Detections matrix nx6 (xyxy, conf, cls) 248 | if multi_label: 249 | i, j = (x[:, 5:] > conf_thres).nonzero() 250 | x = np.concatenate( 251 | (box[i], x[i, j + 5, None], j[:, None].astype(np.float32)), axis=1 252 | ) 253 | else: # best class only 254 | conf, j = x[:, 5:].max(1, keepdim=True) 255 | x = np.concatenate((box, conf, j.float()), axis=1)[ 256 | conf.view(-1) > conf_thres 257 | ] 258 | 259 | # Filter by class 260 | if classes: 261 | x = x[(x[:, 5:6] == np.array(classes)).any(1)] 262 | 263 | # Apply finite constraint 264 | # if not torch.isfinite(x).all(): 265 | # x = x[torch.isfinite(x).all(1)] 266 | 267 | # If none remain process next image 268 | n = x.shape[0] # number of boxes 269 | if not n: 270 | continue 271 | 272 | # Sort by confidence 273 | # x = x[x[:, 4].argsort(descending=True)] 274 | 275 | # Batched NMS 276 | c = x[:, 5:6] * (0 if agnostic else max_wh) # classes 277 | boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores 278 | i = nms(boxes, scores, iou_threshold=iou_thres) 279 | if len(i) > max_det: # limit detections 280 | i = i[:max_det] 281 | if merge and (1 < n < 3e3): # Merge NMS (boxes merged using weighted mean) 282 | try: # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) 283 | iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix 284 | weights = iou * scores[None] # box weights 285 | x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum( 286 | 1, keepdim=True 287 | ) # merged boxes 288 | if redundant: 289 | i = i[iou.sum(1) > 1] # require redundancy 290 | except: # possible CUDA error https://github.com/ultralytics/yolov3/issues/1139 291 | # print(x, i, x.shape, i.shape) 292 | pass 293 | 294 | output[xi] = x[i] 295 | if (time.time() - t) > time_limit: 296 | break # time limit exceeded 297 | 298 | return output 299 | --------------------------------------------------------------------------------