예제 #1
0
def pred_by_drawing(img_path, save_path):
    img = cv2.imread(img_path)
    *_, img_name = os.path.split(img_path)
    rclasses, rscores, rbboxes = process_image(img)
    visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes,
                                     visualization.colors_plasma)
    visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
예제 #2
0
def main():
    # Test on some demo image and visualize output.
    # detect from image
    path = 'demo/'

    image_names = sorted(os.listdir(path))
    for img_name in image_names:
        img = mpimg.imread(path + img_name)
        rclasses, rscores, rbboxes = process_image(img)
        visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
예제 #3
0
def print_ssd_result(img_path):
    """打印ssd 检测结果"""

    img = mpimg.imread(img_path)

    ckpt_filename = r'/Users/zhuangshui/PycharmProjects/github/others/SSD/checkpoint/ssd_300_vgg.ckpt'
    model_params = get_ssd_model_params(ckpt_filename)

    rclasses, rscores, rbboxes = process_image(img, model_params=model_params)

    visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
예제 #4
0
def main():
    # TensorFlow session: grow memory when needed. TF, DO NOT USE ALL MY GPU MEMORY!!!
    gpu_options = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(log_device_placement=False,
                            gpu_options=gpu_options)
    sess = tf.Session(config=config)
    # isess = tf.InteractiveSession(config=config)

    # Input placeholder.
    net_shape = (300, 300)
    data_format = 'NHWC'
    img_input = tf.placeholder(tf.uint8, shape=(None, None, 3))
    # Evaluation pre-processing: resize to SSD net shape.
    image_pre, labels_pre, bboxes_pre, bbox_img = ssd_vgg_preprocessing.preprocess_for_eval(
        img_input,
        None,
        None,
        net_shape,
        data_format,
        resize=ssd_vgg_preprocessing.Resize.WARP_RESIZE)
    image_4d = tf.expand_dims(image_pre, 0)

    # Define the SSD model.
    reuse = True if 'ssd_net' in locals() else None
    ssd_net = ssd_vgg_300.SSDNet()
    with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):
        predictions, localisations, _, _ = ssd_net.net(image_4d,
                                                       is_training=False,
                                                       reuse=reuse)

    # Restore SSD model.
    ckpt_filename = '../checkpoints/ssd_300_vgg.ckpt'
    # ckpt_filename = '../checkpoints/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt'
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, ckpt_filename)

    # SSD default anchor boxes.
    ssd_anchors = ssd_net.anchors(net_shape)

    # Test on some demo image and visualize output.
    path = '../demo/'
    image_names = sorted(os.listdir(path))

    img = mpimg.imread(path + image_names[-1])
    rclasses, rscores, rbboxes = process_image(sess, img, image_4d,
                                               predictions, localisations,
                                               bbox_img, img_input,
                                               ssd_anchors)

    # visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
    visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
예제 #5
0
def run():


    cap = get_video()

    while(True):
        # Capture frame-by-frame
        img = get_video()

        # Our operations on the frame come here
        # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        rclasses, rscores, rbboxes =  process_image(img)
        fig = visualization.plt_bboxes(img, rclasses, rscores, rbboxes)

        frame  = fig2data(fig)

        # Display the resulting frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

    exit(0)

    while KEEP_RUNNING:
        # cv2.imshow('Depth', get_depth())
        img = get_video()
        rclasses, rscores, rbboxes =  process_image(img)
        visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
        # cv2.imshow('Video', get_video())
        # if cv2.waitKey(10) == 27:
        #     break

    # print('Press ESC in window to stop')
    # freenect.runloop(depth=display_depth,
    #                  video=display_rgb,
    #                  body=body)
    return
