def image_callback(msg):
    global net_door, net_ball, meta_door, meta_ball, cam_param, det_flag, has_rev, netInitFinished,np_arr,start_time
    if netInitFinished == False:
        model_name = 'all_640_xy'
        steps = '170000'
        base_dir = '/home/momenta/mc3/python/weight/weights-'+model_name
        net_ball = dn.load_net(os.path.join(base_dir,"yolov3-tiny-"+model_name+".cfg"),  
                    os.path.join(base_dir,"yolov3-tiny-"+model_name+"_"+steps+".weights"), 0)
        meta_ball = dn.load_meta(os.path.join(base_dir,"voc-"+model_name+".data"))
        

        model_name = 'door'
        steps = '250000'
        base_dir = '/home/momenta/mc3/python/weight/weights-'+model_name
        net_door = dn.load_net(os.path.join(base_dir,"yolov3-tiny-"+model_name+".cfg"),  
                    os.path.join(base_dir,"yolov3-tiny-"+model_name+"_"+steps+".weights"), 0)
        meta_door = dn.load_meta(os.path.join(base_dir,"voc-"+model_name+".data"))

        filename="./cam_param/"+tb_name+"_bottom.yaml"
        f = open(filename)
        cam_param = yaml.load(f)
        netInitFinished = True

    t = time.time()
    if det_flag:
        det_flag=False
        np_arr = np.fromstring(msg.data, np.uint8)
        has_rev = True
def camera_init():
    global net_top, meta_top, cam_param_top
    global net, meta, cam_param

    # bottom camera
    model_name = 'all_640_xy'
    steps = '170000'
    base_dir = '/home/momenta/mc3/python/weight/weights-' + model_name
    net = dn.load_net(
        os.path.join(base_dir, "yolov3-tiny-" + model_name + ".cfg"),
        os.path.join(base_dir,
                     "yolov3-tiny-" + model_name + "_" + steps + ".weights"),
        0)
    meta = dn.load_meta(os.path.join(base_dir, "voc-" + model_name + ".data"))
    filename = "cam_param/" + tb_name + "_bottom_new.yaml"
    f = open(filename)
    cam_param = yaml.load(f)

    # top camera
    model_name = 'door'
    steps = '250000'
    base_dir = '/home/momenta/mc3/python/weight/weights-' + model_name
    net_top = dn.load_net(
        os.path.join(base_dir, "yolov3-tiny-" + model_name + ".cfg"),
        os.path.join(base_dir,
                     "yolov3-tiny-" + model_name + "_" + steps + ".weights"),
        0)
    meta_top = dn.load_meta(
        os.path.join(base_dir, "voc-" + model_name + ".data"))
    filename = "cam_param/" + tb_name + "_top.yaml"
    f = open(filename)
    cam_param_top = yaml.load(f)
    netInitFinished_top = True
