欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?OpenCV實(shí)現(xiàn)姿態(tài)識(shí)別的詳細(xì)代碼

 更新時(shí)間:2022年02月23日 11:39:33   作者:SlowFeather  
這篇文章主要介紹了Python?OpenCV實(shí)現(xiàn)姿態(tài)識(shí)別的方法,本文通過截圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

想要使用攝像頭實(shí)現(xiàn)一個(gè)多人姿態(tài)識(shí)別

環(huán)境安裝

下載并安裝 Anaconda

官網(wǎng)連接 https://anaconda.cloud/installers

安裝 Jupyter Notebook

檢查Jupyter Notebook是否安裝

Tip:這里涉及到一個(gè)切換Jupyter Notebook內(nèi)核的問題,在我這篇文章中有提到
AnacondaNavigator Jupyter Notebook更換Python內(nèi)核http://www.dbjr.com.cn/article/238496.htm

生成Jupyter Notebook項(xiàng)目目錄

打開Anaconda Prompt切換到項(xiàng)目目錄

輸入Jupyter notebook在瀏覽器中打開 Jupyter Notebook

并創(chuàng)建新的記事本

下載訓(xùn)練庫(kù)

圖片以及訓(xùn)練庫(kù)都在下方鏈接
https://github.com/quanhua92/human-pose-estimation-opencv
將圖片和訓(xùn)練好的模型放到項(xiàng)目路徑中
graph_opt.pb為訓(xùn)練好的模型

單張圖片識(shí)別

導(dǎo)入庫(kù)

import cv2 as cv
import os
import matplotlib.pyplot as plt

加載訓(xùn)練模型

net=cv.dnn.readNetFromTensorflow("graph_opt.pb")

初始化

inWidth=368
inHeight=368
thr=0.2

BODY_PARTS = { "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
               "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
               "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
               "LEye": 15, "REar": 16, "LEar": 17, "Background": 18 }

