Welcome to my GitHub
https://github.com/zq2599/blog_demos
Content: All original articles are categorized and summarized and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;
Overview of this article
- Is it possible to detect objects in photos with Java?
- Yes, today we will use the least time and the simplest operation to experience this practical function. After you submit an ordinary photo, you will see the effect of the image below. The dogs, people and horses in the original photo will be recognized. The upper left corner of each recognition box is the category and confidence level, and finally, the upper left corner of the picture also has the time used for this recognition:
- Next, please follow this article to achieve the above effect. The whole process is completed in three steps:
- Download models and configuration files
- Run the docker container, this is a web service, we use the browser to access this service, submit photos to complete the detection
- Verify the effect (open the web page on the browser, submit the image, and wait for the test result)
Risk notification in advance
- In order to simplify the operation, docker will be used next. The corresponding image volume is huge, reaching a terrifying 1.57G. It is recommended that you configure acceleration for your docker to reduce the download waiting time;
- Due to the huge size of opencv, and the dependency library of javacv is not small, this has led to the emergence of super large mirrors. I hope you will pay more attention. The "Three Minutes" in the title is to remove the waiting time for mirroring. If you feel happy Chen's title is shameless, I think you're right...
Environmental information
- The recommended environmental information for this actual combat is as follows:
- OS: Ubuntu 16 (also MacBook Pro, version 11.2.3, macOS Big Sur)
- docker:20.10.2 Community
- Not much to say, get started now!
Download models and configuration files
- There are two ways to download the files required for this actual combat, you can choose one of the two
- The first is to download from the official website, and download from the following three addresses:
- YOLOv4 configuration file: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg
- YOLOv4 weights: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
- Category name: https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/coco.names
- The second is to download from csdn (no points required), I have packaged the above three files here: https://download.csdn.net/download/boling_cavalry/33229838
- Either of the above two methods will eventually get three files: yolov4.cfg, yolov4.weights, coco.names, please put them in the same directory, I put them here: <font color="blue"> /home/will/temp/202110/19/model</font>
- Create a new directory to store photos. The new directory I created here is: <font color="blue">/home/will/temp/202110/19/images</font>, be careful to ensure that this directory can be read and written
- The final directory structure looks like this:
/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 just mentioned 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
- The deployment is complete, and then experience the service verification effect
Verify the effect
- Browser access <font color="blue"> http://192.168.50.27:8080 </font>, here <font color="red">192.168.50.27</font> please change to docker host IP (to turn off the firewall!), you can see the operation page as shown below (Xinchen's front-end development level is outrageous, and it is not groundless):
- Select a photo and click the <font color="blue">Submit</font> button according to the prompt in the red box above. After a short wait, the following page will be displayed:
- Looking at the docker console, the identified logs are also output:
2021-10-19 07:39:57.830 : 文件 [person.jpg], 大小 [113880]
2021-10-19 07:39:59.303 : 一共检测到3个目标
2021-10-19 07:39:59.306 : 类别[person],置信度[99.78939%]
2021-10-19 07:39:59.307 : 类别[dog],置信度[99.45358%]
2021-10-19 07:39:59.307 : 类别[horse],置信度[98.37547%]
- The animal recognition effect is very good:
- At this point, the Java version of the target detection experience has been completed, and it only takes three minutes. We are efficient enough (the time to download the super-large image cannot be counted, I dare not count...)
- At this moment, you should be able to feel the charm of Java in the field of target recognition. Of course, you will have many questions, such as:
- What technology was used? (don't tell me to only use springboot, don't believe it)
- What code did you write?
- Is the operating environment configured well? Only jar dependencies? Is there any other action required?
- Is there a hole?
- In fact, it can be seen from the title that it is related to YOLO , java and YOLO4, is there a little expectation?
- These questions will be fully revealed in the next article, and then you can easily make a SpringBoot application that integrates target recognition. Please look forward to it, Xinchen Original will not disappoint you.
https://github.com/zq2599/blog_demos
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。