示例#1
0
def get_bbox(images_path_name):
    # TODO: Currently for 1 image only
    for index, image in enumerate(images_path_name):
        image_name_list = image.split('\\')  # 修改处
        image = image_name_list[0] + '/' + image_name_list[1]  # 修改处
        bboxes = selective_search_bbox(image)
        logging.debug('bboxes {}'.format(bboxes))
        return bboxes
def calculate_bbox_score_and_save_img(image_path_name, dataset_image_path, gt_x1, gt_y1, gt_x2, gt_y2):

    logging.debug('dataset_image_path {}'.format(dataset_image_path))
    logging.debug('image_path_name {}'.format(image_path_name))

    candidates = selective_search_bbox(image_path_name)
    logging.debug('candidates {}'.format(candidates))

    image_name = image_path_name.split('/')[-1].split('.')[0]
    logging.debug('image_name {}'.format(image_name))

    img_read = Image.open(image_path_name)
    logging.debug('{} {} {}'.format(img_read.format, img_read.size, img_read.mode))

    i=0
    for x, y, w, h in (candidates):
        #  left, upper, right, and lower pixel; The cropped section includes the left column and
        #  the upper row of pixels and goes up to (but doesn't include) the right column and bottom row of pixels
        logging.debug('Cropped image: i {}'.format(i))
        i=i+1

        boxA = (gt_x1, gt_y1, gt_x2, gt_y2)
        boxB = (x, y, x+w, y+h)
        iou = bb_intersection_over_union(boxA, boxB)
        logging.debug('boxA {}'.format(boxA))
        logging.debug('boxB {}'.format(boxB))
        logging.debug('iou {}'.format(iou))

        # Uncomment only for testing as too much cpu/memory wastage
        #display_bbox(image_path_name, boxA, boxB)

        #img_crop = img_read.crop((y, x, y+w, x+h))
        img_crop = img_read.crop((x, y, x+w, y+h))

        image_save_name = image_path_name.split('/')[-2] + '_' + image_path_name.split('/')[-1].split('.')[0]
        image_save_path = dataset_image_path.rsplit('/', 1)[0]
        image_save_path_name = image_save_path + '/' + image_save_name + '_crop_' +  str(x) + '-' + str(y) + '-' + str(x+w) + '-' + str(y+h) + '_iou_' +  str(iou) + '.jpg'
        logging.debug('image_save_path_name {}'.format(image_save_path_name))
        img_crop.save(image_save_path_name)
        logging.debug('img_crop {} {} {}'.format(img_crop.format, img_crop.size, img_crop.mode))

        # img_crop_resize = img_crop.resize((img_width, img_height))
        # img_crop_resize.save('temp/test/'+ image_name + '_' + str(i) + '_cropped_resize' + '.jpg')
        # logging.debug('img_crop_resize {} {} {}'.format(img_crop_resize.format, img_crop_resize.size, img_crop_resize.mode))


    # Ground Truth
    image_save_name = image_path_name.split('/')[-2] + '_' + image_path_name.split('/')[-1].split('.')[0]
    image_save_path = dataset_image_path.rsplit('/', 1)[0]
    image_save_path_name = image_save_path + '/' + image_save_name + '_gt_' +  str(gt_x1) + '-' + str(gt_y1) + '-' + str(gt_x2) + '-' + str(gt_y2) + '_iou_' +  '1.0' + '.jpg'
    logging.debug('image_save_path_name {}'.format(image_save_path_name))
    #img_crop = img_read.crop((gt_y1, gt_x1, gt_y2, gt_x2))
    img_crop = img_read.crop((gt_x1, gt_y1, gt_x2, gt_y2))
    img_crop.save(image_save_path_name)
    logging.debug('img_crop {} {} {}'.format(img_crop.format, img_crop.size, img_crop.mode))
