Preface
Text and pictures of the text come from the network for learning and communication purposes only. They do not have any commercial use. Copyright is owned by the original author. If you have any questions, please contact us in time for processing.
Author: Star Ango, AirPython
PS: If you need Python learning materials for your child, click on the link below to get them
http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef
Target Scene
I believe that when you brush short video with shaking sound, you will always have the habit of approving and paying attention to the ladies with high facial values.
If brushing one brush at a time really takes a lot of time, it would save a lot of work if Python could help filter out high-looking ladies.
This article is based on the "Baidu Face Recognition" API, to help us identify the high-facial jitter lady sister, and then download to the mobile phone album.
Dead work
First, the project requires some precise manipulation of the page elements. You need to prepare an Android device in advance, activate the developer options, and turn on the two settings "USB debugging and pointer location" in the developer options.
In order to ensure that the adb command can work properly, you need to configure the adb development environment in advance.
Some elements in a page element cannot be retrieved using common attributes such as name. You may need to retrieve a complete UI tree and use Airtest to determine if a UI element exists.
#Installation Dependency
pip3 install pocoui
In addition, the project will conduct face recognition on the video, get all the faces that appear, and then perform gender recognition and face value judgment.
This requires Baidu Cloud Background to register an application for face recognition and get a set of API Key and Secret Key values.
"access token" can then be obtained from the API documentation provided on the official website. Since ak has a validity period of one month, it only needs to be initialized once before normal face recognition can be performed using the face recognition interface.
1 appid = 'The one you registered for the app appid' 2 api_key = 'The one you registered for the app ak' 3 secret_key = 'The one you registered for the app sk' 4 5 def get_access_token(): 6 """ 7 It access_token Validity period is usually one month 8 """ 9 # This variable is assigned to itself API Key Value of 10 client_id = api_key 11 12 # This variable is assigned to itself Secret Key Value of 13 client_secret = secret_key 14 15 auth_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret 16 17 header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko', 18 "Content-Type": "application/json"} 19 20 # Request Get To token Interface 21 response_at = requests.get(auth_url, headers=header_dict) 22 json_result = json.loads(response_at.text) 23 access_token = json_result['access_token'] 24 return access_token
Scripting
With the adb environment configured above, you can directly use the os module in python to execute the adb command to open the jitter App.
1 # Jitter App Application package name and initial Activity 2 package_name = 'com.ss.android.ugc.aweme' 3 activity_name = 'com.ss.android.ugc.aweme.splash.SplashActivity' 4 5 def start_my_app(package_name, activity_name): 6 """ 7 Open Application 8 adb shell am start -n com.tencent.mm/.ui.LauncherUI 9 :param package_name: 10 :return: 11 """ 12 os.popen('adb shell am start -n %s/%s' % (package_name, activity_name))
Next, we need to take a screenshot of the currently playing video locally.It is important to note that the dithering video playback interface contains some cluttered elements such as the video creator's avatar and the BGM creator's avatar, which may cause some errors in the result of face recognition, so the image after screen capture needs to be "secondary clipped".
1 def get_screen_shot_part_img(image_name): 2 """ 3 Get part of the phone screenshot 4 :return: 5 """ 6 # screenshot 7 os.system("adb shell /system/bin/screencap -p /sdcard/screenshot.jpg") 8 os.system("adb pull /sdcard/screenshot.jpg %s" % image_name) 9 10 # Open Picture 11 img = Image.open(image_name).convert('RGB') 12 13 # Original width and height of picture(1080*2160) 14 w, h = img.size 15 16 # Intercept parts, remove their avatars, other cluttered elements 17 img = img.crop((0, 0, 900, 1500)) 18 19 img.thumbnail((int(w / 1.5), int(h / 1.5))) 20 21 # Save Locally 22 img.save(image_name) 23 24 return image_name
Now you can use the API provided by Baidu to get the face list of the above screenshots.
1 def parse_face_pic(pic_url, pic_type, access_token): 2 """ 3 Face recognition 4 5 Within seconds 5 :param pic_url: 6 :param pic_type: 7 :param access_token: 8 :return: 9 """ 10 url_fi = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + access_token 11 12 # call identify_faces,Get a list of faces 13 json_faces = identify_faces(pic_url, pic_type, url_fi) 14 15 if not json_faces: 16 print('No human face recognized') 17 return None 18 else: 19 # Return to all faces 20 return json_faces
From the above list of faces, sisters with sex between 18 and 30 and facial value over 70 were selected.
1 def analysis_face(face_list): 2 """ 3 Analyze face to determine if it meets the criteria 4 18-30 Between, female, face value greater than 80 5 :param face_list:List of recognized faces 6 :return: 7 """ 8 # Can you find beautiful women with high looks 9 find_belle = False 10 if face_list: 11 print('Total Recognition%d Face, start to identify if there is a beautiful woman below~' % len(face_list)) 12 for face in face_list: 13 # Judging male or female 14 if face['gender']['type'] == 'female': 15 age = face['age'] 16 beauty = face['beauty'] 17 18 if 18 <= age <= 30 and beauty >= 70: 19 print('Pigment Value:%d,Pass, meet the criteria!' % beauty) 20 find_belle = True 21 break 22 else: 23 print('Pigment Value:%d,Fail, continue~' % beauty) 24 continue 25 else: 26 print('Gender is male,Continue~') 27 continue 28 else: 29 print('No face was found in the picture.') 30 31 return find_belle
Because the video is played continuously, it is difficult to judge if there is a lady with a high face in the video by intercepting a frame of the video.
In addition, most short videos are played for "10s+". Here we need to do face recognition for each video multiple times until we recognize the lady sister with high facial value.
1 # Maximum recognition time for a video 2 RECOGNITE_TOTAL_TIME = 10 3 # Number of identifications 4 recognite_count = 1 5 6 # Face Removal from Current Video Screenshot 7 while True: 8 # Get screenshots 9 print('Beginning%d Secondary Screenshot' % recognite_count) 10 11 # Capture useful areas of the screen, filter the avatars of video authors, BGM Author's Avatar 12 screen_name = get_screen_shot_part_img('images/temp%d.jpg' % recognite_count) 13 14 # Face recognition 15 recognite_result = analysis_face(parse_face_pic(screen_name, TYPE_IMAGE_LOCAL, access_token)) 16 17 recognite_count += 1 18 19 # No. n Time after recognition 20 recognite_time_end = datetime.now() 21 22 # This video shows a lady with a high face 23 if recognite_result: 24 pass 25 else: 26 print('Timeout!!!This is an unattractive video!') 27 # Jump out of inner loop 28 break
Once the currently playing video identifies a lady with a high appearance, you need to simulate the operation of saving the video locally.
Get the coordinate locations of the Share and Save Local buttons, then click on the adb to download the video locally.
1 def save_video_met(): 2 """ 3 :return: 4 """ 5 # share 6 os.system("adb shell input tap 1000 1500") 7 time.sleep(0.05) 8 9 # Save Locally 10 os.system("adb shell input tap 350 1700")
In addition, since the process of downloading video is a time-consuming operation, a "simulated wait" operation is required before the download progress dialog box disappears.
1 def wait_for_download_finished(poco): 2 """ 3 Download from Click to download completely 4 :return: 5 """ 6 7 element = Element() 8 while True: 9 # Because it is a dialog box, it cannot be used Element Class to determine if an element exists for accurate processing 10 # element_result = element.findElementByName('Saving to Local') 11 12 # Current Page UI Tree element information 13 # Note: Element exceptions may be retrieved when saving, which needs to be thrown and the loop terminated 14 # com.netease.open.libpoco.sdk.exceptions.NodeHasBeenRemovedException: Node was no longer alive when query attribute "visible". Please re-select. 15 try: 16 ui_tree_content = json.dumps(poco.agent.hierarchy.dump(), indent=4).encode('utf-8').decode('unicode_escape') 17 except Exception as e: 18 print(e) 19 print('Exceptions, handled by download~') 20 break 21 22 if 'Saving to Local' in ui_tree_content: 23 print('Still downloading~') 24 time.sleep(0.5) 25 continue 26 else: 27 print('Download complete~') 28 break
Once the video has been saved locally, you can simulate a sliding up action and skip to the next video.Loop above to filter out all high-looking ladies and sisters and save them locally.
1 def play_next_video(): 2 """ 3 Next Video 4 Slide from bottom to top 5 :return: 6 """ 7 os.system("adb shell input swipe 540 1300 540 500 100")
During the scripting process of brushing videos one by one, you may encounter advertisements, which we need to filter.
1 def is_a_ad(): 2 """ 3 Determine if an ad is on the current page 4 :return: 5 """ 6 element = Element() 7 ad_tips = ['Go play', 'To experience', 'Download now'] 8 9 find_result = False 10 11 for ad_tip in ad_tips: 12 try: 13 element_result = element.findElementByName(ad_tip) 14 # Is an advertisement,Jump out directly 15 find_result = True 16 break 17 except Exception as e: 18 find_result = False 19 20 return find_resul
Result conclusion
Running the script above will automatically turn on the jitter, and perform face recognition for each small video several times until you recognize the high-looking sister, save the video locally, and then continue with the next short video. If you think the article is good, please share it.You must be my greatest encouragement and support.