您的位置 首页 > 数码极客

〈rstart〉restart歌词

使用OpenCV进行手部移动

为了执行视频追踪,算法分析顺序视频帧并输出帧之间的目标移动。有各种各样的算法,每个算法都有优点和缺点。考虑到预期用途在选择使用哪种算法时很重要。视觉跟踪系统有两个主要组件:目标表示和本地化,以及过滤和数据关联。

视频跟踪是使用摄像头随着时间的推移定位移动对象(或多个对象)的过程。它有多种用途,其中一些是:人机交互,安全和监视,视频通信和压缩,增强现实,流量控制,医学成像和视频编辑。

这是您需要重现它的所有Python代码:

import numpy as np

import cv2

import argparse

from collections import deque

cap=cv2.VideoCapture(0)

pts = deque(maxlen=64)

Lower_green = np.array([110,50,50])

Upper_green = np.array([130,255,255])

while True:

ret, img=cap.read()

hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

kernel=np.ones((5,5),np.uint8)

mask=cv2.inRange(hsv,Lower_green,Upper_green)

mask = cv2.erode(mask, kernel, iterations=2)

mask=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernel)

#mask=cv2.morphologyEx(mask,cv2.MORPH_CLOSE,kernel)

mask = cv2.dilate(mask, kernel, iterations=1)

res=cv2.bitwise_and(img,img,mask=mask)

cnts,heir=cv2.findContour(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2:]

center = None

if len(cnts) > 0:

c = max(cnts, key=cv2.contourArea)

((x, y), radius) = cv2.minEnclosingCircle(c)

M = cv2.moments(c)

center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

if radius > 5:

cv2.circle(img, (int(x), int(y)), int(radius),(0, 255, 255), 2)

cv2.circle(img, center, 5, (0, 0, 255), -1)

(center)

for i in xrange (1,len(pts)):

if pts[i-1]is None or pts[i] is None:

continue

thick = in(len(pts) / float(i + 1)) * 2.5)

cv2.line(img, pts[i-1],pts[i],(0,0,225),thick)

cv2.imshow("Frame", img)

cv2.imshow("mask",mask)

cv2.imshow("res",res)

k=cv2.waitKey(30) & 0xFF

if k==32:

break

# cleanup the camera and close any open windows

cap.release()

cv2.destroyAllWindows()

54行代码。很简单!你需要在你的电脑上安装OpenCV。

嗜睡检测OpenCV

依赖

  • CV2

  • immutils

  • DLIB

  • SciPy

算法

每只眼睛由6个(x,y)坐标表示,从眼睛的左角开始(就好像你在看人一样),然后在眼睛周围顺时针工作。

条件

它会检查20个连续帧,如果眼睛纵横比小于0.25,则会生成警报。

关系

综合起来

Python代码:

from import distance

from imutils import face_utils

import imutils

import dlib

import cv2

def eye_aspect_ratio(eye):

A = di(eye[1], eye[5])

B = di(eye[2], eye[4])

C = di(eye[0], eye[3])

ear = (A + B) / * C)

return ear

thresh = 0.25

frame_check = 20

detect = dlib.get_frontal_face_detector()

predict = dlib.shape_predictor("C:\Users\akshaybahadur21\Documents\GitHub\Drowsiness_Detection|\;)# Dat file is the crux of the code

(lStart, lEnd) = ["left_eye"]

(rStart, rEnd) = ["right_eye"]

cap=cv2.VideoCapture(0)

flag=0

while True:

ret, frame=cap.read()

frame = imu(frame, width=450)

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

subjects = detect(gray, 0)

for subject in subjects:

shape = predict(gray, subject)

shape = (shape)#converting to NumPy Array

leftEye = shape[lStart:lEnd]

rightEye = shape[rStart:rEnd]

leftEAR = eye_aspect_ratio(leftEye)

rightEAR = eye_aspect_ratio(rightEye)

ear = (leftEAR + rightEAR) / 2.0

leftEyeHull = cv2.convexHull(leftEye)

rightEyeHull = cv2.convexHull(rightEye)

cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)

cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)

if ear < thresh:

flag += 1

print (flag)

if flag >= frame_check:

cv2.putText(frame, "****************ALERT!****************", (10, 30),

cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

cv2.putText(frame, "****************ALERT!****************", (10,325),

cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

#print ("Drowsy")

else:

flag = 0

cv2.imshow("Frame", frame)

key = cv2.waitKey(1) & 0xFF

if key == ord("q"):

break

cv2.destroyAllWindows()

cap.stop()

使用Softmax回归的数字识别

此代码可帮助您使用softmax回归对不同的数字进行分类。您可以安装Conda for python,它解决了机器学习的所有依赖关系。

描述

Softmax回归(同义词:多项Logistic,最大熵分类器,或者仅仅是多级Logistic回归)是逻辑回归的泛化,我们可以用于多类分类(假设类是互斥的)。相反,我们在二元分类任务中使用(标准)Logistic回归模型。

Python实现

所使用的数据集是MNIST,其图像大小为28×28,并且这里的计划是使用Logistic回归,浅网络和深度神经网络将数字从0分类到9。

这里最好的部分之一是他使用Numpy编码了三个模型,包括优化,前向传播和后向传播以及一切。

对于Logistic回归:

import numpy as np

import ma as plt

def softmax(z):

z -= np.max(z)

sm = (z).T / np.sum(z), axis=1))

return sm

def initialize(dim1, dim2):

"""

:param dim: size of vector w initilazied with zeros

:return:

"""

w = np.zeros(shape=(dim1, dim2))

b = np.zeros(shape=(10, 1))

return w, b

def propagate(w, b, X, Y):

"""

:param w: weights for w

:param b: bias

:param X: size of data(no of features, no of examples)

:param Y: true label

:return:

"""

m = X.shape[1] # getting no of rows

# Forward Prop

A = softmax, X) + b).T)

cost = (-1 / m) * np.sum(Y * np.log(A))

# backwar prop

dW = (1 / m) * np.dot(X, (A - Y).T)

db = (1 / m) * np.sum(A - Y)

cost = np.squeeze(cost)

grads = {"dw": dw,

"db": db}

return grads, cost

def optimize(w, b, X, Y, num_iters, alpha, print_cost=False):

"""

:param w: weights for w

:param b: bias

:param X: size of data(no of features, no of examples)

:param Y: true label

:param num_iters: number of iterations for gradient

:param alpha:

:return:

"""

costs = []

for i in range(num_iters):

grads, cost = propagate(w, b, X, Y)

dw = grads["dw"]

db = grads["db"]

w = w - alpha * dw

b = b - alpha * db

# Record the costs

if i % 50 == 0:

co(cost)

# Print the cost every 100 training examples

if print_cost and i % 50 == 0:

print("Cost after iteration %i: %f" % (i, cost))

params = {"w": w,

"b": b}

grads = {"dw": dw,

"db": db}

return params, grads, costs

def predict(w, b, X):

"""

:param w:

:param b:

:param X:

:return:

"""

# m = X.shape[1]

# y_pred = np.zeros(shape=(1, m))

# w = w.reshape[0], 1)

y_pred = np.argmax(softmax, X) + b).T), axis=0)

return y_pred

def model(X_train, Y_train, Y,X_test,Y_test, num_iters, alpha, print_cost):

"""

:param X_train:

:param Y_train:

:param X_test:

:param Y_test:

:param num_iterations:

:param learning_rate:

:param print_cost:

:return:

"""

w, b = initialize[0], Y_[0])

parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iters, alpha, print_cost)

w = parameters["w"]

b = parameters["b"]

y_prediction_train = predict(w, b, X_train)

y_prediction_test = predict(w, b, X_test)

print("Train accuracy: {} %", sum(y_prediction_train == Y) / (float(len(Y))) * 100)