Exemple #3
0
    def __init__(self, idx_uav):
        if idx_uav == 1:
            self.arm = rospy.ServiceProxy('/uav1/mavros/cmd/arming',
                                          CommandBool)
            self.mode = rospy.ServiceProxy('/uav1/mavros/set_mode', SetMode)
            self.pub_att = rospy.Publisher(
                '/uav1/mavros/setpoint_attitude/attitude',
                PoseStamped,
                queue_size=10)
            self.pub_thr = rospy.Publisher(
                '/uav1/mavros/setpoint_attitude/att_throttle',
                Float64,
                queue_size=10)
            rospy.Subscriber("/uav1/mavros/local_position/pose", PoseStamped,
                             local_position_callback_1)
            # rospy.Subscriber("/uav1/mavros/global_position/global", NavSatFix, local_position_callback_1)
            # rospy.Subscriber("/uav1/mavros/global_position/local", PoseWithCovarianceStamped, local_position_callback_1)

            self.net = darknet.load_net("/home/lwc/git/darknet/cfg/yolo.cfg",
                                        "/home/lwc/git/darknet/yolo.weights",
                                        0)
            self.meta = darknet.load_meta(
                "/home/lwc/git/darknet/cfg/coco.data")

            self.des_x = 0
            self.des_y = 0
            self.des_z = 0

        elif idx_uav == 2:
            self.arm = rospy.ServiceProxy('/uav2/mavros/cmd/arming',
                                          CommandBool)
            self.mode = rospy.ServiceProxy('/uav2/mavros/set_mode', SetMode)
            self.pub_att = rospy.Publisher(
                '/uav2/mavros/setpoint_attitude/attitude',
                PoseStamped,
                queue_size=10)
            self.pub_thr = rospy.Publisher(
                '/uav2/mavros/setpoint_attitude/att_throttle',
                Float64,
                queue_size=10)
            rospy.Subscriber("/uav2/mavros/local_position/pose", PoseStamped,
                             local_position_callback_2)

            self.net = darknet.load_net("/home/lwc/git/darknet/cfg/yolo.cfg",
                                        "/home/lwc/git/darknet/yolo.weights",
                                        0)
            self.meta = darknet.load_meta(
                "/home/lwc/git/darknet/cfg/coco.data")

            self.des_x = 1
            self.des_y = 1
            self.des_z = 0

        self.roll_cmd = 0
        self.pitch_cmd = 0
        self.yaw_cmd = 0
        self.throttle_cmd = 0

        self.state = PoseStamped()
Exemple #4
0
 def SetNet(self):
     if self.GrayImgCkB.isChecked():
         self.net = dn.load_net(
             "../darknet/cfg/yolov3-tiny.cfg".encode("utf-8"),
             "../darknet/yolov3-tiny.weights".encode("utf-8"), 0)
         self.meta = dn.load_meta(
             "../darknet/cfg/coco.data".encode("utf-8"))
     else:
         self.net = dn.load_net("../darknet/cfg/yolov3.cfg".encode("utf-8"),
                                "../darknet/yolov3.weights".encode("utf-8"),
                                0)
         self.meta = dn.load_meta(
             "../darknet/cfg/coco.data".encode("utf-8"))
Exemple #5
0
def main():
    with open("yolo_config.yml", 'r') as ymlfile:
        if sys.version_info[0] > 2:
            cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
        else:
            cfg = yaml.load(ymlfile)

    # Setup yolo config
    yolo_data = cfg['yolo_data']
    yolo_config = cfg['yolo_config']
    yolo_weights = cfg['yolo_weights']
    net = dn.load_net(yolo_config, yolo_weights, 0)
    meta = dn.load_meta(yolo_data)

    coco_data = cfg['coco_data']
    coco_config = cfg['coco_config']
    coco_weights = cfg['coco_weights']
    coco_net = dn.load_net(coco_config, coco_weights, 0)
    coco_meta = dn.load_meta(coco_data)

    img = cv2.imread(cfg['single_image'], cv2.IMREAD_COLOR)  #load image in cv2
    objects = dn.detect(net, meta, cfg['single_image'])  # from filename
    coco_objects = dn.detect(coco_net, coco_meta,
                             cfg['single_image'])  # from filename
    # objects = dn.detect(net, meta, img) #opencv image

    # get bounding boxes
    boundingboxes = get_bounding_boxes(objects)
    coco_boxes = get_bounding_boxes(coco_objects)

    # check overlapping boxes
    box_color = (255, 0, 0)
    for box in boundingboxes:
        print("boundingboxes  {}").format(box['class'])
        upper_left = (box['xmin'], box['ymin'])
        lower_right = (box['xmax'], box['ymax'])
        cv2.rectangle(img, upper_left, lower_right, box_color, 2)

    box_color = (0, 0, 255)
    for box in coco_boxes:
        print("coco_boxes  {}").format(box['class'])
        upper_left = (box['xmin'], box['ymin'])
        lower_right = (box['xmax'], box['ymax'])
        cv2.rectangle(img, upper_left, lower_right, box_color, 2)

    cv2.imshow("image", img)
    cv2.waitKey(0)
    cv2.destroyWindow("image")
