先提前安装相应模块
如:
pip install pyserial //python2
pip3 install pyserial //python3
import cv2 as cv
import numpy as np
import math
import time
import serial
capture = cv.VideoCapture(0)
# video = "http://admin:admin@10.242.200.134:8081/" # admin是账号:admin是密码 后面是局域网
# capture = cv.VideoCapture(video)
# 获得欧几里距离
def _get_eucledian_distance(vect1, vect2):
distant = vect1[0] - vect2[0]
dist = np.sqrt(np.sum(np.square(distant)))
# 或者用numpy内建方法
# vect1 = list(vect1)
# vect2 = list(vect2)
# dist = np.linalg.norm(vect1 - vect2)
return dist
def gesture_recognition():
while True:
ret, frame = capture.read() # 读取摄像头
# frame = cv.flip(frame, 1)
fgbg = cv.createBackgroundSubtractorMOG2() # 利用BackgroundSubtractorMOG2算法消除背景
# fgmask = bgModel.apply(frame)
fgmask = fgbg.apply(frame)
# kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
# res = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
kernel = np.ones((5, 5), np.uint8)
fgmask = cv.erode(fgmask, kernel, iterations=1) # 膨胀
res = cv.bitwise_and(frame, frame, mask=fgmask)
ycrcb = cv.cvtColor(res, cv.COLOR_BGR2YCrCb) # 分解为YUV图像,得到CR分量
(_, cr, _) = cv.split(ycrcb)
cr1 = cv.GaussianBlur(cr, (5, 5<