Skip to main content

How to use opencv-python with Darknet's YOLOv4?

Another post starts with you beautiful people 😊
Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one-
  1. Setup Darknet's YOLOv4
  2. Train custom dataset with YOLOv4
  3. Create production-ready API of YOLOv4 model
  4. Create a web app for your YOLOv4 model
Since now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome computer vision and machine learning software library- OpenCV and of course with Python 🐍. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command-
pip install opencv-python --upgrade

For Google Colab and Kaggle, use the following command-

!pip install opencv-python --upgrade

For example in my Colab notebook, the above command with output is like below-

As you can see in the last line of the output, the version of opencv-python is 4.4.0.40 which is, in fact, the latest one so our installation is done successfully. Now I will show you how can we test a pre-trained YOLOv4 model trained on coco dataset with Python and OpenCV-

Let's understand the above code snippet💭-

First I have imported the OpenCV library as cv and cv2_imshow library specific to Colab for opening image in Colab notebook.

In the next line, I have created an OpenCV model from the YOLOv4 configuration and pre-trained weights file. If you don't have these files then you can download those from this link. Here OpenCV's dnn_DetectionModel() function is all that you need for this purpose 💓

After creating the model, I did some input image preprocessing for this model like resizing the image to a specific resolution, normalizing, and maintaining the RGB format.

In the next line, I have uploaded a new unseen image where I want to detect some objects.

Next, I am iterating the names of the objects. Since our pre-trained model is trained on 80 classes known as the coco dataset, we need to take the names one by one from the file.

In the next line, I am using detect() function to detect the objects on the input image. This function is equivalent to the Darknet's detection function. It gives the object name, calculates the confidence score, and gives the bounding boxes.

Next, inside the for loop, I have written logic to put object name, confidence score, and draw a bounding box around the detected object using OpenCV inbuilt functions.

In the end, to show the image with the detection I am using the cv2_imshow() function. If you are using your own system then instead of this function use the OpenCV's imshow() function.

Now we are ready to test our image. Once I run the above code, I get the following image with the detection-

See the result, it's amazing right 💥. The pre-trained YOLOv4 model with OpenCV and Python is successfully able to detect person, wine glass, dining table, bowl, bottle with a high confidence score. In the same way, you can test your custom trained YOLOv4 model. You just need to replace YOLOv4 specific configuration file, weights, and class name file with your own, and the remaining code will be the same as it is. Let me show you the example where I have used my custom YOLOv4 model which I trained for a Global Wheat Head Detection Challenge. Since at the time of the participation OpenCV support was not released for Darknet's YOLOv4 so I had to convert my YOLOv4 model to keras/TensorFlow model and made changes accordingly. But now I can use it very easily 👲-


Wow, that was so easy 💪. For this competition, I had to put so much effort to make Darknet work in the Kaggle platform. But now with opencv-python, it is very easy for me. If you are interested to see my complete solution to the challenge you can check this link ☝.

That's it guys for today! From this post, you have learned to use your Darknet's YOLOv4 model with OpenCV and Python. Now there is no solid reason to not use this state of the art object detection and recognition neural network model in your project. The links I have shared at the starting of this blog and my solution link of a real-world object detection competition are quite enough to master in this field. So don't stop yourself 💥, try the learning from this post on your own, explore the code with experiments and in my next blog post, I will again share with you something new and exciting. Till then Go chase your dreams, have an awesome day, make every second count, and see you later in my next post😇