Exemple #6
0
def yolo_load():
    net = darknet.load_net(b"yolo/cfg/yolov3.cfg",
                           b"yolo/weights/yolov3.weights", 0)
    meta = darknet.load_meta(b"yolo/cfg/coco.data")
    # net = darknet.load_net(b"yolo/cfg/yolov3-tiny.cfg", b"yolo/weights/yolov3-tiny.weights", 0)
    # meta = darknet.load_meta(b"yolo/cfg/coco.data")
    return (net, meta)
def image_callback(msg):

    global start_t, bottom_bdict, bottom_net, bottom_meta, bottom_cam_param, bottom_net_init, bottom_call_count
    global bottom_wf
    print "Bootom Call back ...", bottom_call_count
    if bottom_net_init:
        model_name = 'all_640_xy'
        steps = '160000'
        base_dir = '/home/momenta/mc3/python/weight/weights-'+model_name
        bottom_net = dn.load_net(os.path.join(base_dir,"yolov3-tiny-"+model_name+".cfg"),  
                    os.path.join(base_dir,"yolov3-tiny-"+model_name+"_"+steps+".weights"), 0)
        bottom_meta = dn.load_meta(os.path.join(base_dir,"voc-"+model_name+".data"))
        filename="cam_top.yaml"
        f = open(filename)
        bottom_cam_param = yaml.load(f)
        bottom_net_init = False

    bottom_call_count += 1
    if bottom_call_count%5 != 0:
        return
    np_arr = np.fromstring(msg.data, np.uint8)
    image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)

    t = str(time.time()-start_t)
    bottom_bdict = detect(bottom_net,bottom_meta,bottom_cam_param,image_np,isshow=False,issave=True,output_dir=bottom_out_dir,name= t)
    bottom_info = bottom_bdict
    bottom_info['filename'] = t
    bottom_wf.write(json.dumps(bottom_info,indent=2))
    bottom_wf.write("\n")
Exemple #8
0
    def __init__(self):

        dn.set_gpu(0)
        self._net = dn.load_net("cfg/yolov3.cfg", "weights/yolov3.weights", 0)
        #self._net = dn.load_net("cfg/yolov3.cfg", "weights/yolov3.weights", 0)
        self._meta = dn.load_meta("cfg/coco.data")
        self.__tmpFile="tmp/tmp.jpg"
Exemple #9
0
 def __init__(self,
              cfgPath=cfgDefault,
              weightPath=weightDefault,
              dataPath=dataDefault):
     dn.set_gpu(0)
     self.net = dn.load_net(cfgPath, weightPath, 0)
     self.meta = dn.load_meta(dataPath)
Exemple #10
0
    def get_dn_net(a):
        # 获得脚本的真实目录而不是执行目录
        # getcwd 是获取工作目录
        path = os.path.split(os.path.realpath(__file__))[0]
        file_list = os.listdir(path + '/' + a)
        # print(file_list)
        # print(path)

        data_path = ""
        weights_path = ""
        config_path = ""

        for i in file_list:
            ext = os.path.splitext(i)[-1]
            if ext == ".data":
                data_path = os.path.sep.join([path, a, i])
            elif ext == ".weights":
                weights_path = os.path.sep.join([path, a, i])
            elif ext == ".cfg":
                config_path = os.path.sep.join([path, a, i])
            else:
                continue

        if data_path == "" or weights_path == "" or config_path == "":
            raise SystemExit("missing yolo file")

        net = dn.load_net(bytes(config_path, encoding="utf8"), bytes(weights_path, encoding="utf8"), 0)
        meta = dn.load_meta(bytes(data_path, encoding="utf8"))

        return net, meta
