你用 Python 写过哪些牛逼的程序/脚本?

编译: python开发者 - jake_on 英文:quoraments thank you on all posts23.#todo convert to batch request later24.forwallpost inwallposts:25.26.r = requests.get('https://graph.facebook/%s' %27.wallpost['actor_id'])28.url = 'https://graph.facebook/%s/comments' % wallpost['post_id']29.user = json.loads(r.text)30.message = 'thanks %s :)' % user['first_name']31.payload = {'access_token': token,'message': message}32.s = requests.post(url,data=payload)33.34.printwall post %s done % wallpost['post_id']35.36.if__name__ == '__main__':37mentall(get_posts())…
为了能够顺利运行脚本,你需要从graph api explorer(需适当权限)获得 token。 本脚本假设特定时间戳之后的所有帖子都是生日祝福。
尽管对评论功能做了一点改变,我仍然喜欢每一个帖子。
当我的点赞数,评论数以及评论结构在 ticker(facebook一项功能,朋友可以看到另一个朋友在做什么,比如点赞,听歌,看电影等) 中爆涨后,我的一个朋友很快发现此事必有蹊跷。
尽管这个不是我最满意的脚本,但是它简单,快捷,有趣。
当我和 sandesh agrawal 在网络实验室讨论时,有了写这个脚本的想法。 为此,sandesh agrawal 耽搁了实验室作业,深表感谢。
tanmay kulshrestha,3300+ 顶
好了,在我失去这个项目之前(一个猪一样的朋友格式化了我的硬盘,我的所有代码都在那个硬盘上)或者说,在我忘记这些代码之前,我决定来回答这个问题。
5、整理照片
当我对图像处理感兴趣之后,我一直致力于研究机器学习。我写这个有趣的脚本,目的是为了分类图片,很像 facebook 做的那样(当然这是一个不够精确的算法)。 我使用了 opencv 的人脸检测算法,“haarcascade_frontalface_default.xml”,它可以从一张照片中检测到人脸。
你可能已经察觉到这张照片的某些地方被错误地识别为人脸。 我试图通过修改一些参数(来修正这一问题),但还是某些地方被错误地识别为人脸,这是由相机的相对距离导致的。我会在下一阶段解决这一问题(训练步骤)。
这个训练算法需要一些训练素材,每个人需要至少需要100-120个训练素材(当然多多益善)。 我太懒了,并没有为每一个人挑选照片,并把它们复制粘帖到训练文件夹。所以,你可能已经猜到,这个脚本会打开一个图片,识别人脸,并显示每一个人脸(脚本会根据处于当前节点的训练素材给每一个人脸预测一个名字)。伴随着每次你标记的照片,recognizer 会被更新,并且还会包含上一次的训练素材。 在训练过程中,你可以增加新的名字。我使用 python 库 tkinter 做了一个 gui。 因此,大多数时候,你必须初始化一小部分照片(给照片中的人脸命名),其他的工作都可以交给训练算法。 因此,我训练了 recognizer ,然后让它(recognizer)去处理所有的图片。
我使用图片中包含的人的人名来命名图片,(例如: tanmay&*****&*****)。 因此,我可以遍历整个文件夹,然后可以通过输入人名的方法来搜索图片。
初始状态下,当一个人脸还没有训练素材时(素材库中还没有包括这个人脸的名字),需要询问他/她的名字。
我可以增加一个名字,像这个样子:
当训练了几个素材后,它会像这个样子:
最后一个是针对应对那些垃圾随机方块而使用的变通解决方案。
带名字的最终文件夹。
所以,现在寻找图片变得相当简单。顺便提一下,很抱歉(我)放大了这些照片。
…importcv2importsysimportos,random,string#choices=['add a name']importoscurrent_directory=os.path.dirname(os.path.abspath(__file__))fromtkinterimporttkfromeasygui import *importnumpy asnpx= os.listdir(current_directory)new_x=[]testing=[]foriinx:ifi.find('.')==-1:new_x+=[i]else:testing+=[i]x=new_xg=xchoices=['add a name']+xy= range(1,len(x)+1)defget_images_and_labels():globalcurrent_directory,x,y,gifx==[]:return(false,false)image_paths=[]foriing:path=current_directory+''+iforfilename inos.listdir(path):final_path=path+''+filenameimage_paths+=[final_path]# images will contains face imagesimages = []# labels will contains the label that is assigned to the imagelabels = []forimage_path inimage_paths:# read the image and convert to grayscaleimg = cv2.imread(image_path,0)# convert the image format into numpy arrayimage = np.array(img,'uint8')# get the label of the imagebackslash=image_path.rindex('')underscore=image_path.index('_',backslash)nbr = image_path[backslash+1:underscore]t=g.index(nbr)nbr=y[t]# if face is detected, append the face to images and the label to labelsimages.append(image)labels.append(nbr)#cv2.imshow(adding faces to traning set..., image)#cv2.waitkey(50)# return the images list and labels listreturnimages,labels# perform the traniningdeftrain_recognizer():recognizer = cv2.createlbphfacerecognizer()images,labels = get_images_and_labels()ifimages==false:returnfalsecv2.destroyallwindows()recognizer.train(images,np.array(labels))returnrecognizerdefget_name(image_path,recognizer):globalx,choices#if recognizer=='':# recognizer=train_recognizer()cascadepath = haarcascade_frontalface_default.xmlfacecascade = cv2.cascadeclassifier(cascadepath)#recognizer=train_recognizer()x1=testingglobalgprintimage_pathimage = cv2.imread(image_path)img = cv2.cvtcolor(image,cv2.color_bgr2gray)predict_image = np.array(img,'uint8')faces = facecascade.detectmultiscale(img,scalefactor=1.3,minneighbors=5,minsize=(30,30),flags = http://cv2.cv.cv_haar_scale_image)for(x,y,w,h)infaces:f= image[y:y+w,x:x+h]cv2.imwrite('temp.jpg',f)im='temp.jpg'nbr_predicted,conf = recognizer.predict(predict_image[y: y + h,x: x + w])predicted_name=g[nbr_predicted-1]print{} is correctly recognized with confidence {}.format(predicted_name,conf)ifconf>=140:continuemsg='is this '+predicted_namereply = buttonbox(msg,image=im,choices=['yes','no'])ifreply=='yes':reply=predicted_namedirectory=current_directory+''+replyifnotos.path.exists(directory):os.makedirs(directory)random_name=''.join(random.choice(string.ascii_uppercase + string.digits)for_inrange(7))path=directory+''+random_name+'.jpg'cv2.imwrite(path,f)else:msg = who is this?reply = buttonbox(msg,image=im,choices=choices)ifreply == 'add a name':name=enterbox(msg='enter the name',title='training',strip=true)printnamechoices+=[name]reply=namedirectory=current_directory+''+replyifnotos.path.exists(directory):os.makedirs(directory)random_name=''.join(random.choice(string.ascii_uppercase + string.digits)for_inrange(7))path=directory+''+random_name+'.jpg'printpathcv2.imwrite(path,f)# calculate window positionroot = tk()pos = int(root.winfo_screenwidth() * 0.5),int(root.winfo_screenheight() * 0.2)root.withdraw()windowposition = +%d+%d % pos# patch rootwindowpositionrootwindowposition = windowpositiondefdetect_faces(img):globalchoices,current_directoryimagepath = imgfacecascade = cv2.cascadeclassifier(cascpath)image = cv2.imread(imagepath)gray = cv2.cvtcolor(image,cv2.color_bgr2gray)faces = facecascade.detectmultiscale(gray,scalefactor=1.3,minneighbors=5,minsize=(30,30),flags = http://cv2.cv.cv_haar_scale_image)printfound {0} faces!.format(len(faces))m=0for(x,y,w,h)infaces:m...