Esempio n. 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
Esempio n. 2
0
    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
Esempio n. 3
0
def gen(camera):
    """Video streaming generator function."""
    while True:
        image = camera.read()
        image = cv2.resize(image,
                           dsize=(640, 480),
                           interpolation=cv2.INTER_AREA)
        frame = darknet.nparray_to_image(image)
        r = darknet.detect_image(net,
                                 meta,
                                 frame,
                                 thresh=.5,
                                 hier_thresh=.5,
                                 nms=.45,
                                 debug=False)
        boxes = []

        for k in range(len(r)):
            width = r[k][2][2]
            height = r[k][2][3]
            center_x = r[k][2][0]
            center_y = r[k][2][1]
            bottomLeft_x = center_x - (width / 2)
            bottomLeft_y = center_y - (height / 2)
            x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
            boxes.append((x, y, w, h))

        for k in range(len(boxes)):
            x, y, w, h = boxes[k]
            top = max(0, np.floor(x + 0.5).astype(int))
            left = max(0, np.floor(y + 0.5).astype(int))
            right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
            bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))
            cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
            cv2.line(image, (top + int(w / 2), left),
                     (top + int(w / 2), left + int(h)), (0, 255, 0), 3)
            cv2.line(image, (top, left + int(h / 2)),
                     (top + int(w), left + int(h / 2)), (0, 255, 0), 3)
            cv2.circle(image, (top + int(w / 2), left + int(h / 2)), 2,
                       tuple((0, 0, 255)), 5)

        ret, jpeg = cv2.imencode('.jpg', image)
        darknet.free_image(frame)
        # print("after get_frame")
        if jpeg is not None:
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() +
                   b'\r\n')
        else:
            print("frame is none")
Esempio n. 4
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
Esempio n. 5
0
def detect(net, cls, color, cwd):
    img = dn.load_image(cwd.encode() + b'/uploads/test.jpg', 0, 0)
    dts = dn.detect_image(net, cls, img, 0.2)
    print(dts)
    res = {}
    for i, d in enumerate(dts):
        tmp = {}
        tmp['object'] = str(d[0])
        tmp['accuracy'] = str(d[1])
        modify_dts = (bbox2points(d[2]))
        print("result :: ", modify_dts)
        tmp['location'] = str(modify_dts)
        res['obj' + str(i)] = tmp

    return json.dumps(res)
Esempio n. 6
0
    def _detect(self, image):
        # Adjust image to darknet compatible
        frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   self._darknet_resolution,
                                   interpolation=cv2.INTER_LINEAR)
        darknet.copy_image_from_bytes(self._darknet_image,
                                      frame_resized.tobytes())

        # Perform detections
        detections = darknet.detect_image(self._darknet_netMain,
                                          self._darknet_metaMain,
                                          self._darknet_image,
                                          thresh=0.25)

        return detections
Esempio n. 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
    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
Esempio n. 9
0
)  #"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/26-4_cam01_assault01_place01_night_spring.mp4")
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
i = 0
while (cap.isOpened()):
    i += 1
    ret, image = cap.read()
    image = cv2.resize(image, dsize=(640, 480), interpolation=cv2.INTER_AREA)
    print(i)
    if not ret:
        break
    frame = darknet.nparray_to_image(image)
    r = darknet.detect_image(net,
                             meta,
                             frame,
                             thresh=.5,
                             hier_thresh=.5,
                             nms=.45,
                             debug=False)
    print(r)
    boxes = []

    for k in range(len(r)):
        width = r[k][2][2]
        height = r[k][2][3]
        center_x = r[k][2][0]
        center_y = r[k][2][1]
        bottomLeft_x = center_x - (width / 2)
        bottomLeft_y = center_y - (height / 2)
        x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
        boxes.append((x, y, w, h))