示例#3
0
def predict_image_name(model, image_path_name):

    logging.debug('image_path_name {}'.format(image_path_name))

    candidates = selective_search_bbox(image_path_name)
    logging.debug('candidates {}'.format(candidates))

    image_name = image_path_name.split('/')[-1].split('.')[0]
    logging.debug('image_name {}'.format(image_name))

    img_read = Image.open(image_path_name)
    logging.debug('{} {} {}'.format(img_read.format, img_read.size,
                                    img_read.mode))
    # img_read.show()
    i = 0
    for x, y, w, h in (candidates):
        #  left, upper, right, and lower pixel; The cropped section includes the left column and
        #  the upper row of pixels and goes up to (but doesn't include) the right column and bottom row of pixels

        img_crop = img_read.crop((y, x, y + w, x + h))
        img_crop.save('temp/test/' + image_name + '_' + str(i) + '_cropped_' +
                      '.jpg')
        logging.debug('img_crop {} {} {}'.format(img_crop.format,
                                                 img_crop.size, img_crop.mode))

        img_crop_resize = img_crop.resize((img_width, img_height))
        img_crop_resize.save('temp/test/' + image_name + '_' + str(i) +
                             '_cropped_resize' + '.jpg')
        logging.debug('img_crop_resize {} {} {}'.format(
            img_crop_resize.format, img_crop_resize.size,
            img_crop_resize.mode))

        i = i + 1

        img = np.array(img_crop_resize).astype(np.float32)
        img[:, :, 0] -= 103.939
        img[:, :, 1] -= 116.779
        img[:, :, 2] -= 123.68
        #img = img.transpose((2,0,1))
        img = np.expand_dims(img, axis=0)

        prediction = model.predict(img, batch_size, verbose=1)
        logging.debug('\n\nprediction \n{}'.format(prediction))

        for index, preds in enumerate(prediction):
            for pred in preds:
                logging.debug('pred {0:6f}'.format(float(pred)))
示例#4
0
def get_bbox(images_path_name):
    # TODO: Currently for 1 image only
    for index, image in enumerate(images_path_name):
        bboxes = selective_search_bbox(image)
        logging.debug('bboxes {}'.format(bboxes))
        return bboxes
示例#5
0
def predict_image_name(image_path_name):

    logging.debug('image_path_name {}'.format(image_path_name))

    candidates = selective_search_bbox(image_path_name)
    logging.debug('candidates {}'.format(candidates))

    image_name = image_path_name.split('/')[-1].split('.')[0]
    logging.debug('image_name {}'.format(image_name))

    # img = Image.open(image_path_name)
    # logging.debug('{} {} {}'.format(img.format, img.size, img.mode))
    #img2 = img.crop((0, 0, 100, 100))
    # img2.save("img2.jpg")
    # img2.show()

    #crop_img = img[200:400, 100:300] # Crop from x, y, w, h -> 100, 200, 300, 400
    # NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h]


    # img = cv2.imread(image_path_name)
    # fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))


    img_read = Image.open(image_path_name)
    logging.debug('{} {} {}'.format(img_read.format, img_read.size, img_read.mode))
    # img_read.show()
    i=0
    for x, y, w, h in (candidates):
        #  left, upper, right, and lower pixel; The cropped section includes the left column and
        #  the upper row of pixels and goes up to (but doesn't include) the right column and bottom row of pixels

        img_crop = img_read.crop((y, x, y+w, x+h))
        img_crop.save('temp/test/'+ image_name + '_' + str(i) + '_cropped_' + '.jpg')
        logging.debug('img_crop {} {} {}'.format(img_crop.format, img_crop.size, img_crop.mode))

        img_crop_resize = img_crop.resize((img_width, img_height))
        img_crop_resize.save('temp/test/'+ image_name + '_' + str(i) + '_cropped_resize' + '.jpg')
        logging.debug('img_crop_resize {} {} {}'.format(img_crop_resize.format, img_crop_resize.size, img_crop_resize.mode))

        i=i+1

#         crop_img = img[x:y, w:h] # Crop from x, y, w, h -> 100, 200, 300, 400
#         logging.debug('crop_img {}'.format(crop_img.shape))
#         ax.imshow(crop_img)
#         # cv2.imshow('cropped', crop_img)
#         # cv2.waitKey(0)
#         plt.show()


# # Convert Image to array
# img = PIL.Image.open("foo.jpg").convert("L")
# arr = numpy.array(img)

# # Convert array to Image
# img = PIL.Image.fromarray(arr)

#       img = cv2.resize(cv2.imread(image_path_name), (224, 224)).astype(np.float32)

#       img2.save('temp/test/img_'+str(i)+'.jpg')

#         img3 = img2.thumbnail((img_width, img_height))
#         logging.debug('img3 {}'.format(type(img3)))
#         # img3.save('temp/test/img_'+str(i)+'_resized.jpg')
#         logging.debug('{} {} {}'.format(img3.format, img3.size, img3.mode))

#         img4 = pad_and_crop_image(img3, img_width, img_height)
#         logging.debug('{} {} {}'.format(img4.format, img4.size, img4.mode))
#         img4.save('temp/test/img_'+str(i)+'_resized1.jpg')


        img=np.array(img_crop_resize).astype(np.float32)



        img[:,:,0] -= 103.939
        img[:,:,1] -= 116.779
        img[:,:,2] -= 123.68
        #img = img.transpose((2,0,1))
        img = np.expand_dims(img, axis=0)

        prediction = model.predict(img, batch_size, verbose=1)
        logging.debug('\n\nprediction \n{}'.format(prediction))

        for index,preds in enumerate(prediction):
            for pred in preds:
                logging.debug('pred {0:6f}'.format(float(pred)))