{"id":844,"date":"2023-06-15T09:39:28","date_gmt":"2023-06-15T09:39:28","guid":{"rendered":"https:\/\/alternative-spaces.com\/blog\/?p=844"},"modified":"2023-06-15T09:42:04","modified_gmt":"2023-06-15T09:42:04","slug":"object-detection-in-chatbots-and-beyond","status":"publish","type":"post","link":"https:\/\/alternative-spaces.com\/blog\/object-detection-in-chatbots-and-beyond\/","title":{"rendered":"Object Detection in Chatbots and Beyond"},"content":{"rendered":"\n<p>This blog has been dealing with the chat bot topic frequently. We have written about how to build a chatbot using a&nbsp;<a href=\"https:\/\/alternative-spaces.com\/blog\/how-to-build-a-chatbot-using-a-bot-development-framework\">bot development framework<\/a>, how to&nbsp;<a href=\"https:\/\/alternative-spaces.com\/blog\/how-to-design-a-chatbot-creating-a-conversational-interface\">create a chatbot user interface<\/a>, paying special attention to the&nbsp;<a href=\"https:\/\/alternative-spaces.com\/blog\/conversational-interface-design-using-the-right-language\">conversation flow and language<\/a>, and even about&nbsp;handing over chatbots&nbsp;built on a specific chatbot platform.<\/p>\n\n\n\n<p>An actual case of building a chatbot was still to be written. In this post, we\u2019ll show how alternative-spaces team made one for Telegram and Facebook messenger. Since the demo project included work with an image object detection API, we\u2019ll dedicate the first chapter to image processing\/object detection. The second will tell about Dogbi, alternative-spaces\u2019 object detection app. If you\u2019re interested in building a chatbot in Python using object recognition, we hope it will be useful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Object Detection Technology<\/strong><\/h2>\n\n\n\n<p>The technology is related to image processing and computer vision and is used in face detection and recognition, video object co-segmentation, and similar tasks. It deals with detecting instances of objects of a certain class in digital images or videos. All items in a class have particular features and an input image can be compared with a specific object model. For example, shape-based object detection uses the items\u2019 similar shapes to classify them.<\/p>\n\n\n\n<p>Object detection methods are based on either&nbsp;<a href=\"https:\/\/alternative-spaces.com\/blog\/what-do-you-need-to-know-about-the-limits-of-machine-learning\">machine learning<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/alternative-spaces.com\/blog\/?p=1152&amp;preview=true&amp;_thumbnail_id=1153\">deep learning<\/a>. The ML-based approach defines the class\u2019 features and eventually uses a support vector machine or other techniques for the classification. Deep learning techniques can detect an object in an image without specifically defining the features. They are typically based on convolutional&nbsp;<a href=\"https:\/\/alternative-spaces.com\/blog\/accelerating-business-growth-with-artificial-neural-networks\">neural networks<\/a>.<\/p>\n\n\n\n<p>In the project mentioned above, alternative-spaces team utilized TensorFlow Object Detection API. It\u2019s a research library whose object detectors follow the deep learning approach. The API empowers developers to build, train, and deploy object detection models for various uses.<\/p>\n\n\n\n<p>The creation of an object detection application starts with assembling a dataset, a collection of images with labels. ImageNet is a valuable service for machine learning and object recognition projects. Moreover, it provides a&nbsp;<a href=\"http:\/\/image-net.org\/download-bboxes\" target=\"_blank\" rel=\"noreferrer noopener\">solution<\/a>&nbsp;for another need &#8211; the bounding box necessary for specifying the location of the object in each image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"639\" src=\"https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/object-detection-app-1-1024x639.jpg\" alt=\"\" class=\"wp-image-846\" srcset=\"https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/object-detection-app-1-1024x639.jpg 1024w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/object-detection-app-1-150x94.jpg 150w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/object-detection-app-1-300x187.jpg 300w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/object-detection-app-1-768x479.jpg 768w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/object-detection-app-1.jpg 1700w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Alternatively, use the open-source&nbsp;<a href=\"https:\/\/github.com\/tzutalin\/labelImg\" target=\"_blank\" rel=\"noreferrer noopener\">LabelImg<\/a>&nbsp;or other tools. Whatever you choose, it must provide you with a folder with .jpg (data)images and .xml (label)file. The latter are eventually converted to .csv format.<\/p>\n\n\n\n<p>If you wish to avoid training an object detection model from scratch for weeks, with a high-end graphics processing unit, use one of&nbsp;<a href=\"https:\/\/github.com\/stormy-ua\/dog-breeds-classification\/blob\/master\/Inference.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">available<\/a><a href=\"https:\/\/github.com\/stormy-ua\/dog-breeds-classification\/blob\/master\/Inference.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">pre-trained models<\/a>. Choose one, download it, and retrain with your custom dataset, replacing that model\u2019s classes with yours. When you stop TensorFlow training, you can export the latest checkpoint file to a graph file and perform live inference with it.<\/p>\n\n\n\n<p>The resulting model can be used in many ways, e.g., for building a real-time&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/medium.com\/@junjiwatanabe\/how-to-build-real-time-object-recognition-ios-app-ca85c193865a\" target=\"_blank\">iOS object recognition application<\/a>&nbsp;or, converted to TensorFlow Lite format, for building an app for Android. Alternative-spaces team utilized it for a chatbot whose description follows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Dogbi: <\/strong>alternative-spaces<strong>&#8216; Dog Breed Identification Chatbot<\/strong><\/h2>\n\n\n\n<p>The goal was to create a chatbot app which can identify dog breeds using a photo. The user&nbsp;sends a photo&nbsp;of a dog, and Dogbi analyzes the image and responds with an estimate of similarity. The input image is not necessarily a picture of a dog only, and the system\u2019s task is twofold:<\/p>\n\n\n\n<p>1) detect a dog in a picture;<\/p>\n\n\n\n<p>2) guess the breed of the dog using the available knowledge.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"603\" src=\"https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/how-to-build-a-chatbot-1024x603.jpg\" alt=\"\" class=\"wp-image-847\" srcset=\"https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/how-to-build-a-chatbot-1024x603.jpg 1024w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/how-to-build-a-chatbot-150x88.jpg 150w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/how-to-build-a-chatbot-300x177.jpg 300w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/how-to-build-a-chatbot-768x452.jpg 768w, https:\/\/alternative-spaces.com\/blog\/wp-content\/uploads\/2019\/04\/how-to-build-a-chatbot.jpg 1700w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Dogbi is using a pre-trained model for dog breeds recognition. The team took advantage of the&nbsp;<a href=\"https:\/\/github.com\/tensorflow\/models\/blob\/master\/tutorials\/image\/imagenet\/classify_image.py#L51\" target=\"_blank\" rel=\"noreferrer noopener\">TensorFlow model<\/a>&nbsp;and ready-made instructions. (<a href=\"https:\/\/github.com\/stormy-ua\/dog-breeds-classification?fbclid=IwAR02fRT08zQMKcPMGxlwuuYFQbGqs1xBIXz5qtlWJYYZWwnFChNVbrHpivI\" target=\"_blank\" rel=\"noreferrer noopener\">Here<\/a>&nbsp;you can find the scripts and data for reproducing the breed classification model training, analysis, and inference.) The bot API is written in Python.<\/p>\n\n\n\n<p>You basically have to download the Inception model (a deep neural network pre-trained by Google) and the&nbsp;<a href=\"http:\/\/vision.stanford.edu\/aditya86\/ImageNetDogs\/\" target=\"_blank\" rel=\"noreferrer noopener\">Stanford Dogs dataset<\/a>. The latter is using 20K images of 120 dog breeds with class labels and bounding boxes from ImageNet to facilitate a fine-grained image classification.<\/p>\n\n\n\n<p>NB: Assuming that the accuracy of a trained object detection model is directly proportional to the number of images, the team initially used 2-5K images for each dog breed. The dataset was downloaded with a script crawling large sets on ImageNet. It was validating, converting, and changing the file sizes for some ten hours. Surprisingly, a model handling a smaller dataset (100-200 images per breed) turned out to work way better. The result may depend on the dataset quality and the weights&nbsp;in the model.<\/p>\n\n\n\n<p>On top of the Inception model, you have to build a dog breed classification neural network model, train it, and then freeze. The duration depends on the depth of your model and number of epochs. A CSV file with predicted vs. actual breed should be used to analyze precision on the training data. The frozen model is used to classify an image either available on the filesystem or downloadable as an HTTP resource.<\/p>\n\n\n\n<p>The frozen model is ready to be used for image classification tasks. The project can be dockerized with premade docker-compose and .dockerfile. There\u2019s a Docker file in our&nbsp;<a href=\"https:\/\/github.com\/Onix-Systems\/dogbi\/blob\/master\/Dockerfile\" target=\"_blank\" rel=\"noreferrer noopener\">repository<\/a>&nbsp;to build the Docker image and run the application.<\/p>\n\n\n\n<p>Here\u2019s how you can build a chatbot for Telegram on your own:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Fill out&nbsp;<em>const_empty.py<\/em>&nbsp;in telegram_bot directory and rename it to&nbsp;<em>const.py<\/em><\/li><li>Generate a legit secret key for&nbsp;<em>settings_without_key.py<\/em>&nbsp;(add&nbsp;<em>SECRET_KEY = &#8216;YOUR KEY&#8217;<\/em>) and rename it to&nbsp;<em>settings.py<\/em><\/li><li>Copy the static files into AI directory: TensorFlow graph and breeds.csv should be placed in&nbsp;<em>root\/AI<\/em>&nbsp;directory, and static header images (one per breed, a picture which displays it best) &#8211; in&nbsp;<em>root\/AI\/static\/media\/ directory<\/em><\/li><li>Build a docker image using&nbsp;<em>docker-compose<\/em>&nbsp;build and launch it with&nbsp;<em>docker-compose up<\/em><\/li><li>Forward the&nbsp;<em>8005&nbsp;<\/em>and&nbsp;<em>8006&nbsp;<\/em>ports to a default&nbsp;<em>80&nbsp;<\/em>port of your server<\/li><\/ol>\n\n\n\n<p>That&#8217;s it! Dogbi is running in a Docker container on your server!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>For Last<\/strong><\/h2>\n\n\n\n<p>The function and the model used in Dogbi are simple, but don\u2019t think the technology can be used only to analyze photos or create chatbots. Object detection using Python can&nbsp;<a href=\"https:\/\/blog.goodaudience.com\/food-detection-app-using-tensorflow-object-detection-apis-1b9302a9aad2\" target=\"_blank\" rel=\"noreferrer noopener\">recognize foods<\/a>&nbsp;or work in a&nbsp;<a href=\"https:\/\/chatbotslife.com\/real-time-image-recognition-and-speech-5545f267f7b3\" target=\"_blank\" rel=\"noreferrer noopener\">system that tells what it sees<\/a>&nbsp;in real time, to name a few.<\/p>\n\n\n\n<p>Object detection has been used for vehicle detection, security systems, and self-driving cars. The use cases should be extended to image segmentation and distance estimation soon. Real-time object recognition on the camera screen (e.g., to help visually impaired individuals) and more challenging applications and goals lay ahead.<\/p>\n\n\n\n<p>Content created by our partner, Onix-systems. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog has been dealing with the chat bot topic frequently. We have written about how to build a chatbot using a&nbsp;bot development framework, how to&nbsp;create a chatbot user interface, paying special attention to the&nbsp;conversation flow and language, and even about&nbsp;handing over chatbots&nbsp;built on a specific chatbot platform. An actual case of building a chatbot [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":845,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-844","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/posts\/844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/comments?post=844"}],"version-history":[{"count":8,"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":2531,"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/posts\/844\/revisions\/2531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/media\/845"}],"wp:attachment":[{"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/media?parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/categories?post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alternative-spaces.com\/blog\/wp-json\/wp\/v2\/tags?post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}