Comments

  1. This concept is a good way to enhance the knowledge.thanks for sharing..

    Data Science

    Python

    Selenium

    ETL Testing

    AWS

    ReplyDelete
  2. Master Data Science with R & Python. Also, Learn Machine Learning & Big data. Further More Details Here-9310332343 or Visit Website-http://www.pythontrainingdelhi.com/

    ReplyDelete
  3. Here at this site really the fastidious material collection so that everybody can enjoy a lot. ExcelR Data Science Courses

    ReplyDelete
  4. Python is one of the most popular, general purpose, interpreted and high-level programming language. SOL Technologies Solutions delivers best python training institute in Delhi.

    Python training institute in delhi
    Python training Course in delhi

    ReplyDelete
  5. Learn Python Programming Course Basic to Advanced Level - Life is Short - you Need Python Course. Further More Details Here-+91-9310332343.

    Python training course in Delhi
    Python training institute in Delhi

    ReplyDelete
  6. Nice post. I was checking this blog and I am impressed! Extremely helpful information specially the last part I care for such info a lot.

    python training course in delhi
    python training Institute in delhi



    ReplyDelete
  7. Information was good,i like your post.Looking forward for more on this topic.
    Python training in bangalore

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. wow! amazing blog I really like it thanks for sharing.
    How to Become a Data Scientist

    ReplyDelete
  10. Dude This really helped a lot
    I will cite your blog in my project
    Thank you

    ReplyDelete
    Replies
    1. Thanks for reading my post. I am glad that somehow it helped you. Keep Learning :)

      Delete
  11. I really appreciate your hard work you put into your blog and detailed information you provide. Further More Information About Python training institute in Delhi Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/python/python-training-institute-in-south-delhi

    ReplyDelete
  12. Nice article. I liked very much. All the information given by you are really helpful for my research. keep on posting your views.
    data analytics course in delhi

    ReplyDelete
  13. Online home tuition in Bangalore is the need of the hour as school learning is not enough, thus students are seeking online home tutors in Bangalore to clear their concepts.
    Call Our Experts :- +91-9654271931
    Visit Us:- home tuition in bangalore

    ReplyDelete
  14. I got an error like this .Can anyone help me please?

    AttributeError Traceback (most recent call last)
    in ()
    12
    13 classes, confidences, boxes = net.detect(frame, confThreshold=0.1, nmsThreshold=0.4)
    ---> 14 for classId, confidence, box in zip(classes.flatten(), confidences.flatten(), boxes):
    15 label = '%.2f' % confidence
    16 label = '%s: %s' % (names[classId],label)

    AttributeError: 'tuple' object has no attribute 'flatten'

    ReplyDelete
    Replies
    1. You can put a check like below just after the line no 13-
      if type(classes) is not tuple:

      Delete
  15. Thanks for sharing this post if anyone looking for Core and Advanced Java training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/java-training-courses

    ReplyDelete
  16. Thank you for your informative post!!!
    Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
    As a best video production company in Bangalore, we produce quality and creative videos to our clients.

    ReplyDelete
  17. Thank you for sharing this post. Data science includes topics like statistics, data analysis, machine learning and other related methods to analyze data. Our experts in Data Science will help you in handling your Big Data and turn it into real business terms.
    Data Science Services in India

    ReplyDelete
  18. Interesting blog every python learners out there
    For best data science course from the best mentors of the industry visit:

    Visit us: Data Science Course in Bhubaneswar

    ReplyDelete
  19. Good information,thanks foe sharing.keep posting.

    For Online MBA check below.

    Innomatics Research Labs is collaborated with JAIN (Deemed-to-be University) and offering the Online MBA in Business Intelligence,Business Analytics Program. This two-year program from JAIN (deemed-to-be) University offers foundation courses, core courses, Specialization courses, and a comprehensive master thesis intermediary, apart from an option to pursue a cross-functional and open elective.
    Online MBA in Business Analytics
    Online MBA in Business Intelligence

    ReplyDelete
  20. This is my first time visit here. From the tons of comments on your articles. I guess I am not only one having all the enjoyment right here.

    Complete Python Programming Training Course in Delhi, India
    Python training institute in delhi
    Python training Course in delhi

    ReplyDelete
  21. Thanks fo sharing on technology topic, keep writing and sharing it with us.

    Data Science Training in Pune

    ReplyDelete
  22. You completely match our expectation and the variety of our information.
    data scientist course

    ReplyDelete
  23. Excellent post! I appreciate your efforts. Keep it up. If you want to promote your business globally then I can help you. Read the given links here.

    Kenya Trade Data

    Kenya Export Data

    Kenya Import Export Data Provider

    Search Kenya Shipment Data

    ReplyDelete
  24. Thanks you and excellent and good to see the best software training courses for freshers and experience candidates to upgade the next level in an Software Industries Technologies,

    Python course in Bangalore
    Python Training in Bangalore
    Python Online Training in Bangalore
    python training institute in Bangalore

    ReplyDelete
  25. After AI Patasala Data Science Course in Hyderabad course is completed, we will share the student's profile with the most prominent MNC firms seeking Data Science talent.
    Data Science Training Hyderabad

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Sign up today for the intensive Python Course in Hyderabad program taught by experienced trainers from AI Patasala.
    Python Training Hyderabad

    ReplyDelete
  28. Join our Online Data Science Course to analyze data, make effective predictions and gain a better understanding of market trends. Create disruptive business models for the topmost industries as we equip you with a sound business analytical & entrepreneurial ideology, alongside an excellent grasp of marketing strategies and trends.



    ReplyDelete
  29. I am excited to watch your upcoming posts too. it encourages me to write and i have written on some hs codes. you can check my writings.

    HS Code 2201|
    HS Code 2202|
    HS Code 2203|
    HS Code 2204|
    HS Code 2205|
    HS Code 2206|
    HS Code 2207|
    HS Code 2208|
    HS Code 2209|
    HS Code 2301

    ReplyDelete
  30. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up.

    HS Code 2912|
    HS Code 2914|
    HS Code 2915|
    HS Code 2916|
    HS Code 2917|
    HS Code 2918|
    HS Code 2920|
    HS Code 2921|
    HS Code 2922|
    HS Code 2923

    ReplyDelete
  31. Wow! Thank you! I constantly wanted to write on my site something like that. Can I take a portion of your post to my website? Best linux cheat sheet commands service provider.

    ReplyDelete
  32. "Nice Blog! such a informative things you are sharing ,I really liked your content. If you wanna know about ""Skillslash | Training with live industry experience that gets you hired""
    go to Data Science Course"

    ReplyDelete
  33. Such a beautiful post you have share. I appriciate your work. I will visit your upcoming posts too. Interested can check the given links here. It can help you to find the hs codes of products.

    HS Code 0806|
    HS Code 0808|
    HS Code 0809|
    HS Code 0810|
    HS Code 0812|
    HS Code 0813|
    HS Code 0901|
    HS Code 0902|
    HS Code 0904|

    ReplyDelete
  34. Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. dark0de market url

    ReplyDelete
  35. Global data gathers all particular data about the products, and those all elegances are more valuable for exchanging. By the export import data trading business increments quickly. Thus, trading information includes all the details about the products that might help in various ways. Get the effective exchange and make your exchanging business undeniable level. Furthermore, get the shipment in universally.

    ReplyDelete
  36. Online Darknet News
    Restor.live is the premier source for all things darknet news, be it important information,marketplace reviews, tor news, and uptime of popular Tor websites.

    ReplyDelete
  37. Great Post with valuable information. I am glad that I have visited this site. Share more updates.

    Machine Learning
    Python

    ReplyDelete
  38. Hi dear,

    Thank you for this wonderful post. It is very informative and useful. I would like to share something here too.In 1997, a department for remote training solutions and services was created. Its activities cover the following e-learning solutions: platforms, content and services, as well as online training and on-site training for different business lines, among which are: technical computing, internet, office automation, languages, quality, environment, occupational health and safety, finance, effective presentations, team management techniques, project management and courses for every type of industry (transportation, industrial, mechanics, tourism, food, etc.).



    DP-090T00: Implementing a Machine Learning Solution with Microsoft Azure Databricks

    ReplyDelete
  39. I like this article, really explained everything in the detail, keep rocking like this. i understood the topic clearly, to learn more join Data Science Course

    ReplyDelete
  40. I really enjoyed reading this article , it's really effective and interesting article python course

    ReplyDelete
  41. Thanks for this post. It proves very informative for me. Great post to read. Visit my website to get best Information About Best IAS coaching Institutes in Thane.
    Best IAS coaching Institutes in Thane
    Top IAS coaching Institutes in Thane

    ReplyDelete
  42. Excellent post. You have shared some wonderful tips. I completely agree with you that it is important for any blogger to help their visitors. Once your visitors find value in your content, they will come back for more What is the Python



    ReplyDelete
  43. Great job for publishing such a beneficial website. premier global consulting hub

    ReplyDelete
  44. finest data science blog I've ever read. I'm grateful that you shared information about data science. 
    data science training in surat

    ReplyDelete
  45. It is the process of using advanced analytics to extract valuable information from data and Data Science is an interdisciplinary field that uses algorithums, procedures, and process to examine large about of data

    ReplyDelete
  46. it’s very helpful useful thanks for your valuable information follow us
    Best Data Science Online Training in Hyderabad

    ReplyDelete
  47. Python is a programming language that enables quicker work and more efficient system integration. Because of its simplicity, ease of use, and accessibility to libraries like NumPy, Pandas, and Matplotlib, it is one of the best languages used by data scientists for a variety of data science applications. You will gain a multidisciplinary skill set and be prepared to work with massive volumes of data to find insights and solutions to business challenges after completing this data science certification course. Having this certification will provide you a technical understanding of computer science and statistics in addition to a postgraduate degree. It covers the spring framework, hibernate, and advanced java.best data science institute in nashik with placement

    ReplyDelete
  48. Great job for publishing such a beneficial post, The excellent Databricks training from TotalCloudAI will improve your data abilities and professional chances with its comprehensive, hands-on learning approach. For more information visit us!

    ReplyDelete
  49. Great job for publishing such a beneficial post, The excellent Databricks training courses from TotalCloudAI will improve your data abilities and professional chances with its comprehensive, hands-on learning approach. For more information visit us!

    ReplyDelete
  50. Thank for good information
    https://www.iihtsrt.com/

    ReplyDelete