Exemple #11
0
	def __init__(self):
		# Darknet params
		self.darknet_cfg = rospy.get_param("~darknet_yolo/cfg")
		self.darknet_data = rospy.get_param("~darknet_yolo/data")
		self.darknet_weights = rospy.get_param("~darknet_yolo/weights")

		self.net = dn.load_net(self.darknet_cfg,self.darknet_weights,0)
		self.meta = dn.load_meta(self.darknet_data)

		print("    self.darknet_cfg:     {}").format(self.darknet_cfg)
		print("    self.darknet_data:    {}").format(self.darknet_data)
		print("    self.darknet_weights: {}").format(self.darknet_weights)

		# Ros params
		self.bridge = CvBridge()
		self.bb_image_pub_topic = rospy.get_param("~publishers/detection_image/topic")
		self.bb_image_pub = rospy.Publisher(self.bb_image_pub_topic, Image, queue_size=10)
		self.bb_image_seq = 0

		# params for adding text to bounding boxes
		self.fontFace = cv2.FONT_HERSHEY_DUPLEX
		self.fontScale = 0.5
		self.text_thickness = 1
		self.box_thickness = 2

		self.bounding_boxes_msg = BoundingBoxes()
		self.bounding_boxes_msg_seq = 0
		self.bounding_boxes_topic = rospy.get_param("~publishers/bounding_boxes/topic")
		self.bounding_boxes_pub = rospy.Publisher(self.bounding_boxes_topic, BoundingBoxes, queue_size=10)

		self.image_topic = rospy.get_param("~subscribers/image/topic")
		self.image_sub = rospy.Subscriber(self.image_topic,Image,self.detect_ros_image)
