Skip to content

Instantly share code, notes, and snippets.

@nbortolotti
Last active July 25, 2017 04:06
Show Gist options
  • Save nbortolotti/99f32ca8d8c08b359a8bef73f04fd2f7 to your computer and use it in GitHub Desktop.
Save nbortolotti/99f32ca8d8c08b359a8bef73f04fd2f7 to your computer and use it in GitHub Desktop.
facebook_posts_analysis.py
import sys
import os
import numpy as np
import tensorflow as tf
sys.path.append("..")
from object_detection.utils import label_map_util
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_coco_11_06_2017'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
NUM_CLASSES = 90
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
def detect_alert(boxes, classes, scores, category_index, max_boxes_to_draw=20,
min_score_thresh=.5,
):
r = []
for i in range(min(max_boxes_to_draw, boxes.shape[0])):
if scores is None or scores[i] > min_score_thresh:
test1 = None
test2 = None
if category_index[classes[i]]['name']:
test1 = category_index[classes[i]]['name']
test2 = int(100 * scores[i])
line = {}
line[test1] = test2
r.append(line)
return r
def detect_objects(image_np, sess, detection_graph):
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
alert_array = detect_alert(np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores),
category_index)
return alert_array
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
def process_image(image):
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
alert_array = detect_objects(image, sess, detection_graph)
return alert_array
import facebook
import urllib, cStringIO
from PIL import Image
def facebook_posts_analysis(username, num_post, token):
access_token = token
user = username
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts', limit=num_post)
for p in posts['data']:
postid = p['id']
print(p['created_time'].encode('utf-8') + ";" + p['id'].encode('utf-8') + ";" + p['message'].encode('utf-8'))
try:
a = graph.get_connections(postid, 'attachments', limit=1)
for att in a['data']:
file = cStringIO.StringIO(urllib.urlopen(att['media']['image']['src']).read())
img = Image.open(file)
if img:
list_elements = process_image(img)
print(list_elements)
except Exception:
pass
facebook_user = '' # change
num_post = 10 #change
token = '' #change
facebook_posts_analysis(facebook_user, num_post, token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment