示例#1
0
def get_images(path: str) -> list[str]:
    if not os.path.exists(path):
        raise FileNotFoundError(f"Cannot find input file/folder {path}")
    if os.path.isfile(path):
        if utils.is_image(path):
            return [path]
        else:
            raise ValueError(f"Specified path is not an image: {path}")
    else:
        # Folder
        return [os.path.join(path, filename) for filename in os.listdir(path) if utils.is_image(filename)]
示例#2
0
文件: models.py 项目: Jarvis105/ximg
def create_image(filename,data,request):
    '''create an image from data and request'''
    from mimetypes import guess_type
    from StringIO import StringIO
    try:
        if not utils.is_image(data):
            print 'data is not image'
            return None
        try:
            name,dot,ext = filename.partition('.')
            if len(ext)>4:
                ext = "jpg"
        except:
            ext = "jpg"
        img = Image()
        img.ext = ext
        img.mime = guess_type(filename)[0]
        img.ip = utils.get_client_ip(request)
        
        #create thumbs and reduce size if necessary
        data = create_thumb_and_reduce_size(img,data)

        img.image.put(data, filename=img.uid+"."+img.ext, content_type=img.mime)
        if request.user.is_authenticated():
            img.user = request.user
        img.edited = datetime.now()
        img.save()
    except Exception,what:
        print repr(what)
        return None
示例#3
0
文件: models.py 项目: lovevn/ximg
def create_image(filename, data, request):
    '''create an image from data and request'''
    from mimetypes import guess_type
    from StringIO import StringIO
    try:
        if not utils.is_image(data):
            print 'data is not image'
            return None
        try:
            name, dot, ext = filename.partition('.')
            if len(ext) > 4:
                ext = "jpg"
        except:
            ext = "jpg"
        img = Image()
        img.ext = ext
        img.mime = guess_type(filename)[0]
        img.ip = utils.get_client_ip(request)

        #create thumbs and reduce size if necessary
        data = create_thumb_and_reduce_size(img, data)

        img.image.put(data,
                      filename=img.uid + "." + img.ext,
                      content_type=img.mime)
        if request.user.is_authenticated():
            img.user = request.user
        img.edited = datetime.now()
        img.save()
    except Exception, what:
        print repr(what)
        return None
示例#4
0
def upload_file():
    """
    API Endpoint for image uploading. Expects request with field 'file_to_upload'
    that contains image. If the file is image it is saved, features are extracted 
    and stored in database.
    :return: JSON with name of image in backend database
    """
    file_object = request.files['file_to_upload']
    filename = secure_filename(file_object.filename)
    if is_image(filename):
        image_name = "{}_{}".format(time.time(), filename)
        save_path = "{}/{}".format(app.config["UPLOAD_FOLDER"], image_name)
        file_object.save(save_path)

        # open and close to update the access time.
        with open(save_path, "r") as f:
            pass

        graph = tf.compat.v1.get_default_graph()
        with graph.as_default():
            features = model.predict(save_path)

        try:
            f_list = [float(v) for v in features]
            print('Saving feature list', f_list)
            print(type(f_list[0]))
            img = Image(image_name, f_list)
            img.insert()
        except Exception as e:
            print(e)

        return jsonify({'image_name': image_name})
    else:
        # TODO: Inform about error
        return jsonify({'image_name': '/'})
示例#5
0
def read_files(base_path, recursive):
    log("Reading files {0}.".format(
        "recursively" if recursive else "shallowly"))
    groups = []

    if recursive:
        for subdir, dirs, files in os.walk(base_path, followlinks=True):
            log("Reading {0}...".format(subdir))
            group_name = subdir.replace(base_path, "")
            items = []

            for filename in files:
                filepath = subdir + os.sep + filename

                if is_image(filepath):
                    item = get_item_from_file(filepath, filename)
                    items.append(item)

            g = Group(group_name, items)
            groups.append(g)
            log_replace_line("Reading {0}...Done".format(subdir))

    else:
        log("Reading {0}...".format(base_path))
        g = Group("")
        groups.append(g)
        files = os.listdir(base_path)

        for filename in files:
            filepath = base_path + os.sep + filename

            if is_image(filepath):
                item = get_item_from_file(filepath, filename)
                g.add(item)

        log_replace_line("Reading {0}...Done".format(base_path))

    return [g for g in groups if len(g.items) > 0]
