def __init__(self, yolo_dir, gpu_num, mode=0): darknet.set_gpu(gpu_num) self.metaMain = None self.netMain = None altNames = None # Use tiny yolov3 configPath = None weightPath = None if (mode == 0): configPath = os.path.join(yolo_dir, "cfg/tiny-yolo.cfg") weightPath = os.path.join(yolo_dir, "yolov3-tiny.weights") # Use yolov3 elif (mode == 1): configPath = os.path.join(yolo_dir, "cfg/yolov3.cfg") weightPath = os.path.join(yolo_dir, "yolov3.weights") metaPath = os.path.join(yolo_dir, "cfg/coco.data") if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if self.netMain is None: self.netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if self.metaMain is None: self.metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError as e: print(e) pass except Exception as e: print(e) pass self.darknet_image = darknet.make_image( darknet.network_width(self.netMain), darknet.network_height(self.netMain), 3)
if i.state == 2 and (time.time() - i.time) > shutdownTime: i.state = 0 i.time = 0 i.index = -1 elif i.state == 1: if findList(indices, i.index) == -1: i.state = 2 i.time = time.time() if __name__ == '__main__': tracker = Sort() colors = [(random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) for i in range(10)] dn.set_gpu(0) net = dn.load_net( "/home/bardoe/sources/sceneLight/model/yolov3.cfg".encode("utf-8"), "/home/bardoe/sources/sceneLight/model/yolov3.weights".encode("utf-8"), 0) meta = dn.load_meta( "/home/bardoe/sources/sceneLight/model/coco.data".encode("utf-8")) cap = cv2.VideoCapture(0) frames = 0 start = time.time() sender.start() sender.activate_output(1) sender[1].multicast = True while cap.isOpened():
def __init__(self, gpu, cfg, weights, data): darknet.set_gpu(gpu) self.net = darknet.load_net(str.encode(cfg), str.encode(weights), 0) self.meta = darknet.load_meta(str.encode(data))
def to_box(r): boxes = [] scores = [] for rc in r: if rc[0] == b'text': cx, cy, w, h = rc[-1] scores.append(rc[1]) prob = rc[1] xmin, ymin, xmax, ymax = cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2 boxes.append([int(xmin), int(ymin), int(xmax), int(ymax)]) return boxes, scores import pdb if GPU: try: dn.set_gpu(GPUID) except: pass net = dn.load_net(yoloCfg.encode('utf-8'), yoloWeights.encode('utf-8'), 0) meta = dn.load_meta(yoloData.encode('utf-8')) os.chdir(pwd) def text_detect(img): r = detect_np(net, meta, img, thresh=0, hier_thresh=0.5, nms=None) ##输出所有box,与opencv dnn统一 bboxes = to_box(r) return bboxes