Module: Levdon
- Defined in:
- lib/levdon.rb,
lib/levdon/version.rb
Defined Under Namespace
Classes: LevdonImpl, NonBlockLineStream, Worker
Constant Summary collapse
- APPLICATION_ID =
"Levdon"
- API_URL =
ENV['EATER_URL'] ? ENV['EATER_URL'] : "https://api.leviadon.com/upload"
- API_VERSION =
"0.1.1"
- ENABLE_SSL =
API_URL.index("https://") ? true : false
- VUUID =
SecureRandom.uuid
- ONTOLOGY_LIST =
{ :CLASS_ADULT => { :available => true, :key => 'adult', :desc => '' }, :CLASS_VIOLENCE => { :available => false, :key => 'violence', :desc => 'closed beta does not support a this class.' }, :CLASS_GENERAL_OBJECT => { :available => false, :key => 'general_object', :desc => 'closed beta does not support a this class.' }, :CLASS_MEDICAL => { :available => false, :key => 'medical', :desc => 'closed beta does not support a this class.' }, :CLASS_MEAL => { :available => false, :key => 'meal', :desc => 'closed beta does not support a this class.' }, :CLASS_3D => { :available => false, :key => '3D', :desc => 'closed beta does not support a this class.' }, :CLASS_2D => { :available => false, :key => '2D', :desc => 'closed beta does not support a this class.' }, :CLASS_FACE => { :available => false, :key => 'face', :desc => 'closed beta does not support a this class.' }, :CLASS_BODY => { :available => false, :key => 'body', :desc => 'closed beta does not support a this class.' }, :CLASS_POSE => { :available => false, :key => 'pose', :desc => 'closed beta does not support a this class.' }, :CLASS_ANIMAL => { :available => false, :key => 'animal', :desc => 'closed beta does not support a this class.' }, :CLASS_OCR => { :available => false, :key => 'ocr', :desc => 'closed beta does not support a this class.' } }
- ONTOLOGY_REV_LIST =
{}
- VERSION =
"0.1.2"
Class Method Summary collapse
- .find_image(path, options = {:no_check => false}, &block) ⇒ Object
- .high_quality_resize(stream) ⇒ Object
- .identify_image(path, &block) ⇒ Object
- .low_quality_resize(stream) ⇒ Object
- .new ⇒ Object
- .predict(access_token, target) ⇒ Object
- .prob_resize(stream) ⇒ Object
- .standardize(path, options = {:mode=>:delete}) ⇒ Object
Class Method Details
.find_image(path, options = {:no_check => false}, &block) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/levdon.rb', line 338 def self.find_image(path,={:no_check => false},&block) unless File.exist?(path) puts "Error" puts " File does not exist => " + path return end proc = lambda() {|e| unless File.directory?(e) begin if( and [:no_check] == true) if %w(.JPG .JPEG .GIF .PNG).member?(File.extname(e).upcase) obj = {} obj[:source_path] = e obj[:source_filename] = File.basename(e) return obj end else if %w(.JPG .JPEG .GIF .PNG).member?(File.extname(e).upcase) img = Magick::Image.from_blob( File.read(e) ).shift if %w(JPEG GIF PNG).member?(img.format.to_s) obj = {} obj[:source_path] = e obj[:source_filename] = File.basename(e) obj[:image] = img; return obj end end end rescue => e puts "Warning: caught a something error." puts e.class puts e. puts e.backtrace # nothing to do end end return nil } unless File.directory?(path) obj = proc.call(path) block.call(obj) if(obj) else Dir.glob(path + "/" + "**/*").each{|e| obj = proc.call(e) block.call(obj) if(obj) } end end |
.high_quality_resize(stream) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/levdon.rb', line 387 def self.high_quality_resize(stream) img = Magick::Image.from_blob(stream).first width = img.columns height = img.rows if(width > 512 and height > 512) img = img.resize_to_fit(512, 512) end img.to_blob { self.quality = 90 self.format = "jpeg" } end |
.identify_image(path, &block) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/levdon.rb', line 269 def self.identify_image(path,&block) proc = lambda() {|e| unless File.directory?(e) begin fname = File.basename(e) # TODO if(fname.index("@STANDARDIZED@_") == 0 or fname.index("@Q@_") == 0) return nil end fsize = File.binread(e).size if(fsize > 1024*1024*5) obj = {} obj[:source_path] = e obj[:source_filename] = File.basename(e) obj[:error] = "Too big file :" + fsize.to_s return obj end if(fsize < 1024*10) obj = {} obj[:source_path] = e obj[:source_filename] = File.basename(e) obj[:error] = "Too small file :" + fsize.to_s return obj end time = Time.new img = Magick::Image.from_blob( File.read(e) ).shift #puts (Time.new - time).to_s + " : " + fsize.to_s + " : "+e if %w(JPEG GIF PNG).member?(img.format) obj = {} obj[:source_path] = e obj[:source_filename] = File.basename(e) obj[:image] = img; obj[:time] = Time.new - time obj[:size] = fsize return obj else obj = {} obj[:source_path] = e obj[:source_filename] = File.basename(e) obj[:image] = img; obj[:error] = "Unknown file" return obj end rescue => err puts "Warning: cought a something error." obj = {} obj[:source_path] = e obj[:source_filename] = File.basename(e) obj[:image] = img; obj[:error] = err return obj end end return nil } unless File.directory?((path)) obj = proc.call(path) block.call(obj) if(obj) else Dir.glob(path + "/" + "**/*").each{|e| obj = proc.call(e) block.call(obj) if(obj) } end end |
.low_quality_resize(stream) ⇒ Object
400 401 402 403 404 405 406 407 |
# File 'lib/levdon.rb', line 400 def self.low_quality_resize(stream) img = Magick::Image.from_blob(stream).first img = img.resize(224,224) img.to_blob { self.quality = 20 self.format = "jpeg" } end |
.new ⇒ Object
874 875 876 |
# File 'lib/levdon.rb', line 874 def self.new LevdonImpl.new end |
.predict(access_token, target) ⇒ Object
878 879 880 881 882 883 884 885 886 887 888 889 890 891 |
# File 'lib/levdon.rb', line 878 def self.predict(access_token,target) api = LevdonImpl.new state = api.start(access_token) if(state[:error]) raise state[:error] else result = api.predict(target) if(result[:error]) raise result[:error] else return result end end end |
.prob_resize(stream) ⇒ Object
409 410 411 412 413 414 |
# File 'lib/levdon.rb', line 409 def self.prob_resize(stream) if(Random.rand() < 0.9) return low_quality_resize(stream) end return high_quality_resize(stream) end |
.standardize(path, options = {:mode=>:delete}) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/levdon.rb', line 417 def self.standardize(path,={:mode=>:delete}) mode = [:mode] dest = [:dest] unless(mode) puts "Error" puts "Require parameter" puts " Specify a :mode => :delete or :move or :rename" exit(9) end unless(%w{delete delete rename}.member?(mode.to_s)) puts "Error" puts "Require parameter" puts " Specify a :mode => :delete or :move or :rename" exit(9) end dirname = path unless File.directory?(path) dirname = File.dirname(path) end puts "Entry path : " + path puts "Destination : " + dirname identify_image(path) {|e| source_path = e[:source_path] source_filename = e[:source_filename]; dst_dirname = File.dirname(source_path) if(e[:error]) File.delete(source_path) if File.exist?(source_path) if(mode == :delete) File.delete(source_path) if File.exist?(source_path) next elsif(mode == :rename) FileUtils.mv(source_path,File.join(dst_dirname,mark+SecureRandom.uuid+"."+source_filename.split(".")[-1])) next else next end end img = e[:image] width = img.columns height = img.rows mark = "" # Check already standardized file or not. if source_filename.index("@STANDARDIZED@") == 0 or source_filename.index("@Q@") == 0 puts "already done : " + source_path next # else # puts "processing : " + source_path end # Validaiton check if(width > 8192 or height > 8192) #puts "too big image: " + source_path mark = "@BIG@_" elsif(width < 300 or height < 300) #puts "thumb image : " + source_path mark = "@THUMB@_" end puts e[:time].to_s + " : " + e[:size].to_s + " : " + mark + " : " + source_path if(mark.length > 0) if(mode == :delete) File.delete(source_path) if File.exist?(source_path) next elsif(mode == :rename) FileUtils.mv(source_path,File.join(dst_dirname,mark+SecureRandom.uuid+"."+source_filename.split(".")[-1])) next elsif(mode == :move) if(dest) if(File.exist?(dest)) if(File.directory?(dest)) FileUtils.mv(source_path,File.join(dest,mark+SecureRandom.uuid+"."+source_filename.split(".")[-1])) next else puts "Error" puts "Destination is not directory. => " + dest exit(9) end else puts "Error" puts "Destination path does not exist. => " + dest exit(9) end else puts "Error" puts "Destination path is nil." puts "You should specify a destination path for :move mode." exit(9) end end end # keeping ratio if(width > 1024 or height > 1024) img.resize_to_fit!(1024, 1024) end fname = source_filename.split(".")[0] img.format = 'JPEG' begin # Reformat and save img.write(File.join(dst_dirname,"@STANDARDIZED@_"+SecureRandom.uuid+".jpg")) { self.quality = 90 } # 1~100 # Delete source file File.delete(source_path) if File.exist?(source_path) rescue => e puts "Write error" puts e.class puts e. puts e.backtrace end # 10~30KB 80 : Q-TRAIN # 30~60KB 90 : Q-SOURCE # 50~200KB 100 : Q-SOURCE } puts "Done" end |