Post a Comment

Popular posts from this blog

How to install and compile YOLO v4 with GPU enable settings in Windows 10?

Another post starts with you beautiful people! Last year I had shared a post about  installing and compiling Darknet YOLOv3   in your Windows machine and also how to detect an object using  YOLOv3 with Keras . This year on April' 2020 the fourth generation of YOLO has arrived and since then I was curious to use this as soon as possible. Due to my project (built on YOLOv3 :)) work I could not find a chance to check this latest release. Today I got some relief and successfully able to install and compile YOLOv4 in my machine. In this post I am going to share a single shot way to do the same in your Windows 10 machine. If your machine does not have GPU then you can follow my  previous post  by just replacing YOLOv3 related files with YOLOv4 files. For GPU having Windows machine, follow my steps to avoid any issue while building the Darknet repository. My machine has following configurations: Windows 10 64 bit Intel Core i7 16 GB RAM NVIDIA GeForce GTX 1660 Ti Version 445.87

How can I make a simple ChatBot?

Another post starts with you beautiful people! It has been a long time of posting a new post. But my friends in this period I was not sitting  where I got a chance to work with chatbot and classification related machine learning problem. So in this post I am going to share all about chatbot- from where I have learned? What I have learned? And how can you build your first bot? Quite interesting right! Chatbot is a program that can conduct an intelligent conversation based on user's input. Since chatbot is a new thing to me also, I first searched- is there any Python library available to start with this? And like always Python has helped me this time also. There is a Python library available with name as  ChatterBot   which is nothing but a machine learning conversational dialog engine. And yes that is all I want to start my learning because I always prefer inbuilt Python library to start my learning journey and once I learn this then only I move ahead for another sources.