def bot_init(self): ############################ # REQUIRED: LOGIN DETAILS! # ############################ self.config['api_key'] = os.environ['TWITTER_API_KEY'] self.config['api_secret'] = os.environ['TWITTER_API_SECRET'] self.config['access_key'] = os.environ['TWITTER_ACCESS_KEY'] self.config['access_secret'] = os.environ['TWITTER_ACCESS_SECRET'] ###################################### # SEMI-OPTIONAL: OTHER CONFIG STUFF! # ###################################### # how often to tweet, in seconds self.config['tweet_interval'] = 30 * 60 # default: 30 minutes # only include bot followers (and original tweeter) in @-replies self.config['reply_followers_only'] = True # fav any tweets that mention this bot? self.config['autofav_mentions'] = False # fav any tweets containing these keywords? self.config['autofav_keywords'] = [] # follow back all followers? self.config['autofollow'] = True ########################################### # CUSTOM: your bot's own state variables! # ########################################### self.register_custom_handler(openshift_wake_up, 60 * 60 * 12) self.face_regions = partial( core.face_regions, core.load_face_detector()) self.face_recognizer = FaceRecognizer() self.store = Store()
import cv2 import torch from facerec import FaceRecognizer, image_to_tensor, PersonType, PersonInfo from utils.images import hconcat_resize_min, get_int_rect import imutils import matplotlib.pyplot as plt VIDEO_PATH = "/media/popikeyshen/30c5a789-895a-4cc2-910a-3c678cc563d7/deepfake/train_sample_videos/alvgwypubw.mp4" ### init torch device device = torch.device('cuda:0') ### init face detector distance_threshold = 1.0 recognizer = FaceRecognizer(device=device, distance_threshold=distance_threshold) ### init video capture cap = cv2.VideoCapture(VIDEO_PATH) while (1): ### read and resize frame ret, im = cap.read() im = imutils.resize(im, height=800) ### detect faces embeddings, boxes = recognizer.get_faces_from_frame(image_to_tensor(im)) for box in boxes: ### draw box around face min_x, min_y, max_x, max_y = get_int_rect(box)
class Autofriend(TwitterBot): def bot_init(self): ############################ # REQUIRED: LOGIN DETAILS! # ############################ self.config['api_key'] = os.environ['TWITTER_API_KEY'] self.config['api_secret'] = os.environ['TWITTER_API_SECRET'] self.config['access_key'] = os.environ['TWITTER_ACCESS_KEY'] self.config['access_secret'] = os.environ['TWITTER_ACCESS_SECRET'] ###################################### # SEMI-OPTIONAL: OTHER CONFIG STUFF! # ###################################### # how often to tweet, in seconds self.config['tweet_interval'] = 30 * 60 # default: 30 minutes # only include bot followers (and original tweeter) in @-replies self.config['reply_followers_only'] = True # fav any tweets that mention this bot? self.config['autofav_mentions'] = False # fav any tweets containing these keywords? self.config['autofav_keywords'] = [] # follow back all followers? self.config['autofollow'] = True ########################################### # CUSTOM: your bot's own state variables! # ########################################### self.register_custom_handler(openshift_wake_up, 60 * 60 * 12) self.face_regions = partial( core.face_regions, core.load_face_detector()) self.face_recognizer = FaceRecognizer() self.store = Store() def get_confidence(self): return float(os.environ.get('AUTOFRIEND_CONFIDENCE') or 50) def on_scheduled_tweet(self): pass def on_follow(self, follower_id): TwitterBot.on_follow(self, follower_id) try: self.store.save_friend((follower_id,)) except pg.IntegrityError: # aborting is harmless, most likely an unfollow/refollow self.log("tried to add duplicate twitter friend %s" % follower_id) def _process_photo(self, friend_id, url): with DownloadedFile(url) as downloaded: if self.store.photo_seen(downloaded): logging.info( '{} tried to add duplicate photo'.format(friend_id)) else: face_regions = self.face_regions( core.prepare_image(downloaded)) self.face_recognizer.update( [(face_region, friend_id) for face_region in face_regions]) self.store.remember_photo(downloaded) def on_direct_message(self, dm): media = dm.entities.get('media', []) photo = media[0] if len(media) > 0 else None if photo: friend_id = self.store.get_or_create_twitter_friend( dm.sender.id)['id'] self._process_photo(friend_id, get_photo_url(photo)) self.send_direct_message(dm.sender, compliments.get_compliment()) def on_mention(self, tweet, prefix): if 'PLEASE FORGET ME' in tweet.text.upper(): self.store.forget_friend( self.store.get_twitter_friend(tweet.author.id)) self.api.destroy_friendship(tweet.author.id) else: friend_id = self.store.get_or_create_twitter_friend( tweet.author.id)['id'] photo_urls = [get_photo_url(photo) for photo in get_photos(tweet)] for url in photo_urls: self._process_photo(friend_id, url) self.favorite_tweet(tweet) def on_timeline(self, tweet, prefix): """ Defines actions to take on a timeline tweet. tweet - a tweepy.Status object. You can access the text with tweet.text prefix - the @-mentions for this reply. No need to include this in the reply string; it's provided so you can use it to make sure the value you return is within the 140 character limit with this. It's up to you to ensure that the prefix and tweet are less than 140 characters. When calling post_tweet, you MUST include reply_to=tweet, or Twitter won't count it as a reply. """ photos = get_photos_from_tweet(tweet) face_regions = core.flatten( [self.face_regions(photo) for photo in photos]) recognitions = [] for region in face_regions: try: recognitions.append( self.face_recognizer.recognize_face(region)) except cv2.error as e: logging.error("Error recognizing face region: " + e.message) likely_recognitions = filter( lambda (_, margin): margin < self.get_confidence(), recognitions) recognized_labels = set([label for (label, _) in likely_recognitions]) for label in recognized_labels: recognized = self.store.get_friend(label) # it's possible someone is in the model but not in the database, # e.g. people that asked to be forgotten if recognized and recognized.get('twitter_id', None): twitter_friend = self.api.get_user(recognized['twitter_id']) self.favorite_tweet(tweet) self.post_tweet( prefix + ' ' + compliments.get_compliment() + ' ' + '@' + twitter_friend.screen_name, reply_to=tweet)
from facerec import FaceRecognizer, ImageSet, LabelSet, concatenate recognizer = FaceRecognizer() img1 = ImageSet("Female_faces") label1 = LabelSet("Female", img1) img2 = ImageSet("Male_faces") label2 = LabelSet("Male", img2) imgs = concatenate(img1, img2) labels = concatenate(label1, label2) recognizer.train(imgs, labels) img = ImageSet("Test_images") print recognizer.predict(img) recognizer.save("gender.xml")