Exemple #12
0
def load_network():
    '''
    Initializes the darknet
    '''
    # use ros param for it
    configPath = "./cfg/yolov3-tiny.cfg"
    weightPath = "./yolov3-tiny.weights"
    metaPath = "./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) + "`")

    netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                      weightPath.encode("ascii"), 0,
                                      1)  # batch size = 1

    metaMain = darknet.load_meta(metaPath.encode("ascii"))

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)

    return netMain, metaMain, darknet_image
 def __init__(self) -> None:
     self.net = dn.load_net(
         YOLO_V3_CFG_PATH.encode(),
         YOLO_V3_WEIGHTS_PATH.encode(),
         0
     )
     self.meta = dn.load_meta(YOLO_META_PATH.encode())
    def __init__(self, classes, confidence=0.5, size=416):

        self.classes = classes

        base_path = os.getcwd()
        dn.set_gpu(0)
        if size == 416:

            self.net = dn.load_net(
                os.path.join(base_path, "darknet/cfg/yolov3_416.cfg").encode(),
                os.path.join(base_path, "weights/yolov3_416.weights").encode(),
                0)
            self.detection_dim = (416, 416)

        else:
            self.net = dn.load_net(
                os.path.join(base_path, "darknet/cfg/yolov3.cfg").encode(),
                os.path.join(base_path, "weights/yolov3.weights").encode(), 0)
            self.detection_dim = (608, 608)

        self.meta = dn.load_meta(
            os.path.join(base_path, "darknet/cfg/coco.data").encode())

        self.confidence = confidence

        return
def init():
    ''' Initializing function
        Input:
                None
        return:
                None
    '''
    yolo_logger.info('Initializing')
    # get darkent parameters
    global dn, net, meta
    dn_path, mdl_cfg, weights, meta = common.get_yolo_config()
    # check darknet path
    common.path_check(dn_path, yolo_logger, 'darknet path NOT set!', 8)
    common.file_check(mdl_cfg, yolo_logger, 'yolo cfg file set wrong!', 9)
    common.file_check(weights, yolo_logger, 'yolo weight file set wrong!', 9)
    common.file_check(meta, yolo_logger, 'yolo meta file set wrong!', 9)
    yolo_logger.info('Yolo parameters get successfully')
    # import darknet
    sys.path.append(dn_path)
    print(dn_path)
    import darknet as dn
    yolo_logger.info('Darknet import successfully')

    # initialize darknet network
    meta = dn.load_meta(meta.encode('utf-8'))
    yolo_logger.info('Darknet meta load successfully')
    net = dn.load_net(mdl_cfg.encode('utf-8'), weights.encode('utf-8'), 0)
    yolo_logger.info('Darknet network load successfully')
    dn.set_gpu(gpu_idx)
    yolo_logger.info('GPU set to {0}'.format(gpu_idx))
def YOLO(self):  # 此函数用于darknet所需文件的路径声明&打开摄像头等功能
    global metaMain, netMain, altNames  # darknet框架自带变量,将其声明为全局变量
    #############################################################################################################################
    # 更改以下三个配置文件&查看一个配置文件,重要!!!
    #############################################################################################################################
    configPath = "./yolov3-tiny.cfg"  # 配置cfg文件的路径
    weightPath = "./yolov3-tiny_final.weights"  # 配置weight文件的路径
    metaPath = "./voc.data"  # 配置data文件的路径(切忌查看data文件内定义的name文件的路径&内容是否与实际相对应)

    if not os.path.exists(configPath):  # 判断cfg文件是否存在
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")  # 若不存在,则报错
    if not os.path.exists(weightPath):  # 判断weight文件是否存在
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")  # 若不存在,则报错
    if not os.path.exists(metaPath):  # 判断data文件是否存在
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")  # 若不存在,则报错
    if netMain is None:  # 判断netMain是否为None
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1 导入weight模型文件
    if metaMain is None:  # 判断netMain是否为None
        metaMain = darknet.load_meta(metaPath.encode("ascii"))  # 导入data配置文件
    if altNames is None:  # 判断netMain是否为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:
                    pass
        except Exception:
            pass

    self.cap = cv2.VideoCapture(
        "rtsp:192.168.1.211/user=admin&password=&channel=1&stream=0.sdp?"
    )  # 设定USB摄像头端口为0,使用opencv打开摄像头
    if self.cap.isOpened() == 0:  # 判断USB摄像头是否被打开,如果未打开,则更换摄像头端口
        self.cap = cv2.VideoCapture(1)  # 更换USB摄像头端口为1
    self.cap.set(3, 768)  # 设定摄像头视频流尺寸
    self.cap.set(4, 1024)  # 设定摄像头视频流尺寸
    print(
        "Starting the YOLO loop...")  # 在终端打印此消息,便于调试;此位置代表摄像头已被打开,即将进入YOLO检测环节
    self.camera_state = 1  # 设定摄像头状态为1,便于Log档记录

    # Create an image we reuse for each detect
    self.darknet_image = darknet.make_image(darknet.network_width(netMain),
                                            darknet.network_height(netMain), 3)
    self.camera_state = 1  # 设定摄像头状态为1,便于Log档记录
def batch_analysis_c6(meta_file, cfg_file, wgt_file, thresh, nms, img_path,
                      xml_path):
    image_list = listdir(img_path, '.jpg')
    image_num = len(image_list)
    meta = dn.load_meta(meta_file)
    net = dn.load_net(cfg_file, wgt_file, 0)
    # meta_fr = dn.load_meta(meta_file_fr)
    # net_fr = dn.load_net(cfg_file_fr,wgt_file_fr,0)
    move_count = 0
    for j, image_path in enumerate(image_list):
        print(str(j) + '/' + str(image_num) + "  " + image_path)
        image_name = getFileName(image_path)
        img_save_path = os.path.join(img_path, image_name + '.jpg')
        xml_save_path = os.path.join(xml_path, image_name + '.xml')
        # if os.path.exists(xml_save_path):
        #     continue
        # print(img_save_path)
        det = dn.detect_ext(net, meta, bytes(image_path, 'utf-8'), thresh)
        # det_fr = dn.detect_ext(net_fr, meta_fr, bytes(image_path,'utf-8'),thresh)
        img = cv2.imread(image_path)
        if img is None:
            print('Can not open image')
            continue
        h, w, c = img.shape
        writeXml(xml_save_path, w, h, image_name, det)
    dn.free_net(net)
Exemple #18
0
def init():
    global net
    global meta

    #dn.set_gpu(0)
    net = dn.load_net(b"model/yolov3.cfg", b"model/yolov3.weights", 0)
    meta = dn.load_meta(b"model/coco.data")
 def __init__(self):
     start = time.time()
     self.net = dn.load_net(
         b"./bike_and_bicycle/yolov3-tiny.cfg",
         "./bike_and_bicycle/dataset/backup/yolov3-tiny_final.weights", 0)
     self.meta = dn.load_meta(b"./bike_and_bicycle/voc.data")
     print(time.time() - start)
Exemple #20
0
def simpson_person_classify(input_path):
    dn.set_gpu(0)
    net = dn.load_net(
        b"/home/sannysjtu/google/google-ml-camp/backend/darknet_yolo/simpsons_test.cfg",
        b"/home/sannysjtu/google/google-ml-camp/backend/darknet_yolo/simpsons_final.weights",
        0)
    meta = dn.load_meta(
        b"/home/sannysjtu/google/google-ml-camp/backend/darknet_yolo/simpsons.data"
    )
    files = os.listdir(input_path)

    # res in the r is the detection result of the model
    # the classname is the only required result of our application

    person_class_list = []

    for imgfile in files:
        imgfilepath = os.path.join(input_path, imgfile)
        r = dn.detect(net, meta, imgfilepath.encode('utf-8'))
        current_class_list, current_scores = [], []
        for res in r:
            classname, score, bbox = res
            classname = classname.decode()
            current_class_list.append(classname)
            current_scores.append(current_scores)
        best_classname = current_class_list[current_scores.index(
            max(current_scores))]

        person_class_list.append(best_classname)
    return person_class_list
Exemple #21
0
def main():
    #net = dn.load_net("cfg/yolov3.cfg", "yolov3.weights", 0)
    net = dn.load_net("cfg/yolov3-tiny.cfg", "yolov3-tiny.weights", 0)
    meta = dn.load_meta("cfg/coco.data")

    while True:
        img = raw_input("Type full path to the image to process: ")

        im = np.array(Image.open(img), dtype=np.uint8)
        #r = detect_np(net, meta, im)
        r = dn.detect(net, meta, img)

        # # Create figure and axes
        fig, ax = plt.subplots(1)

        # Display the image
        ax.imshow(im)

        # Create a Rectangle patch
        for res in r:
            box = res[2]
            rect = patches.Rectangle(
                (box[0] - box[2] / 2, box[1] - box[3] / 2),
                box[2],
                box[3],
                linewidth=1,
                edgecolor='r',
                facecolor='none')
            ax.add_patch(rect)

        # Add the patch to the Axes
        plt.show()
Exemple #22
0
    def __init__(self, augmentation=None, *args, **kwargs):
        print("coin worker created", flush=True)
        sys.path.append(Config.DARKNET_PATH)
        import darknet
        sys.path.pop()
        self.dn = darknet

        self.net_main = darknet.load_net_custom(
            Config.config_path.encode("ascii"),
            Config.weight_path.encode("ascii"), 0, 1)
        self.meta_main = darknet.load_meta(Config.meta_path.encode("ascii"))
        with open(Config.meta_path) as metaFH:
            meta_contents = metaFH.read()
            import re
            match = re.search("names *= *(.*)$", meta_contents,
                              re.IGNORECASE | re.MULTILINE)
            result = match.group(1) if match else None
            if os.path.exists(result):
                with open(result) as namesFH:
                    names_list = namesFH.read().strip().split("\n")
                    self.alt_names = [x.strip() for x in names_list]

        print(
            f"({self.dn.network_width(self.net_main)} {self.dn.network_height(self.net_main)}"
        )
        self.darknet_image = darknet.make_image(
            darknet.network_width(self.net_main),
            darknet.network_height(self.net_main), 3)

        self.augmentation = augmentation if augmentation is not None else lambda x: x
def configure(config_path, weights_path, meta_path):
    # As we are modifying them from darknet
    global netMain, metaMain, altNames

    if not os.path.exists(config_path):
        raiseInvalidFile("config", os.path.abspath(config_path))
    if not os.path.exists(weights_path):
        raiseInvalidFile("weights", os.path.abspath(weights_path))
    if not os.path.exists(meta_path):
        raiseInvalidFile("meta", os.path.abspath(meta_path))

    # Load YOLO network
    netMain = darknet.load_net_custom(config_path.encode("ascii"),
                                      weights_path.encode("ascii"), 0,
                                      1)  # batch size = 1
    # Load data file to YOLO network
    metaMain = darknet.load_meta(meta_path.encode("ascii"))

    meta_file_content = open(meta_path, "r").read()
    match = re.search("labels *= *(.*)$", meta_file_content,
                      re.IGNORECASE | re.MULTILINE)
    # If names file found return it as a string list
    if match:
        names_filename = match.group(1)
        if os.path.exists(names_filename):
            names_file = open(names_filename, "r")
            names = names_file.read().strip().split("\n")
            altNames = [x.strip() for x in names]
Exemple #24
0
    def load_network(self, configPath, weightPath, metaPath):
        '''
        Initializes the darknet
        '''
        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) + "`")

        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1

        metaMain = darknet.load_meta(metaPath.encode("ascii"))

        # Create an image we reuse for each detect
        darknet_image = darknet.make_image(darknet.network_width(netMain),
                                           darknet.network_height(netMain), 3)

        return netMain, metaMain, darknet_image
 def init_detector(self):
     self.net_main = darknet.load_net_custom(
         self.config_path.encode("ascii"), self.weight_path.encode("ascii"),
         0, 1)  # batch size = 1
     self.meta_main = darknet.load_meta(self.meta_path.encode("ascii"))
     try:
         with open(self.meta_path) as metaFH:
             metaContents = metaFH.read()
             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")
                         self.alt_names = [x.strip() for x in namesList]
             except TypeError:
                 pass
     except Exception:
         pass
     self.darknet_image = darknet.make_image(
         darknet.network_width(self.net_main),
         darknet.network_height(self.net_main), 3)
    def __init__(self, videoPth, saveImgFolder='', duration=None):
        self.video = VideoFileClip(videoPth)
        if duration is not None:
            self.video = self.video.subclip(duration[0], duration[1])
        self._saveImgFolder = saveImgFolder

        # Load yolo
        self.netMain = load_net_custom(configPath.encode("ascii"),
                                       weightPath.encode("ascii"), 0, 1)
        self.metaMain = load_meta(metaPath.encode("ascii"))
        altNames = None
        if altNames is None:
            # In Python 3, the metafile default access craps out on Windows (but not Linux)
            # Read the names file and create a list to feed to detect
            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")
                                self.altNames = [x.strip() for x in namesList]
                    except TypeError:
                        pass
            except Exception:
                pass