示例#6
0
def get_local_photos() -> list:
    photo_list = []

    for image_name in os.listdir(IMAGE_LOCAL_DIR):
        fullname_src = os.path.join(IMAGE_LOCAL_DIR, image_name)

        if utils.is_image(fullname_src):
            im_width, im_height = utils.find_image_size(fullname_src)

            if can_use_photo(im_width, im_height):
                fullname_dst = os.path.join(IMAGE_LOCAL_DIR, image_name)
                # shutil.copy2(fullname_src, fullname_dst)  # copy image to new dst
                photo_list.append(fullname_dst)
    print('Resolved photos: ' + str(len(photo_list)))
    return photo_list
示例#7
0
def find_image_not_visited(image_list=None) -> Optional[str]:
    # iterate through the names of contents of the folder
    if image_list is None:
        image_list = []
    image_folder = download_utils.get_wallpaper_dir()
    for image_name in os.listdir(image_folder):
        image_fullname = os.path.join(image_folder, image_name)
        if utils.is_image(image_fullname):

            im_width, im_height = utils.find_image_size(image_fullname)
            if download_utils.can_use_photo(im_width,
                                            im_height):  # check photo file
                photo_hash = utils.sha256sum(image_fullname)
                if photo_hash not in image_list:  # image is not yet used
                    return image_fullname
    return None
示例#8
0
async def getmemeurl(redis, reddit):
    posts = await get_memesbrasil_subreddits(redis, reddit)
    for post in posts:
        if post.url and utils.is_image(post.url):
            if await redis.zscore("memesbrasil", post.id):
                continue
            utils.log.debug(
                "found %s %s %s %s",
                datetime.fromtimestamp(post.created),
                post.id,
                post.title,
                post.url,
            )
            now = datetime.now()
            await redis.zadd("memesbrasil", post.created, post.id)
            return post.url
示例#9
0
def general_settings_event_handler(event, values, window):
    config = get_config()
    if event == "save":
        logo = values["Select"]
        if is_image(logo):
            config["APP"]["logo"] = logo
        elif logo != '':
            sg.Popup("Logo not saved because it is not an image")
        try:
            int(values[1])
            int(values[2])
            int(values[3])

            config["GENERAL"]["online_availability_duration"] = values[1]
            config["GENERAL"]["master_availability_duration"] = values[2]
            config["GENERAL"]["sync_interval"] = values[3]
        except Exception as e:
            print(e)
            sg.popup(
                "online_availability_duration,master_availability_duration, sync_interval must be interger values "
            )

        write_config(config)
