1+ import base64
2+ import os
3+
4+ import subprocess
5+ import time
6+ import requests
7+ from pyecharts .charts import Bar , Pie
8+ from pyecharts import options as opts
9+
10+ class zhubo ():
11+
12+ mobile_root = "/sdcard/zhubo/"
13+ computer_root = "/Users/wubin/Desktop/zhubo/"
14+ except_file = "/Users/wubin/Desktop/zhubo/except.txt"
15+
16+
17+ def __init__ (self ):
18+ '''
19+ 查看连接的手机,没有手机连接则抛出异常
20+ '''
21+
22+ connect = subprocess .Popen ("adb devices" ,
23+ stderr = subprocess .PIPE ,
24+ stdout = subprocess .PIPE ,
25+ shell = True )
26+ stdout , stderr = connect .communicate () # 获取返回命令
27+ # 输出执行命令结果结果
28+ stdout = stdout .decode ("utf-8" )
29+
30+ # if len(stdout) <= 26:
31+ # raise Exception("没有连接到手机")
32+ print ("成功连接手机!" )
33+
34+
35+ def screen (self , platform ):
36+ '''
37+ 截取屏幕,保存到手机中的 /sdcard/zhubo/platform 文件夹中
38+ :param platform: 平台,如:taobao、pdd、jingdong
39+ '''
40+
41+ for i in range (1 , 618 ):
42+ time .sleep (3 )
43+ pic_name = platform + '_' + str (int (time .time () * 1000 )) + '.png'
44+
45+ # 截屏
46+ screencap = subprocess .Popen ('adb shell /system/bin/screencap -p ' + self .mobile_root + platform + '/' + pic_name ,
47+ stderr = subprocess .PIPE ,
48+ stdout = subprocess .PIPE ,
49+ shell = True )
50+
51+ # 滑动屏幕
52+ swipe = subprocess .Popen ('adb shell input swipe 1000 300 1000 10' ,
53+ stderr = subprocess .PIPE ,
54+ stdout = subprocess .PIPE ,
55+ shell = True )
56+ print (str (i ) + ' ' + pic_name )
57+
58+
59+ def pull (self , platform ):
60+ '''
61+ 发送到电脑
62+ '''
63+
64+ # 列出所有图像
65+ connect = subprocess .Popen ('adb shell ls ' + self .mobile_root + platform ,
66+ stderr = subprocess .PIPE , stdout = subprocess .PIPE , shell = True )
67+ stdout , stderr = connect .communicate ()
68+ stdout = stdout .decode ("utf-8" )
69+ pics = stdout .split ('\n ' )
70+
71+ for pic_name in pics :
72+ # 发送到电脑 /Users/xx/Desktop/zhubo/platform 文件夹下
73+ connect = subprocess .Popen ('adb pull' + self .mobile_root + platform + '/' + pic_name + ' ' + self .computer_root + platform + '/' + pic_name ,
74+ stderr = subprocess .PIPE , stdout = subprocess .PIPE , shell = True )
75+ print ('手机中的图像成功发送到电脑' )
76+
77+ def getAccessToken (self ):
78+ '''
79+ 获取百度 AI 开放平台的 access_token
80+ :return: access_token
81+ '''
82+
83+ ak = 'MbDXGOrgXHqsgHKlAZLv6K93'
84+ sk = 'fzIOiK2aEAKntAY7cOEHkUCoZOawe0wR'
85+
86+ host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + ak + '&client_secret=' + sk
87+ response = requests .get (host )
88+ if response :
89+ return response .json ()['access_token' ]
90+
91+ def image2base64 (self , pic_path ):
92+ '''
93+ 图片转base64
94+ :param image_path: 图片地址
95+ :return: base64
96+ '''
97+
98+ with open (pic_path , 'rb' ) as f :
99+ base64_data = base64 .b64encode (f .read ())
100+ s = base64_data .decode ()
101+ return s
102+
103+ def beauty_detect (self , access_token , platform ):
104+ '''
105+ 人脸检测
106+ :param access_token: access_token
107+ :param platform: 平台,如:taobao、pdd、jingdong
108+ :return: 文件
109+ '''
110+
111+ # 人脸检测 url
112+ request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
113+
114+ # 为了防止请求百度发生意外事故,将颜值结果写入文件
115+ filename = self .computer_root + platform + '.txt'
116+
117+ index = 0
118+ # 循环所有图片
119+ for root , dirs , files in os .walk (self .computer_root + platform ):
120+ for pic in files :
121+ index = index + 1
122+ base64img = self .image2base64 (root + '/' + pic )
123+
124+ params = "{\" image\" :\" " + base64img + "\" ,\" image_type\" :\" BASE64\" ,\" face_field\" :\" beauty\" }"
125+ request_url = request_url + "?access_token=" + access_token
126+ headers = {'content-type' : 'application/json' }
127+
128+ # 免费 API QPS只有2个,可以使用多个账号,注意:这里容易异常
129+ response = requests .post (request_url , data = params , headers = headers )
130+
131+ print (response )
132+ if response :
133+ json = response .json ()
134+ print (json )
135+ # 解析获取颜值i
136+ if json ['error_msg' ] == 'SUCCESS' :
137+ face_list = json ['result' ]['face_list' ]
138+ beauty_list = []
139+ for face in face_list :
140+ beauty_list .append (face ['beauty' ])
141+ beauty = max (beauty_list )
142+
143+ with open (filename , 'a' ) as f :
144+ f .write (str (index ) + ',' + pic + ',' + str (beauty ) + '\n ' )
145+ print (str (index ) + ',' + pic + ',' + str (beauty ) + '\n ' )
146+
147+
148+ def calc (self , platform ):
149+ '''
150+ 统计颜值区间的个数
151+ :param platform: 平台,如:taobao、pdd、douyin
152+ :return: 颜值区间汇总、颜值字典
153+ '''
154+
155+ beauty_sum_dir = {"90-100" : 0 , "80-89" : 0 , "70-79" : 0 , "60-69" : 0 , "50-59" : 0 , "40-49" : 0 , "30-39" : 0 ,
156+ "20-29" : 0 , "10-19" : 0 , "0-9" : 0 }
157+ beauty_dir = {}
158+
159+ beauty_areas = ["90-100" , "80-89" , "70-79" , "60-69" , "50-59" , "40-49" , "30-39" , "20-29" , "10-19" , "0-9" ]
160+
161+ filename = self .computer_root + platform + '.txt'
162+
163+ with open (filename ) as f :
164+ lines = f .readlines ()
165+
166+ if lines == None or len (lines ) == 0 :
167+ raise Exception (filename + '中没有颜值数据' )
168+
169+
170+ index = 0
171+ for line in lines :
172+ # 只取 618 个图像
173+ index = index + 1
174+ if index > 618 :
175+ break
176+
177+ l = line .rstrip ()
178+ result = l .split (',' )
179+ beauty = float (result [2 ])
180+
181+ beauty_area = beauty_areas [int ((beauty // 10 * - 1 ) - 1 )]
182+ beauty_sum_dir [beauty_area ] = beauty_sum_dir .get (beauty_area ) + 1
183+
184+ beauty_dir [result [1 ]] = result [2 ]
185+
186+ return beauty_sum_dir , beauty_dir
187+
188+ def bar (self , taobao_beauty_sum_dir = {}, pdd_beauty_sum_dir = {}, douyin_beauty_sum_dir = {}):
189+ '''
190+ 柱状图
191+ :param taobao_beauty_sum_dir: 淘宝颜值区间汇总
192+ :param pdd_beauty_sum_dir: 拼多多颜值区间汇总
193+ :param douyin_beauty_sum_dir: 抖音颜值区间汇总
194+ :return:
195+ '''
196+
197+ bar = (
198+ Bar ()
199+ .add_xaxis (list (taobao_beauty_sum_dir .keys ()))
200+ .add_yaxis ('淘宝' , list (taobao_beauty_sum_dir .values ()))
201+ .add_yaxis ("拼多多" , list (pdd_beauty_sum_dir .values ()))
202+ .add_yaxis ("抖音" , list (douyin_beauty_sum_dir .values ()))
203+ .set_global_opts (title_opts = opts .TitleOpts (title = "主播颜值柱状图" ))
204+
205+ )
206+ bar .render ("颜值柱状图.html" )
207+
208+ def pie (self , platform , beauty_sum_dir = {}):
209+ '''
210+ 饼图
211+ :param platform: 平台,如:taobao、pdd、douyin
212+ :param beauty_sum_dir: 颜值区间汇总
213+ :return:
214+ '''
215+
216+ c = (
217+ Pie ()
218+ .add (
219+ "" ,
220+ [list (z ) for z in zip (beauty_sum_dir .keys (), beauty_sum_dir .values ())],
221+ center = ["35%" , "50%" ],
222+ )
223+ .set_global_opts (
224+ title_opts = opts .TitleOpts (title = platform + '主播颜值饼图' ),
225+ legend_opts = opts .LegendOpts (pos_left = "15%" ),
226+ )
227+ .set_series_opts (label_opts = opts .LabelOpts (formatter = "{b}: {c}({d}%)" ))
228+ .render (platform + "颜值饼图.html" )
229+ )
230+
231+ def sorted_by_value (self , beauty_dir ):
232+ beauty_sorted = sorted (beauty_dir .items (), key = lambda kv :(kv [1 ], kv [0 ]), reverse = True )
233+ print (beauty_sorted )
234+ return beauty_sorted
235+
236+
237+
238+
239+ if __name__ == '__main__' :
240+ a = zhubo ()
241+ a .screen ('pdd' )
242+ a .pull ('pdd' )
243+ access_token = a .getAccessToken ()
244+
245+ platforms = ['taobao' , 'pdd' , 'douyin' ]
246+ for platform in platforms :
247+ a .beauty_detect (access_token , 'taobao' )
248+
249+
250+ taobao_beauty_sum_dir , taobao_beauty_dir = a .calc ('taobao' )
251+ pdd_beauty_sum_dir , pdd_beauty_dir = a .calc ('pdd' )
252+ douyin_beauty_sum_dir , douyin_beauty_dir = a .calc ('douyin' )
253+
254+ # 图表
255+ a .bar (taobao_beauty_sum_dir ,pdd_beauty_sum_dir ,douyin_beauty_sum_dir )
256+ a .pie ('淘宝' , taobao_beauty_sum_dir )
257+ a .pie ('拼多多' , pdd_beauty_sum_dir )
258+ a .pie ('抖音' , douyin_beauty_sum_dir )
259+ taobao_beauty_dir .update (douyin_beauty_dir )
260+ taobao_beauty_dir .update (pdd_beauty_dir )
261+ a .sorted_by_value (taobao_beauty_dir )
0 commit comments