{"id":4099,"date":"2023-11-04T23:14:03","date_gmt":"2023-11-04T23:14:03","guid":{"rendered":"http:\/\/localhost:10003\/how-to-create-a-object-detection-app-with-python-and-yolov3\/"},"modified":"2023-11-05T05:48:01","modified_gmt":"2023-11-05T05:48:01","slug":"how-to-create-a-object-detection-app-with-python-and-yolov3","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-create-a-object-detection-app-with-python-and-yolov3\/","title":{"rendered":"How to Create a Object Detection App with Python and YOLOv3"},"content":{"rendered":"
In this tutorial, we will learn how to create an object detection app using Python and the YOLOv3 (You Only Look Once) algorithm. Object detection is a computer vision task that involves the detection and localization of objects in an image or video.<\/p>\n
We will be using the Darknet framework, which is an open-source neural network framework written in C and CUDA. It has implementations of several state-of-the-art object detection algorithms, including YOLO.<\/p>\n
To follow this tutorial, you should have some familiarity with Python and deep learning concepts. It is recommended to have a basic understanding of convolutional neural networks (CNNs) and how they are used for object detection.<\/p>\n
Let’s get started!<\/p>\n
Before we begin, we need to install the necessary dependencies. Ensure that you have Python 3.x installed on your system.<\/p>\n
We will be using the following libraries:<\/p>\n
To install these libraries, run the following command:<\/p>\n
pip install opencv-python numpy darknet\n<\/code><\/pre>\nStep 2: Downloading the YOLOv3 Pre-trained Weights and Configuration Files<\/h2>\n
YOLOv3 requires pre-trained weights and configuration files to perform object detection. These files are not included in the Darknet library and need to be downloaded separately.<\/p>\n
You can download the pre-trained YOLOv3 weights and configuration files from the official Darknet website using the following command:<\/p>\n
wget https:\/\/pjreddie.com\/media\/files\/yolov3.weights\nwget https:\/\/github.com\/pjreddie\/darknet\/blob\/master\/cfg\/yolov3.cfg\nwget https:\/\/github.com\/pjreddie\/darknet\/blob\/master\/data\/coco.names\n<\/code><\/pre>\nAlternatively, you can manually download these files and place them in a directory of your choice.<\/p>\n
Step 3: Loading the YOLOv3 Model<\/h2>\n
Now that we have the necessary files, let’s load the YOLOv3 model into our Python script.<\/p>\n
First, we need to import the necessary libraries:<\/p>\n
import cv2\nimport numpy as np\nimport darknet\n<\/code><\/pre>\nNext, we’ll define a function to load the YOLOv3 model:<\/p>\n
def load_yolo():\n net = darknet.load_net(b\"yolov3.cfg\", b\"yolov3.weights\", 0)\n meta = darknet.load_meta(b\"coco.names\")\n return net, meta\n<\/code><\/pre>\nThe darknet.load_net()<\/code> function loads the YOLOv3 network from the configuration and weights files. The darknet.load_meta()<\/code> function loads the class names associated with the COCO dataset.<\/p>\nWe can now call this function to load the YOLOv3 model:<\/p>\n
net, meta = load_yolo()\n<\/code><\/pre>\nStep 4: Performing Object Detection<\/h2>\n
Now that we have loaded the YOLOv3 model, we can use it to perform object detection on images or videos.<\/p>\n
Let’s start with image object detection. First, we’ll define a function to detect objects in an image:<\/p>\n
def detect_image(image, net, meta):\n # Resize the image to the network's input size\n resized_image = cv2.resize(image, (darknet.network_width(net), darknet.network_height(net)), interpolation=cv2.INTER_LINEAR)\n\n # Convert the image to a darknet-compatible format\n darknet_image = darknet.make_image(darknet.network_width(net), darknet.network_height(net), 3)\n darknet.copy_image_from_bytes(darknet_image, resized_image.tobytes())\n\n # Perform object detection\n detections = darknet.detect_image(net, meta, darknet_image)\n\n # Clean up\n darknet.free_image(darknet_image)\n\n return detections\n<\/code><\/pre>\nThis function takes an image, the YOLOv3 network, and the metadata as input. It resizes the image to match the network’s input size, converts it to a darknet-compatible format, performs object detection, and returns the detections.<\/p>\n
Let’s test this function by loading an image and detecting objects in it:<\/p>\n
image = cv2.imread(\"image.jpg\")\ndetections = detect_image(image, net, meta)\n\nfor detection in detections:\n print(detection)\n<\/code><\/pre>\nThis code will print the detected objects’ class names, confidence scores, and bounding box coordinates.<\/p>\n
To display the image with bounding boxes around the detected objects, we can modify the detect_image()<\/code> function as follows:<\/p>\ndef detect_image(image, net, meta):\n # ... existing code ...\n\n for detection in detections:\n class_name = detection[0].decode()\n confidence = detection[1]\n bounds = detection[2]\n\n x, y, w, h = bounds\n x1 = int(x - w\/2)\n y1 = int(y - h\/2)\n x2 = int(x + w\/2)\n y2 = int(y + h\/2)\n\n cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n cv2.putText(image, f\"{class_name} ({confidence:.2f})\", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)\n\n cv2.imshow(\"Object Detection\", image)\n cv2.waitKey(0)\n cv2.destroyAllWindows()\n<\/code><\/pre>\nThis modified function draws bounding boxes and class labels on the original image using OpenCV’s drawing functions.<\/p>\n
Step 5: Creating a Simple Object Detection App<\/h2>\n
Now that we can detect objects in images, let’s create a simple app that performs object detection on either an image file or a video file.<\/p>\n
First, we need to create a new Python file, let’s call it object_detection_app.py<\/code>. Open the file and import the necessary libraries:<\/p>\nimport cv2\nimport numpy as np\nimport darknet\n<\/code><\/pre>\nNext, we’ll define the load_yolo()<\/code> and detect_image()<\/code> functions:<\/p>\ndef load_yolo():\n # ... existing code ...\n\n\ndef detect_image(image, net, meta):\n # ... existing code ...\n<\/code><\/pre>\nFinally, we’ll add the app logic:<\/p>\n
def main():\n net, meta = load_yolo()\n\n file_path = input(\"Enter the path of the image or video file: \")\n file_type = file_path.split(\".\")[-1].lower()\n\n if file_type in [\"jpg\", \"jpeg\", \"png\"]:\n image = cv2.imread(file_path)\n detections = detect_image(image, net, meta)\n\n for detection in detections:\n print(detection)\n\n if detections:\n cv2.imshow(\"Object Detection\", image)\n cv2.waitKey(0)\n cv2.destroyAllWindows()\n elif file_type in [\"mp4\", \"avi\", \"mkv\"]:\n video = cv2.VideoCapture(file_path)\n\n while True:\n _, frame = video.read()\n\n if frame is None:\n break\n\n detections = detect_image(frame, net, meta)\n\n for detection in detections:\n print(detection)\n\n if detections:\n cv2.imshow(\"Object Detection\", frame)\n\n if cv2.waitKey(25) & 0xFF == ord(\"q\"):\n break\n\n video.release()\n cv2.destroyAllWindows()\n else:\n print(\"Unsupported file type. Please provide an image or video file.\")\n\nif __name__ == \"__main__\":\n main()\n<\/code><\/pre>\nThis code prompts the user to enter the path of an image or video file. It then determines the file type and performs object detection accordingly. The detected objects and their information are printed to the console. If an image is detected, it is displayed with bounding boxes.<\/p>\n
To run the app, save the file and execute it using Python:<\/p>\n
python object_detection_app.py\n<\/code><\/pre>\nFollow the prompts to enter the file path and see the object detection results.<\/p>\n
Conclusion<\/h2>\n
Congratulations! You have successfully created an object detection app using Python and the YOLOv3 algorithm. You learned how to load the YOLOv3 model, detect objects in images, and create a simple app that performs object detection on either an image or video file.<\/p>\n
Feel free to experiment further with the app by adding additional functionalities, such as saving the detected objects’ information to a file or integrating it with a user interface.<\/p>\n
Remember to explore other object detection algorithms, such as YOLOv4 or SSD, and customize the app according to your requirements.<\/p>\n
Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"
How to Create an Object Detection App with Python and YOLOv3 In this tutorial, we will learn how to create an object detection app using Python and the YOLOv3 (You Only Look Once) algorithm. Object detection is a computer vision task that involves the detection and localization of objects in Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[207,39,428,663,229,1279,230,326,769,41,328,1280,75,1278,1277],"yoast_head":"\nHow to Create a Object Detection App with Python and YOLOv3 - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n