print("Test accuracy: {} %", sum(y_prediction_test == Y_test) / (float(len(Y_test))) * 100)

d = {"costs": costs,

"Y_prediction_test": y_prediction_test,

"Y_prediction_train": y_prediction_train,

"w": w,

"b": b,

"learning_rate": alpha,

"num_iterations": num_iters}

# Plot learning curve (with costs)

#costs = np.squeeze(d['costs'])

#(costs)

#('cost')

#('iterations (per hundreds)')

#("Learning rate =" + str(d["learning_rate"]))

#()

#()

#()

#pri, y_prediction_test)

return d

def pri(X_test, y_prediction_test):

example = X_test[2, :]

print("Prediction for the example is ", y_prediction_test[2])

(example, [28, 28]))

()

()

对于神经网络:

import numpy as np

import ma as plt

def softmax(z):

z -= np.max(z)

sm = (z).T / np.sum(z),axis=1))

return sm

def layers(X, Y):

"""

:param X:

:param Y:

:return:

"""

n_x = X.shape[0]

n_y = Y.shape[0]

return n_x, n_y

def initialize_nn(n_x, n_h, n_y):

"""

:param n_x:

:param n_h:

:param n_y:

:return:

"""

np.random.seed(2)

W1 = np.random.randn(n_h, n_x) * 0.01

b1 = np.random.rand(n_h, 1)

W2 = np.random.rand(n_y, n_h)

b2 = np.random.rand(n_y, 1)

parameters = {"W1": W1,

"b1": b1,

"W2": W2,

"b2": b2}

return parameters

def forward_prop(X, parameters):

W1 = parameters['W1']

b1 = parameters['b1']

W2 = parameters['W2']

b2 = parameters['b2']

Z1 = np.dot(W1, X) + b1

A1 = np.tanh(Z1)

Z2 = np.dot(W2, A1) + b2

A2 = softmax)

cache = {"Z1": Z1,

"A1": A1,

"Z2": Z2,

"A2": A2}

return A2, cache

def compute_cost(A2, Y, parameters):

m = Y.shape[1]

W1 = parameters['W1']

W2 = parameters['W2']

logprobs = np.multiply(A2), Y)

cost = - np.sum(logprobs) / m

cost = np.squeeze(cost)

return cost

def back_prop(parameters, cache, X, Y):

m = Y.shape[1]

W1 = parameters['W1']

W2 = parameters['W2']

A1 = cache['A1']

A2 = cache['A2']

dZ2 = A2 - Y

dW2 = (1 / m) * np.dot(dZ2, A1.T)

db2 = (1 / m) * np.sum(dZ2, axis=1, keepdims=True)

dZ1 = np.multiply, dZ2), 1 - np.square(A1))

dW1 = (1 / m) * np.dot(dZ1, X.T)

db1 = (1 / m) * np.sum(dZ1, axis=1, keepdims=True)

grads = {"dW1": dW1,

"db1": db1,

"dW2": dW2,

"db2": db2}

return grads

def update_params(parameters, grads, alpha):

W1 = parameters['W1']

b1 = parameters['b1']

W2 = parameters['W2']

b2 = parameters['b2']

dW1 = grads['dW1']

db1 = grads['db1']

dW2 = grads['dW2']

db2 = grads['db2']

W1 = W1 - alpha * dW1

b1 = b1 - alpha * db1

W2 = W2 - alpha * dW2

b2 = b2 - alpha * db2

parameters = {"W1": W1,

"b1": b1,

"W2": W2,

"b2": b2}

return parameters

def model_nn(X, Y,Y_real,test_x,test_y, n_h, num_iters, alpha, print_cost):

np.random.seed(3)

n_x,n_y = layers(X, Y)

parameters = initialize_nn(n_x, n_h, n_y)

W1 = parameters['W1']

b1 = parameters['b1']

W2 = parameters['W2']

b2 = parameters['b2']

