{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os \n\nimport numpy as np\nimport pandas as pd\n\nimport tensorflow as tf\nimport tensorflow_hub as th\nimport matplotlib.pyplot as pl\n\nfrom pprint import pprint as pp\nfrom six import BytesIO as io\nfrom tqdm import tqdm\n\nfrom PIL import Image, ImageColor, ImageDraw, ImageFont, ImageOps","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def pred(img_id, rs) :\n    pred_str = []\n    \n    for i in range(len(rs['detection_scores'])) :\n        cn = rs['detection_class_names'][i].decode(\"utf-8\")\n        YMin,XMin,YMax,XMax = rs['detection_boxes'][i]\n        sc = rs['detection_scores'][i]\n        \n        pred_str.append(\n            f\"{cn} {sc} {XMin} {YMin} {XMax} {YMax}\"\n        )\n        \n        pred_str = \" \".join(pred_str)\n        \n        return {\n        \"ImageID\": img_id,\n        \"PredictionString\": pred_str\n    }\n\ndef display_image(image):\n    fig = pl.figure(figsize = (20, 15))\n    pl.grid(False)\n    pl.axis('off')\n    pl.imshow(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def bound_bx_img(image,\n                               ymin,\n                               xmin,\n                               ymax,\n                               xmax,\n                               color,\n                               font,\n                               thickness = 4,\n                               display_str_list = ()) :\n    drw = ImageDraw.Draw(image)\n    im_width, im_height = image.size\n    \n    (left, right, top, bottom) = (xmin * im_width, xmax * im_width,\n                                  ymin * im_height, ymax * im_height)\n    drw.line([(left, top), (left, bottom), (right, bottom), (right, top),\n               (left, top)],\n              width = thickness,\n              fill = color)\n    \n    display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]\n    \n    total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)\n    \n    if top > total_display_str_height:\n        txt_bm = top\n    else:\n        txt_bm = bottom + total_display_str_height\n    \n    for display_str in display_str_list[::-1]:\n        txt_width, txt_height = font.getsize(display_str)\n        margin = np.ceil(0.05 * txt_height)\n        drw.rectangle([(left, txt_bm - txt_height - 2 * margin),\n                        (left + txt_width, txt_bm)],\n                       fill = color)\n        drw.text((left + margin, txt_bm - txt_height - margin),\n                  display_str,\n                  fill = \"black\",\n                  font = font)\n        txt_bm -= txt_height - 2 * margin","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_boxes(image, bxs, cn, sc, max_boxes = 10, min_score = 0.1):\n  \n    colors = list(ImageColor.colormap.values())\n\n    try:\n        font = ImageFont.truetype(\n            \"/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf\",\n            25)\n    except IOError:\n        print(\"Font not found, using default font.\")\n        font = ImageFont.load_default()\n\n    for i in range(min(bxs.shape[0], max_boxes)):\n        if sc[i] >= min_score:\n            ymin, xmin, ymax, xmax = tuple(bxs[i].tolist())\n            display_str = \"{}: {}%\".format(cn[i].decode(\"ascii\"),\n                                           int(100 * sc[i]))\n            \n            color = colors[hash(cn[i]) % len(colors)]\n            image_pil = Image.fromarray(np.uint8(image)).convert(\"RGB\")\n            \n            bound_bx_img(\n                image_pil,\n                ymin,\n                xmin,\n                ymax,\n                xmax,\n                color,\n                font,\n                display_str_list = [display_str])\n            \n            np.copyto(image, np.array(image_pil))\n    \n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sm_img_pth = \"../input/open-images-2019-object-detection/test/4fce161b84175459.jpg\"\n\nwith tf.Graph().as_default() :\n    \n    img_str_ph = tf.placeholder(tf.string)\n    do_img = tf.image.decode_jpeg(img_str_ph)\n    de_img_fl = tf.image.convert_image_dtype(\n        image = do_img, dtype = tf.float32\n    )\n    \n    img_tf = tf.expand_dims(de_img_fl, 0)\n    \n    mdl_url = \"https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1\"\n    dtr = th.Module(mdl_url)\n    dtr_ot = dtr(img_tf, as_dict = True)\n    \n    init_ops = [tf.global_variables_initializer(), tf.tables_initializer()]\n    ss = tf.Session()\n    ss.run(init_ops)   \n    \n    with tf.gfile.Open(sm_img_pth, \"rb\") as bf :\n        img_str = bf.read()\n        \n        rs_ot, img_ot = ss.run(\n        [dtr_ot, do_img],\n        feed_dict = {img_str_ph: img_str}\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_wth_bxs = draw_boxes(\n    np.array(img_ot), rs_ot[\"detection_boxes\"],\n    rs_ot[\"detection_class_entities\"], rs_ot[\"detection_scores\"]\n)\ndisplay_image(img_wth_bxs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_str_ph = tf.placeholder(tf.string)\nde_img = tf.image.decode_jpeg(img_str_ph)\nde_img_fl = tf.image.convert_image_dtype(\n    image = de_img, dtype = tf.float32\n)\nimg_ts = tf.expand_dims(de_img_fl, 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(img_str_ph)\nprint(de_img)\nprint(de_img_fl)\nprint(img_ts)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mdl_url = \"https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1\"\ndc = th.Module(mdl_url)\ndc_ot = dc(img_ts, as_dict = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pprint(dc_ot)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"init_ops = [tf.global_variables_initializer(), tf.tables_initializer()]\nss = tf.Session()\nss.run(init_ops)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"g = tf.Graph()\nwith g.as_default():\n    \n    c = tf.constant(30.0)\n    assert c.graph is g","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with tf.gfile.Open(sm_img_pth, \"rb\") as bf :\n    img_str = bf.read()\n\nrs_ot, img_ot = ss.run(\n    [dc_ot, de_img],\n    feed_dict = {img_str_ph: img_str}\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"result_out keys:\", rs_ot.keys())\nprint(\"First 10 detection scores:\", rs_ot['detection_scores'][:10])\nprint()\nprint(\"Shape of image_out\", img_ot.shape)\nprint(\"Type of image_out:\", type(img_ot))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sm_sub = pd.read_csv('../input/open-images-2019-object-detection/sample_submission.csv')\nimg_ids = sm_sub['ImageId']\npreds = []\n\nfor img_id in tqdm(img_ids):\n    \n    img_ph = f'../input/open-images-2019-object-detection/test/{img_id}.jpg'\n    with tf.gfile.Open(img_ph, \"rb\") as bf :\n        img_str = bf.read()\n    \n    rs_ot = ss.run(\n        dc_ot,\n        feed_dict = {img_str_ph: img_str}\n    )\n    preds.append(pred(img_id, rs_ot))\n\nss.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame(preds)\nsubmission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}