Python+OpenCV實(shí)戰(zhàn)之拖拽虛擬方塊的實(shí)現(xiàn)
一、項(xiàng)目效果
學(xué)校宿舍今天搬家,累麻了,突然發(fā)現(xiàn)展示處理的也很粗糙,就這樣吧嘿嘿~~~
二、核心流程
1、openCV讀取視頻流、在每一幀圖片上畫一個(gè)矩形。
2、使用mediapipe獲取手指關(guān)鍵點(diǎn)坐標(biāo)。
3、根據(jù)手指坐標(biāo)位置和矩形的坐標(biāo)位置,判斷手指點(diǎn)是否在矩形上,如果在則矩形跟隨手指移動(dòng)。
三、代碼流程
環(huán)境準(zhǔn)備:
python: 3.8.8
opencv: 4.2.0.32
mediapipe: 0.8.10.1
注:
1、opencv版本過高或過低可能出現(xiàn)一些如攝像頭打不開、閃退等問題,python版本影響opencv可選擇的版本。
2、pip install mediapipe 后可能導(dǎo)致openCV無法正常使用,卸了重新下載,習(xí)慣了就好。
1. 讀取攝像頭視頻,畫矩形
import cv2 import time import numpy as np # 調(diào)用攝像頭 0 默認(rèn)攝像頭 cap = cv2.VideoCapture(0) # 初始方塊數(shù)據(jù) x = 100 y = 100 w = 100 h = 100 # 讀取一幀幀照片 while True: # 返回frame圖片 rec,frame = cap.read() # 鏡像 frame = cv2.flip(frame,1) # 畫矩形 cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1) # 顯示畫面 cv2.imshow('frame',frame) # 退出條件 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
這是很基礎(chǔ)的一步操作,此時(shí)我們運(yùn)行這段代碼,攝像頭打開,我們會(huì)驚訝地看到自己英俊的臉龐,且左上角有個(gè)100*100的紫色矩形。
2. 導(dǎo)入mediapipe處理手指坐標(biāo)
pip install mediapipe
此時(shí)可能出現(xiàn)一些問題,比如openCV突然用不了了,沒關(guān)系,卸載了重新下。
mediapipe詳細(xì)信息:Hands - mediapipe (google.github.io)
簡(jiǎn)單來說,它會(huì)返回給我們21個(gè)手指關(guān)鍵點(diǎn)的坐標(biāo),即它在視頻畫面的位置比例( 0~1 ),我們乘以對(duì)應(yīng)畫面的寬高,就能得到手指對(duì)應(yīng)的坐標(biāo)了。
本次用到食指和中指指尖,也就是8號(hào)和12號(hào)。
2.1 配置一些基礎(chǔ)信息
import cv2 import time import numpy as np import mediapipe as mp mp_drawing = mp.solutions.drawing_utils mp_drawing_styles = mp.solutions.drawing_styles mp_hands = mp.solutions.hands hands = mp_hands.Hands( static_image_mode=True, max_num_hands=2, min_detection_confidence=0.5)
2.2 在處理每一幀圖像時(shí),加入
frame.flags.writeable = False frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 返回結(jié)果 results = hands.process(frame) frame.flags.writeable = True frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
當(dāng)我們?cè)谝曨l流中讀取每一幀圖片時(shí),將其從BGR轉(zhuǎn)為RGB供給mediapipe生成的hands對(duì)象讀取,它會(huì)返回這張圖片中手指關(guān)鍵點(diǎn)的信息,我們只需要繼續(xù)對(duì)其作畫,畫在每一幀圖片上。
# 如果結(jié)果不為空 if results.multi_hand_landmarks: # 遍歷雙手(根據(jù)讀取順序,一只只手遍歷、畫畫) for hand_landmarks in results.multi_hand_landmarks: mp_drawing.draw_landmarks( frame, hand_landmarks, mp_hands.HAND_CONNECTIONS, mp_drawing_styles.get_default_hand_landmarks_style(), mp_drawing_styles.get_default_hand_connections_style())
2.3 至此步驟完整代碼
import cv2 import time import numpy as np import mediapipe as mp mp_drawing = mp.solutions.drawing_utils mp_drawing_styles = mp.solutions.drawing_styles mp_hands = mp.solutions.hands hands = mp_hands.Hands( static_image_mode=True, max_num_hands=2, min_detection_confidence=0.5) # 調(diào)用攝像頭 0 默認(rèn)攝像頭 cap = cv2.VideoCapture(0) # 方塊初始數(shù)組 x = 100 y = 100 w = 100 h = 100 # 讀取一幀幀照片 while True: # 返回frame圖片 rec,frame = cap.read() # 鏡像 frame = cv2.flip(frame,1) frame.flags.writeable = False frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 返回結(jié)果 results = hands.process(frame) frame.flags.writeable = True frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 如果結(jié)果不為空 if results.multi_hand_landmarks: # 遍歷雙手(根據(jù)讀取順序,一只只手遍歷、畫畫) # results.multi_hand_landmarks n雙手 # hand_landmarks 每只手上21個(gè)點(diǎn)信息 for hand_landmarks in results.multi_hand_landmarks: mp_drawing.draw_landmarks( frame, hand_landmarks, mp_hands.HAND_CONNECTIONS, mp_drawing_styles.get_default_hand_landmarks_style(), mp_drawing_styles.get_default_hand_connections_style()) # 畫矩形 cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1) # 顯示畫面 cv2.imshow('frame',frame) # 退出條件 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
此時(shí)我們運(yùn)行看一下還挺有意思的:
3. 位置計(jì)算
我們這個(gè)實(shí)驗(yàn)要求拖動(dòng)方塊,那肯定也有不拖動(dòng)的時(shí)候,因此不妨根據(jù)上一步獲取食指(8)和中指(12)指尖的位置,如果這倆離得近,我們就在他與方塊重合的時(shí)候,根據(jù)手指的位置改變方塊的坐標(biāo)。
完整代碼
import cv2 import time import math import numpy as np import mediapipe as mp # mediapipe配置 mp_drawing = mp.solutions.drawing_utils mp_drawing_styles = mp.solutions.drawing_styles mp_hands = mp.solutions.hands hands = mp_hands.Hands( static_image_mode=True, max_num_hands=2, min_detection_confidence=0.5) # 調(diào)用攝像頭 0 默認(rèn)攝像頭 cap = cv2.VideoCapture(0) # cv2.namedWindow("frame", 0) # cv2.resizeWindow("frame", 960, 640) # 獲取畫面寬度、高度 width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 方塊初始數(shù)組 x = 100 y = 100 w = 100 h = 100 L1 = 0 L2 = 0 on_square = False square_color = (0, 255, 0) # 讀取一幀幀照片 while True: # 返回frame圖片 rec,frame = cap.read() # 鏡像 frame = cv2.flip(frame,1) frame.flags.writeable = False frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 返回結(jié)果 results = hands.process(frame) frame.flags.writeable = True frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 如果結(jié)果不為空 if results.multi_hand_landmarks: # 遍歷雙手(根據(jù)讀取順序,一只只手遍歷、畫畫) # results.multi_hand_landmarks n雙手 # hand_landmarks 每只手上21個(gè)點(diǎn)信息 for hand_landmarks in results.multi_hand_landmarks: mp_drawing.draw_landmarks( frame, hand_landmarks, mp_hands.HAND_CONNECTIONS, mp_drawing_styles.get_default_hand_landmarks_style(), mp_drawing_styles.get_default_hand_connections_style()) # 記錄手指每個(gè)點(diǎn)的x y 坐標(biāo) x_list = [] y_list = [] for landmark in hand_landmarks.landmark: x_list.append(landmark.x) y_list.append(landmark.y) # 獲取食指指尖 index_finger_x, index_finger_y = int(x_list[8] * width),int(y_list[8] * height) # 獲取中指 middle_finger_x,middle_finger_y = int(x_list[12] * width), int(y_list[12] * height) # 計(jì)算兩指尖距離 finger_distance = math.hypot((middle_finger_x - index_finger_x), (middle_finger_y - index_finger_y)) # 如果雙指合并(兩之間距離近) if finger_distance < 60: # X坐標(biāo)范圍 Y坐標(biāo)范圍 if (index_finger_x > x and index_finger_x < (x + w)) and ( index_finger_y > y and index_finger_y < (y + h)): if on_square == False: L1 = index_finger_x - x L2 = index_finger_y - y square_color = (255, 0, 255) on_square = True else: # 雙指不合并/分開 on_square = False square_color = (0, 255, 0) # 更新坐標(biāo) if on_square: x = index_finger_x - L1 y = index_finger_y - L2 # 圖像融合 使方塊不遮擋視頻圖片 overlay = frame.copy() cv2.rectangle(frame, (x, y), (x + w, y + h), square_color, -1) frame = cv2.addWeighted(overlay, 0.5, frame, 1 - 0.5, 0) # 顯示畫面 cv2.imshow('frame',frame) # 退出條件 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
到此這篇關(guān)于Python+OpenCV實(shí)戰(zhàn)之拖拽虛擬方塊的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python OpenCV拖拽虛擬方塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 獲取當(dāng)前目錄下的文件目錄和文件名實(shí)例代碼詳解
這篇文章主要介紹了python 獲取當(dāng)前目錄下的文件目錄和文件名實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03機(jī)器學(xué)習(xí)10大經(jīng)典算法詳解
這篇文章主要為大家詳細(xì)介紹了機(jī)器學(xué)習(xí)10大經(jīng)典算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Python利用matplotlib實(shí)現(xiàn)動(dòng)態(tài)可視化詳解
Python中的數(shù)據(jù)可視化是指原始數(shù)據(jù)的圖形表示,以更好地可視化、理解和推理,Python提供了各種庫(kù),包含用于可視化數(shù)據(jù)的不同特性,下面我們就來看看如何利用matplotlib實(shí)現(xiàn)動(dòng)態(tài)可視化吧2023-08-08一篇文章搞懂Python程序流程控制結(jié)構(gòu)
這篇文章主要給大家介紹了關(guān)于Python程序流程控制結(jié)構(gòu)的相關(guān)資料,本節(jié)學(xué)習(xí)了Python程序的控制結(jié)構(gòu)之順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09python遍歷 truple list dictionary的幾種方法總結(jié)
下面小編就為大家?guī)硪黄猵ython遍歷 truple list dictionary的幾種方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09Python PyQt5實(shí)戰(zhàn)項(xiàng)目之網(wǎng)速監(jiān)控器的實(shí)現(xiàn)
PyQt5以一套Python模塊的形式來實(shí)現(xiàn)功能。它包含了超過620個(gè)類,600個(gè)方法和函數(shù)。它是一個(gè)多平臺(tái)的工具套件,它可以運(yùn)行在所有的主流操作系統(tǒng)中,包含Unix,Windows和Mac OS。PyQt5采用雙重許可模式。開發(fā)者可以在GPL和社區(qū)授權(quán)之間選擇2021-11-11使用python將時(shí)間轉(zhuǎn)換為指定的格式方法
今天小編就為大家分享一篇使用python將時(shí)間轉(zhuǎn)換為指定的格式方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11