web_index.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import time
  2. from flask import Flask, request
  3. import json
  4. import MTCNN_person
  5. app = Flask(__name__)
  6. @app.route('/')
  7. def hello_world():
  8. return 'Hello World!'
  9. import requests
  10. #图片链接
  11. headers ={
  12. 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
  13. }
  14. dir = 'C:\\Users\\yufen\\Pictures\\face\\'
  15. def do_download(url):
  16. url1 = url.split('?')[0]
  17. name = url1.split('/')[-1]
  18. r = requests.get(url, headers=headers)
  19. # 下载图片
  20. # 二进制数据需要用r.content 进行提取
  21. # 将图片放在‘图库’文件夹下,‘图库’是文件夹的名称,将图片放入该文件夹中,该文件夹与py文件在同一目录下
  22. f = open(dir + name, 'wb')
  23. f.write(r.content)
  24. f.close()
  25. return name
  26. @app.route('/download')
  27. def download():
  28. url = request.args.get('url')
  29. time1 = int(round(time.time() * 1000))
  30. print('info=%s' % url)
  31. do_download(url)
  32. time2 = int(round(time.time() * 1000))
  33. print('over %s from %s' % (time2 - time1, time1))
  34. return 'Hello World!'
  35. @app.route('/face')
  36. def face():
  37. info = request.args.get('url')
  38. time1 = int(round(time.time() * 1000))
  39. print('info=%s' % info)
  40. name = do_download(info)
  41. time2 = int(round(time.time() * 1000))
  42. faces = MTCNN_person.detector(dir + name)
  43. time3 = int(round(time.time() * 1000))
  44. print('over %s from %s' % (time3 - time1, time1))
  45. return json.dumps(faces)
  46. if __name__ == '__main__':
  47. app.run(port=5000)
  48. # do_download(url='https://img.bbztx.com/article/live150178/team217182/20230913192631_tmp_9fbaaecf98fd1a3e91a6571f744f4b34511300d6c5a5b358.jpg?x-oss-process=image/resize,h_1000,m_lfit')
  49. # MTCNN_person.detector('C:\\Users\\yufen\\Pictures\\face\\20230913192631_tmp_9fbaaecf98fd1a3e91a6571f744f4b34511300d6c5a5b358.jpg')