def detection(camera_id):
    cap = cv2.VideoCapture(source_list[camera_id])
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb, (width, height),
                                   interpolation=cv2.INTER_LINEAR)
        frame_queue.put(frame_resized)
        img_for_detect = darknet.make_image(width, height, 3)
        darknet.copy_image_from_bytes(img_for_detect, frame_resized.tobytes())
        darknet_image_queue.put(img_for_detect)
        darknet_image = darknet_image_queue.get()
        prev_time = time.time()
        detections = darknet.detect_image(network, class_names, darknet_image, 0.5)
        detections_queue.put(detections)
        fps = int(1 / (time.time() - prev_time))
        fps_queue.put(fps)
        #print("FPS: {}".format(fps))
        darknet.free_image(darknet_image)
        random.seed(3)  # deterministic bbox colors
        frame_resized = frame_queue.get()
        detections = detections_queue.get()
        fps = fps_queue.get()
        last_frame_list.append(get_frame(frame_resized, detections))
        last_detection_list.append(detections)
        if len(last_frame_list) == 2:
            last_frame_list.pop(0)
        if len(last_detection_list) == 2:
            last_detection_list.pop(0)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + get_frame(frame_resized,
                                                               detections) + b'\r\n')  # concat frame one by one and show result
예제 #2
0
def inference(darknet_image_queue, detections_queue, fps_queue):
    frame_counter = 0
    output_detections = []
    while cap.isOpened():
        darknet_image = darknet_image_queue.get()
        # height, width, channels = darknet_image.size
        frame_counter += 1
        prev_time = time.time()
        detections = darknet.detect_image(network,
                                          class_names,
                                          darknet_image,
                                          thresh=args.thresh)
        detections_queue.put(detections)
        for label, confidence, bbox in detections:
            x, y, w, h = bbox
            # csv_writer.writerow([x,y,frame_counter])
            output_detections.append([x, y, frame_counter])
        # tagpath()
        # visualizepath.visualize(height, width)
        fps = int(1 / (time.time() - prev_time))
        fps_queue.put(fps)
        print("FPS: {}".format(fps))
        # darknet.print_detections(detections, args.ext_output)
        # darknet.print_detections_into_csv(detections, file, frame_counter, args.ext_output)
        darknet.free_image(darknet_image)
    output_detections = np.asarray(output_detections)
    np.savetxt(f, output_detections, delimiter=",")
    cap.release()
예제 #3
0
def Predictor(image, network, class_names, class_colors, confidence_thr=0.4):
    """
    Input:
         image: input 
         network: yolov4 model,the output of load_network
         class_names: the name of class,the output of load_network
         class_color: box color,the output of load_network
         confidence_thr: confidence ,default 0.4
    Output:
         return : predictor
    """
    width = image.shape[1]
    height = image.shape[0]
    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())
    predictor = darknet.detect_image(network,
                                     class_names,
                                     darknet_image,
                                     thresh=0.4)
    darknet.free_image(darknet_image)

    return predictor, image, image_resized
예제 #4
0
def image_detection(image_path, network, class_names, class_colors, thresh):
    # 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)
    new_dets = []
    for det in detections:
        new_x1 = det[2][0] * image.shape[1] / width
        new_y1 = det[2][1] * image.shape[0] / height
        new_x2 = det[2][2] * image.shape[1] / width
        new_y2 = det[2][3] * image.shape[0] / height
        new_det = (new_x1, new_y1, new_x2, new_y2)
        new_dets.append((det[0], det[1], new_det))
    darknet.free_image(darknet_image)

    image = darknet.draw_boxes(new_dets, image, class_colors)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), new_dets
예제 #5
0
def image_detection_original_no_image(image, network, class_names, thresh, fx,
                                      fy):
    global width, height
    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)
    for i in range(len(detections)):
        detections[i] = list(detections[i])
        detections[i][2] = list(detections[i][2])
        detections[i][2][0] = (width / 2 / fx) - (1 / fx) * (width / 2 - detections[i][2][0]) \
            if detections[i][2][0] <= width / 2 \
            else (1 / fx) * (detections[i][2][0] - width / 2) + (width / 2 / fx)
        detections[i][2][1] = (height / 2 / fy) - (1 / fy) * (height / 2 - detections[i][2][1]) \
            if detections[i][2][1] <= height / 2 \
            else (1 / fy) * (detections[i][2][1] - height / 2) + (height / 2 / fy)
        detections[i][2][2] /= fx
        detections[i][2][3] /= fy
    darknet.free_image(darknet_image)
    return detections
