Class: Circe

Inherits:
Object
  • Object
show all
Defined in:
lib/circe.rb,
lib/circe/version.rb,
ext/circe.cpp

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.2.1'

Instance Method Summary collapse

Instance Method Details

#analyze(*args) ⇒ Object



337
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'ext/circe.cpp', line 337

static VALUE
circe_m_analyze(int argc, VALUE* argv, VALUE self) {
    // Retrieve arguments
    VALUE v_imgstr, v_format, v_opts;
    VALUE kwargs[3];
    rb_scan_args(argc, argv, "11:", &v_imgstr, &v_format, &v_opts);
    rb_get_kwargs(v_opts, (ID[]){ id_debug, id_face, id_classify },
      0, 3, kwargs);
    VALUE v_debug    = IF_UNDEF(kwargs[0], Qfalse);
    VALUE v_face     = IF_UNDEF(kwargs[1], Qfalse);
    VALUE v_classify = IF_UNDEF(kwargs[2], Qfalse);

    VALUE v_features = rb_ary_new();
    VALUE v_image    = Qnil;
    
    if (! NIL_P(v_format)) {
  Check_Type(v_format, T_SYMBOL);
  ID i_format = rb_sym2id(v_format);
  if ((i_format != id_png) && (i_format != id_jpg))
      rb_raise(rb_eArgError, "format must be :png, :jpg or nil");
    }
    
    if (!RTEST(v_face) && !RTEST(v_classify)) {
  v_face = v_classify = Qtrue;
    }


    // Load image.
    Mat i_img = cv::imdecode(cv::Mat(1, RSTRING_LEN(v_imgstr), CV_8UC1,
             (unsigned char *)RSTRING_PTR(v_imgstr)),
           IMREAD_UNCHANGED);      
    Mat o_img = NIL_P(v_format) ? cv::Mat() : i_img.clone();

    // Processing
    std::chrono::time_point<std::chrono::system_clock> start_time, end_time;
    std::chrono::duration<double> duration;
    int state = 0;

    start_time = std::chrono::system_clock::now();
    
    if (RTEST(v_classify)) {
  vector<Yolo::Item> items;
  yolo->process(i_img, items);
  yolo_process_features(items, o_img, v_features, &state);
  if (state) goto exception;
    }

    if (RTEST(v_face)) {
  cv::Mat faces;
  yunet->process(i_img, faces);
  yunet_process_features(faces, o_img, v_features, &state);
  faces.release();
  if (state) goto exception;
    }

    end_time = std::chrono::system_clock::now();
    duration = end_time - start_time;
    

    if (! NIL_P(v_format)) {
  if (RTEST(v_debug)) {
      double ms    = duration / 1.0ms;
      string label = cv::format("Inference time : %0.2f ms", ms);
      cv::putText(o_img, label, Point(20, 40),
      FONT_FACE, FONT_SCALE, RED);
  }

  ID   i_format = rb_sym2id(v_format);
  string format;
  if      (i_format == id_png) { format = ".png"; }
  else if (i_format == id_jpg) { format = ".jpg"; }

  std::vector<uchar> buf;  
  cv::imencode(format, o_img, buf);
  v_image = rb_str_new(reinterpret_cast<char*>(buf.data()), buf.size());
  buf.clear();
    }

    i_img.release();
    o_img.release();

    return rb_ary_new_from_args(2, v_features, v_image);

 exception:
    i_img.release();
    o_img.release();
    rb_jump_tag(state);
}