本文將介紹基于米爾電子MYD-LT527開(kāi)發(fā)板(米爾基于全志T527開(kāi)發(fā)板)的OpenCV手勢識別方案測試。
摘自?xún)?yōu)秀創(chuàng )作者-小火苗
米爾基于全志T527開(kāi)發(fā)板
一、軟件環(huán)境安裝 1.安裝OpenCV - sudo apt-get install libopencv-dev python3-opencv
復制代碼
2.安裝pip - sudo apt-get install python3-pip
復制代碼
二、OpenCV手勢識別步驟 1.圖像獲取:從攝像頭或其他圖像源獲取手部圖像。使用OpenCV的VideoCapture類(lèi)可以捕獲視頻流,或者使用imread函數加載圖像。 2.圖像預處理:對圖像進(jìn)行預處理,以提高特征提取的準確性。常用的預處理操作包括灰度化、濾波、邊緣檢測、二值化、噪聲去除和形態(tài)學(xué)處理等。3.特征提取:從預處理后的圖像中提取手部特征。常用的特征包括形狀特征、紋理特征和運動(dòng)軌跡特征等。4.分類(lèi)和識別:使用機器學(xué)習算法對提取的特征進(jìn)行分類(lèi),以識別特定的手勢。
三、代碼實(shí)現 - # -*- coding: utf-8 -*-
- import cv2
- def reg(x):
- o1 = cv2.imread('paper.jpg',1)
- o2 = cv2.imread('rock.jpg',1)
- o3 = cv2.imread('scissors.jpg',1)
- gray1 = cv2.cvtColor(o1,cv2.COLOR_BGR2GRAY)
- gray2 = cv2.cvtColor(o2,cv2.COLOR_BGR2GRAY)
- gray3 = cv2.cvtColor(o3,cv2.COLOR_BGR2GRAY)
- xgray = cv2.cvtColor(x,cv2.COLOR_BGR2GRAY)
- ret, binary1 = cv2.threshold(gray1,127,255,cv2.THRESH_BINARY)
- ret, binary2 = cv2.threshold(gray2,127,255,cv2.THRESH_BINARY)
- ret, binary3 = cv2.threshold(gray3,127,255,cv2.THRESH_BINARY)
- xret, xbinary = cv2.threshold(xgray,127,255,cv2.THRESH_BINARY)
- contours1, hierarchy = cv2.findContours(binary1,
- cv2.RETR_LIST,
- cv2.CHAIN_APPROX_SIMPLE)
- contours2, hierarchy = cv2.findContours(binary2,
- cv2.RETR_LIST,
- cv2.CHAIN_APPROX_SIMPLE)
- contours3, hierarchy = cv2.findContours(binary3,
- cv2.RETR_LIST,
- cv2.CHAIN_APPROX_SIMPLE)
- xcontours, hierarchy = cv2.findContours(xbinary,
- cv2.RETR_LIST,
- cv2.CHAIN_APPROX_SIMPLE)
- cnt1 = contours1[0]
- cnt2 = contours2[0]
- cnt3 = contours3[0]
- x = xcontours[0]
- ret=[]
- ret.append(cv2.matchShapes(x,cnt1,1,0.0))
- ret.append(cv2.matchShapes(x,cnt2,1,0.0))
- ret.append(cv2.matchShapes(x,cnt3,1,0.0))
- max_index = ret.index(min(ret)) #計算最大值索引
- if max_index==0:
- r="paper"
- elif max_index==1:
- r="rock"
- else:
- r="sessiors"
- return r
- t1=cv2.imread('test1.jpg',1)
- t2=cv2.imread('test2.jpg',1)
- t3=cv2.imread('test3.jpg',1)
- # print(reg(t1))
- # print(reg(t2))
- # print(reg(t3))
- # ===========顯示處理結果==================
- org=(0,60)
- font = cv2.FONT_HERSHEY_SIMPLEX
- fontScale=2
- color=(255,255,255)
- thickness=3
- cv2.putText(t1,reg(t1),org,font,fontScale,color,thickness)
- cv2.putText(t2,reg(t2),org,font,fontScale,color,thickness)
- cv2.putText(t3,reg(t3),org,font,fontScale,color,thickness)
- cv2.imshow('test1',t1)
- cv2.imshow('test2',t2)
- cv2.imshow('test3',t3)
- cv2.waitKey()
- cv2.destroyAllWindows()
復制代碼
四、實(shí)踐 1.程序運行
2、原始圖像包含訓練圖像
3.識別結果 識別到了 剪刀 石頭 布 原始圖片
|