Esempio n. 10
0
def gen(camera):
    # """Video streaming generator function."""
    i = 0
    while True:
        i += 1
        image = camera.read()
        image = cv2.resize(image,
                           dsize=(640, 480),
                           interpolation=cv2.INTER_AREA)
        print(i)
        # if not ret:
        # break
        frame = darknet.nparray_to_image(image)
        r = darknet.detect_image(net,
                                 meta,
                                 frame,
                                 thresh=.5,
                                 hier_thresh=.5,
                                 nms=.45,
                                 debug=False)
        print(r)
        boxes = []

        for k in range(len(r)):
            width = r[k][2][2]
            height = r[k][2][3]
            center_x = r[k][2][0]
            center_y = r[k][2][1]
            bottomLeft_x = center_x - (width / 2)
            bottomLeft_y = center_y - (height / 2)
            x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
            mytexts = r[k][0]
            mythresh = r[k][1]
            boxes.append((x, y, w, h, mytexts, mythresh))
        print(1)

        for k in range(len(boxes)):
            x, y, w, h, texts, threshs = boxes[k]
            top = max(0, np.floor(x + 0.5).astype(int))
            left = max(0, np.floor(y + 0.5).astype(int))
            right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
            bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))
            # cv2.rectangle(image, (top, left), (right, bottom), (0, 255, 0), 1)

            if texts.decode('utf-8') == 'normal':
                cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0),
                              2)
                cv2.putText(
                    image,
                    texts.decode('utf-8') + '(' + str(threshs * 100)[:5] +
                    '%)', (top, left - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (255, 0, 0))

            elif texts.decode('utf-8') == 'fighting':
                cv2.rectangle(image, (top, left), (right, bottom), (0, 0, 255),
                              2)
                cv2.putText(
                    image,
                    texts.decode('utf-8') + '(' + str(threshs * 100)[:5] +
                    '%)', (top, left - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (0, 0, 255))

        ret, jpeg = cv2.imencode('.jpg', image)
        darknet.free_image(
            frame)  ## darknet에서 쓰는 c언어 동적할당 해제해주는 함수(써주어야 메모리가 버틴다.)

        if jpeg is not None:
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() +
                   b'\r\n')
        else:
            print("frame is none")
Esempio n. 11
0
def gen(camera):
    # """Video streaming generator function."""
    i = 0
    while True:
        i += 1
        image = camera.read()
        image = cv2.resize(image, dsize=(640, 480),
                           interpolation=cv2.INTER_AREA)
        print(i)
        # if not ret:
        # break
        frame = darknet.nparray_to_image(image)
        r = darknet.detect_image(net, meta, frame, thresh=.5, hier_thresh=.5, nms=.45, debug=False)
        print(f"r:\n{r}")
        # [(b'normal', 0.9838562607765198, (337.76190185546875, 226.85903930664062, 41.72311782836914, 109.13109588623047)), 
        # (b'normal', 0.907978355884552, (302.71875, 253.96533203125, 41.06242752075195, 113.02967834472656)), 
        # (b'normal', 0.8925231695175171, (377.8631286621094, 233.21629333496094, 32.55954360961914, 110.92288970947266))]
        boxes = []

        for k in range(len(r)):
            width = r[k][2][2]
            height = r[k][2][3]
            center_x = r[k][2][0]
            center_y = r[k][2][1]
            bottomLeft_x = center_x - (width / 2)
            bottomLeft_y = center_y - (height / 2)
            x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
            mytexts = r[k][0]
            mythresh = r[k][1]
            boxes.append((x, y, w, h, mytexts, mythresh))
        print("next")

        for k in range(len(boxes)):
            x, y, w, h, texts, threshs = boxes[k]
            top = max(0, np.floor(x + 0.5).astype(int))
            left = max(0, np.floor(y + 0.5).astype(int))
            right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
            bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))
            # cv2.rectangle(image, (top, left), (right, bottom), (0, 255, 0), 1)

            if texts.decode('utf-8') == 'normal':
                cv2.rectangle(image, (top, left),
                              (right, bottom), (255, 0, 0), 2)
                cv2.putText(image, texts.decode('utf-8') + '(' + str(threshs*100)
                            [:5] + '%)', (top, left-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0))

            elif texts.decode('utf-8') == 'fighting':
                cv2.rectangle(image, (top, left),
                              (right, bottom), (0, 0, 255), 2)
                # mark probablity
                cv2.putText(image, texts.decode('utf-8') + '(' + str(threshs*100)
                            [:5] + '%)', (top, left-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))


        # cv2.imshow('frame', image)
        
        ret, jpeg = cv2.imencode('.jpg', image)

        # darknet dynamic allocation
        darknet.free_image(frame)

        if jpeg is not None:
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')
        else:
            print("frame is none")
