Google Cloud Vision API สามารถทำ Face Detection ได้ อีกทั้งยังสามารถบอกได้ด้วยว่าใบหน้านั้นมีความรู้สึกอย่างไรเช่น สนุกสนาน เศร้า โกรธ หรือประหลาดใจเป็นต้น เมื่อนำ Google Cloud Vision API มารวมกับ Google Cloud Video Intelligence เราก็สามารถวิเคราะห์ซีนต่างๆ ของวิดิโอได้ว่าซีนไหนมีอะไรที่น่าสนใจบ้าง บทความนี้เราจะทดลองนำเอาวิดิโอจาก Youtuber ชื่อดังเจ้าหนึ่งมาลองวิเคราะห์ดู
ขั้นตอนการวิเคราะห์อารมณ์ในแต่ละซีน
ถ้ายังไม่ได้สมัคร Google Cloud Platform สามารถสมัคร Free Trial ได้ และสร้าง project ใน Google Cloud Console แล้ว ให้ติดตั้ง Google Cloud SDK (gcloud) ในที่นี้จะอ้างอิงระบบปฏิบัติการ Ubuntu เป็นหลัก ถ้าใช้ Microsoft Windows หรือ macOS สามารถใช้ Docker เพื่อเรียกใช้ Ubuntu ได้
1. ตัดวิดิโอออกเป็นภาพที่ความถี่ 1 fps
$ pip install youtube-dl $ mkdir face-sentiment && cd face-sentiment $ youtube-dl -f "best[height=720, ext=mp4]" https://www.youtube.com/watch?v=exYXWI3tIsQ $ mv *.mp4 bearhug1.mp4 $ mkdir frames && cd frames $ ffmpeg -i ../bearhug1.mp4 -vf fps=1 %d.png
2. นำภาพแต่ละเฟรมเข้า Google Cloud Vision API
$ pip install --upgrade google-cloud google-cloud-vision $ gcloud auth application-default login
import csv | |
import io | |
import os | |
from google.cloud import vision | |
def detect_faces(path): | |
client = vision.ImageAnnotatorClient() | |
with io.open(path, 'rb') as image_file: | |
content = image_file.read() | |
image = vision.types.Image(content=content) | |
response = client.face_detection(image=image) | |
faces = response.face_annotations | |
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', | |
'LIKELY', 'VERY_LIKELY') | |
joy, sorrow, anger, surprise = 0, 0, 0, 0 | |
for face in faces: | |
if face.joy_likelihood > 3: | |
joy += 1 | |
if face.sorrow_likelihood > 3: | |
sorrow += 1 | |
if face.anger_likelihood > 3: | |
anger += 1 | |
if face.surprise_likelihood > 3: | |
surprise += 1 | |
return [path.split('/')[-1], joy, sorrow, anger, surprise] | |
if __name__ == '__main__': | |
filenames = os.listdir('frames') | |
filenames.sort(key=lambda x: int(''.join(filter(str.isdigit, x)))) | |
csv_header = ['n_frame', 'joy', 'sorrow', 'anger', 'surprise'] | |
csv_results = [csv_header] | |
for frame in filenames[823:]: | |
result = detect_faces('frames/{}'.format(frame)) | |
print(result) | |
csv_results.append(result) | |
csv_file = open('results.csv', 'w') | |
with csv_file: | |
writer = csv.writer(csv_file) | |
writer.writerows(csv_results) |
$ python face-sentiment.py
3. Upload วิดิโอขึ้น Google Cloud Storage เพื่อให้ Google Cloud Video Intelligence แบ่งซีนของวิดิโอ
$ wget https://raw.githubusercontent.com/GoogleCloudPlatform/python-docs-samples/master/video/cloud-client/shotchange/shotchange.py $ python shotchange.py gs://teera-ait/videos/bearhug1.mp4
ขั้นตอนนี้ใช้เวลาประมาณ 1 นาที จะได้ผลลัพธ์ดังนี้
Processing video for shot change annotations:
Finished processing. Shot 0: 0.0 to 5.96 Shot 1: 6.0 to 7.84 Shot 2: 7.88 to 13.12 Shot 3: 13.16 to 14.44 ... Shot 208: 917.64 to 926.04
จากนั้นนำผลลัพธ์ที่ได้แปลงเป็นไฟล์ CSV
$ cat shots.csv n_shot,begin,end 0,1.0,5.96 1,6.0,7.84 2,7.88,13.12 3,13.16,14.44 ...
4. นำข้อมูล Face Emotion จาก Google Cloud Vision API มาเรียงตามซีนที่วิเคราะห์ได้มาจาก Google Cloud Video Intelligence API
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) |
จะได้ผลลัพธ์เรียงตามซีนของวิดิโอที่มีความน่าสนใจดังนี้
https://youtu.be/exYXWI3tIsQ?t=4m8s joy: 21 surprise: 1 https://youtu.be/exYXWI3tIsQ?t=2m14s joy: 17 https://youtu.be/exYXWI3tIsQ?t=2m39s joy: 16 https://youtu.be/exYXWI3tIsQ?t=2m92s joy: 11 https://youtu.be/exYXWI3tIsQ?t=3m33s joy: 10 https://youtu.be/exYXWI3tIsQ?t=3m52s joy: 10 https://youtu.be/exYXWI3tIsQ?t=8m41s joy: 10 https://youtu.be/exYXWI3tIsQ?t=8m69s joy: 10 https://youtu.be/exYXWI3tIsQ?t=2m62s joy: 9 https://youtu.be/exYXWI3tIsQ?t=2m72s joy: 9
ตัววิดิโอมีความยาวทั้งหมด 15:27 นาทีมีจำนวนเฟรมต่อ 1 วินาทีจำนวน 927 เฟรม Google Cloud Vision API ในส่วนของ Face Detection ให้ใช้ฟรีถึง 1000 ครั้งต่อเดือน ถ้าเกิน 1000 รูปราคาจะอยู่ที่ 1000 รูปละ $1.5 และ Google Cloud Video Intelligence Shot Detection ให้ใช้ฟรี 1000 นาทีแรก หลังจากนั้น $0.05 ต่อนาที
Google Cloud Vision API และ Google Cloud Intelligence ยังสามารถทำอะไรได้อีกมากมาย เช่นนำข้อมูล 3D Landmark ไปทำ Face Alignment สำหรับการทำ Face Recognition หรือทำการวิเคราะห์ภาพเพื่อสืบค้นข้อมูลต่างๆ จากในภาพหรือวิดิโอได้ และในอนาคตอันใกล้มี Google AutoML ที่สามาถใช้งานกับ dataset ของเราเองได้อีกด้วย นอกเหนือจากที่กล่าวไปข้างต้น Google Cloud Platform ยังมีบริการ Machine Learning API อีกมากมายให้เลือกใช้สำหรับ Application ของเราอีกด้วย

ผู้ที่สนใจสามารถสมัคร Google Cloud Platform เพื่อทดลองใช้งานได้ฟรี $300 ได้ที่
https://cloud.google.com/free หรือหากต้องการติดต่อเพื่อขอรายละเอียดเพิ่มเติมสามารถติดต่อได้ที่ https://cloud.google.com/contact