示例#10
0
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument('--image_min_side',
                            type=float,
                            help='default: {:g}'.format(Config.IMAGE_MIN_SIDE))
        parser.add_argument('--image_max_side',
                            type=float,
                            help='default: {:g}'.format(Config.IMAGE_MAX_SIDE))
        parser.add_argument('--anchor_ratios',
                            type=str,
                            help='default: "{!s}"'.format(
                                Config.ANCHOR_RATIOS))
        parser.add_argument('--anchor_sizes',
                            type=str,
                            help='default: "{!s}"'.format(Config.ANCHOR_SIZES))
        parser.add_argument('--pooler_mode',
                            type=str,
                            choices=Pooler.OPTIONS,
                            help='default: {.value:s}'.format(
                                Config.POOLER_MODE))
        parser.add_argument('--rpn_pre_nms_top_n',
                            type=int,
                            help='default: {:d}'.format(
                                Config.RPN_PRE_NMS_TOP_N))
        parser.add_argument('--rpn_post_nms_top_n',
                            type=int,
                            help='default: {:d}'.format(
                                Config.RPN_POST_NMS_TOP_N))

        args = parser.parse_args()

        input_root = '/home/mmlab/CCTV_Server/models/detectors/FasterRCNN/frames'
        output_root = input_root + '_output'
        path_to_checkpoint = '/home/mmlab/CCTV_Server/models/detectors/FasterRCNN/checkpoints/obstacle/model-90000.pth'
        dataset_name = 'obstacle'
        backbone_name = 'resnet101'
        prob_thresh = 0.6
        Config.setup(image_min_side=args.image_min_side,
                     image_max_side=args.image_max_side,
                     anchor_ratios=args.anchor_ratios,
                     anchor_sizes=args.anchor_sizes,
                     pooler_mode=args.pooler_mode,
                     rpn_pre_nms_top_n=args.rpn_pre_nms_top_n,
                     rpn_post_nms_top_n=args.rpn_post_nms_top_n)

        print('Arguments:')
        for k, v in vars(args).items():
            print(f'\t{k} = {v}')
        print(Config.describe())

        os.makedirs(output_root, exist_ok=True)

        input_sub_dirnames = [
            directory for directory in os.listdir(input_root)
            if os.path.isdir(os.path.join(input_root, directory))
        ]
        dataset_class = DatasetBase.from_name(dataset_name)
        backbone = BackboneBase.from_name(backbone_name)(pretrained=False)
        model = Model(backbone,
                      dataset_class.num_classes(),
                      pooler_mode=Config.POOLER_MODE,
                      anchor_ratios=Config.ANCHOR_RATIOS,
                      anchor_sizes=Config.ANCHOR_SIZES,
                      rpn_pre_nms_top_n=Config.RPN_PRE_NMS_TOP_N,
                      rpn_post_nms_top_n=Config.RPN_POST_NMS_TOP_N).cuda()
        model.load(path_to_checkpoint)

        for sub_dir in input_sub_dirnames:
            input_sub_dirpath = os.path.join(input_root, sub_dir)
            output_sub_dirpath = os.path.join(output_root, sub_dir)

            filenames = [
                image_basename(f) for f in os.listdir(input_sub_dirpath)
                if is_image(f)
            ]
            for filename in filenames:
                path_to_input_image = image_path(input_sub_dirpath, filename,
                                                 '.jpg')
                # path_to_input_image = '/faster-RCNN/frames/1_360p/1_360p_0001.jpg'
                path_to_output_image = image_path(output_sub_dirpath, filename,
                                                  '.jpg')
                # path_to_output_image = '/faster-RCNN/frames_output/1_360p/1_360p_0001.jpg'

                os.makedirs(os.path.join(
                    os.path.curdir, os.path.dirname(path_to_output_image)),
                            exist_ok=True)

                with torch.no_grad():
                    image = transforms.Image.open(path_to_input_image)
                    image_tensor, scale = dataset_class.preprocess(
                        image, Config.IMAGE_MIN_SIDE, Config.IMAGE_MAX_SIDE)

                    detection_bboxes, detection_classes, detection_probs, _, _ = \
                        model.eval().forward(image_tensor.unsqueeze(dim=0).cuda())
                    detection_bboxes /= scale

                    kept_indices = detection_probs > prob_thresh
                    detection_bboxes = detection_bboxes[kept_indices]
                    detection_classes = detection_classes[kept_indices]
                    detection_probs = detection_probs[kept_indices]

                    draw = ImageDraw.Draw(image)

                    for bbox, cls, prob in zip(detection_bboxes.tolist(),
                                               detection_classes.tolist(),
                                               detection_probs.tolist()):
                        color = random.choice([
                            'red', 'green', 'blue', 'yellow', 'purple', 'white'
                        ])
                        bbox = BBox(left=bbox[0],
                                    top=bbox[1],
                                    right=bbox[2],
                                    bottom=bbox[3])
                        category = dataset_class.LABEL_TO_CATEGORY_DICT[cls]

                        draw.rectangle(
                            ((bbox.left, bbox.top), (bbox.right, bbox.bottom)),
                            outline=color)
                        draw.text((bbox.left, bbox.top),
                                  text=f'{category:s} {prob:.3f}',
                                  fill=color)

                    image.save(path_to_output_image)
                    print(f'Output image is saved to {path_to_output_image}')