예제 #6
0
def detect(net, meta, im, thresh=.2, hier_thresh=0, nms=.4):
    # type: (object, METADATA, IMAGE, float, float, float) -> List[YoloData]
    """
    Detect the objects in the given image. free_image function is called inside this function.
    Therefore the input darkent image is not usable after calling this function.
    :param net:
    :param meta:
    :param im:
    :param thresh:
    :param hier_thresh:
    :param nms:
    :return:
    """
    num = c_int(0)
    pnum = pointer(num)
    predict_image(net, im)
    dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)
    num = pnum[0]
    if nms:
        do_nms_sort(dets, num, meta.classes, nms)

    res = []
    for j in range(num):
        for i in range(meta.classes):
            if dets[j].prob[i] > 0:
                b = dets[j].bbox
                res.append(YoloData(id=i, name=meta.names[i], bbox=BBox(b.x - b.w/2.0, b.y - b.h/2.0, b.w, b.h, dets[j].prob[i])))
    res = sorted(res, key=lambda x: -x.bbox.c)
    free_image(im)
    free_detections(dets, num)
    return res
예제 #7
0
    def detect(self, frame):
        darknet_image = dn.make_image(self.width, self.height, 3)
        img_resized = cv2.resize(frame, (self.width, self.height),
                                 interpolation=cv2.INTER_LINEAR)

        # get image ratios to convert bounding boxes to proper size
        img_height, img_width, _ = frame.shape
        width_ratio = img_width / self.width
        height_ratio = img_height / self.height

        # run model on darknet style image to get detections
        dn.copy_image_from_bytes(darknet_image, img_resized.tobytes())
        detections = dn.detect_image(self.network, self.class_names,
                                     darknet_image)
        dn.free_image(darknet_image)

        results = []
        for label, confidence, bbox in detections:
            if float(confidence) <= 98.0:
                continue

            left, top, right, bottom = dn.bbox2points(bbox)
            left, top, right, bottom = int(left * width_ratio), int(top * height_ratio), \
                                       int(right * width_ratio), int(bottom * height_ratio)

            results.append((confidence, (left, top, right, bottom)))

        if len(results) > 0:
            return max(results)[1]
        else:
            return None
예제 #8
0
def detect(filename, threshold):
    im = darknet.load_image(bytes(filename, "ascii"), 0, 0)
    r = darknet.detect_image(network, class_names, im, thresh=threshold)
    darknet.free_image(im)
    # Convert confidence from string to float:
    if len(r) > 0:
        for i in range(len(r)):
            r[i] = (r[i][0], float(r[i][1]), r[i][2])
    return r
예제 #9
0
def image_detection(image, network, class_names, thresh):
    width = image.shape[1]
    height = image.shape[0]
    darknet_image = darknet.make_image(width, height, 3)
    image_rgb = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    image_resized = cv.resize(image_rgb, (width, height), interpolation=cv.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)
    return detections
예제 #10
0
def image_classification(image, network, class_names):
    width = darknet.network_width(network)
    height = darknet.network_height(network)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR)
    darknet_image = darknet.make_image(width, height, 3)
    darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
    detections = darknet.predict_image(network, darknet_image)
    predictions = [(name, detections[idx]) for idx, name in enumerate(class_names)]
    darknet.free_image(darknet_image)
    return sorted(predictions, key=lambda x: -x[1])
예제 #11
0
def inference(darknet_image_queue, detections_queue, fps_queue):
    while cap.isOpened():
        darknet_image = darknet_image_queue.get()
        prev_time = time.time()
        detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh)
        detections_queue.put(detections)
        fps = int(1 / (time.time() - prev_time))
        fps_queue.put(fps)
        print("FPS: {}".format(fps))
        darknet.print_detections(detections, args.ext_output)
        darknet.free_image(darknet_image)
    cap.release()
예제 #12
0
    def image_detection(self,
                        image_bgr,
                        network,
                        class_names,
                        class_colors,
                        thresh=0.25):
        # 判断输入图像是否为3通道
        if len(image_bgr.shape) == 2:
            image_bgr = np.stack([image_bgr] * 3, axis=-1)
        # 获取原始图像大小
        orig_h, orig_w = image_bgr.shape[:2]

        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_bgr, 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)
        '''注意:这里原始代码依旧是608*608,而不是原图大小,因此我们需要转换'''
        new_detections = []
        for detection in detections:
            pred_label, pred_conf, (x, y, w, h) = detection
            new_x = x / width * orig_w
            new_y = y / height * orig_h
            new_w = w / width * orig_w
            new_h = h / height * orig_h

            # 可以约束一下
            (x1, y1, x2, y2) = self.bbox2point((new_x, new_y, new_w, new_h))
            x1 = x1 if x1 > 0 else 0
            x2 = x2 if x2 < orig_w else orig_w
            y1 = y1 if y1 > 0 else 0
            y2 = y2 if y2 < orig_h else orig_h

            (new_x, new_y, new_w, new_h) = self.point2bbox((x1, y1, x2, y2))

            new_detections.append(
                (pred_label, pred_conf, (new_x, new_y, new_w, new_h)))

        image = darknet.draw_boxes(new_detections, image_rgb, class_colors)
        return cv2.cvtColor(image, cv2.COLOR_RGB2BGR), new_detections
