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.1.0'

Instance Method Summary collapse

Instance Method Details

#analyze(*args) ⇒ Object



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
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
# File 'ext/circe.cpp', line 292

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)) {
  vector<YuNet::Face> faces;
  yunet->process(i_img, faces);
  yunet_process_features(faces, o_img, v_features, &state);
  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());
    }
    return rb_ary_new_from_args(2, v_features, v_image);

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