コード例 #1
0
def YOLO(frame):
    global metaMain, netMain, altNames
    try:
        frame_read = frame
        frame_read = np.array(frame, np.uint8)
        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, moving_object, fixed_object = cvDrawBoxes(
            detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, (480, 270))
    except:
        return frame, moving_object, fixed_object

    return image, moving_object, fixed_object
コード例 #2
0
    def __init__(self,
                 config_file="cfg/yolov4-leaky-416.cfg",
                 weights="yolov4-leaky-416.weights",
                 data_file='./cfg/coco.data',
                 darknet_path=os.environ.get('DARKNET_PATH', '../../darknet/'),
                 thresh=0.4,
                 queueSize=128,
                 debug=False):

        self.init_done = False
        self.stopped = True
        self.debug = debug
        self.network, self.class_names, self.class_colors = darknet.load_network(
            utils.join_strings_as_path([darknet_path, config_file]),
            utils.join_strings_as_path([darknet_path, data_file]),
            utils.join_strings_as_path([darknet_path, weights]),
            batch_size=1)

        self.search_box = DARKNET['search_box']
        self.width = darknet.network_width(self.network)
        self.height = darknet.network_height(self.network)
        self.darknet_image = darknet.make_image(self.width, self.height, 3)
        self.thresh = thresh
        self.Q = object_detection_queue

        self.is_night_vision = None
        self.org_frame = None
        self.resize_frame = None
        self.dirs = utils.DirsHandler(DIRS)
        self.small_imgs = []
        self.first_result = None
        self.init_done = True
コード例 #3
0
ファイル: YoloDetector.py プロジェクト: T-Bhuiyan/sap
 def __init__(self, yolo_dir, mode=0):
 
     #darknet.set_gpu(gpu_num)
     self.metaMain = None
     self.netMain = None
     altNames = None
     configPath = None
     weightPath = None
     # Use tiny yolov3
     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)
コード例 #4
0
    def _initialize_darknet(self):

        # Setup paths
        inputDir = os.path.join(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
            "input")
        configPath = os.path.join(inputDir, "yolov3.cfg")
        weightPath = os.path.join(
            inputDir,
            "card.weights")  # Don't change this - change the file name instead
        metaPath = os.path.join(inputDir, "obj.data")
        setupObjPaths(metaPath, os.path.join(inputDir, "obj.names"))

        if not os.path.exists(weightPath):
            print(f"Couldn't find weights path '{weightPath}'")
            sys.exit(101)

        self._darknet_netMain = darknet.load_net_custom(
            configPath.encode("ascii"), weightPath.encode("ascii"), 0,
            1)  # batch size = 1
        self._darknet_metaMain = darknet.load_meta(metaPath.encode("ascii"))

        print("Finished loading")

        # Not sure what is going on here?
        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)
                    sys.exit(1337)
        except Exception as e:
            print(e)
            sys.exit(1337)

        self._darknet_resolution = (darknet.network_width(
            self._darknet_netMain),
                                    darknet.network_height(
                                        self._darknet_netMain))
        self._darknet_image = darknet.make_image(self._darknet_resolution[0],
                                                 self._darknet_resolution[1],
                                                 3)
コード例 #5
0
ファイル: YoloDetector.py プロジェクト: T-Bhuiyan/sap
    def processImgByYolo(self, frame):
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                (darknet.network_width(self.netMain),
                                darknet.network_height(self.netMain)),
                                interpolation=cv2.INTER_LINEAR)
        self.frame_resized = frame_resized

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

        detections = darknet.detect_image(self.netMain, self.metaMain, self.darknet_image, thresh=0.25)

        
        return detections
コード例 #6
0
def imageDetection(image, network, class_names, class_colors, thresh):
    # Modified from darknet_images.py
    width = darknet.network_width(network)
    height = darknet.network_height(network)
    darknet_image = darknet.make_image(width, height, 3)

    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_resized = cv2.resize(image_rgb, (width, height),
                               interpolation=cv2.INTER_LINEAR)

    darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
    detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
    darknet.free_image(darknet_image)
    image = darknet.draw_boxes(detections, image_resized, class_colors)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