POSE_PAIRS = [ ["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
               ["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
               ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
               ["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
               ["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"] ]

載入圖片

img = cv.imread("image.jpg")

顯示圖片

plt.imshow(img)

調(diào)整圖片顏色

plt.imshow(cv.cvtColor(img,cv.COLOR_BGR2RGB))

姿態(tài)識(shí)別

def pose_estimation(frame):
    frameWidth=frame.shape[1]
    frameHeight=frame.shape[0]
    net.setInput(cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (127.5, 127.5, 127.5), swapRB=True, crop=False))
    out = net.forward()
    out = out[:, :19, :, :]  # MobileNet output [1, 57, -1, -1], we only need the first 19 elements
    
    assert(len(BODY_PARTS) == out.shape[1])
    points = []
    for i in range(len(BODY_PARTS)):
        # Slice heatmap of corresponging body's part.
        heatMap = out[0, i, :, :]

        # Originally, we try to find all the local maximums. To simplify a sample
        # we just find a global one. However only a single pose at the same time
        # could be detected this way.
        _, conf, _, point = cv.minMaxLoc(heatMap)
        x = (frameWidth * point[0]) / out.shape[3]
        y = (frameHeight * point[1]) / out.shape[2]
        # Add a point if it's confidence is higher than threshold.
        points.append((int(x), int(y)) if conf > thr else None)
        
    for pair in POSE_PAIRS:
        partFrom = pair[0]
        partTo = pair[1]
        assert(partFrom in BODY_PARTS)
        assert(partTo in BODY_PARTS)
        idFrom = BODY_PARTS[partFrom]
        idTo = BODY_PARTS[partTo]
		# 繪制線條
        if points[idFrom] and points[idTo]:
            cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
            cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
            cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
            
    t, _ = net.getPerfProfile()
    freq = cv.getTickFrequency() / 1000
    cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
    return frame
# 處理圖片
estimated_image=pose_estimation(img)
# 顯示圖片
plt.imshow(cv.cvtColor(estimated_image,cv.COLOR_BGR2RGB))

視頻識(shí)別

Tip:與上面圖片識(shí)別代碼是銜接的

視頻來自互聯(lián)網(wǎng),侵刪

cap = cv.VideoCapture('testvideo.mp4')
cap.set(3,800)
cap.set(4,800)
if not cap.isOpened():
    cap=cv.VideoCapture(0)
    raise IOError("Cannot open vide")
    
while cv.waitKey(1) < 0:
    hasFrame,frame=cap.read()
    if not hasFrame:
        cv.waitKey()
        break
        
    frameWidth=frame.shape[1]
    frameHeight=frame.shape[0]
    net.setInput(cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (127.5, 127.5, 127.5), swapRB=True, crop=False))
    out = net.forward()
    out = out[:, :19, :, :]  # MobileNet output [1, 57, -1, -1], we only need the first 19 elements
    assert(len(BODY_PARTS) == out.shape[1])
    points = []
    for i in range(len(BODY_PARTS)):
        # Slice heatmap of corresponging body's part.
        heatMap = out[0, i, :, :]
        # Originally, we try to find all the local maximums. To simplify a sample
        # we just find a global one. However only a single pose at the same time
        # could be detected this way.
        _, conf, _, point = cv.minMaxLoc(heatMap)
        x = (frameWidth * point[0]) / out.shape[3]
        y = (frameHeight * point[1]) / out.shape[2]
        # Add a point if it's confidence is higher than threshold.
        points.append((int(x), int(y)) if conf > thr else None)
    for pair in POSE_PAIRS:
        partFrom = pair[0]
        partTo = pair[1]
        assert(partFrom in BODY_PARTS)
        assert(partTo in BODY_PARTS)
        idFrom = BODY_PARTS[partFrom]
        idTo = BODY_PARTS[partTo]
        if points[idFrom] and points[idTo]:
            cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
            cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
            cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
            
    t, _ = net.getPerfProfile()
    freq = cv.getTickFrequency() / 1000
    cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
    cv.imshow('Video Tutorial',frame)

實(shí)時(shí)攝像頭識(shí)別

Tip:與上面圖片識(shí)別代碼是銜接的

cap = cv.VideoCapture(0)
cap.set(cv.CAP_PROP_FPS,10)
cap.set(3,800)
cap.set(4,800)
if not cap.isOpened():
    cap=cv.VideoCapture(0)
    raise IOError("Cannot open vide")
    
while cv.waitKey(1) < 0:
    hasFrame,frame=cap.read()
    if not hasFrame:
        cv.waitKey()
        break
        
    frameWidth=frame.shape[1]
    frameHeight=frame.shape[0]
    net.setInput(cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (127.5, 127.5, 127.5), swapRB=True, crop=False))
    out = net.forward()
    out = out[:, :19, :, :]  # MobileNet output [1, 57, -1, -1], we only need the first 19 elements
    assert(len(BODY_PARTS) == out.shape[1])
    points = []
    for i in range(len(BODY_PARTS)):
        # Slice heatmap of corresponging body's part.
        heatMap = out[0, i, :, :]
        # Originally, we try to find all the local maximums. To simplify a sample
        # we just find a global one. However only a single pose at the same time
        # could be detected this way.
        _, conf, _, point = cv.minMaxLoc(heatMap)
        x = (frameWidth * point[0]) / out.shape[3]
        y = (frameHeight * point[1]) / out.shape[2]
        # Add a point if it's confidence is higher than threshold.
        points.append((int(x), int(y)) if conf > thr else None)
    for pair in POSE_PAIRS:
        partFrom = pair[0]
        partTo = pair[1]
        assert(partFrom in BODY_PARTS)
        assert(partTo in BODY_PARTS)
        idFrom = BODY_PARTS[partFrom]
        idTo = BODY_PARTS[partTo]
        if points[idFrom] and points[idTo]:
            cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
            cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
            cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
            
    t, _ = net.getPerfProfile()
    freq = cv.getTickFrequency() / 1000
    cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
    cv.imshow('Video Tutorial',frame)

參考