Esempio n. 12
0
 def inference(self):
     detections = darknet.detect_image(self.network,
                                       self.class_names,
                                       self.darknet_image,
                                       thresh=self.thresh)
     return detections
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()
Esempio n. 14
0
def YOLO():
    #os.chdir("./darknet")
    #os.system("pwd")
    #print('dir changed!')

    from darknet import darknet
    print("Imported")
    #================================================================
    base_dir = get_base_dir()
    image_list = read_image()
    #=================================================================#
    # Read parking regions file
    parked_cars = read_regions()
    #================================================================
    global metaMain, netMain, altNames
    configPath = "./darknet/cfg/yolov4.cfg"
    weightPath = "./darknet/yolov4.weights"
    metaPath = "./darknet/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
    # i = 0
    # while True:
    for i in range(len(image_list)):
        image = cv2.imread(image_list[i])
        width = image.shape[1]
        height = image.shape[0]

        # Create an image we reuse for each detect
        darknet_image = darknet.make_image(width, height, 3)

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

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

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)
        # Print detections
        # image = cvDrawBoxesonCars(detections, image_rgb)
        # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # Draw occupied parking spaces
        image, parked_cars_updated = cvOverlapcheck(parked_cars, detections,
                                                    image_rgb)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        from cv2 import imwrite
        file2 = os.path.join(base_dir +
                             '/Api/templates/static/images/new_output.jpg')
        imwrite(file2, image)
        print("Image saved!")

        # cv2.imshow('Output', image)
        cv2.waitKey(0)
        # i += 1
    cv2.destroyAllWindows()
    #================================================================
    # Save the file with occupancy
    post_process(parked_cars_updated)
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()
Esempio n. 16
0
    def first_phase(self, start_id):
        # read the images batch and run detections
        end_id = min(self.total_frame, int(start_id) + self.batch_size)
        print(end_id)
        total_obj = 0
        unique_obj_bbox = {}
        displacement_check = self.displacement_check

        for i in range(int(start_id), end_id):
            # print(self.dataset_dir + "/" + f"{str(i).zfill(10)}.png")
            image = cv2.imread(self.dataset_dir + "/" + f"{str(i).zfill(10)}.png")
            darknet_image = darknet.make_image(1920, 1080, 3)

            image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            darknet.copy_image_from_bytes(darknet_image, image_rgb.tobytes())

            # detections list of tuple: (class_name, confidence_score, (bbox_info))
            detections = darknet.detect_image(self.network, self.class_names, darknet_image, thresh=0.4)

            total_obj = total_obj + len(detections)
            bboxes = [obj[2] for obj in detections]
            confidence = [obj[1] for obj in detections]
            classes = [obj[0] for obj in detections]

            features = self.encoder(image_rgb, bboxes)

            detections = [Detection(bbox, confidence, cls, feature) for bbox, confidence, cls, feature in
                          zip(bboxes, confidence, classes, features)]

            # Run non-maxima suppression.
            boxes = np.array([d.tlwh for d in detections])
            scores = np.array([d.confidence for d in detections])
            indices = preprocessing.non_max_suppression(boxes, self.nms_max_overlap, scores)
            detections = [detections[i] for i in indices]

            self.tracker.predict()
            self.tracker.update(detections)

            for track in self.tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                bbox = track.to_tlbr()

                if track.track_id not in unique_obj_bbox:
                    # unique_obj_bbox.append(track.track_id)
                    unique_obj_bbox[track.track_id] = {'feature': track.features[0].tolist()}
                    unique_obj_bbox[track.track_id]['length'] = 0

                # find the center point of the tracking object
                center_point = track.to_tlwh()
                c_x = center_point[0] + (center_point[2]) / 2

                c_y = center_point[1] + (center_point[3]) / 2

                if track.track_id not in displacement_check:
                    displacement_check[track.track_id] = (c_x, c_y)
                else:
                    disp = math.sqrt((c_x - displacement_check[track.track_id][0]) ** 2 + (c_y - displacement_check[track.track_id][1]) ** 2)
                    # print(unique_obj_bbox[track.track_id])
                    # print('disp for cam: ', str(track.track_id), " ", str(disp))
                    unique_obj_bbox[track.track_id]['length'] = unique_obj_bbox[track.track_id]['length'] + disp

                    # update the center point for next iteration
                    displacement_check[track.track_id] = (c_x, c_y)
                # print(displacement_check)
                # print(unique_obj_bbox[track.track_id]['length'])

        self.displacement_check = displacement_check
        # print(displacement_check)
        return {'total_obj': total_obj, 'unique_obj_bbox': unique_obj_bbox}
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)
Esempio n. 18
0
    def model(self, img: Image) -> (np.array, set):
        W, H = img.size
        img = np.asarray(img)
        result_set = set()
        darknet_image = dn.make_image(int(W), int(H), 3)
        frame_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        dn.copy_image_from_bytes(darknet_image, frame_rgb.tobytes())
        # im = nparray_to_image(arr)
        R = detect_image(self.vehicle_net,
                         self.vehicle_meta,
                         darknet_image,
                         thresh=self.vehicle_threshold)
        R = [r for r in R if r[0].decode('utf-8') in ['car', 'bus', 'truck']]
        if len(R):
            WH = np.array(img.shape[1::-1], dtype=float)
            Lcars = []
            for i, r in enumerate(R):

                cx, cy, w, h = (np.array(r[2]) / np.concatenate(
                    (WH, WH))).tolist()
                tl = np.array([cx - w / 2., cy - h / 2.])
                br = np.array([cx + w / 2., cy + h / 2.])
                label = Label(0, tl, br)
                Lcars.append(label)
                Icar = crop_region(img, label)
                # print('Searching for license plates using WPOD-NET')
                ratio = float(max(Icar.shape[:2])) / min(Icar.shape[:2])
                side = int(ratio * 288.)
                bound_dim = min(side + (side % (2**4)), 608)
                # print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio))
                Llp, LlpImgs, _ = detect_lp(self.license_model, Icar / 255,
                                            bound_dim, 2**4, (240, 80), 0.5)
                if len(LlpImgs):
                    Ilp = LlpImgs[0]
                    res, confidence = self.ocr_model.recognizeOneframe(Ilp *
                                                                       255.)

                    pts = Llp[0].pts * label.wh().reshape(
                        2, 1) + label.tl().reshape(2, 1)
                    ptspx = pts * np.array(img.shape[1::-1],
                                           dtype=float).reshape(2, 1)
                    draw_losangle(img, ptspx, RED, 3)
                    if confidence > 0.5:
                        llp = Label(0, tl=pts.min(1), br=pts.max(1))
                        img = write2img(img, llp, res)
                        result_set.add(res)
            for i, lcar in enumerate(Lcars):
                draw_label(img, lcar, color=YELLOW, thickness=3)
        # 如果没检测到车,就直接检测-识别车牌(需要改进)
        # ToDO
        else:
            # print('Searching for license plates using WPOD-NET')
            ratio = float(max(W, H)) / min(W, H)
            side = int(ratio * 288.)
            bound_dim = min(side + (side % (2**4)), 608)
            # print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio))
            Llp, LlpImgs, _ = detect_lp(self.license_model, img / 255.0,
                                        bound_dim, 2**4, (240, 80), 0.5)
            if len(LlpImgs):
                Ilp = LlpImgs[0]
                res, confidence = self.ocr_model.recognizeOneframe(Ilp * 255.)
                ptspx = Llp[0].pts * np.array(img.shape[1::-1],
                                              dtype=float).reshape(2, 1)
                draw_losangle(img, ptspx, RED, 3)
                if confidence > 0.5:
                    llp = Label(0, tl=Llp[0].pts.min(1), br=Llp[0].pts.max(1))
                    img = write2img(img, llp, res)
                    result_set.add(res)
            # 没检测到车牌,就直接尝试识别车牌
            else:
                res, confidence = self.ocr_model.recognizeOneframe(img)
                if confidence > 0.5:
                    result_set.add(res)
        return img, result_set