示例#11
0
文件: create.py 项目: quuhua911/cvat
def main():
    args = get_args()

    manifest_directory = os.path.abspath(args.output_dir)
    if not os.path.exists(manifest_directory):
        os.makedirs(manifest_directory)
    source = os.path.abspath(os.path.expanduser(args.source))

    sources = []
    if not os.path.isfile(source):  # directory/pattern with images
        data_dir = None
        if os.path.isdir(source):
            data_dir = source
            for root, _, files in os.walk(source):
                sources.extend(
                    [os.path.join(root, f) for f in files if is_image(f)])
        else:
            items = source.lstrip('/').split('/')
            position = 0
            try:
                for item in items:
                    if set(item) & {'*', '?', '[', ']'}:
                        break
                    position += 1
                else:
                    raise Exception('Wrong positional argument')
                assert position != 0, 'Wrong pattern: there must be a common root'
                data_dir = source.split(items[position])[0]
            except Exception as ex:
                sys.exit(str(ex))
            sources = list(filter(is_image, glob(source, recursive=True)))

        sources = list(
            filter(lambda x: 'related_images{}'.format(os.sep) not in x,
                   sources))

        # If the source is a glob expression, we need additional processing
        abs_root = source
        while abs_root and re.search('[*?\[\]]', abs_root):
            abs_root = os.path.split(abs_root)[0]

        related_images = detect_related_images(sources, abs_root)
        meta = {
            k: {
                'related_images': related_images[k]
            }
            for k in related_images
        }
        try:
            assert len(sources), 'A images was not found'
            manifest = ImageManifestManager(manifest_path=manifest_directory)
            manifest.link(sources=sources,
                          meta=meta,
                          sorting_method=args.sorting,
                          use_image_hash=True,
                          data_dir=data_dir)
            manifest.create(_tqdm=tqdm)
        except Exception as ex:
            sys.exit(str(ex))
    else:  # video
        try:
            assert is_video(
                source
            ), 'You can specify a video path or a directory/pattern with images'
            manifest = VideoManifestManager(manifest_path=manifest_directory)
            manifest.link(media_file=source, force=args.force)
            try:
                manifest.create(_tqdm=tqdm)
            except AssertionError as ex:
                if str(ex) == 'Too few keyframes':
                    msg = 'NOTE: prepared manifest file contains too few key frames for smooth decoding.\n' \
                        'Use --force flag if you still want to prepare a manifest file.'
                    print(msg)
                    sys.exit(2)
                else:
                    raise
        except Exception as ex:
            sys.exit(str(ex))

    print('The manifest file has been prepared')
示例#12
0
import math

images_root = '/home/mmlab/CCTV_Server/datasets/golfDB_18pts_200_test/train'
anchor_annotations_root = '/home/mmlab/CCTV_Server/datasets/golfDB_18pts_200/annotations'
result_annotations_root = '/home/mmlab/CCTV_Server/datasets/golfDB_18pts_200_test/annotations'

anchor_json_filename = 'golfDB_18pts_train_200.json'
result_json_filename = 'golfDB_18pts_train_200.json'
output_json_filename = 'golfDB_18pts_train_200_vis.json'

anchor_json_path = os.path.join(anchor_annotations_root,anchor_json_filename)
result_json_path = os.path.join(result_annotations_root,result_json_filename)
output_json_path = os.path.join(result_annotations_root,output_json_filename)

