def add_photo(path, must_have_fov=False, must_have_exif=False, **args): """ Add a photo to the database """ if not os.path.exists(path): raise ValueError("File does not exist") if 'license' not in args: args['license'] = License.objects.get_or_create( name='All Rights Reserved')[0] # md5: check for duplicates md5 = md5sum(path) duplicate = True try: photo = Photo.objects.get(md5=md5) except Photo.DoesNotExist: duplicate = False except Photo.MultipleObjectsReturned: duplicates = Photo.objects.filter(md5=md5).order_by('id') for d in duplicates[1:]: d.delete() if duplicate: raise ValueError("Duplicate photo import: '%s'" % path) # parse exif if 'exif' not in args: print 'Obtaining EXIF...' exif = get_exif(path) if exif: args['exif'] = exif elif must_have_exif: raise ValueError("Photo has no EXIF: %s" % path) if 'fov' not in args: print 'Obtaining FOV...' fov = get_fov(args['exif']) if fov: args['fov'] = fov elif must_have_fov: raise ValueError("Could not obtain photo FOV: %s" % path) photo = None # use a transaction so that it is only committed to the database # after save() returns. otherwise, there's a small time betwee # when the photo is added and it has an image attached. with transaction.atomic(): with open(path, 'rb') as f: print 'Uploading photo...' photo = Photo.objects.create(image_orig=ImageFile(f), md5=md5, **args) from mturk.tasks import add_pending_objects_task add_pending_objects_task.delay([get_content_tuple(photo)]) return photo
def add_photo(path, must_have_fov=False, must_have_exif=False, **args): """ Add a photo to the database """ if not os.path.exists(path): raise ValueError("File does not exist") if "license" not in args: args["license"] = License.objects.get_or_create(name="All Rights Reserved")[0] # md5: check for duplicates md5 = md5sum(path) duplicate = True try: photo = Photo.objects.get(md5=md5) except Photo.DoesNotExist: duplicate = False except Photo.MultipleObjectsReturned: duplicates = Photo.objects.filter(md5=md5).order_by("id") for d in duplicates[1:]: d.delete() if duplicate: raise ValueError("Duplicate photo import: '%s'" % path) # parse exif if "exif" not in args: print "Obtaining EXIF..." exif = get_exif(path) if exif: args["exif"] = exif elif must_have_exif: raise ValueError("Photo has no EXIF: %s" % path) if "fov" not in args: print "Obtaining FOV..." fov = get_fov(args["exif"]) if fov: args["fov"] = fov elif must_have_fov: raise ValueError("Could not obtain photo FOV: %s" % path) photo = None # use a transaction so that it is only committed to the database # after save() returns. otherwise, there's a small time betwee # when the photo is added and it has an image attached. with transaction.atomic(): with open(path, "rb") as f: print "Uploading photo..." photo = Photo.objects.create(image_orig=ImageFile(f), md5=md5, **args) from mturk.tasks import add_pending_objects_task add_pending_objects_task.delay([get_content_tuple(photo)]) return photo
def save(self, *args, **kwargs): if not self.md5: self.md5 = md5sum(self.image_orig) if not self.orig_width or not self.orig_height: self.orig_width = self.image_orig.width self.orig_height = self.image_orig.height if not self.aspect_ratio: self.aspect_ratio = (float(self.image_orig.width) / float(self.image_orig.height)) if not self.focal_y and self.fov: dim = max(self.image_orig.width, self.image_orig.height) self.focal_y = 0.5 * dim / (self.image_orig.height * math.tan(math.radians(self.fov / 2))) if not self.license and self.flickr_user and self.flickr_id: self.license = License.get_for_flickr_photo( self.flickr_user, self.flickr_id) super(Photo, self).save(*args, **kwargs)
def handle(self, *args, **options): admin_user = User.objects.get_or_create( username='******')[0].get_profile() for filename in progress_bar(args): if not os.path.exists(filename): raise ValueError("File does not exist: '%s'" % filename) blendswap_id = os.path.basename(filename).split('_')[0] license, scene_url, scene_artist = \ License.get_for_blendswap_scene(blendswap_id) print 'file:', filename print 'license:', license print 'url:', scene_url print 'artist:', scene_artist tmpdir = tempfile.mkdtemp() try: print "Loading %s..." % filename md5 = md5sum(filename) if IntrinsicSyntheticDecomposition.objects.filter(md5=md5).exists(): print "Already added: %s" % filename continue multilayer = open_multilayer_exr( filename, tonemap=True, thumb_size=512, show_progress=True) paths = {} for key, img in multilayer.iteritems(): path = os.path.join(tmpdir, '%s-%s.jpg' % (md5, key)) img.save(path) paths[key] = path with transaction.atomic(): photo = add_photo( path=paths["combined"], user=admin_user, license=license, synthetic=True, whitebalanced=True, inappropriate=False, nonperspective=False, stylized=False, rotated=False, ) print "Uploading layers: %s..." % paths.keys() IntrinsicSyntheticDecomposition.objects.create( photo=photo, multilayer_exr=ImageFile(open(filename, 'rb')), scene_artist=scene_artist, scene_url=scene_url, md5=md5, **{ ("%s_thumb" % key): ImageFile(open(path, 'rb')) for key, path in paths.iteritems() if key != "combined" } ) finally: shutil.rmtree(tmpdir) update_synthetic_diff_intensity()
def handle(self, *args, **options): admin_user = User.objects.get_or_create( username='******')[0].get_profile() for filename in progress_bar(args): if not os.path.exists(filename): raise ValueError("File does not exist: '%s'" % filename) blendswap_id = os.path.basename(filename).split('_')[0] license, scene_url, scene_artist = \ License.get_for_blendswap_scene(blendswap_id) print 'file:', filename print 'license:', license print 'url:', scene_url print 'artist:', scene_artist tmpdir = tempfile.mkdtemp() try: print "Loading %s..." % filename md5 = md5sum(filename) if IntrinsicSyntheticDecomposition.objects.filter( md5=md5).exists(): print "Already added: %s" % filename continue multilayer = open_multilayer_exr(filename, tonemap=True, thumb_size=512, show_progress=True) paths = {} for key, img in multilayer.iteritems(): path = os.path.join(tmpdir, '%s-%s.jpg' % (md5, key)) img.save(path) paths[key] = path with transaction.atomic(): photo = add_photo( path=paths["combined"], user=admin_user, license=license, synthetic=True, whitebalanced=True, inappropriate=False, nonperspective=False, stylized=False, rotated=False, ) print "Uploading layers: %s..." % paths.keys() IntrinsicSyntheticDecomposition.objects.create( photo=photo, multilayer_exr=ImageFile(open(filename, 'rb')), scene_artist=scene_artist, scene_url=scene_url, md5=md5, **{("%s_thumb" % key): ImageFile(open(path, 'rb')) for key, path in paths.iteritems() if key != "combined"}) finally: shutil.rmtree(tmpdir) update_synthetic_diff_intensity()