{"id":396,"date":"2023-04-29T10:04:32","date_gmt":"2023-04-29T10:04:32","guid":{"rendered":"https:\/\/rodamoya.com\/?p=396"},"modified":"2023-05-26T07:40:44","modified_gmt":"2023-05-26T07:40:44","slug":"sws","status":"publish","type":"post","link":"https:\/\/rodamoya.com\/index.php\/2023\/04\/29\/sws\/","title":{"rendered":"<strong>Instagram face-recognition:<\/strong>"},"content":{"rendered":"\n<p>The next program is just educational, and you should not execute it unless you want your Instagram account blocked. The code is an automated bot that, with the Instagram username and password, downloads pictures of the followers in order to perform face recognition on their faces. This program uses the web-scraping library Selenium to download the latest pictures of all the followers by retrieving the HTML code. Once all the pictures are downloaded, they are uploaded to the face recognition library to save and recognize the faces. This code is also the reason why my Instagram account was blocked.<\/p>\n\n\n\n<div style=\"height:43px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/rodamoya.com\/wp-content\/uploads\/2023\/04\/unknown_2022.09.07-12.26_clip_1.mp4\"><\/video><\/figure>\n\n\n\n<p>The program is dividen in 4 steps:<\/p>\n\n\n\n<p>     <strong>1) Read the followers:<\/strong><br>The Instagram API has a function that returns a list with the people whom we follow.<\/p>\n\n\n\n<p>     <strong>2) First picture:<\/strong><br>The next step in your program is to enter our Instagram account automatically using Selenium. Once we are logged into your Instagram account, the code will execute a for loop for all the followers and enter their profiles to download their images locally with the name of the user.<\/p>\n\n\n\n<p>Once the images are saved, the code will encode them to detect faces and return a list with the usernames of the users where a face couldn&#8217;t be found.<\/p>\n\n\n\n<p>     <strong>3) Second picture:<\/strong><br>In this step, the code will use an Instagram API function to download the profile picture of the followers that could find a face. It will save and encode the profile picture.<\/p>\n\n\n\n<p><strong>     4) Facial Recognition:<\/strong><br>Finally, we will have to open the camera, send the encoded images, and let the program detect which of our followers is in front of the camera, displaying the username.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import instaloader\nimport time\nfrom selenium import webdriver\nimport datetime\nimport random\nfrom selenium.webdriver.common.keys import Keys\nfrom bs4 import BeautifulSoup\nimport requests\nimport instaloader\n\n# Face recognition\nimport cv2\nimport face_recognition\nfrom matplotlib import pyplot as plt\nfrom datetime import datetime\nimport shutil\nimport glob\nfrom PIL import Image\n\n\n\n# List of the followers:\n\n# get all the followers username of the accound you enter:\n\ntry:\n    L = instaloader.Instaloader()\n\n    # Login or load session\n    username = \"\"  #&lt;----------- Entern username\n    password = \"\"  #&lt;----------- Entern password\n    L.login(username, password)  # (login)\n\n    # Obtain profile metadata\n    profile = instaloader.Profile.from_username(L.context, username)\n\n    # Print list of followees\n    follow_list = &#91;]\n    count = 0\n    for followee in profile.get_followees():\n        follow_list.append(followee.username)\n        file = open(\"prada_followers.txt\", \"a+\")\n        file.write(follow_list&#91;count])\n        file.write(\"\\n\")\n        file.close()\n        print(follow_list&#91;count])\n        count = count + 1\nexcept:\n    pass\n\n# Get the pic of all the followers\n\n#Enter to your perfoil\n\nbrowser = webdriver.Chrome(executable_path = r\"C:\\Users\\....\\driver\\chromedriver.exe\") # ---&gt; Enter Selenium path driver\nbrowser.get(\"https:\/\/www.instagram.com\/\")\n\n# Acceptar cookies\nBut_Cokkies = browser.find_element_by_xpath(\"\/html\/body\/div&#91;4]\/div\/div\/button&#91;1]\")\nBut_Cokkies.click()\n\ntime.sleep(5)\n\n# LOG IN________________________________________________________________\n\n# User\nuser = browser.find_element_by_xpath(\"\/\/*&#91;@id='loginForm']\/div\/div&#91;1]\/div\/label\/input\")\nuser.click()\nuser.send_keys(username)\n\n#Password\npassw = browser.find_element_by_xpath(\"\/\/*&#91;@id='loginForm']\/div\/div&#91;2]\/div\/label\/input\")\npassw.click()\npassw.send_keys(password)\n\n#Button log in\nlibtn = browser.find_element_by_xpath('\/\/*&#91;@id=\"loginForm\"]\/div\/div&#91;3]\/button\/div')\nlibtn.click()\n\ntime.sleep(5)\n\n#Don't save info \ninfobtn = browser.find_element_by_xpath(\"\/\/*&#91;@id='react-root']\/section\/main\/div\/div\/div\/section\/div\/button\")\ninfobtn.click()\n\ntime.sleep(10)\n\n#Notifications of:\nnotibtn = browser.find_element_by_xpath('\/html\/body\/div&#91;1]\/div\/div\/div\/div&#91;2]\/div\/div\/div&#91;1]\/div\/div&#91;2]\/div\/div\/div\/div\/div\/div\/div\/div&#91;3]\/button&#91;2]')\nnotibtn.click()\n\ntime.sleep(5)\n\n# User to download the imatge:\nfollwers = follow_list&#91;:25]\n\n# list with the not saved users pics:\nnot_saved_list = &#91;]\n\n# list with the saved users pics:\nSaved_list = &#91;]\n\nfor follower in follwers:\n    \n    print(\"Dowlondig the pic of:\", follower )\n    \n    try: \n\n        searchbox=browser.find_element_by_css_selector(\"input&#91;placeholder='Busca']\")\n        searchbox.clear()\n        searchbox.send_keys(follower)\n        time.sleep(5)\n        searchbox.send_keys(Keys.ENTER)\n        time.sleep(5)\n        searchbox.send_keys(Keys.ENTER)\n        time.sleep(5)\n\n        # slect image\n\n        img_1 = browser.find_element_by_xpath('\/\/*&#91;@id=\"mount_0_0_ud\"]\/div\/div\/div\/div&#91;1]\/div\/div\/div\/div&#91;1]\/div&#91;1]\/section\/main\/div\/div&#91;2]\/article\/div\/div\/div\/div&#91;1]\/a')\n        img_1.click()\n    \n    \n\n        ''' Fetch the source file of the html page using BeautifulSoup'''\n        soup = BeautifulSoup(browser.page_source, 'lxml')\n\n        ''' Extract the url of the image from the source code''' \n        img = soup.find('img', class_='FFVAD')\n        img_url = img&#91;'src']\n\n\n        '''Download the image via the url using the requests library'''\n        r = requests.get(img_url)\n\n\n\n        with open(follower  +\".png\",'wb') as f: \n            f.write(r.content)\n\n        print('Success!')\n        \n        Saved_list.append(follower)\n    \n    # Cerrar imagen:\n        colseimg = browser.find_element_by_xpath(\"\/html\/body\/div&#91;6]\/div&#91;3]\/button\")\n        colseimg.click()\n    \n        time.sleep(5)\n    \n    # delet the user\n    #del_user = browser.find_element_by_xpath('\/\/*&#91;@id=\"react-root\"]\/section\/nav\/div&#91;2]\/div\/div\/div&#91;2]\/div&#91;2]')\n    #del_user.click()\n    \n        \n    \n    except:\n        \n        print(\"Didn't save the pic, adding to the don't save list not_saved_list.\")\n        \n        # Add the user to the don't saved pics:\n        not_saved_list.append(follower)\n        \n        pass\n\n\n# Where are the pics saved:\nimport os\nprint(os.getcwd())\n\n## List of saved and not:\n\n### Not saved List:\n\nnot_saved_list\n\n### Saved pics list\n\nSaved_list\n\n## Save the pics wher we can find the faces\n\n# encodings imatges:\n# declar the list to fit the users depending if there is a face detected or not:\nface_detected = &#91;]\nNot_face_dtected = &#91;]\n\nfor follower in Saved_list: \n\n    try:\n        print(\"Encoding\", follower , \"pic\")\n        \n        # Get the name of the .png imatge\n        imatge_name = follower + \".png\"\n        \n        print(imatge_name)\n        \n        #Declare img variables:\n        img_follower = \"img_\" + follower\n        rgb_follower = \"rgb_\" + follower\n        enc_img_follower = \"enc_img_\" + follower\n        \n        \n        # Encode rhe faces\n        img_follower = cv2.imread(imatge_name)\n        rgb_follower = cv2.cvtColor(img_follower, cv2.COLOR_BGR2RGB)\n        enc_img_follower = face_recognition.face_encodings(rgb_follower)&#91;0]    \n        \n        \n        face_detected.append(follower)\n        \n        destination = \"C:\\\\Users...\\\\\" + imatge_name # ----&gt; Fill the path\n        shutil.copyfile(imatge_name, destination)\n        #os.replace(imatge_name, destination )\n        print(imatge_name + \" Was moved\")\n        \n        print(\"Success!\")\n        print(\"\")\n    except:\n        \n        print( follower , \"Pic can`t not be encoded\")\n        print(\"\")\n        \n        Not_face_dtected.append(follower)\n        \n        pass\n        \n\n# Listas caras detectadas y no caras:\n\n### Fotos ya guardadas con caras encontradas\n\nface_detected\n\n### Craras no encontradas:\n\nNot_face_dtected\n\n### Merge the 2 list of no face detected and don't saved.\n\nGet_second_pic_list =  Not_face_dtected + not_saved_list\nGet_second_pic_list\n\n# Get the second pic\n\nfor user in Get_second_pic_list:\n    # Donload a folder with the perfoil pic\n    \n    ig = instaloader.Instaloader()\n    ig.download_profile(user , profile_pic_only=True)\n    \n    #Get the  perfoil pic\n    images = glob.glob(\"C:\\\\Users...\\\\\"+ user +\"\/\/*.JPG\")   # ----&gt; Fill the path\n    \n    #Get the first imatge:\n    pic = images&#91;0]\n    #Split the direction of the folder\n    pic_list = pic.split(\"\\\\\")\n    #Get the folder\n    folder_list = pic_list&#91;0:9]\n    folder = \"\/\/\".join(folder_list)\n    \n    try:\n        # rename the pic with the username\n        os.rename(pic , folder + \"\/\/\" +  user + \".jpg\" )\n    \n        user_pic =  user + \".jpg\"\n    \n        folder_with_new_pic = folder + \"\/\/\" + user_pic\n        \n        # Move the photo to the next folder\n        destination = \"C:\\\\Users...Second_pics\\\\\" +  user_pic  # ----&gt; Fill the path\n        shutil.copyfile(folder_with_new_pic , destination)\n        \n    except:\n        \n        print(\"Ya existe\")\n\n\n## Encode second image\n\n# encodings imatges:\n\n# declar the list to fit the users depending if there is a face detected or not:\nface_detected = &#91;]\nNot_face_dtected = &#91;]\n\nfor follower in Get_second_pic_list: \n\n    try:\n        print(\"Encoding\", follower , \"pic\")\n        \n        # Get the name of the .png imatge\n        imatge_name = follower + \".jpg\"\n        \n        print(imatge_name)\n        \n        #Declare img variables:\n        img_follower = \"img_\" + follower\n        rgb_follower = \"rgb_\" + follower\n        enc_img_follower = \"enc_img_\" + follower\n        \n        \n        # Encode rhe faces\n        img_follower = cv2.imread(imatge_name)\n        rgb_follower = cv2.cvtColor(img_follower, cv2.COLOR_BGR2RGB)\n        enc_img_follower = face_recognition.face_encodings(rgb_follower)&#91;0]    \n        \n        \n        face_detected.append(follower)\n        \n        destination = \"C:....\" + imatge_name # Path with the images\n        shutil.copyfile(imatge_name, destination)\n        #os.replace(imatge_name, destination )\n        print(imatge_name + \" Was moved\")\n        \n        print(\"Success!\")\n        print(\"\")\n    except:\n        \n        print( follower , \"Pic cann`t not be encoded\")\n        print(\"\")\n        \n        Not_face_dtected.append(follower)\n        \n        pass\n\n# Facial recognition:\n\n## Encode faces\n\nsfr = SimpleFacerec()\nsfr.load_encoding_images(\"C:....\")  # path with all the faces detected.\n\n## Load Camera\n\ncap = cv2.VideoCapture(0)\n\n# facial recognition in life\nwhile True:\n    \n    \n    # Detect faces:\n    ret, frame = cap.read()\n    face_locations, face_names = sfr.detect_known_faces(frame)\n    \n    for face_loc, name in zip(face_locations, face_names):\n        y1, x1, y2, x2 = face_loc&#91;0], face_loc&#91;1], face_loc&#91;2], face_loc&#91;3]\n        \n        cv2.putText(frame, name,(x1, y1 - 10), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 200), 2)\n        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 200), 4)\n        \n        \n    \n   \n    \n    cv2.imshow(\"Frame\", frame)\n    key = cv2.waitKey(1)\n    \n    if key == 27:\n        break\n\ncap.release()\ncv2.destroyAllWindows()\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The next program is just educational, and you should not execute it unless you want your Instagram account blocked. The code is an automated bot that, with the Instagram username and password, downloads pictures of the followers in order to perform face recognition on their faces. This program uses the web-scraping library Selenium to download &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/rodamoya.com\/index.php\/2023\/04\/29\/sws\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;<strong>Instagram face-recognition:<\/strong>&#8220;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/396"}],"collection":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":13,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"predecessor-version":[{"id":568,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions\/568"}],"wp:attachment":[{"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rodamoya.com\/index.php\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}