filenames = [image_basename(f)
             for f in os.listdir(images_root) if is_image(f)]

file_ids = []
for filename in filenames:
    file_ids.append(int(filename))

print(file_ids)
anchor_json_data = None
result_json_data = None

with open(anchor_json_path) as json_file:
    anchor_json_data = json.load(json_file)
    anchor_json_images = anchor_json_data['images']
    anchor_json_annotations = anchor_json_data['annotations']

with open(result_json_path) as json_file:
import json
import os
from utils import makedir, image_basename, image_path, is_image
import cv2
from time import gmtime, strftime

# json 파일은 같은 폴더에 있다고 가정!

images_root = '/home/mmlab/CCTV_Server/datasets/golfDB_18pts_200_test/train'

filenames = [image_basename(f) for f in os.listdir(images_root) if is_image(f)]

# for filename in filenames:
#     path_to_input_video = video_path(videos_root, filename, '.mp4')

json_data = {}
json_data['info'] = {
    'description': 'golfDB 2020 keypoints Dataset',
    'url': 'http://glee1228.tistory.com',
    'version': '1.0',
    'year': 2020,
    'contributor': 'GolfDB Consortium',
    'date_created': '2020/10/15'
}
json_data['licenses'] = [{
    'url': 'https://creativecommons.org/licenses/by/3.0/legalcode',
    'id': 1,
    'name': 'CREATIVE COMMONS PUBLIC LICENSE'
}]
json_data['images'] = []
json_data['annotations'] = []
示例#14
0
def main():
    global image, boxes, points

    # getting arguments from cli
    args = utils.cli_arguments()
    out = utils.open_file(args)
    img_dir = args["dir"]
    auto_landm = args["land"] is not None
    auto_faces = args["auto"]
    mirror_points = args["mirror"]

    if args["train"] is True:
        # skip everything and train the model
        return utils.train_model(out.path, args["train"])

    # cnn face detector
    utils.init_face_detector(args)

    # load shape predictor if requested
    if auto_faces and auto_landm:
        global predictor
        predictor = dlib.shape_predictor(args["land"])

    # recover last state (if append is true)
    resume, lastpath = utils.load_state(args["append"])

    for file in os.listdir(img_dir):
        # consider only images
        if not is_image(file):
            continue

        # avoid mirrored images
        if is_mirrored(file):
            continue

        # load image:
        path = os.path.join(img_dir, file)

        # trying to resume from the image located at lastpath
        if resume:
            if path == lastpath:
                resume = False
            else:
                continue

        image = cv2.imread(path)

        # clear: stack, boxes and points
        stack.clear()
        boxes.clear()
        points.clear()

        # automatically detect faces
        if auto_faces:
            draw_face(image)

            # and landmarks
            if auto_landm:
                detect_landmarks(image, boxes[-1])

        stack.append(image)

        # create a window with disabled menu when right-clicking with the mouse
        window = file
        cv2.namedWindow(window, cv2.WINDOW_AUTOSIZE | cv2.WINDOW_GUI_NORMAL)

        # mouse callback to window
        cv2.setMouseCallback(window, mouse_callback)

        # removing or skipping the current image without affecting output file
        skipped = False
        removed = False

        # showing image until esc is pressed
        while (1):
            cv2.imshow(window, image)
            key = cv2.waitKey(20) & 0Xff

            # listen to key events
            if key == KEY_ESC:
                break
            elif key == KEY_S:
                skipped = True
                break
            elif key == KEY_R:
                removed = True
                utils.delete_image(path)
                break
            elif key == KEY_Q:
                return quit(path, out)

        if not (skipped or removed):
            # clear point log
            print()

            # write annotations
            add_entry(out, path, boxes, points, mirror_points)

        # close window
        cv2.destroyAllWindows()

    # delete checkpoint file and close output file
    utils.delete_state()
    out.close()