DeepLearning_by_PhDScholar
Human Pose Estimation using opencv | python | OpenPose | stepwise implementation for beginners
https://www.youtube.com/watch?v=9jQGsUidKHs

到此這篇關(guān)于Python OpenCV實(shí)現(xiàn)姿態(tài)識(shí)別的文章就介紹到這了,更多相關(guān)Python姿態(tài)識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中的文件和目錄操作實(shí)現(xiàn)代碼

    Python中的文件和目錄操作實(shí)現(xiàn)代碼

    對(duì)于文件和目錄的處理,雖然可以通過操作系統(tǒng)命令來完成,但是Python語言為了便于開發(fā)人員以編程的方式處理相關(guān)工作,提供了許多處理文件和目錄的內(nèi)置函數(shù)。重要的是,這些函數(shù)無論是在Unix、Windows還是Macintosh平臺(tái)上,它們的使用方式是完全一致的。
    2011-03-03
  • Python查看已安裝包的版本號(hào)的多種方法

    Python查看已安裝包的版本號(hào)的多種方法

    很多朋友一直使用pip list來查詢,但如果想知道單個(gè),應(yīng)該怎么使用呢,在Python中,可以使用多種方法來查看已安裝包的版本號(hào),本文給大家詳細(xì)介紹了Python查看已安裝包的版本號(hào)的多種方法,需要的朋友可以參考下
    2024-02-02
  • Numpy 多維數(shù)據(jù)數(shù)組的實(shí)現(xiàn)

    Numpy 多維數(shù)據(jù)數(shù)組的實(shí)現(xiàn)

    這篇文章主要介紹了Numpy 多維數(shù)據(jù)數(shù)組的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Python中l(wèi)oguru日志庫(kù)的使用

    Python中l(wèi)oguru日志庫(kù)的使用

    本文主要介紹了Python中l(wèi)oguru日志庫(kù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python使用PIL.image保存圖片

    Python使用PIL.image保存圖片

    PIL庫(kù)支持圖像存儲(chǔ)、顯示和處理,它能夠處理幾乎所有圖片格式,可以完成對(duì)圖像的縮放、剪裁、疊加以及向圖像添加線條、圖像和文字等操作,下面這篇文章主要給大家介紹了關(guān)于Python使用PIL.image保存圖片的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • 圖文詳解python安裝Scrapy框架步驟

    圖文詳解python安裝Scrapy框架步驟

    在本篇內(nèi)容中我們給大家整理了關(guān)于python安裝Scrapy框架的圖文詳細(xì)步驟,需要的朋友們跟著學(xué)習(xí)下。
    2019-05-05
  • python實(shí)現(xiàn)防截圖的6種方法詳解

    python實(shí)現(xiàn)防截圖的6種方法詳解

    防截圖是指一組技術(shù)或方法,用于防止他人在未經(jīng)允許的情況下在屏幕上截取或記錄圖像,這是一個(gè)重要的安全措施,它可以防止竊取敏感信息或監(jiān)視個(gè)人信息,本文為大家整理了6種python可以防截圖的方法,需要的可以參考下
    2023-10-10
  • Python寫的英文字符大小寫轉(zhuǎn)換代碼示例

    Python寫的英文字符大小寫轉(zhuǎn)換代碼示例

    這篇文章主要介紹了Python寫的英文字符大小寫轉(zhuǎn)換代碼示例,本文例子相對(duì)簡(jiǎn)單,本文直接給出代碼實(shí)例,需要的朋友可以參考下
    2015-03-03
  • 簡(jiǎn)單分析python的類變量、實(shí)例變量

    簡(jiǎn)單分析python的類變量、實(shí)例變量

    在本篇文章中小編給大家整理的是關(guān)于python類變量、實(shí)例變量的知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-08-08
  • Python OS模塊實(shí)例詳解

    Python OS模塊實(shí)例詳解

    這篇文章主要介紹了Python OS模塊,結(jié)合實(shí)例形式總結(jié)分析了Python使用OS解析文件路徑、判斷文件、目錄等相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04

最新評(píng)論