costs = []

for i in range(0, num_iters):

A2, cache = forward_prop(X, parameters)

cost = compute_cost(A2, Y, parameters)

grads = back_prop(parameters, cache, X, Y)

if (i > 1500):

alpha1 = 0.95*alpha

parameters = update_params(parameters, grads, alpha1)

else:

parameters = update_params(parameters, grads, alpha)

if i % 100 == 0:

co(cost)

if print_cost and i % 100 == 0:

print("Cost after iteration for %i: %f" % (i, cost))

predictions = predict_nn(parameters, X)

print("Train accuracy: {} %", sum(predictions == Y_real) / (float(len(Y_real))) * 100)

predictions=predict_nn(parameters,test_x)

print("Train accuracy: {} %", sum(predictions == test_y) / (float(len(test_y))) * 100)

#(costs)

#('cost')

#('iterations (per hundreds)')

#("Learning rate =" + str(alpha))

#()

return parameters

def predict_nn(parameters, X):

A2, cache = forward_prop(X, parameters)

predictions = np.argmax(A2, axis=0)

return predictions

最后是深度神经网络:

import numpy as np

import ma as plt

def softmax(z):

cache = z

z -= np.max(z)

sm = (z).T / np.sum(z), axis=1))

return sm, cache

def relu(z):

"""

:param z:

:return:

"""

s = np.maximum(0, z)

cache = z

return s, cache

def softmax_backward(dA, cache):

"""

:param dA:

:param activation_cache:

:return:

"""

z = cache

z -= np.max(z)

s = (z).T / np.sum(z), axis=1))

dZ = dA * s * (1 - s)

return dZ

def relu_backward(dA, cache):

"""

:param dA:

:param activation_cache:

:return:

"""

Z = cache

dZ = np.array(dA, copy=True) # just converting dz to a correct object.

dZ[Z <= 0] = 0

return dZ

def initialize_parameters_deep(dims):

"""

:param dims:

:return:

"""

np.random.seed(3)

params = {}

L = len(dims)

for l in range(1, L):

params['W' + str(l)] = np.random.randn(dims[l], dims[l - 1]) * 0.01

params['b' + str(l)] = np.zeros((dims[l], 1))

return params

def linear_forward(A, W, b):

"""

:param A:

:param W:

:param b:

:return:

"""

Z = np.dot(W, A) + b

cache = (A, W, b)

return Z, cache

def linear_activation_forward(A_prev, W, b, activation):

"""

:param A_prev:

:param W:

:param b:

:param activation:

:return:

"""

if activation == "softmax":

Z, linear_cache = linear_forward(A_prev, W, b)

A, activation_cache = softmax)

elif activation == "relu":

Z, linear_cache = linear_forward(A_prev, W, b)

A, activation_cache = relu(Z)

cache = (linear_cache, activation_cache)

return A, cache

def L_model_forward(X, params):

"""

:param X:

:param params:

:return:

"""

caches = []

A = X

L = len(params) // 2 # number of layers in the neural network

# Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.

for l in range(1, L):

A_prev = A

A, cache = linear_activation_forward(A_prev,

params["W" + str(l)],

params["b" + str(l)],

activation='relu')

cac(cache)

A_last, cache = linear_activation_forward(A,

params["W" + str(L)],

params["b" + str(L)],

activation='softmax')

cac(cache)

return A_last, caches

def compute_cost(A_last, Y):

"""

:param A_last:

:param Y:

:return:

"""

m = Y.shape[1]

cost = (-1 / m) * np.sum(Y * np.log(A_last))

cost = np.squeeze(cost) # To make sure your cost's shape is what we expect . this turns [[17]] into 17).

return cost

def linear_backward(dZ, cache):

"""

:param dZ:

:param cache:

:return:

"""

A_prev, W, b = cache

m = A_[1]

dW = (1. / m) * np.dot(dZ, cache[0].T)

