Day 21 - Python embedded in AlexeyAB/darknet
Like the previous Joseph/darknet, AlexeyAB/darknet also provides a python interface, which can be called directly by Python developers. It is convenient to combine with the original Python code. The difference is that it is not specifically placed in the python directory, but directly in the root directory Day 20 - reexamine the accuracy of map, F1, IOU, precision recall This article introduces the file structure of AlexeyAB/darknet in detail. The following figure is the file structure diagram of YOLO with AlexeyAB/darknet version installed. Here, only the parts that need to be used are explained:
- darknet: the main executable of AlexeyAB/darknet.
- darknet_image.py, darknet.py, darknet_video.py: by calling libdarknet.so, you can directly call some main functions of AlexeyAB/darknet in Python, such as training, testing, loading network structure, etc.
- libdarknet.so: AlexeyAB/darknet uses the shared function library written in c language, which can be called by other programming languages.
Figure 1. File structure of YOLO in AlexeyAB/darknet version
Day 15 - description of YOLO related settings In this article, we will use the Joseph/darknet version of the identification model to build a self built dataset. Now copy AlexeyAV/darknet related files (darknet, darknet.py, libdarknet.so) to this folder
Figure 2. Image recognition folder of self built dataset, introduced into AlexeyAB/darknet
Then create a file, import it into darknet.py, and then specify the relevant configuration. The two files yolov3.cfg and obj.data are in Day 15 - description of YOLO related settings This article has a detailed description, and yolov3.backup is in Day 16 - perform image recognition training This article is generated by training.
AlexeyABYolo.py
import cv2 import darknet import time def image_detection(image_path, network, class_names, class_colors, thresh): width = darknet.network_width(network) height = darknet.network_height(network) darknet_image = darknet.make_image(width, height, 3) image = cv2.imread(image_path) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh) darknet.free_image(darknet_image) image = darknet.draw_boxes(detections, image_resized, class_colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections # Specify the network structure configuration file yolov3.cfg, the self built dataset configuration file obj.data, and the pre trained weight file yolov3.backup network, class_names, class_colors = darknet.load_network( "./cfg/yolov3.cfg", "./cfg/obj.data", "./weights/yolov3.backup", 1 ) prev_time = time.time() # It is used to calculate the time to identify a picture print('predicting...', prev_time) # Carry out image recognition, and return the picture of the drawn box and the recognition results. The recognition results include labels, confidence and the coordinates of the box image, detections = image_detection( './labels/00-frame-608x608-0030.jpg', network, class_names, class_colors, 0.25 ) # Print the label, confidence, and the coordinates of the box darknet.print_detections(detections, '--ext_output') # Display identification time print((time.time() - prev_time)) # Write the result picture into the file result.jpg cv2.imwrite('result.jpg', image)
Before running, you need to install OpenCV, because this program will be used. The screen is as follows. Three objects are identified, followed by the coordinates of the upper left corner and the length and width. The time is about 0.566 seconds.
pip3 install opencv-python python3 AlexeyAB.py
Figure 3. Text output screen executed by AlexeyAB.py
Figure 4. Graphic output screen executed by AlexeyAB.py
Compared with the original command mode, the following is the execution syntax of the command line.
./darknet detector test cfg/obj.data cfg/yolov3.cfg weights/yolov3.backup ./labels/00-frame-608x608-0030.jpg -ext_output
Figure 5. Text output screen of AlexeyAB version instruction execution
Figure 6. Graphic output screen of AlexeyAB version instruction execution
It is found that the content of the object with edge is the same, but the confidence part, the part using python, is not rounded to three digits after the decimal point, so the results are 99.22%, 99.94%, 99.97%, while the instruction part is 99%, 100%, 100%; The identification time is also similar. The instruction execution time is about 0.558 seconds, while the python part is 0.566 seconds; The strangest thing is that the bbox data is different. The following table lists the comparison table of the coordinates of the box using Python and the box using Command. Directly look at the result picture. The box is the same. You can find that Python takes the coordinates of the center point and Command takes the coordinates of the upper left corner.
Table 1. Comparison of box coordinates using Python and Command
left_x | top_y | width | height | |
---|---|---|---|---|
Python | 102 | 420 | 171 | 93 |
Command | 16 | 374 | 171 | 93 |
The following is the output coordinate content using Python and Command.
# using Pyhon Altolamprologuscompressiceps: 99.22% (left_x: 102 top_y: 420 width: 171 height: 93) Altolamprologuscompressiceps: 99.94% (left_x: 235 top_y: 373 width: 86 height: 51) Altolamprologuscompressiceps: 99.97% (left_x: 311 top_y: 400 width: 147 height: 77) # using Command Altolamprologuscompressiceps: 99% (left_x: 16 top_y: 374 width: 171 height: 93) Altolamprologuscompressiceps: 100% (left_x: 192 top_y: 348 width: 86 height: 51) Altolamprologuscompressiceps: 100% (left_x: 238 top_y: 361 width: 147 height: 77)
Use the following program to draw the box of the first object according to the coordinates provided by Command, as shown below.
DrawCoord.py
import cv2 inputImgPath = '../labels/00-frame-608x608-0030.jpg' outputImgPath = '../image/00-frame-608x608-0030-1.jpg' img_w= img_h = 608 cv2image = cv2.imread(inputImgPath) # Altolamprologuscompressiceps: 99% (left_x: 16 top_y: 374 width: 171 height: 93) min_x, min_y, max_x, max_y = 16,374,16+171,374+93 cv2.rectangle(cv2image, (int(min_x),int(min_y)), (int(max_x),int(max_y)), (0,255,255), 2) cv2.imwrite(r'{}'.format(outputImgPath), cv2image)
Figure 7. Verifying the output coordinates of Python and Command
reference material
- Yolo v4, v3 and v2 for Windows and Linux, https://github.com/AlexeyAB/darknet