Esempio n. 19
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()
Esempio n. 20
0
#3. opencv로 darknet 실행
# 비디오 캡쳐 객체가 정상으로 open 되는 동안 반복
while (cap.isOpened()):
    # 비디오의 한 프레임씩 읽기
    # 제대로 읽으면 ret=True, 읽은 프레임은 image 변수
    ret, image = cap.read()
    # resize하기 위해서는 픽셀 사이의 값을 결정. INTER_AREA=사이즈를 줄이기 위한 보간법
    image = cv2.resize(image, dsize=(640, 480), interpolation=cv2.INTER_AREA)
    print(image.shape)

    if not ret:
        break

    frame = darknet.nparray_to_image(image)
    r = darknet.detect_image(net, meta, frame)

    boxes = []

    for k in range(len(r)):
        width = r[k][2][2]
        height = r[k][2][3]
        center_x = r[k][2][0]
        center_y = r[k][2][1]
        bottomLeft_x = center_x - (width / 2)
        bottomLeft_y = center_y - (height / 2)
        x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
        boxes.append((x, y, w, h))

    for k in range(len(boxes)):
        x, y, w, h = boxes[k]
        # 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,
            (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)
        cv2.imshow(image)
        key = cv2.waitKey(1)
        if key & 0xFF == ord('q'):
            break
        continue

        # Convert images to grayscale. Yields slight performance improvement
        gray_l = cv2.cvtColor(left, cv2.COLOR_BGR2GRAY)
        gray_r = cv2.cvtColor(right, cv2.COLOR_BGR2GRAY)

        # Rectify images
        rect_l = cv2.remap(gray_l, undistortion_maps_l, rectification_maps_l,