Three minutes: experience the JAVA version of target detection at top speed (YOLO4)

Overview of this article

  • Can I detect objects in photos in Java?
  • Yes, today we use the least time and the simplest operation to experience this practical function. After you submit an ordinary photo, you will see the following effect. The dogs, people and horses in the original photo are identified. The category and confidence are in the upper left corner of each identification box. Finally, the time used for this identification is in the upper left corner of the picture:
  • Next, please join this article to achieve the above effect. The whole process is completed in three steps:
  1. Download models and configuration files
  2. Run the docker container, which is a web service. We use a browser to access this service, submit photos and complete the detection
  3. Verify the effect (open the web page on the browser, submit pictures, and wait for the test results)

Risk notification in advance

  • In order to simplify the operation, the docker will be used next. The corresponding image volume is huge, reaching a terrible 1.57G. It is recommended that you speed up the configuration of your docker to reduce the download waiting time;
  • Due to the huge volume of opencv and the large dependent Library of javacv, this leads to the emergence of super large image. I hope you can forgive me. The "three minutes" in the title is to remove the waiting time of image. If you think Xinchen's title is shameless, I think you are right

environmental information

  • The environmental information recommended in this actual combat is as follows:
  1. Operating system: Ubuntu 16 (MacBook Pro, version 11.2.3, macOS Big Sur)
  2. docker: 20.10.2 Community
  • No more, do it now!

Download models and configuration files

  • There are two ways to download the files required for this actual battle. You can choose one from the other
  • The first is to download from the official website, which can be downloaded from the following three addresses:
  1. YOLOv4 profile: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg
  2. YOLOv4 weight: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
  3. Category name: https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/coco.names
  • The second is to download from CSDN (without points). I have packaged the above three files here: https://download.csdn.net/download/boling_cavalry/33229838
  • Either way, you will eventually get three files: yolov4.cfg, yolov4.weights and coco.names. Please put them in the same directory. I put them here: / home/will/temp/202110/19/model
  • Create a new directory to store photos. My new directory here is: / home/will/temp/202110/19/images. Pay attention to ensure that the directory can be read and written
  • The final directory structure is as follows:
/home/will/temp/202110/19/
├── images
└── model
    ├── coco.names
    ├── yolov4.cfg
    └── yolov4.weights

Run docker container

  • Execute the following command to complete the service deployment (note that the two directories mentioned earlier are mapped to the container):
sudo docker run \
--rm \
--name yolodemo \
-p 8080:8080 \
-v /home/will/temp/202110/19/images:/app/images \
-v /home/will/temp/202110/19/model:/app/model \
bolingcavalry/yolodemo:0.0.1
  • The console will output the startup information of springboot:
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.8)

2021-10-19 07:39:20.112  INFO 1 --- [           main] c.b.yolodemo.YoloDemoApplication         : Starting YoloDemoApplication using Java 1.8.0_292 on 06e6b68f43ca with PID 1 (/app started by root in /)
2021-10-19 07:39:20.115  INFO 1 --- [           main] c.b.yolodemo.YoloDemoApplication         : No active profile set, falling back to default profiles: default
2021-10-19 07:39:20.997  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-10-19 07:39:21.010  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-10-19 07:39:21.010  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-10-19 07:39:21.083  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-10-19 07:39:21.084  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 915 ms
2021-10-19 07:39:21.157 ERROR 1 --- [           main] c.b.y.controller.YoloServiceController   : file.encoding is utf-8
2021-10-19 07:39:23.449  INFO 1 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2021-10-19 07:39:23.627  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-19 07:39:23.640  INFO 1 --- [           main] c.b.yolodemo.YoloDemoApplication         : Started YoloDemoApplication in 3.893 seconds (JVM running for 4.329)
2021-10-19 07:39:49.872  INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-19 07:39:49.872  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-10-19 07:39:49.873  INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
  • After deployment, experience the service verification effect

Verification effect

  • Browser access http://192.168.50.27:8080 , please change 192.168.50.27 here to the docker host IP (to close the firewall!). You can see the operation page as shown in the figure below (Xinchen's front-end development level is appalling, and it's really not groundless):
  • According to the prompt in the red box above, select a photo and click the submit button. After a short wait, the following page will be displayed:
  • Go to the docker console and output the identified log:
2021-10-19 07:39:57.830   : file [person.jpg], size [113880]
2021-10-19 07:39:59.303   : A total of 3 targets were detected
2021-10-19 07:39:59.306   : category[person],Confidence[99.78939%]
2021-10-19 07:39:59.307   : category[dog],Confidence[99.45358%]
2021-10-19 07:39:59.307   : category[horse],Confidence[98.37547%]
  • The animal recognition effect is very good:
  • So far, the Java version of the target detection experience has been completed, which is only three minutes. We are efficient enough (the time to download the super image can't be counted, dare not be counted...)
  • At this moment, you should feel the charm of Java in the field of target recognition. Of course, smart you will have many questions, such as:
  • What technology is used? (don't tell me to only use springboot, don't believe it)
  • What code did you write?
  • Is the operating environment well configured? Just rely on jar? Do you need any other operations?
  • Is there a pit?
  • In fact, as can be seen from the title, and YOLO About java and YOLO4, are you looking forward to it?
  • These questions will be completely revealed in the next article, and then you can easily make a SpringBoot application integrating target recognition. Please look forward to it. Xinchen's original will live up to you.

Posted on Mon, 06 Dec 2021 23:06:47 -0500 by mark_john