def show():

    dn.set_gpu(0)
    net = dn.load_net("/home/spurs/darknet/cfg/yolov3.cfg".encode('ascii'),
                      "/home/spurs/darknet/yolov3.weights".encode('ascii'), 0)
    meta = dn.load_meta("/home/spurs/darknet/cfg/coco.data".encode('ascii'))

    src = '/home/spurs/camera/camera.jpg'
    while True:
        cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
        try:
            frame = cv2.imread(src)
            cv2.imshow('frame', frame)
        except:
            continue

        cv2.imwrite('/home/spurs/camera/camera_1.jpg', frame)
        r = dn.detect(net, meta,
                      "/home/spurs/camera/camera_1.jpg".encode('ascii'))
        for bb in r:
            x, y, w, h = bb[2]
            x_max = int(round((2 * x + w) / 2))
            x_min = int(round((2 * x - w) / 2))
            y_min = int(round((2 * y - h) / 2))
            y_max = int(round((2 * y + h) / 2))
            cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (255, 255, 0),
                          2)
            cv2.putText(frame, bb[0].decode('utf-8'), (x_min, y_min - 10), 2,
                        1, (255, 0, 255), 2)
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Exemple #28
0
def load_network_meta(cfg_path, weights_path, meta_path):
    net = darknet.load_net_custom(
        to_str(cfg_path, True), to_str(weights_path, True), 0, 1)  # batch size = 1
    meta = darknet.load_meta(to_str(meta_path, True))
    darknet.altNames = load_name_list(to_str(meta_path))

    return net, meta
