{"id":10688,"date":"2024-10-06T21:21:44","date_gmt":"2024-10-06T21:21:44","guid":{"rendered":"https:\/\/www.aihello.com\/resources\/?p=10688"},"modified":"2026-09-23T02:40:06","modified_gmt":"2026-09-23T02:40:06","slug":"end-to-end-product-photoshoot-generation-using-sam-2-controlnet","status":"publish","type":"post","link":"https:\/\/www.aihello.com\/resources\/blog\/end-to-end-product-photoshoot-generation-using-sam-2-controlnet\/","title":{"rendered":"End-to-End Product Photoshoot Generation Using SAM 2 &amp; ControlNet"},"content":{"rendered":"<div class=\"wp-block-group ah-reading-guide ah-compact-toc\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\"><style>\n.ah-reading-guide{max-width:820px;margin:0 auto;color:#26364a;font-size:18px;line-height:1.8;counter-reset:ah-section}.ah-reading-guide *{box-sizing:border-box}.ah-reading-guide p{margin:0 0 22px;line-height:1.8}.ah-reading-guide strong{color:#142d48}.ah-reading-guide h2{position:relative;margin:56px 0 24px;padding-top:24px;border-top:1px solid #dce7ef;font-size:29px;line-height:1.35;letter-spacing:-.4px;color:#142d48}.ah-reading-guide h2.ah-numbered{padding-left:54px;counter-increment:ah-section}.ah-reading-guide h2.ah-numbered:before{content:counter(ah-section,decimal-leading-zero);position:absolute;left:0;top:26px;display:flex;align-items:center;justify-content:center;width:38px;height:38px;background:#eaf4fc;color:#216faf;border-radius:10px;font-size:15px;font-weight:700;letter-spacing:0}.ah-reading-guide h3{position:relative;margin:34px 0 16px;font-size:23px;line-height:1.4;color:#163f65}.ah-reading-guide h4{position:relative;margin:28px 0 14px;font-size:20px;line-height:1.45;color:#163f65}.ah-reading-guide .ah-lead{margin:0 0 28px;padding:24px 28px;border-left:4px solid #2479ba;border-radius:0 12px 12px 0;background:#eef6fc;font-size:20px;line-height:1.75}.ah-reading-guide .ah-callout,.ah-reading-guide .ah-formula{margin:28px 0;padding:24px 26px;border:1px solid #d7e5f1;border-left:4px solid #2479ba;border-radius:0 12px 12px 0;background:#f5f9fd}.ah-reading-guide .ah-formula{font-size:20px;font-weight:600;font-variant-numeric:tabular-nums;line-height:1.9;overflow-wrap:anywhere}.ah-reading-guide ul,.ah-reading-guide ol{margin:24px 0 30px;padding-left:28px}.ah-reading-guide li{margin:0 0 12px;padding-left:7px;line-height:1.75}.ah-reading-guide li::marker{color:#2479ba;font-weight:700}.ah-reading-guide ol.ah-steps{list-style:none;counter-reset:ah-step;padding:0;display:grid;gap:14px}.ah-reading-guide ol.ah-steps>li{position:relative;counter-increment:ah-step;margin:0;padding:22px 24px 22px 64px;border:1px solid #dbe7f1;border-radius:12px;background:#f6fafd}.ah-reading-guide ol.ah-steps>li:before{content:counter(ah-step);position:absolute;top:23px;left:20px;display:flex;align-items:center;justify-content:center;width:28px;height:28px;border-radius:50%;background:#2378b9;color:white;font-size:14px;font-weight:700}.ah-reading-guide ul.ah-cards{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px;padding:0;list-style:none}.ah-reading-guide ul.ah-cards>li{margin:0;padding:20px 22px;border:1px solid #dbe8f2;background:#f7fbfe;border-radius:12px;font-size:17px;line-height:1.7}.ah-reading-guide ul.ah-cards>li:last-child:nth-child(odd){grid-column:1\/-1}.ah-reading-guide .ah-cards>li>strong:first-child{display:block;margin-bottom:8px;color:#19598d}.ah-reading-guide .ah-step-panel{margin:22px 0;padding:24px 28px;border-left:4px solid #2479ba;border-radius:0 12px 12px 0;background:#f5f9fd}.ah-reading-guide .ah-step-panel h3{margin-top:0}.ah-reading-guide .ah-step-panel p:last-child{margin-bottom:0}.ah-reading-guide figure{max-width:100%;margin:30px 0}.ah-reading-guide figure.wp-block-image{padding:7px;border:1px solid #e0e8ef;border-radius:12px;background:white;box-shadow:0 5px 18px rgba(20,45,72,.06)}.ah-reading-guide img{max-width:100%;height:auto}.ah-reading-guide figure.wp-block-image img{display:block;border-radius:6px}.ah-reading-guide figcaption{font-size:14px;line-height:1.5;color:#587086;margin-top:10px}.ah-reading-guide figure.wp-block-table{overflow-x:auto;border:1px solid #d8e5ef;border-radius:10px}.ah-reading-guide table{width:100%;border-collapse:collapse;font-size:16px;line-height:1.6}.ah-reading-guide th,.ah-reading-guide td{padding:14px 16px;border:1px solid #d8e5ef;text-align:left;vertical-align:top}.ah-reading-guide th{background:#eaf4fc;color:#163f65;font-weight:700}.ah-reading-guide tbody tr:nth-child(even){background:#f6f9fc}.ah-reading-guide blockquote{margin:30px 0;padding:22px 26px;border-left:4px solid #2479ba;background:#f3f8fc;border-radius:0 10px 10px 0}.ah-reading-guide #ez-toc-container{width:100%;margin:32px 0 42px;padding:22px 24px;border:1px solid #cbdfee;border-top:4px solid #2479ba;border-radius:12px;background:#f7fbfe;box-shadow:none}.ah-reading-guide #ez-toc-container .ez-toc-title{margin:0;font-size:20px;font-weight:700;color:#163f65;line-height:1.5}.ah-reading-guide #ez-toc-container .ez-toc-title-container{margin-bottom:14px}.ah-reading-guide #ez-toc-container ul.ez-toc-list-level-1{display:block;column-count:2;column-gap:26px;padding:0;margin:0}.ah-reading-guide #ez-toc-container ul.ez-toc-list-level-1>li{break-inside:avoid;padding:11px 0;margin:0;border-bottom:1px solid #dce7ef;line-height:1.5;font-size:16px}.ah-reading-guide #ez-toc-container ul ul{margin:7px 0 0;padding-left:18px}.ah-reading-guide #ez-toc-container li li{padding:0;margin:5px 0;font-size:14px}.ah-reading-guide #ez-toc-container a{color:#19598d;text-decoration:none}.ah-reading-guide #ez-toc-container a:hover{text-decoration:underline}.ah-reading-guide #ez-toc-container a:focus-visible{outline:3px solid #2479ba;outline-offset:4px;border-radius:3px}.ah-reading-guide :is(h2,h3,h4)>.ez-toc-section{position:absolute;top:-100px;left:0}.ah-reading-guide :is(h2,h3,h4)>.ez-toc-section-end{position:absolute;top:0;left:0}.ah-reading-guide [id]{scroll-margin-top:100px}.ah-reading-guide .gspb_container{min-width:0}@media(max-width:700px){.ah-reading-guide{font-size:17px}.ah-reading-guide h2{font-size:25px;margin-top:44px}.ah-reading-guide h2.ah-numbered{padding-left:43px}.ah-reading-guide h2.ah-numbered:before{width:30px;height:30px;font-size:13px}.ah-reading-guide h3{font-size:21px}.ah-reading-guide ul.ah-cards{grid-template-columns:1fr}.ah-reading-guide .ah-lead,.ah-reading-guide .ah-callout,.ah-reading-guide .ah-formula,.ah-reading-guide .ah-step-panel{padding:20px}.ah-reading-guide .ah-lead,.ah-reading-guide .ah-formula{font-size:18px}.ah-reading-guide #ez-toc-container{padding:18px}.ah-reading-guide #ez-toc-container ul.ez-toc-list-level-1{column-count:1}}@media(prefers-reduced-motion:reduce){.ah-reading-guide [data-aos]{opacity:1!important;visibility:visible!important;transform:none!important;transition:none!important;animation:none!important}}\n\n.ah-reading-guide .ah-process-heading{padding-left:48px;min-height:36px}.ah-reading-guide .ah-process-heading:before{position:absolute;left:0;top:0;width:34px;height:34px;display:flex;align-items:center;justify-content:center;border-radius:9px;background:#eaf4fc;color:#216faf;font-size:15px;font-weight:700}.ah-reading-guide .ah-process-1:before{content:\"01\"}.ah-reading-guide .ah-process-2:before{content:\"02\"}.ah-reading-guide .ah-process-3:before{content:\"03\"}.ah-reading-guide .ah-process-4:before{content:\"04\"}.ah-reading-guide .ah-process-5:before{content:\"05\"}.ah-reading-guide .ah-process-6:before{content:\"06\"}.ah-reading-guide .ah-process-7:before{content:\"07\"}.ah-reading-guide .ah-process-8:before{content:\"08\"}.ah-reading-guide .ah-process-9:before{content:\"09\"}\n.ah-reading-guide .ah-definition{padding:20px 24px;margin:14px 0;border:1px solid #dbe7f1;border-radius:12px;background:#f8fbfe}.ah-reading-guide .ah-definition strong{color:#163f65}\n.ah-reading-guide .ah-long-form-list>li{margin-bottom:28px}.ah-reading-guide .ah-long-form-list>li>strong:first-child{display:block;font-size:22px;line-height:1.4;margin-bottom:16px}.ah-reading-guide .ah-long-form-list li img{display:block;margin:22px auto}.ah-reading-guide .ah-formula-comparison{font-variant-numeric:tabular-nums}\n.ah-reading-guide h5,.ah-reading-guide h6{font-size:18px;line-height:1.5;color:#163f65;margin:26px 0 14px;font-weight:700}\n.ah-reading-guide h2.ah-instruction-heading{font-size:21px;margin:24px 0 18px;padding-top:18px}.ah-reading-guide h2.ah-instruction-heading:before{top:18px}\n.ah-reading-guide ul.ah-stacked-cards{list-style:none;padding:0}.ah-reading-guide .ah-stacked-cards>li{margin:14px 0;padding:20px 24px;border:1px solid #dbe7f1;border-radius:12px;background:#f8fbfe}\n.ah-reading-guide .ah-minor-label{font-weight:700;color:#1e4e64;font-size:18px;margin:20px 0 8px;}\n.ah-reading-guide h3.ah-do,.ah-reading-guide h3.ah-dont{font-size:21px;padding:18px 22px;border-radius:0 10px 10px 0}.ah-reading-guide h3.ah-do{border-left:4px solid #20877e;background:#f0faf8}.ah-reading-guide h3.ah-dont{border-left:4px solid #ae6340;background:#fff7f0}\n.ah-reading-guide p.ah-fact-row{margin:6px 0;padding:12px 18px;background:#f4f8fc;border-left:3px solid #c7dcf0;border-radius:0 8px 8px 0;font-size:17px;line-height:1.65}.ah-reading-guide p.ah-cta a{display:inline-block;padding:12px 20px;border-radius:9px;background:#135faa;color:#fff;text-decoration:none;font-weight:700}.ah-reading-guide p.ah-cta a:hover{background:#104d89}.ah-reading-guide .ah-agency-summary td:first-child{width:38%;color:#184b75}\n.ah-reading-guide details.ah-original-index{padding:16px 20px;border:1px solid #dce7f0;border-radius:10px;margin:24px 0;background:#f8fafc}.ah-reading-guide details.ah-original-index summary{cursor:pointer;font-size:18px;font-weight:700;color:#244f74}.ah-reading-guide details.ah-original-index>ol{margin-top:18px}\n.ah-reading-guide ul.ah-cycle-cards{grid-template-columns:repeat(3,minmax(0,1fr));gap:22px}.ah-reading-guide ul.ah-cycle-cards>li{position:relative;text-align:center;display:flex;align-items:center;justify-content:center;min-height:110px;font-weight:700;color:#20567d}.ah-reading-guide ul.ah-cycle-cards>li:last-child:nth-child(odd){grid-column:auto}.ah-reading-guide ul.ah-cycle-cards>li:not(:last-child)::after{content:\"\u203a\";position:absolute;right:-19px;top:50%;transform:translateY(-50%);font-size:28px;color:#3785b4}@media(max-width:700px){.ah-reading-guide ul.ah-cycle-cards{grid-template-columns:1fr}.ah-reading-guide ul.ah-cycle-cards>li:not(:last-child)::after{right:50%;top:auto;bottom:-25px;transform:rotate(90deg)}}\n.ah-reading-guide .ah-process-10::before{content:\"10\"}\n.ah-reading-guide pre.wp-block-code{background:#122238;color:#eff6ff;border:1px solid #29415f;border-radius:12px;padding:22px 24px;margin:24px 0;overflow-x:auto;font-size:15px;line-height:1.7;white-space:pre;tab-size:4}.ah-reading-guide pre.wp-block-code code{background:transparent;color:inherit;padding:0;white-space:pre;font-family:Consolas,Monaco,monospace}.ah-reading-guide p code{background:#edf3fa;color:#204d78;border-radius:4px;padding:2px 5px}\n.ah-reading-guide ol.ah-steps.ah-start-2{counter-reset:ah-step 1}\n.ah-reading-guide ol.ah-steps.ah-start-3{counter-reset:ah-step 2}\n.ah-reading-guide ol.ah-steps.ah-start-4{counter-reset:ah-step 3}\n.ah-reading-guide ol.ah-steps.ah-start-5{counter-reset:ah-step 4}\n.ah-reading-guide ol.ah-steps.ah-start-6{counter-reset:ah-step 5}\n.ah-reading-guide ol.ah-steps.ah-start-7{counter-reset:ah-step 6}\n.ah-reading-guide ol.ah-steps.ah-start-8{counter-reset:ah-step 7}\n.ah-reading-guide ol.ah-steps.ah-start-9{counter-reset:ah-step 8}\n.ah-reading-guide ol.ah-steps.ah-start-10{counter-reset:ah-step 9}\n.ah-reading-guide ol.ah-steps.ah-start-11{counter-reset:ah-step 10}\n.ah-reading-guide ol.ah-steps.ah-start-12{counter-reset:ah-step 11}\n.ah-reading-guide #ez-toc-container nav{max-height:460px;overflow-y:auto;padding-right:8px}.ah-reading-guide figure.ah-comparison-wide{max-height:650px;overflow:auto;border:1px solid #dae4ee;border-radius:12px}.ah-reading-guide .ah-comparison-wide table{min-width:760px;font-size:15px}.ah-reading-guide .ah-comparison-wide thead th{position:sticky;top:0;background:#183f65;color:#fff;z-index:1}.ah-reading-guide figure.ah-agency-summary tbody th{width:30%;background:#edf4fa;color:#244764;font-weight:700}\n.ah-reading-guide #ez-toc-container a.ez-toc-link::before{content:none!important;display:none!important}\n.ah-reading-guide .ah-comparison-wide thead th,.ah-reading-guide .ah-comparison-wide thead th *{color:#fff!important}\n.ah-reading-guide .ah-equation{display:block;margin:16px 0;padding:16px 20px;border-left:4px solid #2378b9;background:#edf4fa;border-radius:8px;font-size:20px;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}\n.ah-reading-guide .ah-feature-comparison thead th:nth-child(1){width:18%}.ah-reading-guide .ah-feature-comparison thead th:nth-child(2){width:12%;white-space:nowrap}.ah-reading-guide .ah-feature-comparison thead th:nth-child(3){width:48%}.ah-reading-guide .ah-feature-comparison thead th:nth-child(4){width:22%}\n.ah-reading-guide.ah-compact-toc #ez-toc-container ul.ez-toc-list>li>ul>li>ul{display:none}.ah-reading-guide h4.ah-do{color:#276044!important}.ah-reading-guide h4.ah-dont{color:#855218!important}.ah-reading-guide p.ah-tool-bullet{margin:.45em 0!important;padding:.6em 1em;border-radius:8px;background:#f4f7fa;line-height:1.65}\n.ah-reading-guide .ah-glossary-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px;margin:30px 0}.ah-reading-guide .ah-glossary-entry{background:#f7f9fc;border:1px solid #dce5ef;border-radius:12px;padding:22px;height:100%;box-sizing:border-box}.ah-reading-guide .ah-glossary-entry h2{font-size:21px!important;margin:0 0 14px!important;line-height:1.4!important;border:0;padding:0}.ah-reading-guide .ah-glossary-entry p{font-size:16px!important;line-height:1.7!important;margin:0!important}@media(max-width:650px){.ah-reading-guide .ah-glossary-grid{grid-template-columns:1fr}}\n.ah-reading-guide .ah-glossary-grid{display:block}.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px;align-items:stretch}.ah-reading-guide .ah-glossary-entry{height:auto!important;margin:0!important;min-width:0}.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container>.wp-block-greenshift-blocks-container{display:flex;min-width:0}.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container>.wp-block-greenshift-blocks-container>.ah-glossary-entry{flex:1}@media(max-width:650px){.ah-reading-guide .ah-glossary-grid>.wp-block-group__inner-container{grid-template-columns:1fr}}\n.ah-reading-guide ol.ah-steps.ah-feature-grid{grid-template-columns:repeat(2,minmax(0,1fr))}@media(max-width:700px){.ah-reading-guide ol.ah-steps.ah-feature-grid{grid-template-columns:1fr}}\n.ah-reading-guide pre.wp-block-preformatted{background:#122238;color:#eff6ff;border:1px solid #29415f;border-radius:12px;padding:22px 24px;margin:24px 0;overflow-x:auto;font-size:15px;line-height:1.7;white-space:pre;tab-size:4;font-family:Consolas,Monaco,monospace}.ah-reading-guide pre.wp-block-preformatted code{background:transparent;color:inherit;padding:0;white-space:pre;font:inherit}.ah-reading-guide .ah-output-sample{max-height:360px;overflow:auto;padding:24px;border:1px solid #d7e5f1;border-radius:12px;background:#f5f9fd;font-size:16px;line-height:1.8}\n.ah-reading-guide pre.wp-block-preformatted,.ah-reading-guide pre.wp-block-preformatted code{white-space:pre!important;text-align:left!important}.ah-reading-guide pre br{display:initial!important}.ah-reading-guide pre.ah-code-long{max-height:620px;overflow:auto;white-space:pre!important;text-align:left!important}<\/style><noscript><style>.ah-reading-guide [data-aos]{opacity:1!important;visibility:visible!important;transform:none!important}<\/style><\/noscript><p class=\"ah-lead wp-block-paragraph\" id=\"3c33\">Harnessing the power of Generative AI, SAM 2 and ControlNet collaborate to <a  data-ilj-link-preview=\"true\"  data-featured-image=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2021\/04\/amazon_ppc_automation_ppc_automation_amazon_ads_automation_amazon_ppc_automation_software_4029-300x124.png\"  data-excerpt=\"Struggling with Amazon PPC visibility? Learn how AI-powered automation optimizes campaigns, slashes ACoS, and boosts your Buy Box chances for more sales.\" href=\"https:\/\/www.aihello.com\/resources\/blog\/10-reasons-your-amazon-seller-central-business-needs-ppc-automation-software\/\">automate<\/a> the entire product photoshoot process. This cutting-edge approach enables businesses to create diverse, high-quality product images at scale, without the need for traditional photography setups.<\/p><p class=\"wp-block-paragraph\" id=\"c346\">In this article, let\u2019s learn about these two models and go through a basic end-to-end pipeline that can allow <a  data-ilj-link-preview=\"true\"  data-featured-image=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2022\/06\/selling-on-amazon-mexico-300x225.jpg\"  data-excerpt=\"Mexico&#039;s eCommerce boom presents huge opportunities for Amazon sellers. This guide explores the thriving Mexican online market, Amazon.mx&#039;s growth, and key considerations for successfully selling your products to millions of Mexican consumers. Learn how to navigate logistics, expand your reach, and capitalize on this expanding market.\" href=\"https:\/\/www.aihello.com\/resources\/blog\/how-to-launch-an-amazon-mexico-business\/\">us<\/a> to generate a photoshoot image.<\/p><figure class=\"wp-block-image size-large\"><img decoding=\"async\" title=\"can you use google shopping for amazon products | what is dv360 vs google ads | how many business sell on amazon germany\" src=\"https:\/\/i.ibb.co\/23Kybyg\/process.jpg\" \/><\/figure><h2 id=\"pipeline-overview\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Pipeline_Overview\"><\/span>Pipeline Overview<span class=\"ez-toc-section-end\"><\/span><\/h2><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10688-1\" id=\"gspb_container-id-gsbp-f10688-1\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><ol class=\"ah-steps\"><li><p id=\"4e39\"><strong><a  data-ilj-link-preview=\"true\"  data-featured-image=\"https:\/\/www.aihello.com\/resources\/wp-content\/uploads\/2023\/08\/how-to-reduce-acos-on-amazon-the-ultimate-guide-300x169.jpg\"  data-excerpt=\"Amazon PPC costs soaring? Learn expert strategies to reduce your ACoS. Discover prioritization, segmentation, dayparting, and ad analysis tips.\" href=\"https:\/\/www.aihello.com\/resources\/blog\/lower-acos-amazon\/\">Segmentation<\/a><\/strong>\u2014 The first step to generate a photoshoot of a product is to separate it from the background that\u2019s already present in the image provided by the user. This can be done by generating a mask for the object. We can use any segmentation model for this task, and so, we\u2019ll go with the newly released state-of-the-art&nbsp;<em>Segment Anything Model 2 (SAM 2).<\/em><\/p><\/li><li><p id=\"2fe8\"><strong>Generation<\/strong>\u2014 The second and final part of the pipeline is to use the separated product and user\u2019s text prompt to generate a new image having the product as-it-is without any manipulations but with a new background. A regular stable-diffusion model which is usually used for image generation can\u2019t keep the product as-it-is, it\u2019s meant to completely generate a new image, and so, we will use another model \u2014 called a \u2018<em>ControlNet<\/em>\u2019 which is like an extension of Stable Diffusion.<\/p><\/li><\/ol><\/div><h2 id=\"segment-anything-model-2-sam-2\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Segment_Anything_Model_2_SAM_2\"><\/span>Segment Anything Model 2 (SAM 2)<span class=\"ez-toc-section-end\"><\/span><\/h2><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10688-2\" id=\"gspb_container-id-gsbp-f10688-2\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><p class=\"ah-definition wp-block-paragraph\">SAM 2 is an advanced computer vision model developed by Meta AI. This model uses a Vision Transformer (ViT) as its backbone and is popularly used for segmentation tasks.<\/p><\/div><figure class=\"wp-block-image size-large\"><img decoding=\"async\" title=\"how to sell books online amazon | does saudi arabia have amazon | what is ecommerce customer service\" src=\"https:\/\/i.ibb.co\/98p9M1G\/model-diagram.png\" alt=\"\" \/><\/figure><p class=\"wp-block-paragraph\">The model is trained on both images as well as videos, and accepts various kinds of prompts such as \u2014 text, points and boxes.<\/p><figure class=\"wp-block-image size-large\"><img decoding=\"async\" title=\"what report can help you identify opportunities to improve your keywords and ads | how to optimize ppc | is adding too many keywords bad for google ads\" src=\"https:\/\/i.ibb.co\/q7y9C2P\/0-0auemk29m-Di8-IQn3.png\" alt=\"\" \/><\/figure><p class=\"wp-block-paragraph\">A key architectural feature is the hierarchical prediction system, which allows SAM 2 to produce masks at multiple levels of detail, e.g., for an image of a dog\u2014it might produce multiple masks for the same dog where one mask may cover the head of the dog, one might cover the body, and a third one can outline the entire dog.<\/p><figure class=\"wp-block-image size-large\"><img decoding=\"async\" title=\"how to improve click-through rate amazon | what are lightning deals on amazon | what are the top tools for amazon fbm shipping management?\" src=\"https:\/\/i.ibb.co\/Qvztzjf\/Copy-of-Blog-Image-Template-2.jpg\" alt=\"\" \/><\/figure><h3 id=\"segmentation\" class=\"wp-block-heading ah-process-heading ah-process-1\"><span class=\"ez-toc-section\" id=\"Segmentation\"><\/span>Segmentation<span class=\"ez-toc-section-end\"><\/span><\/h3><p class=\"wp-block-paragraph\" id=\"6982\">SAM 2, when given an image, produces segmentation masks for various parts of the image at different levels.<\/p><p class=\"wp-block-paragraph\" id=\"c7a0\">This is where we need to identify which mask out of all the masks properly covers the main object in our image, so that we are separating the correct part from the background.<\/p><div id=\"2add\"><p>Now a lot of complex heuristics can be used to identify the correct mask, but since this pipeline is supposed to give a general overview, we will assume that all our product images have a simple white background \u2014 and so, adding white borders to the product image and combining all the masks that are not touching the edges of the extended image would be an easy-to-implement and well-working heuristic. <\/p><p>This combined mask would be our final product mask.<\/p><\/div><pre class=\"wp-block-code ah-code-long\"><code>import torchimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageOpsfrom ultralytics import SAMfrom skimage import morphology# Load SAM modelmodel = SAM(\"sam2_l.pt\")def add_white_border(image, border_size=50):    return ImageOps.expand(image, border=border_size, fill='white')def process_image(image_path, border_size=50):    # Load the original image    original_image = Image.open(image_path)    # Add white border    bordered_image = add_white_border(original_image, border_size)    # Process the bordered image    results = model(bordered_image, device='cuda' if torch.cuda.is_available() else 'cpu')    # Get all masks    masks = results&#091;0].masks.data    # Select all masks that don't touch the border    selected_masks = &#091;]    for mask in masks:        mask_np = mask.cpu().numpy()        if not touches_border(mask_np):            selected_masks.append(mask_np)    # If no suitable masks found, return None    if not selected_masks:        return None, None, None    # Combine all selected masks    combined_mask = np.any(selected_masks, axis=0)    # Apply morphological operations to refine the mask    refined_mask = morphology.closing(combined_mask, morphology.disk(5))    refined_mask = morphology.remove_small_holes(refined_mask, area_threshold=500)    # Remove the border from the refined mask    original_size = original_image.size&#091;::-1]  # PIL uses (width, height)    refined_mask_original = refined_mask&#091;border_size:-border_size, border_size:-border_size]    # Resize the mask if necessary    if refined_mask_original.shape != original_size:        refined_mask_original = Image.fromarray(refined_mask_original)        refined_mask_original = refined_mask_original.resize(original_image.size, Image.NEAREST)        refined_mask_original = np.array(refined_mask_original)    # Invert the mask (True becomes False and False becomes True)    refined_mask_original = ~refined_mask_original    # Create masked image    original_array = np.array(original_image)    masked_image = original_array.copy()    masked_image&#091;refined_mask_original] = &#091;0, 0, 0]  # Set mask area to black    return original_array, masked_image, refined_mask_originaldef touches_border(mask):    return np.any(mask&#091;0,:]) or np.any(mask&#091;-1,:]) or np.any(mask&#091;:,0]) or np.any(mask&#091;:,-1])# Example usageimage_path = 'path_to_image.jpg'image, masked_image, mask = process_image(image_path)if image is not None:    plot_results(image, masked_image, mask, 'SAM 2 Mask Generation')        # Save the masked image    mask_image = Image.fromarray(mask.astype(np.uint8) * 255)    mask_output_path = 'mask_output.png'    mask_image.save(mask_output_path)    print(f\"Mask saved to {mask_output_path}\")else:    print(\"No suitable masks found for the image\")<\/code><\/pre><figure class=\"wp-block-image size-large\"><img decoding=\"async\" title=\"is amazon available in the netherlands | what is ams amazon | how do tariffs affect amazon\" src=\"https:\/\/i.ibb.co\/LtyhSKz\/download-2.png\" alt=\"\" \/><\/figure><h2 id=\"controlnet\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ControlNet\"><\/span>ControlNet<span class=\"ez-toc-section-end\"><\/span><\/h2><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10688-3\" id=\"gspb_container-id-gsbp-f10688-3\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><p class=\"ah-definition wp-block-paragraph\" id=\"0175\">ControlNets are like adapters to existing generation models that help the user retain some sort of control on the generated image and guide the model to retain selected features of the original image given as a prompt.<\/p><\/div><p class=\"wp-block-paragraph\" id=\"37fb\">There are various different type of ControlNet models and they are divided based on the type of input they take of the subject that needs to be retained in the generated image \u2014 the inputs might be edge detection maps, pose estimation, depth maps, masks and many more.<\/p><figure class=\"wp-block-image size-large\"><img decoding=\"async\" title=\"how well do i rank on amazon | how to sale books on amazon | how to sell on amazon uk\" src=\"https:\/\/i.ibb.co\/RyGhqrB\/1-yjv-TQ1zwg-PU-Pk-Ky4-Yf0-Pg.png\" alt=\"\" \/><\/figure><h3 id=\"generation\" class=\"wp-block-heading ah-process-heading ah-process-2\"><span class=\"ez-toc-section\" id=\"Generation\"><\/span>Generation<span class=\"ez-toc-section-end\"><\/span><\/h3><p class=\"wp-block-paragraph\" id=\"73dd\">We will use a&nbsp;<a href=\"https:\/\/huggingface.co\/yahoo-inc\/photo-background-generation\" rel=\"noreferrer noopener\" target=\"_blank\">pre-trained ControlNet-based model by Yahoo<\/a>&nbsp;for our task.<\/p><p class=\"wp-block-paragraph\" id=\"6a67\">It would be using the mask generated by the first step to input along with the original image and a prompt that the user can give to describe what kind of photoshoot image they want.<\/p><pre class=\"wp-block-code ah-code-long\"><code>from diffusers import DiffusionPipelinefrom PIL import Image, ImageOpsimport torchimport matplotlib.pyplot as plt# Load the Yahoo ControlNet modelmodel_id = \"yahoo-inc\/photo-background-generation\"pipeline = DiffusionPipeline.from_pretrained(model_id, custom_pipeline=model_id)pipeline = pipeline.to('cuda')def resize_with_padding(img, expected_size):    img.thumbnail((expected_size&#091;0], expected_size&#091;1]))    delta_width = expected_size&#091;0] - img.size&#091;0]    delta_height = expected_size&#091;1] - img.size&#091;1]    pad_width = delta_width \/\/ 2    pad_height = delta_height \/\/ 2    padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)    return ImageOps.expand(img, padding)def generate_background(image, mask, prompt, seed=13, cond_scale=1.0):    # Ensure images are in RGB mode    image = image.convert('RGB')    # Resize images to 512x512 (typical size for many models)    image = resize_with_padding(image, (512, 512))    mask = resize_with_padding(mask, (512, 512))    # Set up the generator with the provided seed    generator = torch.Generator(device='cuda').manual_seed(seed)        # Generate the image    with torch.autocast(\"cuda\"):        result_image = pipeline(            prompt=prompt,            image=image,            mask_image=mask,            control_image=mask,            num_images_per_prompt=1,            generator=generator,            num_inference_steps=20,            guess_mode=False,            controlnet_conditioning_scale=cond_scale        ).images&#091;0]        return result_image# Example usageimage_path = \"path_to_image.jpg\"mask_path = \"mask_output.png\"image = Image.open(image_path)mask = Image.open(mask_path)prompt = \"Bottle kept on glass table\"result = generate_background(image, mask, prompt)# Plotting the resultsfig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))ax1.imshow(image)ax1.set_title(\"Original Image\")ax1.axis('off')ax2.imshow(mask, cmap='gray')ax2.set_title(\"Mask\")ax2.axis('off')ax3.imshow(result)ax3.set_title(\"Generated Image\")ax3.axis('off')plt.tight_layout()plt.show()# Save the generated imageoutput_path = 'generated_image.png'result.save(output_path)print(f\"Generated image saved to {output_path}\")<\/code><\/pre><div class=\"wp-block-greenshift-blocks-container gspb_container gspb_container-gsbp-f10688-4\" id=\"gspb_container-id-gsbp-f10688-4\" data-aos=\"fade\" data-aos-easing=\"ease\" data-aos-duration=\"500\" data-aos-once=\"true\"><figure class=\"wp-block-image size-large\"><img decoding=\"async\" title=\"how does amazon advertise their products | how do you market your merch by amazon store | how to make money on amazon canada\" src=\"https:\/\/i.ibb.co\/Q8cyHmp\/download-1.png\" alt=\"\" \/><\/figure><\/div><h2 id=\"afterword\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Afterword\"><\/span>Afterword<span class=\"ez-toc-section-end\"><\/span><\/h2><div id=\"6e8b\"><p>This same pipeline can be used for photoshoots of any kind and not just products. Although this was a basic pipeline to help readers understand a general overview, there are various ways we can improve the quality much more, such as \u2014 instead of directly using a ControlNet, we should fine-tune different LoRA adapters for different kinds of scenes we want to generate and use those adapters on top of the ControlNet. <\/p><p>Along with that various clarity upscaler models also exist, which can make the generated images look even better.<\/p><\/div><p class=\"ah-callout wp-block-paragraph\" id=\"5969\">The whole pipeline is quite GPU intensive, so a note of that needs to be taken. Users can purchase cloud GPU instances or use various Replicate models published for the same tasks in case a physical GPU is absent.<\/p><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Unlock automated product photoshoots with Generative AI. Explore how SAM 2 and ControlNet generate stunning, scalable images without traditional setups.<\/p>\n","protected":false},"author":30,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_gspb_post_css":".gspb_container-id-gsbp-f10688-1,.gspb_container-id-gsbp-f10688-2,.gspb_container-id-gsbp-f10688-3,.gspb_container-id-gsbp-f10688-4{flex-direction:column;box-sizing:border-box}#gspb_container-id-gsbp-f10688-1.gspb_container>p:last-of-type,#gspb_container-id-gsbp-f10688-2.gspb_container>p:last-of-type,#gspb_container-id-gsbp-f10688-3.gspb_container>p:last-of-type,#gspb_container-id-gsbp-f10688-4.gspb_container>p:last-of-type{margin-bottom:0}#gspb_container-id-gsbp-f10688-1.gspb_container.aos-animate,#gspb_container-id-gsbp-f10688-1.gspb_container[data-gs-aos],#gspb_container-id-gsbp-f10688-2.gspb_container.aos-animate,#gspb_container-id-gsbp-f10688-2.gspb_container[data-gs-aos],#gspb_container-id-gsbp-f10688-3.gspb_container.aos-animate,#gspb_container-id-gsbp-f10688-3.gspb_container[data-gs-aos],#gspb_container-id-gsbp-f10688-4.gspb_container.aos-animate,#gspb_container-id-gsbp-f10688-4.gspb_container[data-gs-aos]{opacity:1;transform:translateZ(0)}#gspb_container-id-gsbp-f10688-1.gspb_container,#gspb_container-id-gsbp-f10688-2.gspb_container,#gspb_container-id-gsbp-f10688-3.gspb_container,#gspb_container-id-gsbp-f10688-4.gspb_container{position:relative;transition-duration:.5s;transition-timing-function:var(--gs-root-animation-easing, cubic-bezier(0.42, 0, 0.58, 1));opacity:var(--gs-root-animation-opacity, 0);transition-property:opacity,transform,filter;box-sizing:border-box}","footnotes":""},"categories":[1,103],"tags":[1224,1225,1226,1223],"republish_tag":[],"class_list":["post-10688","post","type-post","status-publish","format-standard","hentry","category-advertising","category-machine-learning","tag-controlnet","tag-end-to-end-product-photoshoot","tag-generative-ai","tag-sam-2"],"_links":{"self":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts\/10688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/users\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/comments?post=10688"}],"version-history":[{"count":2,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts\/10688\/revisions"}],"predecessor-version":[{"id":14254,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/posts\/10688\/revisions\/14254"}],"wp:attachment":[{"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/media?parent=10688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/categories?post=10688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/tags?post=10688"},{"taxonomy":"republish_tag","embeddable":true,"href":"https:\/\/www.aihello.com\/resources\/wp-json\/wp\/v2\/republish_tag?post=10688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}