Ignore:
Timestamp:
2022-12-09T00:10:37+13:00 (17 months ago)
Author:
davidb
Message:

Collection now developed to the point where Google Vision OCR bounding boxes are displayed over the screen-size image for a simple doc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-installations/intermuse/trunk/sites/intermuse/collect/programmes/js/document_viewer.js

    r36993 r36997  
     1function load_gv_dococr_json(json_file, callback) {
     2
     3    var http_assocfilepath = gs.collectionMetadata["httpPath"]+"/index/assoc/"+gs.documentMetadata["assocfilepath"];
     4    var json_url = http_assocfilepath +"/"+json_file;
     5
     6    $.ajax({
     7    method: "GET",
     8    url: json_url
     9    })
     10        .fail(function(jqXHR,textStatus) {
     11            console.error("load_gv_dococr_json(): failed to retrieve url '" + json_url +"'");
     12        console.error(textStatus);
     13        })
     14        .done(function(gv_ocr_json_result) {
     15        callback(gv_ocr_json_result);
     16
     17        });
     18   
     19}
     20
     21function display_gv_ocr_bounding_boxes(gv_ocr_json)
     22{
     23    console.log(gv_ocr_json);
     24
     25   
     26    var full_text_annotation = gv_ocr_json.fullTextAnnotation;
     27    var pages = full_text_annotation.pages;
     28    var num_pages = pages.length;
     29   
     30    if (num_pages == 1) {
     31    var page = pages[0];
     32
     33    var bounding_box_rects = [];
     34    var page_x_dim = page.width;
     35    var page_y_dim = page.height;
     36
     37    var blocks = page.blocks;
     38    var num_blocks = blocks.length;
     39
     40    for (b=0; b<num_blocks; b++) {
     41        var block = blocks[b];
     42
     43        var boundingBox = block.boundingBox;
     44
     45        var min_x = Number.MAX_SAFE_INTEGER;
     46        var min_y = Number.MAX_SAFE_INTEGER;
     47        var max_x = Number.MIN_SAFE_INTEGER;
     48        var max_y = Number.MIN_SAFE_INTEGER;
     49
     50        var vertices = boundingBox.vertices;
     51        var num_vertices = vertices.length;
     52
     53        for (v=0; v<num_vertices; v++) {
     54        var x = vertices[v].x;
     55        var y = vertices[v].y;
     56       
     57        min_x = Math.min(min_x,x);
     58        min_y = Math.min(min_y,y);
     59        max_x = Math.max(max_x,x);
     60        max_y = Math.max(max_y,y);
     61        }
     62
     63        var x_org = min_x;
     64        var y_org = min_y;
     65        var x_dim = max_x - min_x +1;
     66        var y_dim = max_y - min_y +1;
     67
     68        var rect = { "x_org": x_org, "y_org": y_org, "x_dim": x_dim, "y_dim": y_dim};
     69        bounding_box_rects.push(rect);
     70    }
     71
     72       
     73    display_scaled_div_bounding_boxes(bounding_box_rects, page_x_dim,page_y_dim);
     74   
     75    }
     76    else {
     77    console.error("display_gv_ocr_bounding_boxes(): incorrect number of pages found.")
     78    console.error("  Expected 1 page, found " + num_pages +" page(s)");
     79    }
     80
     81}
     82
     83
     84function display_scaled_div_bounding_boxes(bounding_box_rects,fullsize_x_dim,fullsize_y_dim)
     85{
     86    var screen_x_dim = gs.variables.screenImageWidth;
     87    var screen_y_dim = gs.variables.screenImageHeight;
     88
     89    var scale_x = screen_x_dim / fullsize_x_dim;
     90    var scale_y = screen_y_dim / fullsize_y_dim;
     91
     92    //console.log("scale x = " + scale_x);
     93    //console.log("scale y = " + scale_y);
     94   
     95    var docID = gs.variables.d;   
     96    var screen_image_id = "small"+docID;
     97
     98    var $boundingbox_overlay = $("<div>")
     99    .attr("id","ocr-boundingbox-overlay-"+docID)
     100    .attr("class","ocr-boundingbox-overlay");
     101
     102    var $screen_div = $('#'+screen_image_id);
     103    //var $screen_img = $screen_div.find("img");
     104   
     105    $screen_div.append($boundingbox_overlay);
     106    //$screen_img.append($boundingbox_overlay);
     107
     108   
     109    var num_bb_rects = bounding_box_rects.length;
     110
     111    console.log("Block bounding boxes:")
     112
     113    for (r=0; r<num_bb_rects; r++) {
     114    var rect = bounding_box_rects[r];
     115        console.log("  " + JSON.stringify(rect));
     116
     117    var scaled_x_org = rect.x_org * scale_x;
     118    var scaled_y_org = rect.y_org * scale_y;
     119    var scaled_x_dim = rect.x_dim * scale_x;
     120    var scaled_y_dim = rect.y_dim * scale_y;
     121       
     122    var $boundingbox_div = $("<div>")
     123        .attr("class","ocr-boundingbox")
     124        .css("left",  scaled_x_org)
     125        .css("top",   scaled_y_org)
     126        .css("width", scaled_x_dim)
     127        .css("height",scaled_y_dim);
     128   
     129    $boundingbox_overlay.append($boundingbox_div)
     130    }
     131}
     132
    1133/*
    2134
Note: See TracChangeset for help on using the changeset viewer.