{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport matplotlib.pyplot as plt\nfrom six import BytesIO\nimport numpy as np\nimport xml.etree.ElementTree as et\nimport ast\nimport tqdm\nfrom itertools import chain\nfrom xml.dom import minidom\nfrom PIL import Image\nfrom PIL import ImageColor\nfrom PIL import ImageDraw\nfrom PIL import ImageFont\nfrom PIL import ImageOps\nimport cv2\nimport glob\nimport time","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path1='/kaggle/input/open-images-object-detection-rvc-2020/test/'\nsample = pd.read_csv(\"/kaggle/input/open-images-object-detection-rvc-2020/sample_submission.csv\")\nsample.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = []\nfor i in range(len(sample)):\n    ids.append(sample['ImageId'][i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_data=[]\nfor i in range(len(sample)):\n    img_data.append(glob.glob('/kaggle/input/open-images-object-detection-rvc-2020/test/{0}.jpg'.format(ids[i])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_data[0:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_prediction_string(result):\n    with tf.device('/device:GPU:0'):\n        df = pd.DataFrame(columns=['Ymin','Xmin','Ymax', 'Xmax','Score','Label','Class_label','Class_name'])\n        min_score=0.01\n        for i in range(result['detection_boxes'].shape[0]):\n           if (result[\"detection_scores\"][i]) >= min_score:\n              df.loc[i]= tuple(result['detection_boxes'][i])+(result[\"detection_scores\"][i],)+(result[\"detection_class_labels\"][i],)+(result[\"detection_class_names\"][i],)+(result[\"detection_class_entities\"][i],)\n        return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow.compat.v1 as tf\ntf.disable_v2_behavior()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"module_handle = \"https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1\"\nwith tf.device('/device:GPU:0'):\n    with tf.Graph().as_default():\n        detector = hub.Module(module_handle)\n        image_string_placeholder = tf.placeholder(tf.string)\n        decoded_image = tf.image.decode_jpeg(image_string_placeholder)\n        decoded_image_float = tf.image.convert_image_dtype(\n            image=decoded_image, dtype=tf.float32)\n        module_input = tf.expand_dims(decoded_image_float, 0)\n        result = detector(module_input, as_dict=True)\n        init_ops = [tf.global_variables_initializer(), tf.tables_initializer()]\n\n        session = tf.Session()\n        session.run(init_ops)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def nms(dets, thresh):\n    x1 = dets[:, 0]\n    y1 = dets[:, 1]\n    x2 = dets[:, 2]\n    y2 = dets[:, 3]\n    scores = dets[:, 4]\n\n    areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n    order = scores.argsort()[::-1]\n\n    keep = []\n    while order.size > 0:\n        i = order[0]\n        keep.append(i)\n        xx1 = np.maximum(x1[i], x1[order[1:]])\n        yy1 = np.maximum(y1[i], y1[order[1:]])\n        xx2 = np.minimum(x2[i], x2[order[1:]])\n        yy2 = np.minimum(y2[i], y2[order[1:]])\n\n        w = np.maximum(0.0, xx2 - xx1 + 1)\n        h = np.maximum(0.0, yy2 - yy1 + 1)\n        inter = w * h\n        ovr = inter / (areas[i] + areas[order[1:]] - inter)\n\n        inds = np.where(ovr <= thresh)[0]\n        order = order[inds + 1]\n\n    return keep","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_paths = img_data[0:10]\nimages = []\nfor f in image_paths:\n    images.append(np.asarray(Image.open(f[0])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir detect","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id = sample['ImageId']\ndef format_prediction_string(image_id, result):\n    prediction_strings = []\n    \n    for i in range(len(result['Score'])):\n        class_name = result['Class_label'][i].decode(\"utf-8\")\n        YMin,XMin,YMax,XMax = result['Ymin'][i],result['Xmin'][i],result['Ymax'][i],result['Xmax'][i]\n        score = result['Score'][i]\n        \n        prediction_strings.append(\n            f\"{class_name} {score} {XMin} {YMin} {XMax} {YMax}\"\n        )\n        \n    prediction_string = \" \".join(prediction_strings)\n\n    return {\n        \"PredictionString\": prediction_string\n    }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"k =-1\npredictions = []\nwith tf.device('/device:GPU:0'):\n    for image_path in image_paths:\n        k=k+1\n        img_path = img_data[k]\n        img = cv2.imread(img_path[0])\n        with tf.io.gfile.GFile(image_path[0], \"rb\") as binfile:\n            image_string = binfile.read()\n\n        inference_start_time = time.time()\n        result_out, image_out = session.run(\n            [result, decoded_image],\n            feed_dict={image_string_placeholder: image_string})\n        df1=get_prediction_string(result_out)\n        z1=nms(df1.values,0.68)\n        z=df1.iloc[z1]\n        z=z.reset_index()\n        predictions.append(format_prediction_string(image_id, z))\n        data1=z\n        COLORS = np.random.uniform(0, 255, size=(len(z['Class_name']), 3))\n        for m in range(len(data1)):\n            if data1['Score'][m] >=0.01:\n                img_class=data1.iloc[m].Class_name\n                img_xmax, img_ymax =images[k].shape[1],images[k].shape[0]\n                bbox_x_max, bbox_x_min = data1.Xmax[m] * img_xmax, data1.Xmin[m] * img_xmax\n                bbox_y_max ,bbox_y_min = data1.Ymax[m] * img_ymax, data1.Ymin[m] * img_ymax\n                xmin = int(bbox_x_min)\n                ymin = int(bbox_y_min)\n                xmax = int(bbox_x_max)\n                ymax = int(bbox_y_max)\n                width = xmax - xmin\n                height = ymax - ymin\n                label = str(data1['Class_name'][m])\n                color = COLORS[m]\n                cv2.rectangle(img, (xmin, ymax), (xmax, ymin), color, 2)\n                path1 = '/kaggle/working/detect/'+str(k)+'.jpg'\n                cv2.imwrite(path1, img)\n                cv2.putText(img, label, (xmax,ymin), cv2.FONT_HERSHEY_SIMPLEX, 0.9,color, 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_images(folder):\n    images = []\n    for filename in os.listdir(folder):\n        img = Image.open(os.path.join(folder, filename))\n        if img is not None:\n            images.append(img)\n    return images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"z = load_images(\"/kaggle/working/detect\")\nz[0]","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":4}