コード例 #7
0
def image_detection(image_path, network, class_names, class_colors, thresh):
    """Run YOLOv4 models."""
    # Darknet doesn't accept numpy images.
    # Create one with image we reuse for each detect
    width = darknet.network_width(network)
    height = darknet.network_height(network)
    darknet_image = darknet.make_image(width, height, 3)

    image = cv2.imread(image_path)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_resized = cv2.resize(image_rgb, (width, height),
                               interpolation=cv2.INTER_LINEAR)

    darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
    detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
    image = darknet.draw_boxes(detections, image_resized, class_colors)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
コード例 #8
0
    def run(self, frame):
        # Preprocess image
        frame_size = frame.shape
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb, (darknet.network_width(
            self.netMain), darknet.network_height(self.netMain)),
                                   interpolation=cv2.INTER_LINEAR)

        # Forward pass
        darknet.copy_image_from_bytes(self.darknet_image,
                                      frame_resized.tobytes())
        detections = darknet.detect_image(self.netMain,
                                          self.metaMain,
                                          self.darknet_image,
                                          frame_size,
                                          thresh=self.thresh)

        return detections, frame_resized
コード例 #9
0
    def setup(self, netMain=None, metaMain=None, altNames=None):
        # Load the model
        if netMain is None:
            self.netMain = darknet.load_net_custom(
                self.configPath.encode('ascii'),
                self.weightPath.encode('ascii'), 0, 1)  # batch size = 1
        else:
            self.netMain = netMain

        if metaMain is None:
            self.metaMain = darknet.load_meta(self.metaPath.encode('ascii'))
        else:
            self.metaMain = metaMain

        if 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 osp.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
        else:
            self.altNames = altNames

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

        # Performance logging
        self.count = 0
        self.total = 0
コード例 #10
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "requirements/yolov3_custom.cfg"
    weightPath = "weights/yolov3_custom_last.weights"
    metaPath = "requirements/detector.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)
    videopath = argv[1]
    if os.path.exists(videopath):
        name = videopath.split('/')[-1].split('.')[0]
        cap = cv2.VideoCapture(videopath)
    else:
        print("Incorrect path to video")
        return
    cap.set(3, 1280)
    cap.set(4, 720)
    out = cv2.VideoWriter(
        "detections/" + name + "_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.1)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print(1 / (time.time() - prev_time))
    cap.release()
    out.release()
コード例 #11
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "Configuration/yolov4_obj.cfg"
    weightPath = "Weights/yolov4_obj_final.weights"
    metaPath = "Configuration/detector.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)
    imagepath = argv[1]
    if os.path.exists(imagepath):
        name = imagepath.split('/')[-1].split('.')[0]
        frame_read = cv2.imread(imagepath)
    else:
        print("Incorrect path to image")
        return
    print("Starting the YOLO loop...")

    inputshape = (darknet.network_width(netMain),
                  darknet.network_height(netMain))

    darknet_image = darknet.make_image(inputshape[0], inputshape[1], 3)
    prev_time = time.time()
    frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
    frame_resized = cv2.resize(frame_rgb,
                               inputshape,
                               interpolation=cv2.INTER_LINEAR)

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

    detections = darknet.detect_image(netMain,
                                      metaMain,
                                      darknet_image,
                                      thresh=0.2)
    image = cvDrawBoxes(detections, frame_rgb, inputshape)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    print("FPS : ", end='')
    print(1.0 / (time.time() - prev_time))
    cv2.imwrite(name + "_output.jpg", image)
コード例 #12
0
                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

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

movable = [
    'bicycle', 'bus', 'car', 'carrier', 'cat', 'dog', 'motorcycle',
    'movable_signage', 'person', 'scooter', 'stroller', 'truck', 'wheelchair'
]
fixed = [
    'barricade', 'bench', 'bollard', 'chair', 'fire_hydrant', 'kiosk',
    'parking_meter', 'pole', 'potted_plant', 'power_controller', 'stop',
    'table', 'traffic_light', 'traffic_light_controller', 'traffic_sign',
    'tree_trunk'
]


def convertBack(x, y, w, h):
    xmin = int(round(x - (w / 2)))
コード例 #13
0
def main():

    global metaMain, netMain, altNames
    configPath = "darknet/cfg/yolov3-seabird.cfg"
    weightPath = "darknet/backup_608/yolov3-seabird_final.weights"
    metaPath = "darknet/cfg/seabird.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


   # Definition of the parameters
    max_cosine_distance = 0.3
    max_euclidean_distance = 150.0
    nn_budget = None
    nms_max_overlap = 1
   # deep_sort 
    
    metric = nn_matching.NearestNeighborDistanceMetric("euclidean", max_euclidean_distance, nn_budget)
    #metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
    tracker = Tracker(metric)

    writeVideo_flag = False
    
    #video_capture = cv2.VideoCapture('/data/Farallon3_20190706_000001_No001.avi')
    #video_capture = cv2.VideoCapture('/data/15_fps/Farallon3_20190620_021546_No001.mp4')
    video_capture = cv2.VideoCapture('/data/15_fps/Farallon3_20190621_090300_No004.mp4')
    #video_capture = cv2.VideoCapture('/data/rows_data/15_fps/Farallon3_20190603_155007_No001.avi')

    video_fps = video_capture.get(cv2.CAP_PROP_FPS)
    #video_fps = 25
    list_file = open('events.csv', 'w')
    track_log_file = open('track_log.csv', 'w')
    wr = csv.writer(list_file, dialect='excel')
    wr_tracklog = csv.writer(track_log_file, dialect='excel')
    if writeVideo_flag:
    # Define the codec and create VideoWriter object
        w = int(video_capture.get(3))
        h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        out = cv2.VideoWriter('1.avi', fourcc, 15, (w, h))
        zone = cv2.imread('mask/test_zone.png', -1)
     # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                    darknet.network_height(netMain),3)
    
    fps = 0.0
    frame_index = -1 
    mask = cv2.imread('mask/mask_new.jpg')
    mask = np.uint8(mask/255)
    ##############################################
    #points = [(130,102),(535,370),(808,345),(1570,391),(1494,808),(373,817),(4,496),(1,276)]
    #points = [(4,22),(121,96),(207,99),(537,366),(819,324),(1564,385),(1764,322),(1648,889),(105,915),(3,762)]
    points = [(2,24),(127,24),(579,321),(1746,336),(1674,878),(1912,957),(1926,1081),(2,1074)]
    zone_polygon = Polygon(points)
    text_count_size = 9e-3 * 200
    text_count_x,text_count_y = 550,  1000
    avg_area_box_all_frames = 0
    time_stamp = datetime(2019, 6, 21, 10, 33, 5) 
    
    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
            break
        frame_index = frame_index +1  
        t1 = time.time()
        write = 0 
        if frame_index % 15 == 0:
           write = 1 
            
        draw_frame  = frame 
        frame = np.multiply(frame, mask)
        # image = Image.fromarray(frame)
        #image = Image.fromarray(frame[...,::-1]) #bgr to rgb
        
        frame_rgb = cv2.cvtColor(frame, 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.5)

        boxs, class_ids=convert(detections, dw, dh)
        
        # score to 1.0 here).
        dets = [Detection(bbox, 1.0, class_id) for bbox,class_id in zip(boxs, class_ids)]
        
        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in dets])
        scores = np.array([d.confidence for d in dets])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores)
        detections_tracker = [dets[i] for i in indices]
        ids = [class_ids[i] for i in indices]
        time_lapsed_sec = frame_index / video_fps
        time_stamp_now = time_stamp + timedelta(seconds=time_lapsed_sec) 

        # Call the tracker
        tracker.predict()
        tracker.update(detections_tracker, ids, time_stamp_now)
        tracker.update_events(draw_frame, time_stamp_now, wr, wr_tracklog, write )
   
        avg_area_box = 0
        for det in detections:
            name , x, y, w, h = det[0],det[2][0],det[2][1],det[2][2],det[2][3]
            class_id = altNames.index(name.decode("utf-8") )
            bbox = [(x-w/2)*dw,(y-h/2)*dh,(x+w/2)*dw,(y+h/2)*dh]
            area_box = w*h*dw*dh
            if area_box > 3*avg_area_box_all_frames:
                 cv2.rectangle(draw_frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,255,255), 3)
                 cv2.putText(draw_frame,"Flapping detected !!!!",(text_count_x,text_count_y),4,text_count_size, (255,0,0),4) 
            avg_area_box = avg_area_box + area_box
            cv2.rectangle(draw_frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),get_rgb(class_id,3), 2)
        avg_area_box = avg_area_box/len(detections)
        avg_area_box_all_frames=avg_area_box 
        
        if writeVideo_flag:
            # save a frame
            out.write(draw_frame)
      
        fps  = ( fps + (1./(time.time()-t1)) ) / 2
        print("fps= %f"%(fps))
        

    video_capture.release()
    if writeVideo_flag:
        out.release()
        list_file.close()
    cv2.destroyAllWindows()
