Skip to content

Commit

Permalink
crash detection model
Browse files Browse the repository at this point in the history
  • Loading branch information
aleronarjun authored Aug 19, 2018
1 parent a53b26d commit 0319522
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 0 deletions.
288 changes: 288 additions & 0 deletions detection_model/code_model.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.3185865\n",
"0.3185865\n",
"0.25940105\n",
"0.25940105\n",
"0.21081719\n",
"0.21081719\n",
"0.0077585094\n",
"Request sent\n"
]
}
],
"source": [
"import numpy as np\n",
"import os\n",
"import six.moves.urllib as urllib\n",
"import sys\n",
"import tarfile\n",
"import tensorflow as tf\n",
"import zipfile\n",
"import requests, json, time\n",
"from collections import namedtuple\n",
"\n",
"from collections import defaultdict\n",
"from io import StringIO\n",
"from matplotlib import pyplot as plt\n",
"from PIL import Image\n",
"\n",
"import cv2\n",
"cap = cv2.VideoCapture(\"v13.mp4\")\n",
"#v71.mp4,v13.mp4\n",
"# This is needed since the notebook is stored in the object_detection folder.\n",
"sys.path.append(\"..\")\n",
"\n",
"\n",
"# ## Object detection imports\n",
"# Here are the imports from the object detection module.\n",
"\n",
"# In[3]:\n",
"\n",
"from utils import label_map_util\n",
"\n",
"from utils import visualization_utils as vis_util\n",
"\n",
"\n",
"# # Model preparation \n",
"\n",
"# ## Variables\n",
"# \n",
"\n",
"# Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file. \n",
"# \n",
"# By default we use an \"SSD with Mobilenet\" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/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.\n",
"\n",
"# In[4]:\n",
"\n",
"# What model to download.\n",
"MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'\n",
"MODEL_FILE = MODEL_NAME + '.tar.gz'\n",
"DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'\n",
"\n",
"# Path to frozen detection graph. This is the actual model that is used for the object detection.\n",
"PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'\n",
"\n",
"# List of the strings that is used to add correct label for each box.\n",
"PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')\n",
"\n",
"NUM_CLASSES = 90\n",
"\n",
"\n",
"# ## Download Model\n",
"\n",
"# In[5]:\n",
"\n",
"# opener = urllib.request.URLopener()\n",
"# opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)\n",
"tar_file = tarfile.open(MODEL_FILE)\n",
"for file in tar_file.getmembers():\n",
" file_name = os.path.basename(file.name)\n",
" if 'frozen_inference_graph.pb' in file_name:\n",
" tar_file.extract(file, os.getcwd())\n",
"\n",
"\n",
"# ## Load a (frozen) Tensorflow model into memory.\n",
"\n",
"# In[6]:\n",
"\n",
"detection_graph = tf.Graph()\n",
"with detection_graph.as_default():\n",
" od_graph_def = tf.GraphDef()\n",
" with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:\n",
" serialized_graph = fid.read()\n",
" od_graph_def.ParseFromString(serialized_graph)\n",
" tf.import_graph_def(od_graph_def, name='')\n",
"\n",
"\n",
"# ## Loading label map\n",
"# 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\n",
"\n",
"# In[7]:\n",
"\n",
"label_map = label_map_util.load_labelmap(PATH_TO_LABELS)\n",
"categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)\n",
"category_index = label_map_util.create_category_index(categories)\n",
"\n",
"\n",
"# ## Helper code\n",
"\n",
"# In[8]:\n",
"def cal_collision(boxes,classes,scores):\n",
" for i, b in enumerate(boxes[0]):\n",
" if classes[0][i] == 3 or classes[0][i] == 6 or classes[0][i] == 8:\n",
" if scores[0][i] > 0.5:\n",
" for j, c in enumerate(boxes[0]):\n",
" if (i != j) and (classes[0][j] == 3 or classes[0][j] == 6 or classes[0][j] == 8) and scores[0][j]> 0.5:\n",
" Rectangle = namedtuple('Rectangle', 'xmin ymin xmax ymax')\n",
" ra = Rectangle(boxes[0][i][3], boxes[0][i][2], boxes[0][i][1], boxes[0][i][3])\n",
" rb = Rectangle(boxes[0][j][3], boxes[0][j][2], boxes[0][j][1], boxes[0][j][3])\n",
" ar = rectArea(boxes[0][i][3], boxes[0][i][1],boxes[0][i][2],boxes[0][i][3])\n",
" col_threshold = 0.6*np.sqrt(ar)\n",
" print(area(ra, rb))\n",
"# area(ra, rb)\n",
" if (area(ra,rb)<col_threshold) :\n",
" postData = {\n",
" \"cameraId\": 42,\n",
" \"time\": int(time.time())\n",
" }\n",
" r = requests.post('https://infinite-ravine-29568.herokuapp.com/accident', json.dumps(postData))\n",
" if r.status_code == 200:\n",
" print (\"Request sent\")\n",
" return True\n",
" else:\n",
" return False\n",
" \n",
" # cv2.putText(image_np, \"COLLISION!\", (230, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2, cv2.LINE_AA)\n",
" # intersection here is (3, 3, 4, 3.5), or an area of 1*.5=.5\n",
"# mid_x = (boxes[0][i][3] + boxes[0][i][1]) / 2\n",
"# mid_y = (boxes[0][i][2] + boxes[0][i][3]) / 2\n",
"# cv2.putText(image_np, \"rasik\", (int(mid_x*800, int(mid_y*600)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)\n",
"\n",
" \n",
" \n",
" \n",
" \n",
"\n",
"def load_image_into_numpy_array(image):\n",
" (im_width, im_height) = image.size\n",
" return np.array(image.getdata()).reshape(\n",
" (im_height, im_width, 3)).astype(np.uint8)\n",
"\n",
"def area(a, b): # returns None if rectangles don't intersect\n",
" dx = min(a.xmax, b.xmax) - max(a.xmin, b.xmin)\n",
" dy = min(a.ymax, b.ymax) - max(a.ymin, b.ymin)\n",
"# print (dx, dy)\n",
"# if (dx>=0) and (dy>=0):\n",
" return dx*dy\n",
"\n",
"def rectArea(xmax, ymax, xmin, ymin):\n",
" x = np.abs(xmax-xmin)\n",
" y = np.abs(ymax-ymin)\n",
" \n",
" return x*y\n",
"# # Detection\n",
"\n",
"# In[9]:\n",
"\n",
"# For the sake of simplicity we will use only 2 images:\n",
"# image1.jpg\n",
"# image2.jpg\n",
"# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.\n",
"PATH_TO_TEST_IMAGES_DIR = 'test_images'\n",
"TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]\n",
"\n",
"# Size, in inches, of the output images.\n",
"IMAGE_SIZE = (12, 8)\n",
"\n",
"\n",
"# In[10]:\n",
"\n",
"with detection_graph.as_default():\n",
" with tf.Session(graph=detection_graph) as sess:\n",
" while True:\n",
" ret, image_np = cap.read()\n",
" # Expand dimensions since the model expects images to have shape: [1, None, None, 3]\n",
" image_np_expanded = np.expand_dims(image_np, axis=0)\n",
" image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')\n",
" # Each box represents a part of the image where a particular object was detected.\n",
" boxes = detection_graph.get_tensor_by_name('detection_boxes:0')\n",
" # Each score represent how level of confidence for each of the objects.\n",
" # Score is shown on the result image, together with the class label.\n",
" scores = detection_graph.get_tensor_by_name('detection_scores:0')\n",
" classes = detection_graph.get_tensor_by_name('detection_classes:0')\n",
" num_detections = detection_graph.get_tensor_by_name('num_detections:0')\n",
" # Actual detection.\n",
" (boxes, scores, classes, num_detections) = sess.run(\n",
" [boxes, scores, classes, num_detections],\n",
" feed_dict={image_tensor: image_np_expanded})\n",
" # Visualization of the results of a detection.\n",
" vis_util.visualize_boxes_and_labels_on_image_array(\n",
" image_np,\n",
" np.squeeze(boxes),\n",
" np.squeeze(classes).astype(np.int32),\n",
" np.squeeze(scores),\n",
" category_index,\n",
" use_normalized_coordinates=True,\n",
" line_thickness=8)\n",
" \n",
" if cal_collision(boxes, classes, scores):\n",
" cv2.destroyAllWindows()\n",
" break\n",
"\n",
" cv2.imshow('object detection', image_np)\n",
" \n",
" if cv2.waitKey(25) & 0xFF == ord('q'):\n",
" cv2.destroyAllWindows()\n",
" break\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added detection_model/new.avi
Binary file not shown.
Binary file added detection_model/v13.mp4
Binary file not shown.
Binary file added detection_model/v71.mp4
Binary file not shown.

0 comments on commit 0319522

Please sign in to comment.