You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

566 lines
1.7 MiB

6 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {
  6. "colab_type": "text",
  7. "id": "V8-yl-s-WKMG"
  8. },
  9. "source": [
  10. "# Object Detection Demo\n",
  11. "Welcome to the object detection inference walkthrough! This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image. Make sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) before you start."
  12. ]
  13. },
  14. {
  15. "cell_type": "markdown",
  16. "metadata": {
  17. "colab_type": "text",
  18. "id": "kFSqkTCdWKMI"
  19. },
  20. "source": [
  21. "# Imports"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 51,
  27. "metadata": {
  28. "colab": {
  29. "autoexec": {
  30. "startup": false,
  31. "wait_interval": 0
  32. }
  33. },
  34. "colab_type": "code",
  35. "id": "hV4P5gyTWKMI"
  36. },
  37. "outputs": [],
  38. "source": [
  39. "import numpy as np\n",
  40. "import os\n",
  41. "import six.moves.urllib as urllib\n",
  42. "import sys\n",
  43. "import tarfile\n",
  44. "import tensorflow as tf\n",
  45. "import zipfile\n",
  46. "\n",
  47. "from distutils.version import StrictVersion\n",
  48. "from collections import defaultdict\n",
  49. "from io import StringIO\n",
  50. "\n",
  51. "from utils import label_map_util\n",
  52. "\n",
  53. "from utils import visualization_utils as vis_util\n",
  54. "from matplotlib import pyplot as plt\n",
  55. "from PIL import Image\n",
  56. "\n",
  57. "# This is needed since the notebook is stored in the object_detection folder.\n",
  58. "sys.path.append(\"..\")\n",
  59. "from object_detection.utils import ops as utils_ops\n",
  60. "\n",
  61. "if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):\n",
  62. " raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')\n"
  63. ]
  64. },
  65. {
  66. "cell_type": "markdown",
  67. "metadata": {
  68. "colab_type": "text",
  69. "id": "Wy72mWwAWKMK"
  70. },
  71. "source": [
  72. "## Env setup"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 52,
  78. "metadata": {
  79. "colab": {
  80. "autoexec": {
  81. "startup": false,
  82. "wait_interval": 0
  83. }
  84. },
  85. "colab_type": "code",
  86. "id": "v7m_NY_aWKMK"
  87. },
  88. "outputs": [],
  89. "source": [
  90. "# This is needed to display the images.\n",
  91. "%matplotlib inline"
  92. ]
  93. },
  94. {
  95. "cell_type": "markdown",
  96. "metadata": {
  97. "colab_type": "text",
  98. "id": "r5FNuiRPWKMN"
  99. },
  100. "source": [
  101. "## Object detection imports\n",
  102. "Here are the imports from the object detection module."
  103. ]
  104. },
  105. {
  106. "cell_type": "code",
  107. "execution_count": null,
  108. "metadata": {
  109. "colab": {
  110. "autoexec": {
  111. "startup": false,
  112. "wait_interval": 0
  113. }
  114. },
  115. "colab_type": "code",
  116. "id": "bm0_uNRnWKMN"
  117. },
  118. "outputs": [],
  119. "source": []
  120. },
  121. {
  122. "cell_type": "markdown",
  123. "metadata": {
  124. "colab_type": "text",
  125. "id": "cfn_tRFOWKMO"
  126. },
  127. "source": [
  128. "# Model preparation "
  129. ]
  130. },
  131. {
  132. "cell_type": "markdown",
  133. "metadata": {
  134. "colab_type": "text",
  135. "id": "X_sEBLpVWKMQ"
  136. },
  137. "source": [
  138. "## Variables\n",
  139. "\n",
  140. "Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_FROZEN_GRAPH` to point to a new .pb file. \n",
  141. "\n",
  142. "By default we use an \"SSD with Mobilenet\" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies."
  143. ]
  144. },
  145. {
  146. "cell_type": "code",
  147. "execution_count": 53,
  148. "metadata": {
  149. "colab": {
  150. "autoexec": {
  151. "startup": false,
  152. "wait_interval": 0
  153. }
  154. },
  155. "colab_type": "code",
  156. "id": "VyPz_t8WWKMQ"
  157. },
  158. "outputs": [],
  159. "source": [
  160. "# What model to download.\n",
  161. "MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'\n",
  162. "MODEL_FILE = MODEL_NAME + '.tar.gz'\n",
  163. "DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'\n",
  164. "\n",
  165. "# Path to frozen detection graph. This is the actual model that is used for the object detection.\n",
  166. "PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'\n",
  167. "\n",
  168. "# List of the strings that is used to add correct label for each box.\n",
  169. "PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')"
  170. ]
  171. },
  172. {
  173. "cell_type": "markdown",
  174. "metadata": {
  175. "colab_type": "text",
  176. "id": "7ai8pLZZWKMS"
  177. },
  178. "source": [
  179. "## Download Model"
  180. ]
  181. },
  182. {
  183. "cell_type": "code",
  184. "execution_count": null,
  185. "metadata": {
  186. "colab": {
  187. "autoexec": {
  188. "startup": false,
  189. "wait_interval": 0
  190. }
  191. },
  192. "colab_type": "code",
  193. "id": "KILYnwR5WKMS"
  194. },
  195. "outputs": [],
  196. "source": []
  197. },
  198. {
  199. "cell_type": "code",
  200. "execution_count": null,
  201. "metadata": {},
  202. "outputs": [],
  203. "source": []
  204. },
  205. {
  206. "cell_type": "markdown",
  207. "metadata": {
  208. "colab_type": "text",
  209. "id": "YBcB9QHLWKMU"
  210. },
  211. "source": [
  212. "## Load a (frozen) Tensorflow model into memory."
  213. ]
  214. },
  215. {
  216. "cell_type": "code",
  217. "execution_count": 54,
  218. "metadata": {
  219. "colab": {
  220. "autoexec": {
  221. "startup": false,
  222. "wait_interval": 0
  223. }
  224. },
  225. "colab_type": "code",
  226. "id": "KezjCRVvWKMV"
  227. },
  228. "outputs": [],
  229. "source": [
  230. "detection_graph = tf.Graph()\n",
  231. "with detection_graph.as_default():\n",
  232. " od_graph_def = tf.GraphDef()\n",
  233. " with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:\n",
  234. " serialized_graph = fid.read()\n",
  235. " od_graph_def.ParseFromString(serialized_graph)\n",
  236. " tf.import_graph_def(od_graph_def, name='')"
  237. ]
  238. },
  239. {
  240. "cell_type": "markdown",
  241. "metadata": {
  242. "colab_type": "text",
  243. "id": "_1MVVTcLWKMW"
  244. },
  245. "source": [
  246. "## Loading label map\n",
  247. "Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine"
  248. ]
  249. },
  250. {
  251. "cell_type": "code",
  252. "execution_count": 55,
  253. "metadata": {
  254. "colab": {
  255. "autoexec": {
  256. "startup": false,
  257. "wait_interval": 0
  258. }
  259. },
  260. "colab_type": "code",
  261. "id": "hDbpHkiWWKMX"
  262. },
  263. "outputs": [],
  264. "source": [
  265. "category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)"
  266. ]
  267. },
  268. {
  269. "cell_type": "markdown",
  270. "metadata": {
  271. "colab_type": "text",
  272. "id": "EFsoUHvbWKMZ"
  273. },
  274. "source": [
  275. "## Helper code"
  276. ]
  277. },
  278. {
  279. "cell_type": "code",
  280. "execution_count": 56,
  281. "metadata": {
  282. "colab": {
  283. "autoexec": {
  284. "startup": false,
  285. "wait_interval": 0
  286. }
  287. },
  288. "colab_type": "code",
  289. "id": "aSlYc3JkWKMa"
  290. },
  291. "outputs": [],
  292. "source": [
  293. "def load_image_into_numpy_array(image):\n",
  294. " (im_width, im_height) = image.size\n",
  295. " return np.array(image.getdata()).reshape(\n",
  296. " (im_height, im_width, 3)).astype(np.uint8)"
  297. ]
  298. },
  299. {
  300. "cell_type": "markdown",
  301. "metadata": {
  302. "colab_type": "text",
  303. "id": "H0_1AGhrWKMc"
  304. },
  305. "source": [
  306. "# Detection"
  307. ]
  308. },
  309. {
  310. "cell_type": "code",
  311. "execution_count": 57,
  312. "metadata": {
  313. "colab": {
  314. "autoexec": {
  315. "startup": false,
  316. "wait_interval": 0
  317. }
  318. },
  319. "colab_type": "code",
  320. "id": "jG-zn5ykWKMd"
  321. },
  322. "outputs": [],
  323. "source": [
  324. "# For the sake of simplicity we will use only 2 images:\n",
  325. "# image1.jpg\n",
  326. "# image2.jpg\n",
  327. "# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.\n",
  328. "PATH_TO_TEST_IMAGES_DIR = 'test_images'\n",
  329. "TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 4) ]\n",
  330. "\n",
  331. "# Size, in inches, of the output images.\n",
  332. "IMAGE_SIZE = (12, 8)"
  333. ]
  334. },
  335. {
  336. "cell_type": "code",
  337. "execution_count": 58,
  338. "metadata": {
  339. "colab": {
  340. "autoexec": {
  341. "startup": false,
  342. "wait_interval": 0
  343. }
  344. },
  345. "colab_type": "code",
  346. "id": "92BHxzcNWKMf"
  347. },
  348. "outputs": [],
  349. "source": [
  350. "def run_inference_for_single_image(image, graph):\n",
  351. " with graph.as_default():\n",
  352. " with tf.Session() as sess:\n",
  353. " # Get handles to input and output tensors\n",
  354. " ops = tf.get_default_graph().get_operations()\n",
  355. " all_tensor_names = {output.name for op in ops for output in op.outputs}\n",
  356. " tensor_dict = {}\n",
  357. " for key in [\n",
  358. " 'num_detections', 'detection_boxes', 'detection_scores',\n",
  359. " 'detection_classes', 'detection_masks'\n",
  360. " ]:\n",
  361. " tensor_name = key + ':0'\n",
  362. " if tensor_name in all_tensor_names:\n",
  363. " tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(\n",
  364. " tensor_name)\n",
  365. " if 'detection_masks' in tensor_dict:\n",
  366. " # The following processing is only for single image\n",
  367. " detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])\n",
  368. " detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])\n",
  369. " # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.\n",
  370. " real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)\n",
  371. " detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])\n",
  372. " detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])\n",
  373. " detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(\n",
  374. " detection_masks, detection_boxes, image.shape[1], image.shape[2])\n",
  375. " detection_masks_reframed = tf.cast(\n",
  376. " tf.greater(detection_masks_reframed, 0.5), tf.uint8)\n",
  377. " # Follow the convention by adding back the batch dimension\n",
  378. " tensor_dict['detection_masks'] = tf.expand_dims(\n",
  379. " detection_masks_reframed, 0)\n",
  380. " image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')\n",
  381. "\n",
  382. " # Run inference\n",
  383. " output_dict = sess.run(tensor_dict,\n",
  384. " feed_dict={image_tensor: image})\n",
  385. "\n",
  386. " # all outputs are float32 numpy arrays, so convert types as appropriate\n",
  387. " output_dict['num_detections'] = int(output_dict['num_detections'][0])\n",
  388. " output_dict['detection_classes'] = output_dict[\n",
  389. " 'detection_classes'][0].astype(np.int64)\n",
  390. " output_dict['detection_boxes'] = output_dict['detection_boxes'][0]\n",
  391. " output_dict['detection_scores'] = output_dict['detection_scores'][0]\n",
  392. " if 'detection_masks' in output_dict:\n",
  393. " output_dict['detection_masks'] = output_dict['detection_masks'][0]\n",
  394. " return output_dict"
  395. ]
  396. },
  397. {
  398. "cell_type": "code",
  399. "execution_count": 59,
  400. "metadata": {
  401. "colab": {
  402. "autoexec": {
  403. "startup": false,
  404. "wait_interval": 0
  405. }
  406. },
  407. "colab_type": "code",
  408. "id": "3a5wMHN8WKMh"
  409. },
  410. "outputs": [
  411. {
  412. "data": {
  413. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAskAAAHCCAYAAAAdGlSzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpY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lX3P39iGzaQupx4eezXzglZfu8ef+/E9TtQ2DgrXVB+aMyPPjvp0XaeeUFCCblJEzaIDz8yu+/d57zBcr3nxyzsVqxdMuEpNhtQm4GFgsFnRdx2azea7NVvKabZsGU9bvdDzBGctoNKK2DjWCcZZoEmIMkUjaRKLmPhtCJESlT2HXRuccIoKLhpAy0SQWxASwoOuegPDR7/8ktw5OSKuBK+s5mk44mExJMbBZzNmsB+rGM2mmEFtUA/16zmRc8fLxLWKvLC8eYlyPFYNpxvQKfeiZTVoeHB3gJiMen0X6YaAdbziMAzo6pYuWs4tntFUGw3XrGIxlvhbeePcRzajiuBp49dY9krvN20/eoakS47Di6GhKN7nNxXKge/IMZz0npuf29BZfezfxjUePedw9xYUVHz8a42j4gyfPOLjdcjyq+eitCZrgy+9ueDzfcGsaOD2quT87ZdUJ33zyiIdPz3njyZxRY/jsKyfMxjOeXK24WsxJEmhnDbPpIa9+3+u04zEvvPI6o8mE8cEh1lqGbpIJJ2OoXcW4bohuAFXatkWS7kiHuskEgKqgybL1fE6OpjRNQww9qkpjDWDAVCQVQkgkYwlhYLGa5/lqldZM6IYeVDBVJhOG0GOMYSjkY0opnwERRk2NqyyV2QL4SFV
  414. "text/plain": [
  415. "<Figure size 864x576 with 1 Axes>"
  416. ]
  417. },
  418. "metadata": {
  419. "needs_background": "light"
  420. },
  421. "output_type": "display_data"
  422. },
  423. {
  424. "data": {
  425. "image/png": "iVBORw0KGgoAAAANSUhEUgAAArkAAAHWCAYAAABzFJGmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzsvUuubUuyJTTM3OfeN14XgAKCR/lJSNSQoIBEjSp0IEs0gA7QiWwBokgBKZFoQZYBgVJICUnxlSP2mu5mFMyGuc11zo24Kb3IvHraHjpxzt17rfnxj/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7x+wAjPw4ubQnHTwFsHDKA+wLHWlWDZFFA1KHguAY4tH2HE4BdfRPO5SpA5E6bu6EyA2C6lk2ToRii4WQ5YGuHHYYBPoFaywnqROPnFk4LBgrM8D0l9wVVBcSgMepw3/AN+Now0SCOmlOtdmO7QT2cBfd9xg549GPY4I3tC65Pu1XzymMdDUfZlzlnfZeOiEIK4JZdlBsKw8aGCeASjuhK2749+td8Id1aTBw7UsRT7mWbQDrnrI/4VjgIsU8+1vjIfVGPwzjGwExSQVWhDsAcw2MOF6mmCrWYi1NmYBo3OG5cQ5KgiXv5OPtvOfxJFHU7ZNh5HRRG4PtBBdB8xwLusUdwL1cEjiub2H53XVeSA+Eczo+Pso2xBhtB9KsU9I/tHxzkuvsC8N8C+GcA/g8A/6O7/29/7jvkQABgzo8gcebAawW7M3ABUKx1gKXbwHVduZGxKxpIVIGOz/jp2uFlzRne7bYzwVXDTU7WDHbYQ99pFHRib8c
  426. "text/plain": [
  427. "<Figure size 864x576 with 1 Axes>"
  428. ]
  429. },
  430. "metadata": {
  431. "needs_background": "light"
  432. },
  433. "output_type": "display_data"
  434. },
  435. {
  436. "data": {
  437. "image/png": "iVBORw0KGgoAAAANSUhEUgAAAccAAAHVCAYAAACE4jAiAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzsvdnvLVmW3/VZe4iIc85vuEPezKzM7Krq6qruQo0EyMgGtVpICBCDDAIJGfsdiwcav1k8IHhFlnixZWT3A7xZ/AUWIIMZhICecLsnyl3d1ZWVVZmVmXf6DedExB4WD2tHnHNvZVbdairdafu3Uqn7+/1i2rFj7zV+11qiqtzRHd3RHd3RHd3Rkdyf9ADu6I7u6I7u6I4+b3QnHO/oju7oju7ojl6iO+F4R3d0R3d0R3f0Et0Jxzu6ozu6ozu6o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T3UbblNGqrIdHH1n75dLgXJOjIHiKt22p7sI7Pc2dyllC2Q4hxCpaeL2+prpYMIfX7m43LEZtuALm9iz25xRygN7f1fZbjq6fmGK5qbOB7N6UknGvGZFSwD11OmANKbVuYpogdqYswpNdjDlCRc9U6loN7BPiaurG8bGAK+vn1Ny4PYwkYvj+mZGQqQ2t2boPH23Zbvpic7b2PueXWcP2ERht/X0nSckcOKBQDnx8tRayLkgWnExoEvkBvMWmcs4rCGQqVSct5+DF4K45rWiCZ9Tf5FDF2+UtFCB6LqtVKq53dU1QRWbx2pxy5qLvWL80ITQ0UJVKeYtqH4NJyDHkIJKZN3iFBBz+a+hI3VNMJZ2Xb+OfHFxw1F
  438. "text/plain": [
  439. "<Figure size 864x576 with 1 Axes>"
  440. ]
  441. },
  442. "metadata": {
  443. "needs_background": "light"
  444. },
  445. "output_type": "display_data"
  446. }
  447. ],
  448. "source": [
  449. "for image_path in TEST_IMAGE_PATHS:\n",
  450. " image = Image.open(image_path)\n",
  451. " # the array based representation of the image will be used later in order to prepare the\n",
  452. " # result image with boxes and labels on it.\n",
  453. " image_np = load_image_into_numpy_array(image)\n",
  454. " # Expand dimensions since the model expects images to have shape: [1, None, None, 3]\n",
  455. " image_np_expanded = np.expand_dims(image_np, axis=0)\n",
  456. " # Actual detection.\n",
  457. " output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)\n",
  458. " # Visualization of the results of a detection.\n",
  459. " vis_util.visualize_boxes_and_labels_on_image_array(\n",
  460. " image_np,\n",
  461. " output_dict['detection_boxes'],\n",
  462. " output_dict['detection_classes'],\n",
  463. " output_dict['detection_scores'],\n",
  464. " category_index,\n",
  465. " instance_masks=output_dict.get('detection_masks'),\n",
  466. " use_normalized_coordinates=True,\n",
  467. " line_thickness=8)\n",
  468. " plt.figure(figsize=IMAGE_SIZE)\n",
  469. " plt.imshow(image_np)"
  470. ]
  471. },
  472. {
  473. "cell_type": "code",
  474. "execution_count": null,
  475. "metadata": {
  476. "colab": {
  477. "autoexec": {
  478. "startup": false,
  479. "wait_interval": 0
  480. }
  481. },
  482. "colab_type": "code",
  483. "id": "LQSEnEsPWKMj"
  484. },
  485. "outputs": [],
  486. "source": []
  487. },
  488. {
  489. "cell_type": "code",
  490. "execution_count": null,
  491. "metadata": {},
  492. "outputs": [],
  493. "source": []
  494. },
  495. {
  496. "cell_type": "code",
  497. "execution_count": null,
  498. "metadata": {},
  499. "outputs": [],
  500. "source": []
  501. },
  502. {
  503. "cell_type": "code",
  504. "execution_count": null,
  505. "metadata": {},
  506. "outputs": [],
  507. "source": []
  508. },
  509. {
  510. "cell_type": "code",
  511. "execution_count": null,
  512. "metadata": {},
  513. "outputs": [],
  514. "source": []
  515. },
  516. {
  517. "cell_type": "code",
  518. "execution_count": null,
  519. "metadata": {},
  520. "outputs": [],
  521. "source": []
  522. },
  523. {
  524. "cell_type": "code",
  525. "execution_count": null,
  526. "metadata": {},
  527. "outputs": [],
  528. "source": []
  529. },
  530. {
  531. "cell_type": "code",
  532. "execution_count": null,
  533. "metadata": {},
  534. "outputs": [],
  535. "source": []
  536. }
  537. ],
  538. "metadata": {
  539. "colab": {
  540. "default_view": {},
  541. "name": "object_detection_tutorial.ipynb?workspaceId=ronnyvotel:python_inference::citc",
  542. "provenance": [],
  543. "version": "0.3.2",
  544. "views": {}
  545. },
  546. "kernelspec": {
  547. "display_name": "Python 3",
  548. "language": "python",
  549. "name": "python3"
  550. },
  551. "language_info": {
  552. "codemirror_mode": {
  553. "name": "ipython",
  554. "version": 3
  555. },
  556. "file_extension": ".py",
  557. "mimetype": "text/x-python",
  558. "name": "python",
  559. "nbconvert_exporter": "python",
  560. "pygments_lexer": "ipython3",
  561. "version": "3.7.3rc1"
  562. }
  563. },
  564. "nbformat": 4,
  565. "nbformat_minor": 1
  566. }