コード例 #14
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "Configuration/yolov4_obj.cfg"
    weightPath = "Weights/yolov4_obj_final.weights"
    metaPath = "Configuration/detector.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
    videopath = argv[1]
    if os.path.exists(videopath):
        name = videopath.split('/')[-1].split('.')[0]
        cap = cv2.VideoCapture(videopath)
    else:
        print("Incorrect path to video")
        return
    cap = cv2.VideoCapture(videopath)
    fps = cap.get(cv2.CAP_PROP_FPS)
    print(fps)
    cap.set(3, 1280)
    cap.set(4, 720)
    ret, frame_read = cap.read()
    out = cv2.VideoWriter(name + "_output.avi",
                          cv2.VideoWriter_fourcc(*"MJPG"), fps,
                          (frame_read.shape[1], frame_read.shape[0]))
    print("Starting the YOLO loop...")

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

    start = time.time()
    cnt = 1
    while ret:
        cnt += 1
        prev_time = time.time()
        if ret:
            frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
            frame_resized = cv2.resize(frame_rgb,
                                       inputshape,
                                       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_rgb, inputshape)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            print(1 / (time.time() - prev_time))
            out.write(image)

        ret, frame_read = cap.read()
        # cv2.imshow('Demo', image)     #uncomment if running on local machine
        # cv2.waitKey(3)    #uncomment if running on local machine
    print()
    print("Average FPS : ", end='')
    print(cnt * 1.0 / (time.time() - start))
    cap.release()
    out.release()
コード例 #15
0
undistortion_maps_r = np.load("calibration/zed/undistortion_map_right.npy")
rectification_maps_l = np.load("calibration/zed/rectification_map_left.npy")
rectification_maps_r = np.load("calibration/zed/rectification_map_right.npy")

# Calibration parameters from zed calibration files. In my experience they didn't work very well
# mtx_l = np.load("camera_matrix_left.npy")
# mtx_r = np.load("camera_matrix_right.npy")
# dist_l = np.load("dist_coeffs_left.npy")
# dist_r = np.load("dist_coeffs_right.npy")
# newmtx_l, roi_l = cv2.getOptimalNewCameraMatrix(mtx_l,dist_l,(672,376),0)
# newmtx_r, roi_r = cv2.getOptimalNewCameraMatrix(mtx_r,dist_r,(672,376),0)

cap = cv2.VideoCapture(0)

darknet_image = darknet.make_image(darknet.network_width(netMain),
                                   darknet.network_height(netMain), 3)

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        start = time.time()

        # Split frame into left and right images since the ZED camera
        #  gives us both images side by side
        # left = frame[0:, 0:frame.shape[1]//2]
        # right = frame[0:, frame.shape[1]//2:]

        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        frame_resized = cv2.resize(
            frame_rgb,