{"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":"len(s_sub), s_sub.iloc[-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"s_sub['PredictionString'][0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"저게 멀 뜻할까..."},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filename = os.listdir('../input/test')\ntest_filename[:5]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"` '209b3b3b4102fba5.jpg',`얘 기준으로 좀 봐야겠다\n"},{"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":"labelMap.loc[labelMap['labelName'].isin(['/m/05s2s','/m/0c9ph5'])]","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(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_image_by_filename('../input/test/209b3b3b4102fba5.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":"# load model\ndetector = ObjectDetection()\ndetector.setModelTypeAsYOLOv3() #Retina도 있고 tinyYOLO도 있음\ndetector.setModelPath(os.path.join(execution_path, \"yolo.h5\"))\ndetector.loadModel()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test detection on one image \ndetections = detector.detectObjectsFromImage(input_image=os.path.join('../input/test', '209b3b3b4102fba5.jpg'),\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') #( x_min,y_min, x_max, y_max)으로 그려지네","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"format_prediction_string(image_id, result): image_id is the ID of the test image you are trying to label. \n    result is the dictionary created from running a tf.Session. \n    The output is a formatted output row (i.e. {Label Confidence XMin YMin XMax YMax},{...}), \n    so we need to modify the order from Tensorflow, which is by default YMin XMin YMax XMax \n"},{"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":"# Test prediction on input images\nres = []\nfor i in tqdm(os.listdir('../input/test')[0:3]):\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    print(currentImg.size) #사이즈가 다 다르구나\n    xSize = currentImg.size[0]\n    ySize = currentImg.size[1]\n    print(detections)\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","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"아에 못 찾은 것도 존재"},{"metadata":{},"cell_type":"markdown","source":"# format_prediction_string() 뜯어보기"},{"metadata":{},"cell_type":"markdown","source":"(1024, 682)\n[{'name': 'person', 'percentage_probability': 98.61317276954651, 'box_points': (601, 206, 871, 682)}, {'name': 'person', 'percentage_probability': 98.87862801551819, 'box_points': (400, 161, 646, 672)}, {'name': 'person', 'percentage_probability': 99.01441931724548, 'box_points': (118, 33, 438, 669)}]  \n얘가 아래 처럼 변한 함수  \n{'ImageID': '1fdf5c3bf33c9ef0.jpg',\n  'PredictionString': '/m/01g317 98.61317276954651 0.5869140625 0.3020527859237537 0.8505859375 1.0 /m/01g317 98.87862801551819 0.390625 0.23607038123167157 0.630859375 0.9853372434017595 /m/01g317 99.01441931724548 0.115234375 0.04838709677419355 0.427734375 0.9809384164222874'},\n"},{"metadata":{},"cell_type":"markdown","source":"`1.0`인거 보니 사이즈에서 위치의 비율같아 보이네"},{"metadata":{},"cell_type":"markdown","source":"```\ndef 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```"},{"metadata":{"trusted":true},"cell_type":"code","source":"detections[0]['name'].capitalize()\nclass_name=detections[0]['name'].capitalize()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"```python\n`str.capitalze()`\nabc -> Abc\naBC -> Abc\n```\n>대문자로 시작하다 뜻"},{"metadata":{"trusted":true},"cell_type":"code","source":"class_name=pd.DataFrame(labelMap.loc[labelMap['Label'].isin([class_name])]['labelName'])\nclass_name","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":"pd.DataFrame(res)","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\n# 여기가 오래 걸리네\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":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}