A neural network for promoting a dog account on Instagram or a robot dog in action

Mechanics

For the sake of brevity, the script for the promotion will be called "robot dog" . Imagine that this script on behalf of a dog account periodically collects posts by hashtags on dog topics and likes such posts. Some of the authors of these posts will be interested in who liked them, and will go to the dog's account page. Well, then, as it goes. Someone will scroll down a couple of screens and move on. Someone will give the robot a couple of reciprocal likes (which is also not bad, likes increase the reach of posts). And if you like the account (which is possible, because the dog is very charismatic ), he can subscribe.





Caution: Mass Liking

It should be noted that such actions are called "mass-liking" and are not particularly welcomed by Instagram. When a suspicious robotic activity appears, Instagram will first issue several warnings, and then it may ban the account forever. By the way, manual massliking also falls under the ban (i.e., if it is not an automated service that likes, but you personally).





There is an opinion that massliking does not work very well now. Well, we'll check it at the same time.





Services and libraries for massliking and massfollowing

Despite all of the above, dozens of different services for massliking and massfollowing live and flourish (those who are interested can look here , here or google themselves). This hints that massliking works anyway.





It turns out like with sex. It seems that everyone is engaged, but it is not accepted to discuss in a decent society. Again, you can find empirically established limits for massliking on the internet. If the robot dog does not exceed them, then it should not be sent to the ban. For young (less than six months from the date of registration) accounts, this is no more than 30 likes per hour or 720 per day.





There are special libraries for pythonists. The most famous is Instapy ( 12k stars on Github, for a minute). There are lesser known ones. By the way, recently on Habré there was an article in which a very decent instabot library understands . But I personally would not use them "head-on" for auto-hacking by hashtags. For the following reason.





What's wrong with hashtags

, ( Instagram , ).





Do you see a dog, dog or dog?  And they are ...
, ? ...

, , . , , .





, ( ), ! - .





"" . , , . - , - . Object Detection, , SSD .





- , SSD MobileNet v.2, COCO2017. MobileNet, GPU . , 94%.





, SSD python. Github, , , .





import cv2
import json
from datetime import datetime
import requests
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

def id_class_name(class_id, classes):
    for key, value in classes.items():
        if class_id == key:
            return value

#    
shortcode = 'CJ.........'

classNames = {}

with open('models/coco2017_labels.txt', 'r+', encoding='utf-8') as file:

    for line in file:
        key = int(line.split(':')[0])
        value = line.split(':')[1]
        classNames[key] = value.strip()
        
COLORS = np.random.uniform(0, 255, size=(len(classNames), 3))

s = requests.session()
r = s.get(f'https://www.instagram.com/p/{shortcode}/?__a=1', headers = {'User-agent': 'bot'})
url = r.json()['graphql']['shortcode_media']['display_resources'][0]['src']

resp = requests.get(url, stream=True)
image = np.asarray(bytearray(resp.content), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
image_height, image_width, _ = image.shape

frame_resized = cv2.resize(image,(300,300))
model = cv2.dnn.readNetFromTensorflow('models/frozen_inference_graph.pb',
                                      'models/ssd_mobilenet_v2_coco_2018_03_29.pbtxt')
model.setInput(cv2.dnn.blobFromImage(frame_resized, size=(300, 300), swapRB=True))

output = model.forward()
detections = output[0, 0, :, :]
detections = detections[detections[:,2].argsort()]

for detection in detections:

    confidence = detection[2]
    class_id = int(detection[1])
    class_name = id_class_name(class_id, classNames)

    if (confidence > 0.3):

        box_x =      int(detection[3] * image_width)
        box_y =      int(detection[4] * image_height)
        box_width =  int(detection[5] * image_width)
        box_height = int(detection[6] * image_height)

        cv2.rectangle(image, (box_x, box_y), (box_width, box_height), COLORS[class_id], thickness=2)

        label = class_name + ": " + str(round(confidence, 2))
        labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_DUPLEX, 0.5, 1)
        yLeftBottom_ = max(box_y, labelSize[1])

        cv2.rectangle(image, (box_x, box_y + labelSize[1]), (box_x + labelSize[0], box_y), COLORS[class_id], cv2.FILLED)
        cv2.putText(image, label, (box_x, box_y + labelSize[1] - baseLine//2), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))

plt.figure(figsize=(8,8))
plt.axis("off")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#           -   
#plt.savefig(f'{shortcode}.png')
      
      







OpenCV . , - , pip conda install.





/?__a=1 OpenAPI Instagram. , - , , . , Instagram, .





I must say that dogs with a given implementation of a neural network are not always defined correctly. For example:





Why don't dogs fly like birds?
Why don't dogs fly like birds?

The fact is that the angle in the photo is rather unusual - the dog's nose looks like the beak of some bird like a toucan. That is, in real conditions, some of the suitable content will inevitably be rejected, the robot dog will not like all dogs. But within the framework of the problem being solved, it is quite possible to put up with this.





To be continued. In the near future, I will deploy a robot on a local machine, I will go on a trip, collect statistics on the response to likes and write off the results.








All Articles