【Python】爬取百度图片进行人脸识别

匿名 (未验证) 提交于 2019-12-02 22:51:30
import os,cv2,requests,json,re,time import tensorflow as tf from bs4 import BeautifulSoup  def check_path(path):     try:         a = []         for i in path.split('/'):             if i != '':                 a.append(i)         path = '/'.join(a)     except:         pass     return path  def decrypt_objURL(str):     """      :param str: 加密的图片地址     :return:解密后的图片地址 type=str     """     table = {'w': "a", 'k': "b", 'v': "c", '1': "d", 'j': "e", 'u': "f", '2': "g", 'i': "h",              't': "i", '3': "j", 'h': "k", 's': "l", '4': "m", 'g': "n", '5': "o", 'r': "p",              'q': "q", '6': "r", 'f': "s", 'p': "t", '7': "u", 'e': "v", 'o': "w", '8': "1",              'd': "2", 'n': "3", '9': "4", 'c': "5", 'm': "6", '0': "7",              'b': "8", 'l': "9", 'a': "0", '_z2C$q': ":", "_z&e3B": ".", 'AzdH3F': "/"}      url = re.sub(r'(?P<value>_z2C\$q|_z\&e3B|AzdH3F+)', lambda matched: table.get(matched.group('value')),str)     new_url = re.sub(r'(?P<value>[0-9a-w])', lambda matched: table.get(matched.group('value')), url)      return new_url  def Request_Img(word='佟丽娅',imgNum=300):     objURL_list = []     for i,page in enumerate(range(0,imgNum,30)):         Url = 'http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&word={}&pn={}'.format(word,str(page))         response = requests.get(url=Url).json()['data']         # print(response)         try:             for img in response:                 url = decrypt_objURL(img['objURL'])                 # print(url)                 objURL_list.append(url)         except Exception as e:             print('出现异常!!!',e)      return objURL_list  def Face_Detection(urllist,savepath='./TLY'):     if len(urllist) != 0:         for url in urllist:             print(url)             try:                 re = requests.get(url=url).content                 with open('./.img','wb') as f:                     f.write(re)                 face_cascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')                 img = cv2.imread('./.img')                 # cv2.imshow('etst',img)                 # cv2.waitKey(10)                 # cv2.destroyAllWindows()                 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)                 faces = face_cascade.detectMultiScale(gray,                                                       scaleFactor=1.15,                                                       minNeighbors=10,                                                       minSize=(1,1))                 if len(faces) != 0:                     print(faces)                     for x,y,w,h in faces:                         if not os.path.exists(savepath):                             os.mkdir(savepath)                         if not os.path.exists(check_path(savepath+'/face')):                             os.mkdir(check_path(savepath+'/face'))                         name = ''.join(str(time.time()).split('.'))                         cv2.imwrite(savepath+'/face/'+name+'_face'+'.jpg',img[y-10:y+h+10,x-10:x+w+10])                         cv2.imwrite(savepath+'/'+name+'.jpg',cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2))              except:                 pass   if __name__ == '__main__':     Face_Detection(Request_Img())

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!