예제 #13
0
def det_img(img, length):

    im = dn.load_mem_image(img, length, 0, 0, 3)
    #im = dn.load_image(fname, 0, 0)
    #print im.w,im.h,im.c
    if (im.w == 0) or (im.h == 0) or (im.c == 0):
        return []
    r = dn.extract(net, im)
    #r = dn.detect(net, meta, fname)#D:/workspace/alex_darknet_180628/build/darknet/x64/
    #r = dn.detect_mem(net, meta, img, length)
    dn.free_image(im)
    #print type(r)#,len(r)
    print "extract finished!"
    return r
예제 #14
0
def darknet_image_detection(image_path, network, class_names, thresh):
    # Create one with image we reuse for each detect : images with the same size.
    img_data, nodata = raster_io.read_raster_all_bands_np(image_path)
    img_data = img_data.transpose(1, 2, 0)
    height, width, band_num = img_data.shape
    if band_num not in [1,3]:
        raise ValueError('only accept one band or three band images')
    darknet_image = darknet.make_image(width, height, band_num)

    # darknet.copy_image_from_bytes(darknet_image, image_rgb.tobytes())
    darknet.copy_image_from_bytes(darknet_image, img_data.tobytes())
    detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
    darknet.free_image(darknet_image)

    return detections
예제 #15
0
def image_detection(image_path, network, class_names, class_colors, thresh):
    # 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)
    darknet.free_image(darknet_image)
    image = darknet.draw_boxes(detections, image_resized, class_colors)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
예제 #16
0
def detect():
    print('Loading image')

    # Use tmp.jpg for demo purposes
    im = dn.load_image(bytes('tmp.jpg', encoding='utf-8'), 0, 0)
    num = dn.c_int(0)
    pnum = dn.pointer(num)

    print('Predicting image')

    dn.predict_image(net, im)

    print('Getting boxes')

    dets = dn.get_network_boxes(net, im.w, im.h, 0.5, 0.5, None, 1, pnum)

    print('Marking boxes')

    res = []
    classes = 1
    for j in range(num.value):
        for i in range(classes):
            if dets[j].prob[i] > 0.75:
                b = dets[j].bbox
                res.append((b.x, b.y, b.w, b.h))
    dn.free_image(im)
    dn.free_detections(dets, num)

    print('Saving image')

    source_img = Image.open('tmp.jpg').convert("RGB")
    size = source_img.size
    w = size[0]
    h = size[1]

    draw = ImageDraw.Draw(source_img)

    for b in res:
        x1 = (b[0] - b[2] / 2.) * w
        x2 = (b[0] + b[2] / 2.) * w
        y1 = (b[1] - b[3] / 2.) * h
        y2 = (b[1] + b[3] / 2.) * h
        draw.rectangle(((x1, y1), (x2, y2)), outline="red")
        print(b)

    source_img.save('tmp.jpg', "JPEG")
예제 #17
0
def image_detection(image, network, class_names, class_colors, thresh):
    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
예제 #18
0
def Predictor(image, network, class_names, class_colors, confidence_thr=0.3):
    width = image.shape[1]
    height = image.shape[0]
    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())
    predictor = darknet.detect_image(network,
                                     class_names,
                                     darknet_image,
                                     thresh=confidence_thr)
    darknet.free_image(darknet_image)

    return predictor, image, image_resized
예제 #19
0
def detect_from_file(net, meta, image_path, thresh=.5, hier_thresh=.5, nms=.45, debug=False):
    #pylint: disable= C0321
    if USING_DARKNET_IMAGE_IO:
        im = darknet.load_image(to_str(image_path, True), 0, 0)
    else:
        import cv2
        custom_image = cv2.imread(to_str(image_path))
        im, arr = darknet.array_to_image(custom_image)
    if debug:
        print("Loaded image")
    det = detect_from_memory(net, meta, im, thresh, hier_thresh, nms, debug)
    if USING_DARKNET_IMAGE_IO:
        darknet.free_image(im)
        if debug:
            print("freed image")

    return det
