(原)下载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代码 ...
 
随机推荐
- jade中mixin的使用
			
h2 mixin mixin lesson p jade study +lesson mixin study(name,courses) p #{name} ul.courses each cours ...
 - 拉姆达表达式 追加 条件判断 Expression<Func<T, bool>>
			
public static class PredicateBuilder { /// <summary> /// 机关函数应用True时:单个AND有效,多个AND有效:单个OR无效,多个 ...
 - 函数式C代码
			
代码如下: #include <stdlib.h> #include <stdio.h> typedef ]; typedef FILE* File; typedef stru ...
 - LeetCode_Flatten Binary Tree to Linked List
			
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...
 - Qt下HBoxLayout里的按钮有重叠
			
没想到是一个bug,而且六年了都没有解决: https://bugreports.qt.io/browse/QTBUG-14591 http://stackoverflow.com/questions ...
 - 转:PHP分页技术的代码和示例
			
本文来自:10 Helpful PHP Pagination Scripts For Web Developers 分页是目前在显示大量结果时所采用的最好的方式.有了下面这些代码的帮助,开发人员可以在 ...
 - 需要考虑的9个SEO实践
			
搜索引擎优化重要吗?我们知道,网站设计是把屏幕上平淡无奇变成令人愉快的美感,更直观地辨认信息.这也是人与人之间在沟通想法,这样的方式一直在演变. 穴居人拥有洞穴壁画,古埃及人有象形文字,现代人有网页设 ...
 - MFC调用c#的dll
			
一.使用 /clr 编译 MFC 可执行文件或规则 DLL 1.打开“项目属性”对话框,方法是右键单击“解决方案资源管理器”中的项目并选择“属性”. 2.展开“配置属性”旁边的节点并选择“常规”.在右 ...
 - C#中通过位运算实现多个状态的判断
			
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
 - UGUI实现摇杆(模仿太极熊猫)
			
核心代码: using UnityEngine; using System.Collections; using UnityEngine.UI; public delegate void Joysti ...