国产无码免费,人妻口爆,国产V在线,99中文精品7,国产成人无码AA精品一,制度丝袜诱惑av,久久99免费麻辣视频,蜜臀久久99精品久久久久久酒店
        訂閱
        糾錯(cuò)
        加入自媒體

        使用 OpenCV 和 Python 在直播中模糊人臉

        本文將學(xué)習(xí)如何使用 OpenCV 和 Python 在直播中模糊人臉。這將是一個(gè)非常有趣的博客,讓我們開(kāi)始吧!

        我們最終結(jié)果的快照:

        第 1 步:導(dǎo)入所需的庫(kù)

        · 為圖像操作導(dǎo)入 cv2

        · 為數(shù)組操作導(dǎo)入 Numpy

        import cv2

        import numpy as np

        第 2 步:定義模糊函數(shù)

        · 這里我們定義了 Blur 函數(shù)。

        · 它需要 2 個(gè)參數(shù),圖像 img 和模糊因子 k 。

        · 然后我們通過(guò)將高度和寬度除以模糊因子來(lái)簡(jiǎn)單地計(jì)算內(nèi)核高度和內(nèi)核寬度。kw 和 kh 越小,模糊度越高。

        · 然后我們檢查 kw 和 kh 是否為奇數(shù),如果它們是偶數(shù),則減 1 以使它們?yōu)槠鏀?shù)。

        · 然后簡(jiǎn)單地我們將高斯模糊應(yīng)用于我們的圖像并返回它。

        def blur(img,k):

           h,w = img.shape[:2]

           kh,kw = h//k,w//k

           if kh%2==0:

               kh-=1

           if kw%2==0:

               kw-=1

           img = cv2.GaussianBlur(img,ksize=(kh,kw),sigmaX=0)

           return img

        第 3 步:定義 pixelate_face 函數(shù)

        · 這是一個(gè)簡(jiǎn)單地為模糊圖像添加像素化效果的函數(shù)。

        def pixelate_face(image, blocks=10):

           # divide the input image into NxN blocks

           (h, w) = image.shape[:2]

           xSteps = np.linspace(0, w, blocks + 1, dtype="int")

           ySteps = np.linspace(0, h, blocks + 1, dtype="int")

           # loop over the blocks in both the x and y direction

           for i in range(1, len(ySteps)):

               for j in range(1, len(xSteps)):

                   # compute the starting and ending (x, y)-coordinates

                   # for the current block

                   startX = xSteps[j - 1]

                   startY = ySteps[i - 1]

                   endX = xSteps[j]

                   endY = ySteps[i]

                   # extract the ROI using NumPy array slicing, compute the

                   # mean of the ROI, and then draw a rectangle with the

                   # mean RGB values over the ROI in the original image

                   roi = image[startY:endY, startX:endX]

                 (B, G, R) = [int(x) for x in cv2.mean(roi)[:3]]

                   cv2.rectangle(image, (startX, startY), (endX, endY),

                       (B, G, R), -1)

           # return the pixelated blurred image

           return image

        第 4 步:讓我們?cè)趯?shí)時(shí)提要中模糊面孔

        · 下面的代碼是代碼的主要部分。

        · 這里的 factor  定義了模糊量。

        · 定義一個(gè)級(jí)聯(lián)分類器對(duì)象 face_cascade 來(lái)檢測(cè)人臉。

        · 下載 haarcascade_frontalface_default.xml 文件

        然后簡(jiǎn)單地運(yùn)行一個(gè)無(wú)限循環(huán),從網(wǎng)絡(luò)攝像頭讀取圖像,檢測(cè)其中的人臉,然后用像素化的人臉替換該人臉部分。

        閱讀更多關(guān)于使用 HAARCASCADES 進(jìn)行面部和眼睛檢測(cè)的信息

        factor = 3    

        cap = cv2.VideoCapture(0)

        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

        while 1:

           ret,frame = cap.read()

           gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

           faces = face_cascade.detectMultiScale(gray, 1.5, 5)

           for (x,y,w,h) in faces:

               frame[y:y+h,x:x+w] = pixelate_face(blur(frame[y:y+h,x:x+w],factor))

           cv2.imshow('Live',frame)

           if cv2.waitKey(1)==27:

               break


        cap.release()

        cv2.destroyAllWindows()

        讓我們看看完整代碼

        import cv2

        import numpy as np

        def blur(img,k):

           h,w = img.shape[:2]

           kh,kw = h//k,w//k

           if kh%2==0:

               kh-=1

           if kw%2==0:

               kw-=1

           img = cv2.GaussianBlur(img,ksize=(kh,kw),sigmaX=0)

           return img
           

        def pixelate_face(image, blocks=10):

           # divide the input image into NxN blocks

           (h, w) = image.shape[:2]

           xSteps = np.linspace(0, w, blocks + 1, dtype="int")

           ySteps = np.linspace(0, h, blocks + 1, dtype="int")

           # loop over the blocks in both the x and y direction

           for i in range(1, len(ySteps)):

               for j in range(1, len(xSteps)):

                   # compute the starting and ending (x, y)-coordinates

                   # for the current block

                   startX = xSteps[j - 1]

                   startY = ySteps[i - 1]

                   endX = xSteps[j]

                   endY = ySteps[i]

                   # extract the ROI using NumPy array slicing, compute the

                   # mean of the ROI, and then draw a rectangle with the

                   # mean RGB values over the ROI in the original image

                   roi = image[startY:endY, startX:endX]

                   (B, G, R) = [int(x) for x in cv2.mean(roi)[:3]]

                   cv2.rectangle(image, (startX, startY), (endX, endY),

                       (B, G, R), -1)

           # return the pixelated blurred image

           return image

        factor = 3    

        cap = cv2.VideoCapture(0)

        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


        while 1:

           ret,frame = cap.read()

           gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
           

           faces = face_cascade.detectMultiScale(gray, 1.5, 5)

           for (x,y,w,h) in faces:

               frame[y:y+h,x:x+w] = pixelate_face(blur(frame[y:y+h,x:x+w],factor))

           cv2.imshow('Live',frame)

           if cv2.waitKey(1)==27:

               break


        cap.release()

        cv2.destroyAllWindows()

        這就是你在直播中模糊面孔的方式!

               原文標(biāo)題 : 使用 OpenCV 和 Python 在直播中模糊人臉

        聲明: 本文由入駐維科號(hào)的作者撰寫(xiě),觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wèn)題,請(qǐng)聯(lián)系舉報(bào)。

        發(fā)表評(píng)論

        0條評(píng)論,0人參與

        請(qǐng)輸入評(píng)論內(nèi)容...

        請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

        您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

        • 看不清,點(diǎn)擊換一張  刷新

        暫無(wú)評(píng)論

        暫無(wú)評(píng)論

          掃碼關(guān)注公眾號(hào)
          OFweek人工智能網(wǎng)
          獲取更多精彩內(nèi)容
          文章糾錯(cuò)
          x
          *文字標(biāo)題:
          *糾錯(cuò)內(nèi)容:
          聯(lián)系郵箱:
          *驗(yàn) 證 碼:

          粵公網(wǎng)安備 44030502002758號(hào)

          主站蜘蛛池模板: 自拍偷拍亚洲| 公安县| jizzjizz亚洲| 自拍视频啪| 人妻少妇精品视频一区二区三区| 亚洲欧洲AV| 亚洲中文av| 91福利社| 精品国产99久久久久久www| 谷城县| 第一福利在线视频| aV无码av高潮aV三区| 吴忠市| 婷婷五月深爱激情| 卫辉市| 亚州成人在线| 亚洲制服丝袜在线| 综合一区二区三区| 国产精品XXX| 制服丝袜在线亚洲| 中文字幕av在线| 丰宁| 91视频亚洲| 国产精品1区| 人妻少妇久久| 97香蕉碰碰人妻国产欧美| 中文字字幕在线中文| 成人自拍偷拍| 茄子av| 日韩无码一区二区三区| 株洲县| 国产18禁网站| 91视频免费入口| 欧美精品在线观看| 五家渠市| 永久免费看mv网站入口| 尼勒克县| 国产成人精品777777| 国产熟女掹操| 当雄县| 亚洲中文字幕av|