예제 #20
0
def detect_image(image=cv2.imread('data/dog.jpg')):

    darknet_image = darknet.make_image(resize_width, resize_height, 3)

    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_resized = cv2.resize(image_rgb, (resize_width, resize_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)
    image = image_resized
    return cv2.cvtColor(image, cv2.COLOR_RGB2BGR), detections
예제 #21
0
def image_detection_lime(image, network, class_names, class_colors, thresh):

    # Basically same as image_detection, but we get the image as a numpy array
    #print("entrou image detection LIME")
    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, detection_prob_list = darknet.detect_image_lime(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
예제 #22
0
def image_detection(image_path, network, class_names, class_colors, thresh):
    # 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(darkmask: 99%	(left_x:   43   top_y:   28   width:   40   height:   39)
mask: 100%	(le< /home/fadi/다운로드/Yolo_mark/x64/Release/data/train.txt >ft_x:  134   top_y:   24   width:   32   height:   34)
mask: 99%	(left_x:  205   top_y:   20   width:   35   height:   41)net_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
예제 #23
0
def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
    # num = dn.c_int(0)
    # pnum = dn.pointer(num)
    # dn.predict_image(net, image)
    # dets = dn.get_network_boxes(net, image.w, image.h, thresh, hier_thresh, None, 0, pnum)
    # num = pnum[0]
    # if (nms): dn.do_nms_obj(dets, num, meta.classes, nms);

    # res = []
    # for j in range(num):
    #     for i in range(meta.classes):
    #         if dets[j].prob[i] > 0:
    #             b = dets[j].bbox
    #             res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
    # res = sorted(res, key=lambda x: -x[1])
    # dn.free_image(image)
    # dn.free_detections(dets, num)
    # return res
    num = dn.c_int(0)
    pnum = dn.pointer(num)
    dn.predict_image(net, image)
    dets = dn.get_network_boxes(net, image.w, image.h, thresh, hier_thresh,
                                None, 0, pnum)
    num = pnum[0]
    if nms: dn.do_nms_obj(dets, num, meta.classes, nms)

    res = []
    for j in range(num):
        a = dets[j].prob[0:meta.classes]
        if any(a):
            ai = np.array(a).nonzero()[0]
            for i in ai:
                b = dets[j].bbox
                res.append(
                    (meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))

    res = sorted(res, key=lambda x: -x[1])
    if isinstance(image, bytes):
        dn.free_image(image)  #这步什么情况下执行?多次无法执行的原因是未对这句做判断
        print("free image")
    # dn.free_image(image)
    dn.free_detections(dets, num)
    return res
예제 #24
0
def image_detection(image_path, network, class_names, class_colors, thresh):
    # 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)

    # Setup BCM Mode
    GPIO.setmode(GPIO.BCM)

    # Setup pin number
    pinGreen = 18  #Green led
    pinRed = 24  #Red led
    # Setup the pin as output direction
    valueLow = GPIO.LOW
    valueHigh = GPIO.HIGH
    GPIO.setup(pinGreen, GPIO.OUT)
    GPIO.setup(pinRed, GPIO.OUT)

    if (detections[0][0] == "with_mask"):
        GPIO.output(pinGreen, valueHigh)
        GPIO.output(pinRed, valueLow)
    else:
        GPIO.output(pinRed, valueHigh)
        GPIO.output(pinGreen, valueLow)
    # Clean it up
    GPIO.cleanup()

    darknet.free_image(darknet_image)
    image = darknet.draw_boxes(detections, image_resized, class_colors)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
예제 #25
0
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
    """
	From darknet/python/darknet.py
	"""
    # im = dn.load_image(image, 0, 0)
    im = dn.load_image(image, dn.network_width(net), dn.network_height(net))
    boxes = dn.make_boxes(net)
    probs = dn.make_probs(net)
    num = dn.num_boxes(net)
    dn.network_detect(net, im, thresh, hier_thresh, nms, boxes, probs)
    res = []
    for j in range(num):
        for i in range(meta.classes):
            if probs[j][i] > 0:
                res.append((i, probs[j][i], (boxes[j].x, boxes[j].y,
                                             boxes[j].w, boxes[j].h)))
    res = sorted(res, key=lambda x: -x[1])
    dn.free_image(im)
    dn.free_ptrs(dn.cast(probs, dn.POINTER(dn.c_void_p)), num)
    return res
예제 #26
0
    def inference(flow_id: str, frame: object):
        image = frame_data_2_bytes(frame, width, height)
        darknet.copy_image_from_bytes(darknet_image, image)
        detections = darknet.detect_image(network,
                                          class_names,
                                          darknet_image,
                                          thresh=thresh)
        result = PyDetectionBox(frame_id=frame.frame_id, engine_id='darknet')
        for label, confidence, bbox in detections:
            left, top, right, bottom = darknet.bbox2points(bbox)
            result.add_box(category_id=labels_rev.get(label, ''),
                           category_label=label,
                           x1=left,
                           y1=top,
                           x2=right,
                           y2=left,
                           probability=float(confidence))
        darknet.free_image(darknet_image)

        return flow_id, result
예제 #27
0
    def detect(self, filename, filters):
        filtersParser = FiltersParser(self.class_names)
        filters = filtersParser.parse(filters)

        self.NL = "\n"
        self.SEP = ","

        image = cv2.imread(filename)
        height, width, channels = image.shape[:3]

        # Darknet doesn't accept numpy images.
        # Create one with image we reuse for each detect

        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(self.network,
                                          self.class_names,
                                          darknet_image,
                                          thresh=self.THRESHOLD)
        darknet.free_image(darknet_image)
        drawer = DetectedObjectDrawer()

        image, objects_detail, objects_stats = drawer.draw_boxes_with_filters(
            detections, image_resized, self.class_colors, filters)

        rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        sfilters = self.filters_to_string(filters)
        out_filename = self.output_dir + "/" + sfilters + os.path.basename(
            filename)

        cv2.imwrite(out_filename, rgb_image)
        print("=== Saved image file {}".format(out_filename))

        self.save_detected_objects(out_filename, objects_detail)
        self.save_objects_stats(out_filename, objects_stats)
예제 #28
0
    def __image_detection(self, image, network, class_names, class_colors, thresh):
        width = darknet.network_width(network)
        self.__logger.info('Network width calculated')
        height = darknet.network_height(network)
        self.__logger.info('Network hight calculated')
        darknet_image = darknet.make_image(width, height, 3)
        self.__logger.info('Darknet image produced')
        image_rgb = image
        image_resized = cv2.resize(image_rgb, (width, height),
                                interpolation=cv2.INTER_LINEAR)
        self.__logger.info('Image resized')

        darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
        self.__logger.info('Image copyed')
        detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
        self.__logger.info('Detection completed')
        darknet.free_image(darknet_image)
        self.__logger.info('Image freed')
        image = darknet.draw_boxes(detections, image_resized, class_colors)
        self.__logger.info('Boxes drawn')
        return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
예제 #29
0
def darknet_image_detection_v0(image_path, network, class_names, thresh):
    # Darknet doesn't accept numpy images.
    # Create one with image we reuse for each detect

    # no need to use network width and size, only if images have the same size
    # width = darknet.network_width(network)
    # height = darknet.network_height(network)

    image = cv2.imread(image_path)

    height, width, _ = image.shape
    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)

    print('image_rgb shape', image_rgb.shape)
    for ii in range(3):
        band = image_rgb[:, :, ii]
        print('band %d' % ii, 'mean: %f' % np.mean(band))
    # using rasterio to read, then check if it's the same as cv2.
    img_data, nodata = raster_io.read_raster_all_bands_np(image_path)
    print('img_data shape', img_data.shape)
    img_data = img_data.transpose(1, 2, 0)
    print('img_data shape', img_data.shape)
    for ii in range(3):
        band = img_data[:, :, ii]
        print('band %d' % ii, 'mean: %f' % np.mean(band))

    # darknet.copy_image_from_bytes(darknet_image, image_rgb.tobytes())
    darknet.copy_image_from_bytes(darknet_image, img_data.tobytes())
    detections = darknet.detect_image(network,
                                      class_names,
                                      darknet_image,
                                      thresh=thresh)

    darknet.free_image(darknet_image)

    return detections
예제 #30
0
 def _image_detection(self, image, thresh):
     # Darknet doesn't accept numpy images.
     # Create one with image we reuse for each detect
     width = darknet.network_width(self._network)
     height = darknet.network_height(self._network)
     logger.debug('Darknet network width=%s height=%s', width, height)
     darknet_image = darknet.make_image(width, height, 3)
     logger.debug('Generating RGB resized image for Darknet')
     image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
     image_resized = cv2.resize(image_rgb, (width, height),
                                interpolation=cv2.INTER_LINEAR)
     logger.debug('Copying image to darknet')
     darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
     logger.debug('Running darknet.detect_image()')
     with suppress_stdout_stderr(suppress=not self.debug):
         detections = darknet.detect_image(
             self._network, self._names, darknet_image, thresh=thresh
         )
     darknet.free_image(darknet_image)
     logger.info('Darknet result: %s', detections)
     image = darknet.draw_boxes(detections, image_resized, self._colors)
     return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections