Skip to content

Instantly share code, notes, and snippets.

@mrteera
Created July 22, 2018 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrteera/a55211f9bdfa4c340ee37230b38caca9 to your computer and use it in GitHub Desktop.
Save mrteera/a55211f9bdfa4c340ee37230b38caca9 to your computer and use it in GitHub Desktop.
import math
import pandas as pd
shots = pd.read_csv('shots.csv')
faces = pd.read_csv('face_result.csv')
significant_scenes = []
for idx, row in shots.iterrows():
frames = list(range(
int(round(row['begin'])),
int(round(row['end'])) + 1
))
joy, sorrow, anger, surprise = 0, 0, 0, 0
for frame in frames:
frame_png = '{}.png'.format(frame)
face = faces.loc[faces['n_frame'] == frame_png]
joy += int(face['joy'])
sorrow += int(face['sorrow'])
anger += int(face['anger'])
surprise += int(face['surprise'])
frames_faces = {
'frames': [round(frame / 60.0, 2) for frame in frames],
'joy': joy,
'sorrow': sorrow,
'anger': anger,
'surprise': surprise,
'total_emotions': joy + sorrow + anger + surprise
}
if (joy + sorrow + anger + surprise) != 0:
significant_scenes.append(frames_faces)
significant_scenes = sorted(significant_scenes, key=lambda k: k['total_emotions'], reverse=True)
for scene in significant_scenes[:10]:
emotions = ['joy', 'sorrow', 'anger', 'surprise']
begin = scene['frames'][0]
end = scene['frames'][-1]
detected_emotions = []
for emotion in emotions:
if scene[emotion] != 0:
detected_emotions.append({emotion: scene[emotion]})
(begin_sec, begin_min) = math.modf(begin)
url = 'https://youtu.be/exYXWI3tIsQ?t={}m{}s'.format(int(begin_min), int(begin_sec * 100))
report = '{} '.format(url)
for detected_emotion in detected_emotions:
for key, value in detected_emotion.iteritems():
report = report + key + ': ' + str(value) + ' '
print(report)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment