Skip to content

Instantly share code, notes, and snippets.

@localabjp
Last active July 18, 2017 22:27
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 localabjp/8ed77c294b056e173b10e31429db8bb4 to your computer and use it in GitHub Desktop.
Save localabjp/8ed77c294b056e173b10e31429db8bb4 to your computer and use it in GitHub Desktop.
#必要なライブラリをインポート
import matplotlib.pyplot as plt
import dlib
import urllib
import cv2
import numpy as np
from PIL import Image
import cStringIO
#画像のURLを読み込む
URL_to_test = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/2011_MuchMusic_Video_Awards_-_Foster_the_People.jpg/800px-2011_MuchMusic_Video_Awards_-_Foster_the_People.jpg'
file = cStringIO.StringIO(urllib.urlopen(URL_to_test).read())
img = Image.open(file)
orig_img = img.copy()
img = np.asarray(img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Face Detector
detector = dlib.get_frontal_face_detector()
dets = detector(gray,1)
print('OK')
faces = []
for k,d in enumerate(dets):
x = d.left()
y = d.top()
w = d.right() - d.left()
h = d.top()- d.bottom()
cv2.rectangle(img,(x,y),(x-h,y-h),(255,0,0),5)
fig = plt.figure(figsize = (15,15))
ax1 = fig.add_subplot(211)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('オリジナル画像')
ax1.imshow(orig_img)
#表示
#plt.show()
ax2 = fig.add_subplot(212)
ax2.set_xticks([])
ax2.set_yticks([])
ax2.set_title('顔検出済み画像')
ax2.imshow(img)
#表示
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment