Skip to content

Instantly share code, notes, and snippets.

@AKB428
Last active August 31, 2015 17:20
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 AKB428/961522a3ed95e06e947e to your computer and use it in GitHub Desktop.
Save AKB428/961522a3ed95e06e947e to your computer and use it in GitHub Desktop.
MSの画像解析サービスProject Oxfordでコスプレ ラブライブ四天王の年齢判定をプログラミングしてみる ref: http://qiita.com/AKB428/items/ba32bdf917037956b40f
{
"Azure" : {
"subscription_key": ""
}
}
require 'net/http'
require 'json'
File.open './config/application.json' do |file|
@conf = JSON.load(file.read)
@subscription_key = @conf['Azure']['subscription_key']
end
uri = URI('https://api.projectoxford.ai/vision/v1/analyses')
uri.query = URI.encode_www_form({
# Request parameters
'visualFeatures' => 'All'
})
request = Net::HTTP::Post.new(uri.request_uri)
# Request headers
request['Content-Type'] = 'application/json'
# Request headers
request['Ocp-Apim-Subscription-Key'] = @subscription_key
# Request body
request.body = sprintf('{ "Url": "%s"}', ARGV[0])
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
puts response.body
{
"categories": [
{
"name": "people_",
"score": 0.98046875
}
],
"adult": {
"isAdultContent": false,
"isRacyContent": false,
"adultScore": 0.009153234772384167,
"racyScore": 0.014479533769190311
},
"requestId": "1757a848-d47a-4cb6-860d-96e72a65ac7a",
"metadata": {
"width": 225,
"height": 338,
"format": "Jpeg"
},
"faces": [
{
"age": 20,
"gender": "Female",
"faceRectangle": {
"left": 84,
"top": 47,
"width": 40,
"height": 40
}
}
],
"color": {
"dominantColorForeground": "White",
"dominantColorBackground": "White",
"dominantColors": [
"White",
"Black"
],
"accentColor": "356375",
"isBWImg": false
},
"imageType": {
"clipArtType": 0,
"lineDrawingType": 0
}
}
require 'net/http'
require 'json'
file_path = ARGV[0]
File.open './config/application.json' do |file|
@conf = JSON.load(file.read)
@subscription_key = @conf['Azure']['subscription_key']
end
uri = URI('https://api.projectoxford.ai/vision/v1/analyses')
uri.query = URI.encode_www_form({
# Request parameters
'visualFeatures' => 'All'
})
request = Net::HTTP::Post.new(uri.request_uri)
# Request headers
request['Content-Type'] = 'application/octet-stream'
# Request headers
request['Ocp-Apim-Subscription-Key'] = @subscription_key
# Request body
File.open(file_path) do |filename_stream_data|
request.body = filename_stream_data.read
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
puts response.body
end
require 'json'
require './lib/image_string_write'
class AzureOxfordVision
def initialize(target_file_or_url, conf)
@file_path = target_file_or_url
@conf = conf
# Local Fileの場合コピー
@write_filename = File.join(conf['oxford_app']['output_directory'], File::basename(target_file_or_url))
FileUtils.cp(target_file_or_url, @write_filename)
#TODO urlだったらファイルをダウンロード
end
def iamge_info_write(face_data)
p @write_filename
#imageを初期化
#loopして情報を書き込む
#出力ファイルを返す
face_data.each do |data|
puts data['age']
puts data['gender']
puts data['faceRectangle']
left = data['faceRectangle']['left']
top = data['faceRectangle']['top']
width = data['faceRectangle']['width']
height = data['faceRectangle']['height']
write_string = sprintf('age=%d gender=%s',data['age'],data['gender'])
position_x = left
position_y = top + height + 1
puts position_y
#(filename, write_string = nil, input_desc = nil, del_exif = false, font_size = 16, position_x = 5, position_y = 5)
#"faceRectangle": {
# "left": 256,
# "top": 126,
# "width": 108,
# "height": 108
#}
ImageStringWrite.new(@write_filename, write_string, "", false, 16, position_x, position_y).write
end
end
def image_info_parse(response_body)
image_info = JSON.load(response_body)
face_data = image_info['faces']
return if face_data.nil?
#[{"age"=>19, "gender"=>"Female", "faceRectangle"=>{"left"=>77, "top"=>39, "width"=>51, "height"=>51}}]
p face_data
iamge_info_write(face_data)
end
end
oxford_vision_sample2 (master)$ bundle exe ruby from_local.rb sample/single/1.jpg
{"categories":[{"name":"people_","score":0.9765625}],"adult":{"isAdultContent":false,"isRacyContent":false,"adultScore":0.025486733764410019,"racyScore":0.27363106608390808},"requestId":"8cc0d06e-98e2-433d-b13d-0e92a8e8c72d","metadata":{"width":600,"height":900,"format":"Jpeg"},"faces":[{"age":8,"gender":"Female","faceRectangle":{"left":256,"top":126,"width":108,"height":108}}],"color":{"dominantColorForeground":"White","dominantColorBackground":"White","dominantColors":["White"],"accentColor":"6D3A3E","isBWImg":false},"imageType":{"clipArtType":0,"lineDrawingType":0}}
[{"age"=>8, "gender"=>"Female", "faceRectangle"=>{"left"=>256, "top"=>126, "width"=>108, "height"=>108}}]
"./private/1.jpg"
8
Female
{"left"=>256, "top"=>126, "width"=>108, "height"=>108}
235
oxford_vision_sample2 (master)$ bundle exe ruby from_local.rb sample/single/2.jpg
{"categories":[{"name":"people_young","score":0.6015625}],"adult":{"isAdultContent":false,"isRacyContent":false,"adultScore":0.045799832791090012,"racyScore":0.085988312959671021},"requestId":"44c64b3d-c4b8-4685-a7ce-0fd1cbd69825","metadata":{"width":599,"height":900,"format":"Jpeg"},"faces":[{"age":17,"gender":"Female","faceRectangle":{"left":254,"top":236,"width":142,"height":142}}],"color":{"dominantColorForeground":"White","dominantColorBackground":"White","dominantColors":["White"],"accentColor":"2673A5","isBWImg":false},"imageType":{"clipArtType":0,"lineDrawingType":0}}
[{"age"=>17, "gender"=>"Female", "faceRectangle"=>{"left"=>254, "top"=>236, "width"=>142, "height"=>142}}]
"./private/2.jpg"
17
Female
{"left"=>254, "top"=>236, "width"=>142, "height"=>142}
379
oxford_vision_sample2 (master)$ bundle exe ruby from_local.rb sample/single/3.jpg
{"categories":[{"name":"others_","score":0.00390625},{"name":"people_","score":0.65625}],"adult":{"isAdultContent":false,"isRacyContent":false,"adultScore":0.045954208821058273,"racyScore":0.1188076063990593},"requestId":"221d32bb-d3be-4a48-abce-8346b0850e28","metadata":{"width":590,"height":885,"format":"Jpeg"},"faces":[{"age":36,"gender":"Female","faceRectangle":{"left":186,"top":68,"width":107,"height":107}}],"color":{"dominantColorForeground":"White","dominantColorBackground":"White","dominantColors":["White"],"accentColor":"326E99","isBWImg":false},"imageType":{"clipArtType":0,"lineDrawingType":0}}
[{"age"=>36, "gender"=>"Female", "faceRectangle"=>{"left"=>186, "top"=>68, "width"=>107, "height"=>107}}]
"./private/3.jpg"
36
Female
{"left"=>186, "top"=>68, "width"=>107, "height"=>107}
176
oxford_vision_sample2 (master)$bundle exe ruby from_local.rb sample/single/4.jpg
{"categories":[{"name":"people_crowd","score":0.4453125}],"adult":{"isAdultContent":false,"isRacyContent":true,"adultScore":0.046315658837556839,"racyScore":0.40509960055351257},"requestId":"386ae066-a4f8-4d46-9d8e-595024813e51","metadata":{"width":599,"height":900,"format":"Jpeg"},"faces":[{"age":20,"gender":"Female","faceRectangle":{"left":270,"top":121,"width":114,"height":114}},{"age":22,"gender":"Female","faceRectangle":{"left":40,"top":566,"width":84,"height":84}}],"color":{"dominantColorForeground":"White","dominantColorBackground":"White","dominantColors":["White","Pink"],"accentColor":"8A417C","isBWImg":false},"imageType":{"clipArtType":0,"lineDrawingType":0}}
[{"age"=>20, "gender"=>"Female", "faceRectangle"=>{"left"=>270, "top"=>121, "width"=>114, "height"=>114}}, {"age"=>22, "gender"=>"Female", "faceRectangle"=>{"left"=>40, "top"=>566, "width"=>84, "height"=>84}}]
"./private/4.jpg"
20
Female
{"left"=>270, "top"=>121, "width"=>114, "height"=>114}
236
22
Female
{"left"=>40, "top"=>566, "width"=>84, "height"=>84}
651
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment