当前位置: 首页>>代码示例>>Python>>正文


Python BlockStore.byid方法代码示例

本文整理汇总了Python中blockstore.BlockStore.byid方法的典型用法代码示例。如果您正苦于以下问题:Python BlockStore.byid方法的具体用法?Python BlockStore.byid怎么用?Python BlockStore.byid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在blockstore.BlockStore的用法示例。


在下文中一共展示了BlockStore.byid方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from blockstore import BlockStore [as 别名]
# 或者: from blockstore.BlockStore import byid [as 别名]
class TinyImage:
  def __init__(self):
    self.config = config.config()  
    self.meta = BlockStore(self.config['tinyimages']['metapath'], 768)
    self.data = BlockStore(self.config['tinyimages']['datapath'], 3072) 
    self.img_count = 79302017
 
  #public functions
  def byid(self, ids):
    if isinstance(ids, int):
      return numpy.fromstring(self.data.byid(ids), dtype='uint8')    
    elif isinstance(ids, tuple):
      o = []
      for s in self.data.slice(ids[0], ids[1]):
        o.append(numpy.fromstring(s, dtype='uint8'))
      return o 
    else:
      o = []
      for i in ids:
        o.append(numpy.fromstring(self.data.byid(i), dtype='uint8'))  
      return o

  def display(self, items):
    import cStringIO as StringIO
    import base64
    from IPython.core.display import HTML
    output_html = ""
    for i in items:
      t = i.reshape(32,32,3, order="F").copy()
      img = scipy.misc.toimage(t) 
      output = StringIO.StringIO()
      img.save(output, format="PNG")
      output_html += '<img src="data:image/png;base64,%s"/>' % base64.b64encode(output.getvalue())
    return HTML(output_html) 
    
  def search(self, keyword, limit):
    (l, h) = self._logSearch(keyword)
    found = False
    found_count = 0
    o = []
    for i in range(l, h):
      curr_word = self._keywordFromMeta(i)
      if curr_word.lower() == keyword.lower():
        found = True
        o.append(i)
        found_count += 1
        if (found_count == limit):
          break
      else:
        if (found):
          break  
    return o

  def _keywordFromMeta(self, index):
    for s in self.meta.slice(index, index):
      return s[0:80].strip()

  def _logSearch(self, term):
    low = 0
    high = self.img_count
    for i in range(0, 9):
      curr_word = self._keywordFromMeta(int((low + high) / 2))
      cmp = _strcmp(curr_word.lower(), term.lower())
      if (cmp == 0):
        return (low, high)
      if (cmp == 1):
        high = ((low + high) / 2)
      if (cmp == -1):
        low = ((low + high) / 2)
    return (low, high)

  def subsets(self):
    return None
开发者ID:cioc,项目名称:DAL,代码行数:75,代码来源:tinyimages_deprecated.py


注:本文中的blockstore.BlockStore.byid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。