根据接口返回的结果。对男女年龄段的做统计数据(这个是一个例子。只供参考)

#coding:utf-8
#!/usr/bin/env python
import base64
import os
import requests
import time
from hashlib import sha1
from urllib import quote
import hmac
import sys
reload(sys)
sys.setdefaultencoding('utf-8')



#不要按照当前的方式运行。由于是特殊原因,去掉核心部分。只提供的了。思考的方式,提供参考
def Filepath():
'''路径'''
cur_path1 = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
return cur_path1

def ImagName():
'''遍历目录下jpg的文件'''
if not os.path.exists(os.path.join(Filepath(),"bak")):
return " folder location path : %s "%os.path.join(Filepath(),"bak")
else:
filepath =os.walk(os.path.join(Filepath(),"bak"))
jpglist = []
for path,d,filelists in filepath:
for filename in filelists:
if filename.endswith("jpg"):
jpglist.append(os.path.join(path,filename))
return jpglist

def convert_image(filepath):
'''将文件转换成base64编码'''
# Picture ==> base64 encode
with open(filepath, 'rb') as fin:
image_data = fin.read()
base64_data = base64.b64encode(image_data)
return base64_data
def Face_detet(picture64):
URL1 = "/hsr/v4/face_detect"
URL ="xxx.xxx.xxxx"
data = {
"image":picture64,
"face_num":1,
  }
r = requests.post(URL,data=data)
return r

def localtime(glocal):
'''
glocal = '2018-3-31 00:00:00'
'''
settime = int(time.mktime(time.strptime(glocal,'%Y-%m-%d %H:%M:%S')))
return settime
if __name__ == "__main__":
error = []#记录
ltime = localtime('2018-4-8 23:59:59')
oldtime = localtime('2018-3-30 00:00:00')
daysec = 24 * 60 * 60
count_filepath = [] #存储list对应的每天的图片个数

dd = '%Y-%m-%d %H:%M:%S'
for d in range(int(( ltime - oldtime )/daysec) + 1) : #时间区间数
startday = oldtime + daysec * d #0:0:0
daylast = oldtime + daysec * ( d + 1) -1 #23:59:59
for i in ImagName():
if startday < int(i.split("\\")[-1][:10]) < daylast: #文件的命名格式是时间戳的格式,所以取文件的时间,使用区间的方式,划分超过4-8号。小于3-30号的都过滤
count_filepath.append([startday,i])
for i in range(int(( ltime - oldtime )/daysec)):
daytime = oldtime + daysec * i
listmale30 = []#记录小于30岁之间的男性人数
listmale31 = []#记录大于30岁之间的男性人数
listfemale30 = []#记录小于30岁之间的女性人数
listfemale31 = []#记录大于30岁之间的女性人数
for u in range(len(count_filepath)):
if daytime in count_filepath[u]:
try:
faceret = Face_detet(convert_image(count_filepath[u][1])).json()
sttt = float(faceret['data'][0]['age'].decode('gb2312').encode('utf-8'))
if faceret['data'][0]['sex'] == "male" and int(sttt) < 30:
listmale30.append([daytime,count_filepath[u][1]]) #将取到的数据存到list中,list中存了时间和本地的文件的路径
elif faceret['data'][0]['sex'] == "male" and int(sttt) > 30:
listmale31.append([daytime,count_filepath[u][1]])
elif faceret['data'][0]['sex'] == "female" and int(sttt) < 30:
listfemale30.append([daytime,count_filepath[u][1]])
elif faceret['data'][0]['sex'] == "female" and int(sttt) > 30:
listfemale31.append([daytime,count_filepath[u][1]])
except:
if faceret['data'] == []:
error.append("not face picture %s msg: %s"%(count_filepath[u][1],faceret['msg']))
a = 0
     #根据时间的方式,判断是否在其中。增加数值
for m1 in range(len(listmale30)):
if daytime in listmale30[m1]:
a+=1
print "%s male < 30 count = %s "%(time.strftime(dd,time.localtime(int(daytime))),a)
b = 0

for m2 in range(len(listmale31)):
if daytime in listmale31[m2]:
b+=1
print "%s male > 30 count = %s "%(time.strftime(dd,time.localtime(int(daytime))),a)

c = 0
for f1 in range(len(listfemale30)):
if daytime in listfemale30[f1]:
c+=1
print "%s male < 30 count = %s "%(time.strftime(dd,time.localtime(int(daytime))),c)

d = 0
for f2 in range(len(listfemale31)):
if daytime in listfemale30[f2]:
c+=1
print "%s male > 30 count = %s "%(time.strftime(dd,time.localtime(int(daytime))),d)

#后续有空。看看能否报表的形式展现


结果:

[u'not face picture D:\\test\\yf_test\\bak\\1522413610_3955.jpg msg: ok', u'not face picture D:\\test\\yf_test\\bak\\1522414262_4117.jpg msg: ok', u'not face picture D:\\test\\yf_test\\bak\\1522414324_4118.jpg msg: ok', u'not face picture D:\\test\\yf_test\\bak\\1522414336_4142.jpg msg: ok']
2018-03-30 00:00:00 male < 30 count = 12
2018-03-30 00:00:00 male > 30 count = 12
2018-03-30 00:00:00 male < 30 count = 6
2018-03-30 00:00:00 male > 30 count = 0

posted @ 2018-04-12 17:01  小~yytt~  阅读(387)  评论(0编辑  收藏  举报