opencv/samples
Abduragim Shtanchaev 050085c996
Merge pull request #25950 from Abdurrahheem:ash/add-inpainting-sample
Diffusion Inpainting Sample #25950

This PR adds inpaiting sample that is based on [High-Resolution Image Synthesis with Latent Diffusion Models](https://arxiv.org/pdf/2112.10752) paper (reference github [repository](https://github.com/CompVis/latent-diffusion)).


Steps to run the model:

1. Firstly needs ONNX graph of the Latent Diffusion Model. You can get it in two different ways. 

> a. Generate the using this [repo](https://github.com/Abdurrahheem/latent-diffusion/tree/ash/export2onnx) and follow instructions below

```bash
git clone https://github.com/Abdurrahheem/latent-diffusion.git
cd latent-diffusion
conda env create -f environment.yaml
conda activate ldm
wget -O models/ldm/inpainting_big/last.ckpt https://heibox.uni-heidelberg.de/f/4d9ac7ea40c64582b7c9/?dl=1
python -m scripts.inpaint.py --indir data/inpainting_examples/ --outdir outputs/inpainting_results --export=True
```

> b. Download the ONNX graph (there 3 fiels) using this link: TODO make a link

2. Build opencv (preferebly with CUDA support enabled
3. Run the script 

```bash
cd opencv/samples/dnn
python ldm_inpainting.py 
python ldm_inpainting.py -e=<path-to-InpaintEncoder.onnx file> -d=<path-to-InpaintDecoder.onnx file> -df=<path-to-LatenDiffusion.onnx file> -i=<path-to-image>
```
Right after the last command you will be prompted with image. You can click on left mouse bottom and starting selection a region you would like to be inpainted (deleted). Once you finish marking the region, click on left mouse botton again and press esc button on your keyboard. The inpainting proccess will start. 

Note: If you are running it on CPU it might take a large chank of time. Also make sure to have about 15GB of RAM to make process faster (other wise swapping will click in and everything will be slower)
 
Current challenges: 

1. Diffusion process is slow (many layers fallback to CPU with running with CUDA backend) 
2. The diffusion result is does exactly mach that of the original torch pipeline

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x]The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2024-08-21 14:48:37 +03:00
..
android Merge branch 4.x 2024-02-12 14:20:35 +03:00
cpp Update orbbec(uvc) tutorial 2024-07-31 15:19:23 +08:00
data Merge pull request #24496 from cabelo:yolov3 2023-11-14 09:06:36 +03:00
directx Merge pull request #22462 from Biswa96:fix-directx-check 2022-10-03 08:37:36 +03:00
dnn Merge pull request #25950 from Abdurrahheem:ash/add-inpainting-sample 2024-08-21 14:48:37 +03:00
gdb Merge pull request #21805 from rogday:pretty_fix 2022-04-20 04:55:04 +00:00
gpu Move objdetect HaarCascadeClassifier and HOGDescriptor to contrib xobjdetect (#25198) 2024-03-21 23:40:10 +03:00
hal build: suppress the warning 2022-07-12 19:42:39 +09:00
install Merge branch 4.x 2021-12-30 21:43:45 +00:00
java Move objdetect HaarCascadeClassifier and HOGDescriptor to contrib xobjdetect (#25198) 2024-03-21 23:40:10 +03:00
opencl samples: fix OpenCL events leaks 2020-05-14 17:15:09 +00:00
opengl Merge branch 4.x 2024-07-17 10:08:16 +03:00
python Merge pull request #25268 from gursimarsingh:samples_cleanup_python 2024-07-31 16:11:00 +03:00
semihosting Merge branch 4.x 2021-12-30 21:43:45 +00:00
swift/ios Update tests and samples to work with changes to dynamic build 2020-09-27 21:12:28 +09:00
sycl ocl: fixes for OpenCL multiple contexts support 2020-09-03 20:34:49 +00:00
tapi Move objdetect HaarCascadeClassifier and HOGDescriptor to contrib xobjdetect (#25198) 2024-03-21 23:40:10 +03:00
va_intel samples(va): fix build warnings, use cv::format() 2021-02-19 10:36:26 +00:00
winrt Move objdetect HaarCascadeClassifier and HOGDescriptor to contrib xobjdetect (#25198) 2024-03-21 23:40:10 +03:00
winrt_universal Move objdetect HaarCascadeClassifier and HOGDescriptor to contrib xobjdetect (#25198) 2024-03-21 23:40:10 +03:00
wp8 Merge pull request #20223 from jogo-:3.4_wp8_samples_typos 2021-06-07 17:32:14 +00:00
_winpack_build_sample.cmd next: update versions handling 2020-10-11 08:11:32 +00:00
_winpack_run_python_sample.cmd Merge branch 4.x 2024-01-19 17:32:22 +03:00
CMakeLists.example.in cmake: enable C++11 implicitly 2020-02-03 17:45:37 +03:00
CMakeLists.txt Merge pull request #25197 from invarrow:invbranch-cleanup 2024-03-26 15:17:18 +03:00
samples_utils.cmake imgproc: add IntelligentScissors 2020-12-25 10:57:11 +00:00