(原)下载pubFig的python代码
转载请注明出处:
http://www.cnblogs.com/darkknightzh/p/5715305.html
pubFig数据库网址:
http://www.cs.columbia.edu/CAVE/databases/pubfig/
由于版权的原因,该数据库未提供图片,只提供了图片的链接,并且某些链接已经失效。
说明:1. 某些网址需要跨越绝境长城,因而最好开代理
2. dev_urls.txt和eval_urls.txt均可在官网下载。
3. python新手,因而程序写的不好看,并且还有问题。。。
问题1:文件不存在,这个没法避免。
问题2:有时候链接某个url时,时间很长,之后会抛出异常,并提示类似下面的信息:
HTTPConnectionPool(host='www.stardepot.ca', port=): Max retries exceeded with url: /img/Miley_Cyrus_27.jpg (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x02AAC3B0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
暂时不知道怎么解决。
__author__ = 'XXX' import os
import numpy as np
import urllib
import re # regular expression libiary
import requests
import time def findAllStrLoc(inStr, findStr):
loc = []
start = 0
while True:
curLoc = inStr.find(findStr, start)
if curLoc == -1: # if search string not found, find() returns -1
break # search is complete, break out of the while loop
start = curLoc + 1 # move to next possible start position
loc.append(curLoc)
return loc def loadData(dataPath, startLine):
datas = []
f = open(dataPath, 'r') # with open(dataPath, 'r') as f:
for line in f.readlines()[startLine:]:
# data = line.strip().split()
loc = findAllStrLoc(line, '\t')
data = []
data.append(line[0:(loc[0])]) # person # the end index of the sub str is excluded
data.append(line[loc[0]+1:loc[1]]) # imagenum
data.append(line[loc[1]+1:loc[2]]) # url
rect = line[loc[2]+1:loc[3]] # rect
rectLoc = re.findall(r'\d+', rect)
for ind in range(len(rectLoc)):
data.append(rectLoc[ind])
data.append(line[loc[3]+1:len(line)-1]) # md5sum
datas.append(data)
f.close()
return np.array(datas) # datas def createimgfolder(imgFolder):
if not os.path.isdir(imgFolder):
os.makedirs(imgFolder) def getImgNameFromURL(url):
loc = findAllStrLoc(url, '/')
imgName = url[loc[len(loc)-1]+1:]
txtName = imgName.split('.')[0] + '.txt'
return (imgName, txtName) def exists(path):
r = requests.head(path)
return r.status_code == requests.codes.ok def main():
print('loading data')
imgInfo = loadData('D:/dev_urls.txt', 2)
print('finish loading data\n') databaseFolder = 'D:/pubFig'
createimgfolder(databaseFolder) for i in range(9526, len(imgInfo)):
curtime = time.strftime('%y%m%d-%H%M%S',time.localtime())
imgFolder = databaseFolder + '/' + imgInfo[i][0]
createimgfolder(imgFolder)
url = imgInfo[i][2]
(imgName, txtName) = getImgNameFromURL(url)
try:
if exists(url):
page = urllib.urlopen(url)
img = page.read()
page.close()
imgPath = imgFolder + '/' + imgName
f = open(imgPath, "wb")
f.write(img)
f.close() txtPath = imgFolder + '/' + txtName
f = open(txtPath, "w")
for j in range(4):
f.write(imgInfo[i][j+3] + ' ')
f.close()
print('%s:%d/%d %s finish'%(curtime, i+1, len(imgInfo), url))
else:
print('%s:%d/%d %s does not exist'%(curtime, i+1, len(imgInfo), url))
except (Exception) as e:
print('%s:%d/%d %s exception %s'%(curtime, i+1, len(imgInfo), url, e)) print('finish') if __name__ == '__main__':
main()
(原)下载pubFig的python代码的更多相关文章
- beamer中插入c代码,python代码的经验
下面是插入的scala代码,它与python在某些语法上类似,所在在https://github.com/olivierverdier/python-latex-highlighting下载了一个py ...
- 单链表反转的原理和python代码实现
链表是一种基础的数据结构,也是算法学习的重中之重.其中单链表反转是一个经常会被考察到的知识点. 单链表反转是将一个给定顺序的单链表通过算法转为逆序排列,尽管听起来很简单,但要通过算法实现也并不是非常容 ...
- [转] Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- Python代码性能优化技巧
摘要:代码优化能够让程序运行更快,可以提高程序的执行效率等,对于一名软件开发人员来说,如何优化代码,从哪里入手进行优化?这些都是他们十分关心的问题.本文着重讲了如何优化Python代码,看完一定会让你 ...
- Python 代码性能优化技巧(转)
原文:Python 代码性能优化技巧 Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化. ...
- Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- 如何在Java中调用Python代码
有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...
- 在Java中调用Python代码
极少数时候,我们会碰到类似这样的问题:与A同学合作写代码, A同学只会写Python,不熟悉Java ,而你只会写Java不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方 ...
- Effective Python之编写高质量Python代码的59个有效方法
这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码 ...
随机推荐
- 求实现sql?
id name pid1 曾祖父 02 祖父 13 父亲 24 儿子 35 孙子 4备注:用一条数据库语句来解决查询结果:name1 name2 name3曾祖父 祖父 父亲曾祖父 父亲 儿子曾祖父 ...
- Jasper_sheetName_defined by parameter or hard coding or filed name
1.根据传递的参数定义sheet name (jasper sheet name defined by parameter) (1) 获取后台参数 <parameter name="P ...
- IOS响应式编程框架ReactiveCocoa(RAC)使用示例-备
ReactiveCocoa是响应式编程(FRP)在IOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...
- 必须弄懂的495个C语言问题
1.1 我如何决定使用那种整数类型? 如果需要大数 值(大于32, 767 或小于¡32, 767), 使用long 型.否则, 如果空间很重要(如有大数组或很多结构), 使用short 型.除此之外 ...
- position 为absolute时/float 为right,span为block
元素分为内联元素和区块元素两类(当然也有其它的),在内联元素中有个非常重要的常识,即内两元素是不可以设置区块元素所具有的样式,例如:width | height.relative : 原来是什么类型的 ...
- 统计的基本操作语法 <第五篇>
1.创建统计语法: CREATE STATISTICS statistics_name ON { table_or_indexed_view_name } ( column [ ,...n ] ) [ ...
- 关于sencha touch 的JSONP跨域请求的学习研究
此篇文章是对自己在研究学习sencha touch的过程中的点滴记录,主要是JSONP的跨域请求这方面,对于何为是跨域概念还有不熟悉的,可以自己问下度娘. 先上张图: 我要完成的功能就是表格下拉刷新, ...
- 负载均衡 > 常见问题
证书管理相关问题 常用证书申请流程 1.本地生成私钥:openssl genrsa -out privateKey.pem 2048 其中privateKey.pem为您的私钥文件,请妥善保管. 2. ...
- 比较有用的log4j.properties
转自 http://walsh.iteye.com/blog/314941 log4j.properties log4j.rootLogger=DEBUG,CONSOLE,DATABASE,FIL ...
- Sql Server 2005的1433端口打开和进行远程连接
参考地址:http://topic.csdn.net/u/20090828/16/e693935a-99b7-4090-a6bc-0123c91183eb.html 1.如何打开sql server ...