Exemple #29
0
 def initYOLO(self):
     if not os.path.exists(self.configPath):
         raise ValueError("Invalid config path `" +
                         os.path.abspath(self.configPath)+"`")
     if not os.path.exists(self.weightPath):
         raise ValueError("Invalid weight path `" +
                         os.path.abspath(self.weightPath)+"`")
     if not os.path.exists(self.metaPath):
         raise ValueError("Invalid data file path `" +
                         os.path.abspath(self.metaPath)+"`")
     if self.netMain is None:
         self.netMain = darknet.load_net_custom(self.configPath.encode(
             "ascii"), self.weightPath.encode("ascii"), 0, 1)  # batch size = 1
     if self.metaMain is None:
         self.metaMain = darknet.load_meta(self.metaPath.encode("ascii"))
     if self.altNames is None:
         try:
             with open(self.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")
                             self.altNames = [x.strip() for x in namesList]
                 except TypeError:
                     pass
         except Exception:
             pass
Exemple #30
0
def main():
    # Declare network
    darknet.set_gpu(0)
    net = darknet.load_net(b"cfg/yolov3.cfg", b"weights/yolov3.weights", 0)
    meta = darknet.load_meta(b"cfg/coco.data")

    test_detect_data_structure(net, meta)
Exemple #31
0
# Stupid python path shit.
# Instead just add darknet.py to somewhere in your python path
# OK actually that might not be a great idea, idk, work in progress
# Use at your own risk. or don't, i don't care

import sys, os
sys.path.append(os.path.join(os.getcwd(),'python/'))

import darknet as dn

net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
meta = dn.load_meta("cfg/coco.data")
r = dn.detect(net, meta, "data/dog.jpg")
print r

# And then down here you could detect a lot more images like:
r = dn.detect(net, meta, "data/eagle.jpg")
print r
r = dn.detect(net, meta, "data/giraffe.jpg")
print r
r = dn.detect(net, meta, "data/horses.jpg")
print r
r = dn.detect(net, meta, "data/person.jpg")
print r

Exemple #32
0
# Stupid python path shit.
# Instead just add darknet.py to somewhere in your python path
# OK actually that might not be a great idea, idk, work in progress
# Use at your own risk. or don't, i don't care

import sys, os
sys.path.append(os.path.join(os.getcwd(),'python/'))

import darknet as dn
import pdb

dn.set_gpu(0)
net = dn.load_net("cfg/yolo-thor.cfg", "/home/pjreddie/backup/yolo-thor_final.weights", 0)
meta = dn.load_meta("cfg/thor.data")
r = dn.detect(net, meta, "data/bedroom.jpg")
print r

# And then down here you could detect a lot more images like:
r = dn.detect(net, meta, "data/eagle.jpg")
print r
r = dn.detect(net, meta, "data/giraffe.jpg")
print r
r = dn.detect(net, meta, "data/horses.jpg")
print r
r = dn.detect(net, meta, "data/person.jpg")
print r

Exemple #33
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "./cfg/yolov3.cfg"
    weightPath = "./yolov3.weights"
    metaPath = "./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 netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            "ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
    if metaMain is None:
        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:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture("test.mp4")
    cap.set(3, 1280)
    cap.set(4, 720)
    out = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                    darknet.network_height(netMain),3)
    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   (darknet.network_width(netMain),
                                    darknet.network_height(netMain)),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes())

        detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print(1/(time.time()-prev_time))
        cv2.imshow('Demo', image)
        cv2.waitKey(3)
    cap.release()
    out.release()