db = (1. / m) * np.sum(dZ, axis=1, keepdims=True)

dA_prev = np.dot(cache[1].T, dZ)

return dA_prev, dW, db

def linear_activation_backward(dA, cache, activation):

"""

:param dA:

:param cache:

:param activation:

:return:

"""

linear_cache, activation_cache = cache

if activation == "relu":

dZ = relu_backward(dA, activation_cache)

dA_prev, dW, db = linear_backward(dZ, linear_cache)

elif activation == "softmax":

dZ = softmax_backward(dA, activation_cache)

dA_prev, dW, db = linear_backward(dZ, linear_cache)

return dA_prev, dW, db

def L_model_backward(A_last, Y, caches):

"""

:param A_last:

:param Y:

:param caches:

:return:

"""

grads = {}

L = len(caches) # the number of layers

m = A_la[1]

Y = Y.reshape(A_la) # after this line, Y is the same shape as A_last

dA_last = - (Y, A_last) - np.divide(1 - Y, 1 - A_last))

current_cache = caches[-1]

grads["dA" + str(L)], grads["dW" + str(L)], grads["db" + str(L)] = linear_activation_backward(dA_last,

current_cache,

activation="softmax")

for l in reversed(range(L - 1)):

current_cache = caches[l]

dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(l + 2)], current_cache,

activation="relu")

grads["dA" + str(l + 1)] = dA_prev_temp

grads["dW" + str(l + 1)] = dW_temp

grads["db" + str(l + 1)] = db_temp

return grads

def update_params(params, grads, alpha):

"""

:param params:

:param grads:

:param alpha:

:return:

"""

L = len(params) // 2 # number of layers in the neural network

for l in range(L):

params["W" + str(l + 1)] = params["W" + str(l + 1)] - alpha * grads["dW" + str(l + 1)]

params["b" + str(l + 1)] = params["b" + str(l + 1)] - alpha * grads["db" + str(l + 1)]

return params

def model_DL( X, Y, Y_real, test_x, test_y, layers_dims, alpha, num_iterations, print_cost): # lr was 0.009

"""

Implements a L-layer neural network: [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID.

Arguments:

X -- data, numpy array of shape (number of examples, num_px * num_px * 3)

Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples)

layers_dims -- list containing the input size and each layer size, of length (number of layers + 1).

alpha -- learning rate of the gradient descent update rule

num_iterations -- number of iterations of the optimization loop

print_cost -- if True, it prints the cost every 100 steps

Returns:

params -- params learnt by the model. They can then be used to predict.

"""

np.random.seed(1)

costs = [] # keep track of cost

params = initialize_parameters_deep(layers_dims)

for i in range(0, num_iterations):

A_last, caches = L_model_forward(X, params)

cost = compute_cost(A_last, Y)

grads = L_model_backward(A_last, Y, caches)

if (i > 800 and i<1700):

alpha1 = 0.80 * alpha

params = update_params(params, grads, alpha1)

elif(i>=1700):

alpha1 = 0.50 * alpha

params = update_params(params, grads, alpha1)

else:

params = update_params(params, grads, alpha)

if print_cost and i % 100 == 0:

print("Cost after iteration %i: %f" % (i, cost))

if print_cost and i % 100 == 0:

co(cost)

predictions = predict(params, X)

print("Train accuracy: {} %", sum(predictions == Y_real) / (float(len(Y_real))) * 100)

predictions = predict(params, test_x)

print("Test accuracy: {} %", sum(predictions == test_y) / (float(len(test_y))) * 100)

#(costs))

#('cost')

#('iterations (per tens)')

#("Learning rate =" + str(alpha))

#()

return params

def predict(parameters, X):

A_last, cache = L_model_forward(X, parameters)

predictions = np.argmax(A_last, axis=0)

return predictions

执行通过网络摄像头写入

要运行代码,请键入 python Dig-Rec.py

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“rstart,restart歌词,restart翻译中文,restart,start”边界阅读