{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\n\nfrom PIL import Image, ImageDraw","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Install ImageAI (A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities http://imageai.org)"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.3/imageai-2.0.3-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Copy pre-trained model (yolo.h5) for Image Recognition and Object Recognition tasks in ImageAI"},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Copy openimages class names"},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget https://storage.googleapis.com/openimages/v5/class-descriptions-boxable.csv","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"View input files"},{"metadata":{"trusted":true},"cell_type":"code","source":"s_sub = pd.read_csv('../input/sample_submission.csv')\ns_sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filename = os.listdir('../input/test')\ntest_filename[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labelMap = pd.read_csv('class-descriptions-boxable.csv', header=None, names=['labelName', 'Label'])\nlabelMap.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Show one image\ndef show_image_by_index(i):\n    sample_image = plt.imread(f'../input/test/{test_filename[i]}')\n    plt.imshow(sample_image)\n\ndef show_image_by_filename(filename):\n    sample_image = plt.imread(filename)\n    plt.imshow(sample_image)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Test procedures"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_image_by_index(22)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_image_by_filename(f'../input/test/e7c0991d9a37bdef.jpg')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Import additional modules"},{"metadata":{"trusted":true},"cell_type":"code","source":"from imageai.Detection import ObjectDetection","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Get the path to the working directory\nexecution_path = os.getcwd()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n# load model\ndetector = ObjectDetection()\ndetector.setModelTypeAsYOLOv3()\ndetector.setModelPath(os.path.join(execution_path, \"yolo.h5\"))\ndetector.loadModel()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n# test detection on one image\ndetections = detector.detectObjectsFromImage(input_image=os.path.join('../input/test', 'e7c0991d9a37bdef.jpg'),\n                                                                      #test_filename[64]), \n                                             output_image_path=os.path.join(execution_path , \"result.jpg\"),\n#                                            output_type = 'array',\n                                             extract_detected_objects = False)\nfor eachObject in detections:\n    print(eachObject[\"name\"] , \" : \", eachObject[\"percentage_probability\"], \" : \", eachObject[\"box_points\"] )\n\n# show the result\nshow_image_by_filename('./result.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#view detection variable\ndetections","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def format_prediction_string(image_id, result, labelMap, xSize, ySize):\n    prediction_strings = []\n    #print(xSize, ySize)\n    for i in range(len(result)):\n        class_name = result[i]['name'].capitalize()\n        class_name = pd.DataFrame(labelMap.loc[labelMap['Label'].isin([class_name])]['labelName'])\n        #print(result[i]['box_points'])\n        xMin = result[i]['box_points'][0] / xSize\n        xMax = result[i]['box_points'][2] / xSize\n        yMin = result[i]['box_points'][1] / ySize\n        yMax = result[i]['box_points'][3] / ySize\n        \n        if len(class_name) > 0:\n            class_name = class_name.iloc[0]['labelName']\n            boxes = [xMin, yMin, xMax, yMax]#result[i]['box_points']\n            score = result[i]['percentage_probability']\n\n            prediction_strings.append(\n                f\"{class_name} {score} \" + \" \".join(map(str, boxes))\n            )\n        \n    prediction_string = \" \".join(prediction_strings)\n\n    return {\n            \"ImageID\": image_id,\n            \"PredictionString\": prediction_string\n            }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n# Test prediction on input images\nres = []\nfor i in tqdm(os.listdir('../input/test')[20:25]):\n    detections = detector.detectObjectsFromImage(input_image=os.path.join('../input/test', i),\n                                                 output_image_path=os.path.join(execution_path , \"result.jpg\"),\n                                                 #output_type = 'array',\n                                                 extract_detected_objects = False)\n    currentImg = Image.open(os.path.join('../input/test', i))\n    xSize = currentImg.size[0]\n    ySize = currentImg.size[1]\n    #print(xSize, ySize)\n    p = format_prediction_string(i, detections, labelMap, xSize, ySize)\n    res.append(p)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res[1:2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Convert res variable to DataFrame\npred_df = pd.DataFrame(res)\npred_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Get the file name without extension\npred_df['ImageID'] = pred_df['ImageID'].map(lambda x: x.split(\".\")[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# Run detection on test images\nsample_submission_df = pd.read_csv('../input/sample_submission.csv')\nimage_ids = sample_submission_df['ImageId']\npredictions = []\nres = []\nfor image_id in tqdm(image_ids):\n    detections = detector.detectObjectsFromImage(input_image=os.path.join('../input/test', image_id + '.jpg'),\n                                                 output_image_path=os.path.join(execution_path , \"result.jpg\"),\n                                                 #output_type = 'array',\n                                                 extract_detected_objects = False)\n    currentImg = Image.open(os.path.join('../input/test', image_id + '.jpg'))\n    xSize = currentImg.size[0]\n    ySize = currentImg.size[1]\n    p = format_prediction_string(image_id, detections, labelMap, xSize, ySize)\n    res.append(p)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Save submission file\npred_df = pd.DataFrame(res)\npred_df['ImageID'] = pred_df['ImageID'].map(lambda x: x.split(\".\")[0])\npred_df.to_csv('result.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}