forked from CamHD-Analysis/CamHD_motion_metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieve_frames.py
More file actions
executable file
·124 lines (84 loc) · 3.6 KB
/
retrieve_frames.py
File metadata and controls
executable file
·124 lines (84 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
# retrieve_frames.py uses the regions.csv datapackage and Lazycache to pull
# sequences of images for making timelapses (the actual timelapse generation
# is done using e.g, ffmpeg)
#
# This is the "full feature" version. "examples/rdemo_timelapse.py" is
# a much simpler demo script (which doesn't have all of the options)
#
# TODO. Check for existing images
import argparse
import logging
import os
from datapackage import Package
import math
import pycamhd.lazycache as camhd
DEFAULT_DATAPACKAGE_URL = "https://raw.githubusercontent.com/CamHD-Analysis/CamHD_motion_metadata/master/datapackage/datapackage.json"
parser = argparse.ArgumentParser(description='Retrieve a set of images to make a timelapse movie')
parser.add_argument('--scene-tag', metavar='scene_tag', nargs='?',
help='Scene tag to extract')
parser.add_argument('--log', metavar='log', nargs='?', default='INFO',
help='Logging level')
parser.add_argument('--force', nargs='?', default=False,
help='Download images even if a file with the same name already exists')
parser.add_argument('--output', dest='outdir', nargs='?', default=None,
help='Directory for timelapse images')
parser.add_argument('--image-size', dest='imgsize', nargs='?', default=None)
parser.add_argument('--data-package', dest='datapackage',
default=DEFAULT_DATAPACKAGE_URL)
parser.add_argument('--lazycache-url', dest='lazycache',
default=os.environ.get("LAZYCACHE_URL", None),
help='URL to Lazycache repo server')
args = parser.parse_args()
logging.basicConfig(level=args.log.upper())
if not args.scene_tag:
logging.fatal("Scene tag must be specified with --scene-tag")
exit(-1)
if not args.outdir:
args.outdir = 'output/' + parser.parse_args().scene_tag + '/frames'
qt = camhd.lazycache(args.lazycache)
if not os.path.exists("output/" + args.scene_tag + "/frames"):
os.makedirs("output/" + args.scene_tag + "/frames")
img_size = None
if args.imgsize:
img_size = args.imgsize.split('x')
img_size = (int(img_size[0]), int(img_size[1]))
dp = Package(args.datapackage)
regions = dp.get_resource('regions')
mov = {}
count = 0
for r in regions.iter(keyed=True):
count += 1
if r['scene_tag'] == args.scene_tag:
basename = r['mov_basename']
if basename not in mov:
mov[basename] = []
mov[basename].append(r)
keys = sorted(mov.keys())
logging.info("Found scene tag %s in %d / %d keys" % (args.scene_tag,len(keys),count))
count = 0
for basename in keys:
print(basename)
r = mov[basename]
# Select which region
region = r[math.floor(len(r)/2)] if len(r) > 1 else r[0]
logging.info("Using regions from frames %d to %d" %
(region['start_frame'], region['end_frame']))
# Select the precise frame to retrieve
frame = math.floor((region['end_frame'] + region['start_frame'])/2.0)
# Make filename
filename = "%s/%s_%06d.png" % (args.outdir, basename, frame)
if os.path.isfile( filename ) and not args.force:
logging.debug("File %s exists, skipping" % filename)
continue
logging.info("Retrieving frame %d" % frame)
logging.info("Saving to %s" % filename)
if img_size:
img = qt.get_frame(camhd.convert_basename(basename), frame,
format='Image', timeout=30)
img.thumbnail(img_size)
img.save(filename)
else:
img = qt.save_frame(camhd.convert_basename(basename), frame, filename,
format='png', timeout=30)
count += 1