예제 #6
0
    rclasses, rscores, rbboxes = np_methods.ssd_bboxes_select(
            rpredictions, rlocalisations, ssd_anchors,
            select_threshold=select_threshold, img_shape=net_shape, num_classes=21, decode=True)
    
    rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)
    rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses, rscores, rbboxes, top_k=400)
    rclasses, rscores, rbboxes = np_methods.bboxes_nms(rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
    # Resize bboxes to original image shape. Note: useless for Resize.WARP!
    rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
    return rclasses, rscores, rbboxes
# Test on some demo image and visualize output.
path = '../VOC2007/demo/'
image_names = sorted(os.listdir(path))
'''
print(image_names[-5])

img = mpimg.imread(path + image_names[1])
rclasses, rscores, rbboxes =  process_image(img)

# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
'''
for i in range(len(image_names)):
    img = Image.open(path + image_names[i])
    out = img.convert("RGB")
    imgg=np.array(out)
    rclasses, rscores, rbboxes =  process_image(imgg)

# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
    visualization.plt_bboxes(imgg, rclasses, rscores, rbboxes)
예제 #7
0
def preprocess_img(img, shape=(300, 300, 3)):

    # VGG mean parameters.
    _R_MEAN = 123.
    _G_MEAN = 117.
    _B_MEAN = 104.

    means = [_R_MEAN, _G_MEAN, _B_MEAN]
    img = misc.imresize(img, shape, interp='bilinear')
    return img - means


mtime.start()
img = misc.imread('dog.jpg')
img2 = misc.imread('person.jpg')

p_img = preprocess_img(img, shape=(300, 300, 3))
p_img2 = preprocess_img(img2, shape=(300, 300, 3))
mtime.consume('preprocess_pic')

bat_p_img = [p_img, p_img2]
bat_normal_img = [img, img2]

bat_rclasses, bat_rscores, bat_rbboxes = process_image(bat_p_img)

for i in range(batch_size):
    visualization.plt_bboxes(bat_normal_img[i], bat_rclasses[i],
                             bat_rscores[i], bat_rbboxes[i])

### 预处理和后续处理已分开,可得到与源代码一样的结果
### 下一步,多进程预处理
예제 #8
0
def inference(input=0,inputType=1):
    slim = tf.contrib.slim
    sys.path.append('../')
    from nets import ssd_vgg_300, ssd_common, np_methods
    from preprocessing import ssd_vgg_preprocessing
    from notebooks import visualization
    # TensorFlow session: grow memory when needed. TF, DO NOT USE ALL MY GPU MEMORY!!!
    gpu_options = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options)
    isess = tf.InteractiveSession(config=config)

    # Input placeholder.
    net_shape = (300, 300)
    data_format = 'NHWC'
    img_input = tf.placeholder(tf.uint8, shape=(None, None, 3))
    # Evaluation pre-processing: resize to SSD net shape.
    image_pre, labels_pre, bboxes_pre, bbox_img = ssd_vgg_preprocessing.preprocess_for_eval(
        img_input, None, None, net_shape, data_format, resize=ssd_vgg_preprocessing.Resize.WARP_RESIZE)
    image_4d = tf.expand_dims(image_pre, 0)

    # Define the SSD model.
    reuse = True if 'ssd_net' in locals() else None
    ssd_net = ssd_vgg_300.SSDNet()
    with slim.arg_scope(ssd_net.arg_scope(data_format=data_format)):
        predictions, localisations, _, _ = ssd_net.net(image_4d, is_training=False, reuse=reuse)

    # Restore SSD model.
    ckpt_filename = '../checkpoints/ssd_300_vgg.ckpt'
    # ckpt_filename = '../checkpoints/VGG_VOC0712_SSD_300x300_ft_iter_120000.ckpt'
    isess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(isess, ckpt_filename)

    # SSD default anchor boxes.
    ssd_anchors = ssd_net.anchors(net_shape)

    # Main image processing routine.
    def process_image(img, select_threshold=0.5, nms_threshold=.45, net_shape=(300, 300)):
        # Run SSD network.
        rimg, rpredictions, rlocalisations, rbbox_img = isess.run([image_4d, predictions, localisations, bbox_img],
                                                                  feed_dict={img_input: img})

        # Get classes and bboxes from the net outputs.
        rclasses, rscores, rbboxes = np_methods.ssd_bboxes_select(
            rpredictions, rlocalisations, ssd_anchors,
            select_threshold=select_threshold, img_shape=net_shape, num_classes=21, decode=True)

        rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)
        rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses, rscores, rbboxes, top_k=400)
        rclasses, rscores, rbboxes = np_methods.bboxes_nms(rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
        # Resize bboxes to original image shape. Note: useless for Resize.WARP!
        rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
        return rclasses, rscores, rbboxes

    # input is a image
    inputType = int(inputType)
    if inputType is 1:
        if input == 0:
            print("At least indicate 1 input video")
            exit(-1)
        # Test on some demo image and visualize output.
        img = mpimg.imread(input)
        rclasses, rscores, rbboxes = process_image(img)

        # Find the name of the category num
        print(list(map(lambda i:"{}:{}".format(i,category[i]),list(rclasses))))
        rclasses = np.array(list(map(lambda i:"{}:{}".format(i,category[i]),list(rclasses))))

        # visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
        # plot the image directly
        visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
    elif inputType == 2:
        # input is the video
        # plot the boxes into the image
        cap = cv2.VideoCapture(input)
        fps = cap.get(cv2.CAP_PROP_FPS)
        size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        fourcc = cap.get(cv2.CAP_PROP_FOURCC)
        #fourcc = cv2.CAP_PROP_FOURCC(*'CVID')
        print('fps=%d,size=%r,fourcc=%r'%(fps,size,fourcc))
        delay=10/int(fps)
        print(delay)
        if delay <= 1:
            delay = 1
        while (cap.isOpened()):
            ret, frame = cap.read()
            print(ret)
            if ret == True:
                image = frame
                # the array based representation of the image will be used later in order to prepare the
                # result image with boxes and labels on it.
                image_np = image
                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Actual detection.
                rclasses, rscores, rbboxes = process_image(image_np)

                #print(list(map(lambda i: "{}:{}".format(i, category[i]), list(rclasses))))
                rclasses = np.array(list(map(lambda i: "{}:{}".format(i, category[i]), list(rclasses))))

                # Visualization of the results of a detection.
                visualization.bboxes_draw_on_img(image_np, rclasses, rscores, rbboxes)
                cv2.imshow('frame', image_np)
                #cv2.waitKey(np.uint(delay))
                if cv2.waitKey(delay) & 0xFF == ord('q'):
                    break
                print('Ongoing...')
            else:
                break
        cap.release()
        cv2.destroyAllWindows()
    elif inputType ==3:
        print("save video")
        if input == 0:
            print("At least indicate 1 input video")
            exit(-1)
        def save_image(image_np):
            rclasses, rscores, rbboxes = process_image(image_np)
            # print(list(map(lambda i: "{}:{}".format(i, category[i]), list(rclasses))))
            rclasses = np.array(list(map(lambda i: "{}:{}".format(i, category[i]), list(rclasses))))
            visualization.bboxes_draw_on_img(image_np, rclasses, rscores, rbboxes)
            return image_np

        from moviepy.editor import VideoFileClip
        cap = cv2.VideoCapture(input)
        fps = cap.get(cv2.CAP_PROP_FPS)
        cap.release()
        cv2.destroyAllWindows()

        video = VideoFileClip(input)
        result = video.fl_image(save_image)
        output = os.path.join("./videos/output_{}".format(input.split("/")[-1]))
        result.write_videofile(output, fps=fps)
    else:
        cap = cv2.VideoCapture(0)

        while (True):
            # Capture frame-by-frame
            ret, frame = cap.read()

            #cv2.imshow('frame', frame)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(frame, axis=0)
            # Actual detection.
            rclasses, rscores, rbboxes = process_image(frame)

            # print(list(map(lambda i: "{}:{}".format(i, category[i]), list(rclasses))))
            rclasses = np.array(list(map(lambda i: "{}:{}".format(i, category[i]), list(rclasses))))
            # Visualization of the results of a detection.
            visualization.bboxes_draw_on_img(frame, rclasses, rscores, rbboxes)
            cv2.imshow('frame', frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        # When everything done, release the capture
        cap.release()
        cv2.destroyAllWindows()
예제 #9
0
        if cls_id >= 0:
            score = scores[i]
            print(score)
            if cls_id not in colors :
                colors[cls_id] = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
                print(colors[cls_id])
            ymin = int(bboxes[i, 0] * height)
            xmin = int(bboxes[i, 1] * width)
            ymax = int(bboxes[i, 2] * height)
            xmax = int(bboxes[i, 3] * width)
            cv2.rectangle(img_painted, (xmin,ymin), (xmax, ymax), colors[cls_id], 1)
            class_name = str(cls_id)
            cv2.putText(img_painted, '{:s} | {:.3f}'.format(class_name, score), (xmin,ymin), cv2.FONT_HERSHEY_PLAIN, 1.5, colors[cls_id], 2, 1)
    return img_painted

'''
# Test on some demo image and visualize output.
# 测试的文件夹
path = '../demo/'
image_names = sorted(os.listdir(path))
# 文件夹中的第几张图,-1代表最后一张
img = mpimg.imread(path + image_names[-2])
rclasses, rscores, rbboxes = process_image(img)

# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
visualization.plt_bboxes(img, rclasses, rscores, rbboxes)

'''
'''
class A():
    aa = ""
예제 #10
0
def bndbox_image(image):
    img = mpimg.imread(path + image)
    rclasses, rscores, rbboxes = process_image(img)
    plt_obj = visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
    plt_obj.savefig(output_dir + image)
예제 #11
0
    rclasses, rscores, rbboxes = np_methods.bboxes_nms(
        rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
    # Resize bboxes to original image shape. Note: useless for Resize.WARP!
    rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
    return rclasses, rscores, rbboxes


dataset_dir = os.path.join(os.getcwd(), 'datasets')
labels_to_names = None
if dataset_utils.has_labels(dataset_dir):
    labels_to_names = dataset_utils.read_label_file2(dataset_dir,
                                                     filename='labels_voc.txt')

# Test on some demo image and visualize output.
path = '/home/ace19/dl_data/MOT/MOT17/test/sample-test/'
# path = './detection_image/coco/'
image_names = sorted(os.listdir(path))

for image in image_names:
    img = mpimg.imread(path + image)
    # img = mpimg.imread(path + image_names[-5])

    start_time = time.time()

    rclasses, rscores, rbboxes = process_image(img)

    print('Speed %.3f sec' % (time.time() - start_time))

    # visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
    visualization.plt_bboxes(img, rclasses, rscores, rbboxes, labels_to_names)
예제 #12
0
파일: a.py 프로젝트: huawang123/CNN
    rclasses, rscores, rbboxes = np_methods.ssd_bboxes_select(
        rpredictions,
        rlocalisations,
        ssd_anchors,
        select_threshold=select_threshold,
        img_shape=net_shape,
        num_classes=21,
        decode=True)

    rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)
    rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses,
                                                        rscores,
                                                        rbboxes,
                                                        top_k=400)
    rclasses, rscores, rbboxes = np_methods.bboxes_nms(
        rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
    # Resize bboxes to original image shape. Note: useless for Resize.WARP!
    rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
    return rclasses, rscores, rbboxes


# Test on some demo image and visualize output.
path = './demo/'
image_names = sorted(os.listdir(path))

img = mpimg.imread(path + image_names[-5])
rclasses, rscores, rbboxes = process_image(img)

# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
visualization.plt_bboxes(img, rclasses, rscores, rbboxes, name='pp')
예제 #13
0
        rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
    # 将box的坐标重新映射到原图上(上文所有的坐标都进行了归一化,所以要逆操作一次)
    rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
    if case == 1:
        bboxes_draw_on_img(img,
                           rclasses,
                           rscores,
                           rbboxes,
                           colors_plasma,
                           thickness=8)
        return img
    else:
        return rclasses, rscores, rbboxes


# 做目标定位,同时做预测分析
case = 2
path = '../demo/12.jpg'
#  读取图片
img = mpimg.imread(path)
# 执行主流程函数
rclasses, rscores, rbboxes = process_image(img, case)
#isualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
# 显示分类结果图
visualization.plt_bboxes(img,
                         rclasses,
                         rscores,
                         rbboxes,
                         figsize=(10, 10),
                         linewidth=2)
예제 #14
0
    rbboxes = np_methods.bboxes_clip(rbbox_img, rbboxes)
    rclasses, rscores, rbboxes = np_methods.bboxes_sort(rclasses,
                                                        rscores,
                                                        rbboxes,
                                                        top_k=400)
    rclasses, rscores, rbboxes = np_methods.bboxes_nms(
        rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
    # Resize bboxes to original image shape. Note: useless for Resize.WARP!
    rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)
    return rclasses, rscores, rbboxes


print('loading frame')
frame = cv2.imread('E:\\challenge2_val\\test\\01\\rgb\\0001.png')
print(frame.shape)
print('getting boxes')
rclasses, rscores, rbboxes = process_image(frame,
                                           select_threshold=0.6,
                                           nms_threshold=.9,
                                           net_shape=(100, 100))
print('drawing boxes')
# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
img = visualization.plt_bboxes(frame, rclasses, rscores, rbboxes)
print(img.shape)
# Our operations on the frame come here
#color = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame', img)
cv2.waitKey(0)
                                                        top_k=400)
    # 去重,将重复检测到的目标去掉
    rclasses, rscores, rbboxes = np_methods.bboxes_nms(
        rclasses, rscores, rbboxes, nms_threshold=nms_threshold)
    # 将box的坐标重新映射到原图上(上文所有的坐标都进行了归一化,所以要逆操作一次)
    rbboxes = np_methods.bboxes_resize(rbbox_img, rbboxes)

    if case is not None:
        bboxes_draw_on_img(img,
                           rclasses,
                           rscores,
                           rbboxes,
                           colors_plasma,
                           thickness=8)
        return img
    else:
        return rclasses, rscores, rbboxes


if __name__ == '__main__':

    # 做目标定位,同时做预测分析
    path = 'd:/input_data/threepeople.jpg'
    # 读取图片
    img = mpimg.imread(path)
    # 执行主流程函数
    rclasses, rscores, rbboxes = process_image(img)
    # visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes,colors_plasma)
    # 显示分类结果图
    visualization.plt_bboxes(img, rclasses, rscores, rbboxes, label_to_class)