def sizeAttrs(graph, foafUser, uri, sizeName): # this is similar to serve.ImageSizeResponse.renderHTTP, to avoid # an http call size = sizes[sizeName] r = MediaResource(graph, uri) w, h = r.getSize(size) return {'width': w, 'height': h}
def imageDoc(self, uri): # check image first- maybe it was deleted try: t = photoCreated(self.graph, uri, useImageSet=False) unixTime = time.mktime(t.timetuple()) # untested except ValueError: t = None unixTime = 0 m = MediaResource(self.graph, uri, allVideos=self.allVideos) viewableBy = [] doc = { 'uri': uri, 't': t, # may be none 'unixTime': unixTime, # always a number, maybe 0 'isVideo': m.isVideo(), 'tags': set(unicode(lit) for lit in getTagLabels(self.graph, 'todo', uri)), } doc['_docTime'] = time.time() - t1 return [doc]
def GET(self): uri = URIRef(web.input()['uri']) r = MediaResource(graph, uri) size = r.getSize(sizes["screen"]) try: created = photoCreated(graph, uri) prettyDate = created.date().isoformat() except ValueError: prettyDate = "(unknown date)" tmpl = loader.load("sharesingle.html") stream = tmpl.generate( title="photo", prettyDate=prettyDate, bestJqueryLink=networking.jqueryLink( web.ctx.environ.get('HTTP_X_FORWARDED_FOR', '')), featuredImg=Markup('<img src="%s" width="%s" height="%s"/>' % (localSite(uri)+"?size=screen", size[0], size[1])), loginWidget=Markup(networking.getLoginBarSync( web.ctx.environ.get('HTTP_COOKIE', ''))), actionsAllowed=[], otherSizeLinks=[], link="l", allowedToWriteMeta=False, pageJson="", ) return (''.join(serializer(stream))).encode('utf8')
def sizeAttrs(graph, foafUser, uri, sizeName): # this is similar to serve.ImageSizeResponse.renderHTTP, to avoid # an http call size = sizes[sizeName] r = MediaResource(graph, uri) w, h = r.getSize(size) return {'width' : w, 'height' : h}
def onChange(filename): filename = fixZfsMount(filename) for root, sync in syncs.items(): if filename.startswith(root): # this one reads .n3 files into sesame log.info("rdf data sync on %r", filename) sync.someChange(filename, doubleCheckMtime=True) break if filename.startswith('/my/pic') and '/.hide/' not in filename: # this one wants to hear about image files for path/exif data if filename.startswith('/my/pic/upload'): fixSftpPerms() log.info("scanFs and scanExif on %r", filename) picUri = scanFs.fileChanged(filename) log.info('picUri is %r', picUri) if picUri is not None: # this will fail on videos (though i wish i could get the Pre metadata out of them) scanExif.addPic(picUri, rerunScans=True) mr = MediaResource(graph, picUri) if mr.isVideo(): mr.videoProgress() # todo: freshen thumbs here too? that should be on a lower # priority queue than getting the exif/file data v2.imageset.client.changed(picUri)
def scanPic(self, uri): mr = MediaResource(graph, uri) jpg, mtime = mr.getImageAndMtime(1000) mat = cv.CreateMatHeader(1, len(jpg), cv.CV_8UC1) cv.SetData(mat, jpg, len(jpg)) img = cv.DecodeImage(mat) grayscale = cv.CreateImage((img.width, img.height), 8, 1) cv.CvtColor(img, grayscale, cv.CV_RGB2GRAY) cv.EqualizeHist(grayscale, grayscale) storage = cv.CreateMemStorage(0) faces = cv.HaarDetectObjects( grayscale, self.cascade, storage, 1.2, # scaleFactor between scans 3, # minNeighbors cv.CV_HAAR_DO_CANNY_PRUNING, (20, 20) # min window size ) size = cv.GetSize(grayscale) for f, neighbors in faces: desc = { 'source': str(uri), 'types': [PHO.Crop], 'tag': 'face', 'x1': f[0] / size[0], 'y1': f[1] / size[1], 'x2': (f[0] + f[2]) / size[0], 'y2': (f[1] + f[3]) / size[1], # this ought to have a padded version for showing, and # also the face coords inside that padded version, for # recognition. Note that the padded one may run into # the margins 'neighbors': neighbors, } alt = restkit.Resource( uri.replace('http://photo.bigasterisk.com/', 'http://bang:8031/') + "/alt") resp = alt.post(payload=json.dumps(desc), headers={ 'content-type': 'application/json', 'x-foaf-agent': 'http://bigasterisk.com/tool/scanFace' }) print resp.status, resp.body_string()
class Build(object): def __init__(self): pool = Pool(processes=2) self.graph = getGraph() files = findFiles(opts) self.progressQueue = Queue() reporter = Process(target=ProgressReport, args=(self.progressQueue, len(files))) reporter.start() result = pool.map(self.cacheFile, enumerate(files), chunksize=5) self.progressQueue.put('END') log.info("finished, %s results", len(result)) reporter.join() def cacheFile(self, (i, filename)): log.debug("cacheFile %s" % filename) try: uri = photoUri(filename) m = MediaResource(self.graph, uri) # video extension but not isVideo? where do we correct that? m.cacheAll() except Exception: traceback.print_exc() self.progressQueue.put("%s" % filename)
def scanPic(self, uri): mr = MediaResource(graph, uri) jpg, mtime = mr.getImageAndMtime(1000) mat = cv.CreateMatHeader(1, len(jpg), cv.CV_8UC1) cv.SetData(mat, jpg, len(jpg)) img = cv.DecodeImage(mat) grayscale = cv.CreateImage((img.width, img.height), 8, 1) cv.CvtColor(img, grayscale, cv.CV_RGB2GRAY) cv.EqualizeHist(grayscale, grayscale) storage = cv.CreateMemStorage(0) faces = cv.HaarDetectObjects( grayscale, self.cascade, storage, 1.2, # scaleFactor between scans 3, # minNeighbors cv.CV_HAAR_DO_CANNY_PRUNING, (20, 20), # min window size ) size = cv.GetSize(grayscale) for f, neighbors in faces: desc = { "source": str(uri), "types": [PHO.Crop], "tag": "face", "x1": f[0] / size[0], "y1": f[1] / size[1], "x2": (f[0] + f[2]) / size[0], "y2": (f[1] + f[3]) / size[1], # this ought to have a padded version for showing, and # also the face coords inside that padded version, for # recognition. Note that the padded one may run into # the margins "neighbors": neighbors, } alt = restkit.Resource(uri.replace("http://photo.bigasterisk.com/", "http://bang:8031/") + "/alt") resp = alt.post( payload=json.dumps(desc), headers={"content-type": "application/json", "x-foaf-agent": "http://bigasterisk.com/tool/scanFace"}, ) print resp.status, resp.body_string()
def imageResource(self, uri, ctx, t1, t2): r = MediaResource(graph, uri) size = getRequestedSize(ctx) useMp4 = ctx.arg('type') == 'mp4' jpg, mtime = r.getImageAndMtime(size, useMp4=useMp4) if r.isVideo(): if size is Video2: if useMp4: ct = 'video/mp4' else: ct = 'video/webm' else: ct = 'application/binary' else: ct = 'image/jpeg' if uri.endswith('webm'): ct = 'video/webm' return StaticCached(jpg, ct, mtime, t1, t2)
def imageResource(self, uri, ctx, t1, t2): r = MediaResource(graph, uri) size = getRequestedSize(ctx) useMp4 = ctx.arg('type') == 'mp4' jpg, mtime = r.getImageAndMtime(size, useMp4=useMp4) if r.isVideo(): if size is Video2: if useMp4: ct = 'video/mp4' else: ct = 'video/webm' else: ct = 'application/binary' else: ct = 'image/jpeg' if uri.endswith('webm'): ct = 'video/webm' return StaticCached(jpg, ct, mtime, t1, t2)
def photosInSetPlus(self): """for use by other tools who want to draw some photos """ out = [] for p in self.desc.photos(): r = MediaResource(self.graph, p) try: s = r.getSize(sizes["thumb"]) thumbSize = {"thumbSize": dict(w=s[0], h=s[1])} except (ValueError, IOError, subprocess.CalledProcessError): thumbSize = {} out.append( dict(link=absoluteSite(self.desc.otherImageUrl(p)), uri=p, facts=self.facts(p), thumb="%s?size=thumb" % p, screen="%s?size=screen" % p, isVideo=self.desc.isVideo(p))) out[-1].update(thumbSize) return out
def photosInSetPlus(self): """for use by other tools who want to draw some photos """ out = [] for p in self.desc.photos(): r = MediaResource(self.graph, p) try: s = r.getSize(sizes["thumb"]) thumbSize = {"thumbSize" : dict(w=s[0], h=s[1])} except (ValueError, IOError, subprocess.CalledProcessError): thumbSize = {} out.append(dict( link=absoluteSite(self.desc.otherImageUrl(p)), uri=p, facts=self.facts(p), thumb="%s?size=thumb" % p, screen="%s?size=screen" % p, isVideo=self.desc.isVideo(p) )) out[-1].update(thumbSize) return out
def onChange(filename): for root, sync in syncs.items(): if filename.startswith(root): # this one reads .n3 files into sesame log.info("rdf data sync on %r", filename) sync.someChange(filename, doubleCheckMtime=True) break if filename.startswith('/my/pic') and '/.hide/' not in filename: # this one wants to hear about image files for path/exif data if filename.startswith('/my/pic/upload'): fixSftpPerms() log.info("scanFs and scanExif on %r", filename) picUri = scanFs.fileChanged(filename) log.info('picUri is %r', picUri) if picUri is not None: # this will fail on videos (though i wish i could get the Pre metadata out of them) scanExif.addPic(picUri, rerunScans=True) mr = MediaResource(graph, picUri) if mr.isVideo(): mr.videoProgress()
def featured(self): current = self.desc.currentPhoto() if current is None: return '' currentLocal = localSite(current) _, nextUri = self.prevNext() feat = MediaResource(self.graph, current) if feat.isVideo(): progress = feat.videoProgress() if progress is Done: w, h = feat.getSize(Video2) return dict( video=dict( sources=[ dict(src=currentLocal+"?size=video2", type='video/webm'), dict(src=currentLocal+"?size=video2&type=mp4", type='video/mp4') ], width=600, height=600 / w * h)) else: return dict(videoNotReady=dict( progress=progress, failed=isinstance(progress, FailedStatus))) else: try: size = feat.getSize(sizes["large"]) except (ValueError, IOError) as e: log.warn('current=%r', current) import traceback;traceback.print_exc() size = (0,0) marg = (602 - 2 - size[0]) // 2 return dict(image=dict( nextClick=self.desc.otherImageUrl(nextUri), src=currentLocal+"?size=large", w=size[0], h=size[1], marg=marg, alt=self.graph.label(current), ))
def gatherDocs(self, uri): t1 = time.time() # check image first- maybe it was deleted try: t = photoCreated(self.graph, uri) unixTime = time.mktime(t.timetuple()) # untested except ValueError: t = None unixTime = 0 m = MediaResource(self.graph, uri) viewableBy = [] doc = { 'uri': uri, 't': t, # may be none 'unixTime': unixTime, # always a number, maybe 0 'isVideo': m.isVideo(), 'tags': set(str(lit) for lit in getTagLabels(self.graph, 'todo', uri)), } doc['_docTime'] = time.time() - t1 return [doc]
def featured(self): current = self.desc.currentPhoto() if current is None: return '' currentLocal = localSite(current) _, nextUri = self.prevNext() feat = MediaResource(self.graph, current) if feat.isVideo(): progress = feat.videoProgress() if progress is Done: w, h = feat.getSize(Video2) return dict(video=dict(sources=[ dict(src=currentLocal + "?size=video2", type='video/webm'), dict(src=currentLocal + "?size=video2&type=mp4", type='video/mp4') ], width=600, height=600 / w * h)) else: return dict(videoNotReady=dict(progress=progress, failed=isinstance( progress, FailedStatus))) else: try: size = feat.getSize(sizes["large"]) except (ValueError, IOError) as e: log.warn('current=%r', current) import traceback traceback.print_exc() size = (0, 0) marg = (602 - 2 - size[0]) // 2 return dict(image=dict( nextClick=self.desc.otherImageUrl(nextUri), src=currentLocal + "?size=large", w=size[0], h=size[1], marg=marg, alt=self.graph.label(current), ))
def localPath(self): cur = self.desc.currentPhoto() if cur is None: return '(none)' return MediaResource(self.graph, cur).sourcePath()
#!bin/python from db import getGraph from mediaresource import MediaResource graph = getGraph() for row in graph.queryd(""" SELECT ?pic WHERE { ?pic a foaf:Image . { ?pic exif:orientation exif:bottom-right } UNION { ?pic exif:orientation exif:right-top } }"""): MediaResource(graph, row['pic']).purgeCached()