{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-24T05:49:01.073290Z","iopub.execute_input":"2022-10-24T05:49:01.073610Z","iopub.status.idle":"2022-10-24T05:49:45.314300Z","shell.execute_reply.started":"2022-10-24T05:49:01.073580Z","shell.execute_reply":"2022-10-24T05:49:45.313615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-10-24T05:53:44.445023Z","iopub.execute_input":"2022-10-24T05:53:44.445380Z","iopub.status.idle":"2022-10-24T05:53:44.967095Z","shell.execute_reply.started":"2022-10-24T05:53:44.445350Z","shell.execute_reply":"2022-10-24T05:53:44.966177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:53:47.484490Z","iopub.execute_input":"2022-10-24T05:53:47.484828Z","iopub.status.idle":"2022-10-24T05:53:47.698977Z","shell.execute_reply.started":"2022-10-24T05:53:47.484798Z","shell.execute_reply":"2022-10-24T05:53:47.698233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nfor i in range(len(sample)):\n    ids.append(sample['ImageId'][i])","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:53:51.944262Z","iopub.execute_input":"2022-10-24T05:53:51.944601Z","iopub.status.idle":"2022-10-24T05:53:53.774045Z","shell.execute_reply.started":"2022-10-24T05:53:51.944568Z","shell.execute_reply":"2022-10-24T05:53:53.773058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ids[0:5","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"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])))","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:53:57.973674Z","iopub.execute_input":"2022-10-24T05:53:57.974006Z","iopub.status.idle":"2022-10-24T05:54:47.099551Z","shell.execute_reply.started":"2022-10-24T05:53:57.973975Z","shell.execute_reply":"2022-10-24T05:54:47.098636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_data[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:54:47.101215Z","iopub.execute_input":"2022-10-24T05:54:47.101547Z","iopub.status.idle":"2022-10-24T05:54:47.110713Z","shell.execute_reply.started":"2022-10-24T05:54:47.101511Z","shell.execute_reply":"2022-10-24T05:54:47.110003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:54:56.769658Z","iopub.execute_input":"2022-10-24T05:54:56.770002Z","iopub.status.idle":"2022-10-24T05:54:56.777337Z","shell.execute_reply.started":"2022-10-24T05:54:56.769970Z","shell.execute_reply":"2022-10-24T05:54:56.776014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.compat.v1 as tf\ntf.disable_v2_behavior()","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:54:58.982737Z","iopub.execute_input":"2022-10-24T05:54:58.983061Z","iopub.status.idle":"2022-10-24T05:54:58.987530Z","shell.execute_reply.started":"2022-10-24T05:54:58.983030Z","shell.execute_reply":"2022-10-24T05:54:58.986636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:55:01.281667Z","iopub.execute_input":"2022-10-24T05:55:01.281983Z","iopub.status.idle":"2022-10-24T05:55:55.748111Z","shell.execute_reply.started":"2022-10-24T05:55:01.281953Z","shell.execute_reply":"2022-10-24T05:55:55.747109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:57:21.368898Z","iopub.execute_input":"2022-10-24T05:57:21.369270Z","iopub.status.idle":"2022-10-24T05:57:21.380005Z","shell.execute_reply.started":"2022-10-24T05:57:21.369239Z","shell.execute_reply":"2022-10-24T05:57:21.379152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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])))","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:57:28.622460Z","iopub.execute_input":"2022-10-24T05:57:28.622810Z","iopub.status.idle":"2022-10-24T05:57:28.929775Z","shell.execute_reply.started":"2022-10-24T05:57:28.622776Z","shell.execute_reply":"2022-10-24T05:57:28.928843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir detect","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:57:32.070850Z","iopub.execute_input":"2022-10-24T05:57:32.071194Z","iopub.status.idle":"2022-10-24T05:57:33.174265Z","shell.execute_reply.started":"2022-10-24T05:57:32.071163Z","shell.execute_reply":"2022-10-24T05:57:33.173229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    }","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:57:37.259498Z","iopub.execute_input":"2022-10-24T05:57:37.259853Z","iopub.status.idle":"2022-10-24T05:57:37.268919Z","shell.execute_reply.started":"2022-10-24T05:57:37.259819Z","shell.execute_reply":"2022-10-24T05:57:37.267870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:57:44.702328Z","iopub.execute_input":"2022-10-24T05:57:44.702660Z","iopub.status.idle":"2022-10-24T05:58:57.037325Z","shell.execute_reply.started":"2022-10-24T05:57:44.702629Z","shell.execute_reply":"2022-10-24T05:58:57.036220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2022-10-24T05:58:57.039917Z","iopub.execute_input":"2022-10-24T05:58:57.040703Z","iopub.status.idle":"2022-10-24T05:58:57.047199Z","shell.execute_reply.started":"2022-10-24T05:58:57.040654Z","shell.execute_reply":"2022-10-24T05:58:57.046450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z = load_images(\"/kaggle/working/detect\")\nz[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-24T06:00:36.152825Z","iopub.execute_input":"2022-10-24T06:00:36.153257Z","iopub.status.idle":"2022-10-24T06:00:36.567396Z","shell.execute_reply.started":"2022-10-24T06:00:36.153217Z","shell.execute_reply":"2022-10-24T06:00:36.566451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z[1]","metadata":{"execution":{"iopub.status.busy":"2022-10-24T06:00:53.850118Z","iopub.execute_input":"2022-10-24T06:00:53.850452Z","iopub.status.idle":"2022-10-24T06:00:54.144242Z","shell.execute_reply.started":"2022-10-24T06:00:53.850423Z","shell.execute_reply":"2022-10-24T06:00:54.143154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z[3]","metadata":{"execution":{"iopub.status.busy":"2022-10-24T06:01:42.552660Z","iopub.execute_input":"2022-10-24T06:01:42.553012Z","iopub.status.idle":"2022-10-24T06:01:42.914000Z","shell.execute_reply.started":"2022-10-24T06:01:42.552971Z","shell.execute_reply":"2022-10-24T06:01:42.913184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z[9]","metadata":{"execution":{"iopub.status.busy":"2022-10-24T06:03:35.749748Z","iopub.execute_input":"2022-10-24T06:03:35.750097Z","iopub.status.idle":"2022-10-24T06:03:36.035808Z","shell.execute_reply.started":"2022-10-24T06:03:35.750050Z","shell.execute_reply":"2022-10-24T06:03:36.035056Z"},"trusted":true},"execution_count":null,"outputs":[]}]}