成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用 YOLO11 分割和高斯模糊創建人像效果

開發 后端 人工智能
本文通過結合最新的 YOLO11 實例分割模型和高斯模糊,為你的圖片應用人像效果。

分割和高斯模糊后的圖像

本文通過結合最新的YOLO11實例分割模型和高斯模糊,為你的圖片應用人像效果。我們將使用YOLO11將人物從背景中分割出來,并對除了主體之外的所有內容應用模糊效果。

1. 安裝Ultralytics庫

首先創建并激活一個Python虛擬環境來管理依賴項。如果你不熟悉虛擬環境,請查看這個教程:

激活虛擬環境后,我們需要安裝ultralytics庫,這將允許我們使用YOLO11實例分割模型。運行以下命令在你的環境里安裝庫:

pip install ultralytics

2. 下載測試圖片

接下來,讓我們從Unsplash下載一張測試圖片進行測試,你可以使用任何你選擇的圖片。我為我們的測試目的選擇了以下圖片:

在.py文件中,添加以下代碼來下載和加載圖片:

import urllib.request
import cv2

# Download the image
url, filename = ("https://images.unsplash.com/photo-1634646493821-9fca74f85f59?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mzk0fHx1bmJsdXJyZWQlMjBwb3J0YWl0fGVufDB8fDB8fHww", "scene.jpg")
urllib.request.urlretrieve(url, filename)

# Load the input image using OpenCV
image = cv2.imread(filename)

3. 生成分割掩碼

圖片加載后,下一步是創建一個分割掩碼,以識別圖片中的人物。有關使用YOLO11實例分割模型識別人物的更詳細教程,請查看這個教程:《YOLO11 實例分割模型做行人分割

模型將檢測人物,我們將創建一個掩碼以將主體與背景隔離。我們將使用yolo11n-seg.pt模型,但你可以使用Ultralytics YOLO11文檔中的任何你喜歡的模型。以下是加載模型并生成掩碼的代碼:

import urllib.request
import cv2
from ultralytics import YOLO
import numpy as np

def segment_image(image, model):
    # Predict with the model
    results = model(filename)  # predict on an image 

    # Create an empty mask for segmentation
    segmentation_mask = np.zeros_like(image, dtype=np.uint8)
    
    # Iterate over the results
    for i, r in enumerate(results):
        # Iterate through the detected masks
        for j, mask in enumerate(r.masks.xy):
            # Convert the class tensor to an integer
            class_id = int(r.boxes.cls[j].item())  # Extract the class ID as an integer
            
            # Check if the detected class corresponds to 'person' (class ID 0)
            if class_id == 0:
                # Convert mask coordinates to an integer format for drawing
                mask = np.array(mask, dtype=np.int32)
                
                # Fill the segmentation mask with color (e.g., white for people)
                cv2.fillPoly(segmentation_mask, [mask], (255, 255, 255))
    
    return segmentation_mask

# Download the image
url, filename = ("https://images.unsplash.com/photo-1634646493821-9fca74f85f59?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mzk0fHx1bmJsdXJyZWQlMjBwb3J0YWl0fGVufDB8fDB8fHww", "scene.jpg")
urllib.request.urlretrieve(url, filename)

# Load the input image using OpenCV
image = cv2.imread(filename)

# Load the model
model = YOLO("yolo11n-seg.pt")  # load an official YOLO model
 
# Generate the segmentation mask   
segmentation_mask = segment_image(image, model)

# Visualize the segmentation mask before combining it with the original image
cv2.imwrite("mask.jpg", segmentation_mask)

這一步將生成一個二進制掩碼,其中人物被突出顯示,如下例所示:

圖像二進制分割掩碼

4. 使用掩碼對圖像應用高斯模糊

現在我們有了分割掩碼,我們可以在保持人物清晰的同時對背景應用高斯模糊。我們將模糊整個圖像,然后使用掩碼將清晰的人物區域與模糊的背景結合起來。以下是分割和應用模糊的所有代碼:

import urllib.request
import cv2
from ultralytics import YOLO
import numpy as np

def segment_image(image, model):
    # Predict with the model
    results = model(filename)  # predict on an image 

    # Create an empty mask for segmentation
    segmentation_mask = np.zeros_like(image, dtype=np.uint8)
    
    # Iterate over the results
    for i, r in enumerate(results):
        # Iterate through the detected masks
        for j, mask in enumerate(r.masks.xy):
            # Convert the class tensor to an integer
            class_id = int(r.boxes.cls[j].item())  # Extract the class ID as an integer
            
            # Check if the detected class corresponds to 'person' (class ID 0)
            if class_id == 0:
                # Convert mask coordinates to an integer format for drawing
                mask = np.array(mask, dtype=np.int32)
                
                # Fill the segmentation mask with color (e.g., white for people)
                cv2.fillPoly(segmentation_mask, [mask], (255, 255, 255))
    
    return segmentation_mask

def apply_blur_using_mask(image, mask, blur_strength=(25, 25)):
    # Apply Gaussian blur to the entire image
    blurred_image = cv2.GaussianBlur(image, blur_strength, 0)

    # Create an inverted mask where the background is white and the person is black
    inverted_mask = cv2.bitwise_not(mask)

    # Use the mask to keep the person sharp and blur the background
    background_blur = cv2.bitwise_and(blurred_image, blurred_image, mask=inverted_mask[:, :, 0])
    person_region = cv2.bitwise_and(image, image, mask=mask[:, :, 0])

    # Combine the sharp person region with the blurred background
    final_image = cv2.add(person_region, background_blur)
    
    return final_image

# Download the image
url, filename = ("https://images.unsplash.com/photo-1634646493821-9fca74f85f59?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mzk0fHx1bmJsdXJyZWQlMjBwb3J0YWl0fGVufDB8fDB8fHww", "scene.jpg")
urllib.request.urlretrieve(url, filename)

# Load the input image using OpenCV
image = cv2.imread(filename)

# Load the model
model = YOLO("yolo11n-seg.pt")  # load an official YOLO model
 
# Generate the segmentation mask   
segmentation_mask = segment_image(image, model)

# Call the function to apply the blur and save the result
final_image = apply_blur_using_mask(image, segmentation_mask)

# Visualize the segmentation mask before combining it with the original image
cv2.imwrite("mask.jpg", segmentation_mask)

# Save the result
cv2.imwrite("blurred_image.jpg", final_image)

# Optionally display the image (make sure you're running in a GUI environment)
cv2.imshow("Blurred Image Result", final_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

最終結果

這段代碼將清晰的人物與模糊的背景結合起來,為你的圖像提供專業的人像效果。分割掩碼確保人物保持聚焦,而背景則通過高斯模糊變柔和。

示例結果

完整代碼:https://github.com/Brianhulela/background_blur

責任編輯:趙寧寧 來源: 小白玩轉Python
相關推薦

2024-10-10 14:56:39

2024-12-30 08:00:00

YOLO11AI智能

2024-11-20 16:06:20

2024-10-15 10:47:12

2017-01-17 16:45:35

githubinstagramandroid

2024-10-28 16:12:26

2016-08-30 21:36:56

JavascriptCSSWeb

2024-10-07 11:12:55

2024-10-30 16:34:56

2023-04-18 10:47:32

2017-04-13 10:03:29

Java高斯模糊圖像

2024-11-27 10:27:56

2022-01-10 09:00:00

人工智能數據機器

2012-11-15 09:43:08

開發算法高斯模糊

2022-01-02 07:11:19

Windows 11操作系統微軟

2014-04-02 10:29:12

iOS 7模糊效果

2024-09-12 17:19:43

YOLO目標檢測深度學習

2013-11-18 15:50:17

2016-09-18 23:56:51

Java開源中文分詞器

2023-09-26 21:53:27

Java圖像處理
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 岛国毛片| av午夜电影 | 大学生a级毛片免费视频 | 黑人性hd | 成年人的视频免费观看 | 欧美久久久久久 | 天天人人精品 | 日韩中文字幕在线不卡 | 久久久国产一区二区三区 | 丝袜 亚洲 另类 欧美 综合 | .国产精品成人自产拍在线观看6 | av网站在线播放 | 欧美91| 一区二区福利视频 | 欧美精三区欧美精三区 | 91精品国产91久久久久久 | 在线观看中文字幕一区二区 | 另类a v| 狠狠操婷婷 | 男女精品久久 | 久久国产婷婷国产香蕉 | 一级全黄视频 | 91视视频在线观看入口直接观看 | 久久999 | 日韩精品 | 成人av观看 | www免费视频 | 久久久久久一区 | 波多野结衣一区二区三区在线观看 | 免费三级av | 久久久久国产一区二区三区不卡 | 国产精品一区二 | 国产一区在线看 | 91久久国产综合久久 | 99精品久久久国产一区二区三 | 免费爱爱视频 | 一区二区三区网站 | 欧美一区二区小视频 | 99精品欧美一区二区三区综合在线 | 亚洲精品乱码久久久久久蜜桃91 | 午夜小视频在线播放 |