{"id":2927,"date":"2020-04-13T13:24:04","date_gmt":"2020-04-13T13:24:04","guid":{"rendered":"https:\/\/cloudxlab.com\/blog\/?p=2927"},"modified":"2020-07-13T06:13:37","modified_gmt":"2020-07-13T06:13:37","slug":"how-to-run-yolo-on-cctv-feed","status":"publish","type":"post","link":"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/","title":{"rendered":"How to run object detection on CCTV feed"},"content":{"rendered":"\n<p>In this blog we explore how to run a very popular computer vision algorithm YOLO on a CCTV live feed. YOLO (You Only Look Once) is a very popular object detection, remarkably fast and efficient. There is a lot of documentation on running YOLO on video from files, USB or raspberry pi cameras. This series of blogs, describes in details how to setup a generic CCTV camera and run YOLO object detection on the live feed.  In case you are interested in finding more about YOLO, I have listed out a few articles for your perusal at the end of this blog.<\/p>\n\n\n\n<h4>Setup a CCTV with RTSP<\/h4>\n\n\n\n<p>This<a href=\"https:\/\/www.aloka.ai\/setup-xmeye-cctv-camera-rtsp\/\"> blog<\/a> lists out in details methods to setup a generic CCTV camera with a live RTSP feed. Note the RTSP url, as we will need it in the later stages. The RTSP (<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img src=\"http:\/\/www.aloka.ai\/wp-content\/uploads\/2020\/01\/IMG_20200117_104111.jpg\" alt=\"CCTV camera with RTSP\" width=\"620\" height=\"464\" \/><\/figure>\n\n\n\n<!--more-->\n\n\n\n<h4>Install Python and OPENCV<\/h4>\n\n\n\n<p>We will use Python 3.6 and openCV 4 in this walkthrough. Ensure you have a computer that has both and the appropriate versions. In case you have never installed OPENCV, please refer to this <a href=\"https:\/\/www.pyimagesearch.com\/opencv-tutorials-resources-guides\/\">guide<\/a>. It documents the installation of OPENCV on several different operating systems<\/p>\n\n\n\n<h4>Install virtualenv for managing python libraries<\/h4>\n\n\n\n<p>I strongly recommend you to use virtualenv to manage your python development workflows, especially if you work on multiple python projects simultaneously. For more details on this package refer to <a href=\"https:\/\/virtualenvwrapper.readthedocs.io\/en\/latest\/\">documentation<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-python line-numbers\">pip3 install virtualenvwrapper\nmkvirtualenv env1\n<\/code><\/pre>\n\n\n\n<h4>Install necessary python libraries with pip<\/h4>\n\n\n\n<p>We will need the following libraries to run YOLO on a live CCTV feed. The required libraries can be installed using the command below.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-python line-numbers\">pip3 install numpy imutils time cv2 os <\/code><\/pre>\n\n\n\n<h4>Download the YOLOv3 weights and config files <\/h4>\n\n\n\n<p>The weights, config and names files to run Yolo v3 can be downloaded from the <a href=\"https:\/\/pjreddie.com\/darknet\/yolo\/\">Darknet website<\/a>. Make a  directory called yolo-coco and keep the files there. <\/p>\n\n\n\n<h4>Python code<\/h4>\n\n\n\n<p>Open a file called python-yolo-cctv.py and copy the following code there. Replace the string &lt;RTSP_URL&gt; with the RTSP url for your camera. <\/p>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code class=\"language-python line-numbers\"># import the necessary packages\nimport numpy as np\nimport argparse\nimport imutils\nimport time\nimport cv2\nimport os\nfrom imutils.video import FPS\nfrom imutils.video import VideoStream\n\n\nRTSP_URL=&lt;RTSP URL&gt;\nYOLO_PATH=\"yolo-coco\"\nOUTPUT_FILE=\"output\/outfile.avi\"\n# load the COCO class labels our YOLO model was trained on\nlabelsPath = os.path.sep.join([YOLO_PATH, \"coco.names\"])\nLABELS = open(labelsPath).read().strip().split(\"\\n\")\nCONFIDENCE=0.5\nTHRESHOLD=0.3\n\n# initialize a list of colors to represent each possible class label\nnp.random.seed(42)\nCOLORS = np.random.randint(0, 255, size=(len(LABELS), 3),\n\tdtype=\"uint8\")\n\n# derive the paths to the YOLO weights and model configuration\nweightsPath = os.path.sep.join([YOLO_PATH, \"yolov3.weights\"])\nconfigPath = os.path.sep.join([YOLO_PATH, \"yolov3.cfg\"])\n\n# load our YOLO object detector trained on COCO dataset (80 classes)\n# and determine only the *output* layer names that we need from YOLO\nprint(\"[INFO] loading YOLO from disk...\")\nnet = cv2.dnn.readNetFromDarknet(configPath, weightsPath)\nln = net.getLayerNames()\nln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]\n\n# initialize the video stream, pointer to output video file, and\n# frame dimensions\nvs = cv2.VideoCapture(RTSP_URL)\ntime.sleep(2.0)\nfps = FPS().start()\nwriter = None\n(W, H) = (None, None)\n\ncnt=0\n\n\n\n# loop over frames from the video file stream\nwhile True:\n\tcnt+=1\n\t# read the next frame from the file\n\t(grabbed, frame) = vs.read()\n\n\t# if the frame was not grabbed, then we have reached the end\n\t# of the stream\n\tif not grabbed:\n\t\tbreak\n\t# if the frame dimensions are empty, grab them\n\tif W is None or H is None:\n\t\t(H, W) = frame.shape[:2]\n\n\t# construct a blob from the input frame and then perform a forward\n\t# pass of the YOLO object detector, giving us our bounding boxes\n\t# and associated probabilities\n\tblob = cv2.dnn.blobFromImage(frame, 1 \/ 255.0, (416, 416),\n\t\tswapRB=True, crop=False)\n\tnet.setInput(blob)\n\tstart = time.time()\n\tlayerOutputs = net.forward(ln)\n\tend = time.time()\n\n\t# initialize our lists of detected bounding boxes, confidences,\n\t# and class IDs, respectively\n\tboxes = []\n\tconfidences = []\n\tclassIDs = []\n\n\t# loop over each of the layer outputs\n\tfor output in layerOutputs:\n\t\t# loop over each of the detections\n\t\tfor detection in output:\n\t\t\t# extract the class ID and confidence (i.e., probability)\n\t\t\t# of the current object detection\n\t\t\tscores = detection[5:]\n\t\t\tclassID = np.argmax(scores)\n\t\t\tconfidence = scores[classID]\n\n\t\t\t# filter out weak predictions by ensuring the detected\n\t\t\t# probability is greater than the minimum probability\n\t\t\tif confidence &gt; CONFIDENCE:\n\t\t\t\t# scale the bounding box coordinates back relative to\n\t\t\t\t# the size of the image, keeping in mind that YOLO\n\t\t\t\t# actually returns the center (x, y)-coordinates of\n\t\t\t\t# the bounding box followed by the boxes' width and\n\t\t\t\t# height\n\t\t\t\tbox = detection[0:4] * np.array([W, H, W, H])\n\t\t\t\t(centerX, centerY, width, height) = box.astype(\"int\")\n\n\t\t\t\t# use the center (x, y)-coordinates to derive the top\n\t\t\t\t# and and left corner of the bounding box\n\t\t\t\tx = int(centerX - (width \/ 2))\n\t\t\t\ty = int(centerY - (height \/ 2))\n\n\t\t\t\t# update our list of bounding box coordinates,\n\t\t\t\t# confidences, and class IDs\n\t\t\t\tboxes.append([x, y, int(width), int(height)])\n\t\t\t\tconfidences.append(float(confidence))\n\t\t\t\tclassIDs.append(classID)\n\n\t# apply non-maxima suppression to suppress weak, overlapping\n\t# bounding boxes\n\tidxs = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE,\n\t\tTHRESHOLD)\n\n\t# ensure at least one detection exists\n\tif len(idxs) &gt; 0:\n\t\t# loop over the indexes we are keeping\n\t\tfor i in idxs.flatten():\n\t\t\t# extract the bounding box coordinates\n\t\t\t(x, y) = (boxes[i][0], boxes[i][1])\n\t\t\t(w, h) = (boxes[i][2], boxes[i][3])\n\n\t\t\t# draw a bounding box rectangle and label on the frame\n\t\t\tcolor = [int(c) for c in COLORS[classIDs[i]]]\n\t\t\tcv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)\n\t\t\ttext = \"{}: {:.4f}\".format(LABELS[classIDs[i]],\n\t\t\t\tconfidences[i])\n\t\t\tcv2.putText(frame, text, (x, y - 5),\n\t\t\t\tcv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)\n\n\t# check if the video writer is None\n\tif writer is None:\n\t\t# initialize our video writer\n\t\tfourcc = cv2.VideoWriter_fourcc(*\"MJPG\")\n\t\twriter = cv2.VideoWriter(OUTPUT_FILE, fourcc, 30,\n\t\t\t(frame.shape[1], frame.shape[0]), True)\n\n\t\t\n\n\t# write the output frame to disk\n\twriter.write(frame)\n\t# show the output frame\n\tcv2.imshow(\"Frame\", cv2.resize(frame, (800, 600)))\n\tkey = cv2.waitKey(1) &amp; 0xFF\n\t#print (\"key\", key)\n\t# if the `q` key was pressed, break from the loop\n\tif key == ord(\"q\"):\n\t\tbreak\n\n\t# update the FPS counter\n\tfps.update()\n\n# stop the timer and display FPS information\nfps.stop()\n\n\nprint(\"[INFO] elasped time: {:.2f}\".format(fps.elapsed()))\nprint(\"[INFO] approx. FPS: {:.2f}\".format(fps.fps()))\n\n# do a bit of cleanup\ncv2.destroyAllWindows()\n# release the file pointers\nprint(\"[INFO] cleaning up...\")\nwriter.release()\nvs.release()\n<\/code><\/pre>\n\n\n\n<p>The program is now ready to run. The live feed from the camera is fed via RTSP. Each frame is run through the YOLO object detector and identified items are highlighted as can be seen below. The program can be stopped by pressing the key &#8216;q&#8217; at any time. <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img src=\"https:\/\/www.aloka.ai\/wp-content\/uploads\/2020\/02\/Screenshot-2020-02-23-at-9.33.17-PM-1024x734.png\" alt=\"How to run YOLO on a CCTV live feed\" class=\"wp-image-336\" width=\"619\" height=\"444\" \/><\/figure>\n\n\n\n<h4>Final Notes<\/h4>\n\n\n\n<p>I ran this program on my non-GPU MacAir laptop, with an FPS of 1. Using a GPU or an accelerator the FPS can be increased significantly to achieve a real time full FPS object detection. Alternatively, you can choose run every 10th or 20th frame in case you don&#8217;t have a GPU acceleration. <\/p>\n\n\n\n<h4>REFERENCES<\/h4>\n\n\n\n<ol><li><a href=\"https:\/\/pjreddie.com\/darknet\/yolo\/\">Darknet<\/a><\/li><li><a href=\"https:\/\/www.pyimagesearch.com\/2018\/11\/12\/yolo-object-detection-with-opencv\/\">Yolo object detection with OPENCV<\/a><\/li><\/ol>\n\n\n\n<p>We also have an offer for you!<\/p>\n\n\n\n<p>&nbsp;<strong>Flat 75% Off + Additional 25% Off + 30-days Extra Lab<\/strong>&nbsp;<\/p>\n\n\n\n<p>Please use the coupon code&nbsp;<strong>LD25<\/strong>&nbsp;during checkout to avail the above offer. Please note that this is a limited time offer and may expire any time soon.<\/p>\n\n\n\n<p>This offer is available for ALL courses (including EICT, IIT Roorkee Certification Courses) available on CloudxLab.com.<\/p>\n\n\n\n<p>Enroll in the most sought-after courses such as AI\/ML, Data Science, DevOps, Big Data, Deep Learning, Python and more!<\/p>\n\n\n\n<p><a href=\"http:\/\/e.gtolink.in\/t\/em1\/9662\/1\/634ef67a-714d-4d32-b45f-58124767ca76\/fb29ad8f6f3c11eaa14b0268638e5f02\"><strong>Enroll Now \u00bb<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we explore how to run a very popular computer vision algorithm YOLO on a CCTV live feed.<\/p>\n","protected":false},"author":26,"featured_media":3074,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[67],"tags":[38,61],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to run object detection on CCTV feed | CloudxLab Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to run object detection on CCTV feed | CloudxLab Blog\" \/>\n<meta property=\"og:description\" content=\"In this blog we explore how to run a very popular computer vision algorithm YOLO on a CCTV live feed.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudxLab Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cloudxlab\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-13T13:24:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-13T06:13:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2020\/04\/camera.png\" \/>\n\t<meta property=\"og:image:width\" content=\"621\" \/>\n\t<meta property=\"og:image:height\" content=\"467\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:site\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"6 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"CloudxLab Blog\",\"description\":\"Learn AI, Machine Learning, Deep Learning, Devops &amp; Big Data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/cloudxlab.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2020\/04\/camera.png\",\"contentUrl\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2020\/04\/camera.png\",\"width\":621,\"height\":467},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/#webpage\",\"url\":\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/\",\"name\":\"How to run object detection on CCTV feed | CloudxLab Blog\",\"isPartOf\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/#primaryimage\"},\"datePublished\":\"2020-04-13T13:24:04+00:00\",\"dateModified\":\"2020-07-13T06:13:37+00:00\",\"author\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/e2c5cc7b933ebd4b15f9b463dc7cf1b4\"},\"breadcrumb\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/how-to-run-yolo-on-cctv-feed\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/e2c5cc7b933ebd4b15f9b463dc7cf1b4\",\"name\":\"Praveen Pavithran\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/03c8d253347dcf9e04ec550cd6144973?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/03c8d253347dcf9e04ec550cd6144973?s=96&d=mm&r=g\",\"caption\":\"Praveen Pavithran\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/2927"}],"collection":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/comments?post=2927"}],"version-history":[{"count":16,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/2927\/revisions"}],"predecessor-version":[{"id":3147,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/2927\/revisions\/3147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media\/3074"}],"wp:attachment":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media?parent=2927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/categories?post=2927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/tags?post=2927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}