人工智能

人工智能

强化学习

强化学习是一种通过与环境交互来学习如何采取行动以最大化某种累积奖励的机器学习方法。在这个过程中,智能体(Agent)通过尝试不同的行为并根据所接收的反馈(奖励或惩罚)来优化其策略。强化学习的核心在于探索(Exploration)与利用(Exploitation)的平衡:既要尝试新的行为以发现可能更好的策略,又要充分利用已知的最优行为来获得最大奖励。

深度学习

深度学习是一种人工神经网络(特别是具有多个隐藏层的神经网络)的学习方法,它能够自动地从数据中学习并提取特征,进而完成分类、识别、生成等一系列复杂的任务。深度学习的关键技术包括卷积神经网络(CNN)、循环神经网络(RNN)、长短期记忆网络(LSTM)等,这些技术在图像识别、自然语言处理等领域取得了显著的成功。

OpenCV使用

读取展示img

1
2
3
4
5
6
import cv2 as cv

img = cv.imread('../resource/face.png')
cv.imshow('read_img', img)
cv.waitKey(0)
cv.destroyAllWindows()

转为灰度图片

1
2
gray_img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.imshow("gray_img",gray_img)

改变尺寸

1
2
re_img =cv.resize(img, (300, 300))
cv.imshow('resize_img', re_img)

画图

1
2
3
x, y, w, h = 100, 100, 100, 100;
cv.rectangle(re_img, (x, y), (x + w, y + h), (0, 0, 255), 2) # center坐标和weight和height,颜色,厚度
cv.circle(re_img, (x + w // 2, y + h // 2), 50, (0, 255, 0), 2)# center坐标和半径,颜色,厚度

信息录入

未命名文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from mysqlUtil import Connector
import yaml
import cv2 as cv


class Information_Entry:
name = ''
num = 1
con = None

def __init__(self):
self.num = 1
self.con = Connector()

@staticmethod
def increment_id_in_yaml():
# 假设YAML文件名为data.yaml,且其中有一个id字段
yaml_file = '../application.yml'
# 读取YAML文件
with open(yaml_file, 'r') as file:
data = yaml.safe_load(file)

# 检查id是否存在并增加它,如果不存在则初始化为1
if 'id' in data:
data['id'] += 1
else:
data['id'] = 1

# 将更新后的内容写回YAML文件
with open(yaml_file, 'w') as file:
yaml.dump(data, file)
return data['id'] - 1

@staticmethod
def face_exist(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# 3、将图片转换为Numpy数组,并将人脸画圈
face_detect = cv.CascadeClassifier("D:/app/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml")
faces = face_detect.detectMultiScale(gray, 1.02, 5)
for x, y, w, h in faces:
return True
return False

def video_Capture(self, capture):
capture = cv.VideoCapture(0)
while capture.isOpened():
flag, frame = capture.read()
cv.imshow("test", frame)
k = cv.waitKey(1) & 0xff
if k == ord('s'):
# 加一个人脸检测的屏障,如果没有则跳过
if self.face_exist(frame):
# num += 1
num = Information_Entry.increment_id_in_yaml()
# cv.imwrite("../record/" + str(num) + name + ".jpg", frame)
cv.imencode(".jpg", frame)[1].tofile("../record/" + str(num) + "." + name + ".jpg")
print(str(num) + name + ".jpg保存成功")
self.con.insertUser(num, name)
else:
print("没有检测到人脸!")
elif k == ord('q'):
break
capture.release()
cv.destroyAllWindows()

def picture_Entry(self, image):
# num += 1
num = Information_Entry.increment_id_in_yaml()
# cv.imwrite("../record/" + str(num) + name + ".jpg", frame)
cv.imencode(".jpg", image)[1].tofile("../record/" + str(num) + "." + name + ".jpg")
print(str(num) + name + ".jpg保存成功")
self.con.insertUser(num, name)
cv.destroyAllWindows()


if __name__ == '__main__':
name = input("请输入名字(英文):")
cap = cv.VideoCapture(0)
Information_Entry().video_Capture(cap)
# Information_Entry().picture_Entry(cv.imread('../resource/fanfan_test3.jpg'))

图片识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import cv2 as cv


class Picture_Recognition:

@staticmethod
def face_detect_demo():
re_img = cv.resize(img, (0, 0), fx=0.25, fy=0.25)
# re_img = img
gray = cv.cvtColor(re_img, cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier("D:/app/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml")
faces = face_detect.detectMultiScale(gray, 1.01, 5)
for x, y, w, h in faces:
cv.rectangle(re_img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv.imshow('result', re_img)
cv.waitKey(0)
cv.destroyAllWindows()


if __name__ == '__main__':
# img = cv.imread('../resource/face1.jpeg')
# img = cv.imread('../record/1.小达.jpg')
img = cv.imread('../resource/xiaoda_test.jpg')
Picture_Recognition.face_detect_demo()

训练数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import cv2 as cv
import numpy as np
from PIL import Image


class Face_Trainer:
@staticmethod
def getImageAndLabel(path):
# face出问题的,需要安装opencv-contrib-python,且版本为3.4.2.16
face_detect = cv.face.LBPHFaceRecognizer.create()
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
face_list = []
label_list = []
face_detector = cv.CascadeClassifier('D:/app/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
for imagePath in imagePaths:
# label = int(os.path.split(imagePath)[1].split('.')[0])
label = int(os.path.split(imagePath)[1].split('.')[0])
# label = np.array(label)
print(label)
image = Image.open(imagePath).convert('L')
# img = cv.imread(imagePath)
# image = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img_numpy = np.array(image, 'uint8')
faces = face_detector.detectMultiScale(img_numpy)
# 预防无面容的照片
for (x, y, w, h) in faces:
# img_numpy[y:y + h, x:x + w] 使用numpy数组切片语法,从原始图像中提取出这个矩形区域的数据。
face_list.append(img_numpy[y:y + h, x:x + w])
label_list.append(label)
print("face_list", face_list)
# 第一个参数image为[np.array(多维矩阵)],第二个参数为np.array[]
face_detect.train(face_list, np.array(label_list))
face_detect.save('../face_detect.yml')
print('训练完成')
return face_list, label_list


if __name__ == '__main__':
path = 'E:/pythonProject/openCV/record/'
image, label = Face_Trainer.getImageAndLabel(path)


人脸识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import cv2 as cv
from mysqlUtil import Connector

import numpy as np

recognizer = cv.face.LBPHFaceRecognizer.create()
recognizer.read('../face_detect.yml')
con = Connector()


class Face_Recognition:

def face_detect_demo(self,image):
# 2、将图片转换为灰度图
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# 3、将图片转换为Numpy数组,并将人脸画圈
face_detect = cv.CascadeClassifier("D:/app/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml")
faces = face_detect.detectMultiScale(gray, 1.02, 5)
img_numpy = np.array(gray, 'uint8')
for x, y, w, h in faces:
cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
id, confidence = recognizer.predict(img_numpy[y:y + h, x:x + w])
print(id, confidence)
if confidence < 100:
print("用户id为:", id)
print("用户置信度为:", confidence)
name = con.getUserNameById(id)
print("用户名:", name)
cv.putText(image, name, (x + 10, y - 10), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else:
print("unknown")
cv.putText(image, 'unknown', (x + 10, y - 10), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv.imshow('image', image)


if __name__ == '__main__':
# 取出图片进行人脸识别
# 1、采集图片
# image = cv.imread('../resource/xiaoda.jpg')
image = cv.imread('../resource/fanfan_test.jpg')
# image = cv.imread('../resource/xiaoda_test.jpg')
# image = cv.imread('../resource/we.jpg')
# image = cv.imread('../record/5.daqinhai.jpg')
image = cv.resize(image, (0, 0), fx=0.25, fy=0.25)
Face_Recognition().face_detect_demo(image)
cv.waitKey(0)

视频识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import cv2 as cv
from 人脸识别 import Face_Recognition


def face_detect_demo(img):
re_img = cv.resize(img, (300, 300))
gray = cv.cvtColor(re_img, cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier("D:/app/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml")
faces = face_detect.detectMultiScale(gray, 1.02, 5)
for x, y, w, h in faces:
cv.rectangle(re_img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv.imshow('result', re_img)


if __name__ == '__main__':
cap = cv.VideoCapture(0)
while True:
flag, frame = cap.read()
if flag:
# face_detect_demo(frame)
Face_Recognition().face_detect_demo(frame)
c = cv.waitKey(50)
if c == 27:
break
cv.destroyAllWindows()
cap.release()

网络视频

电视节目rtmp推流地址搜集 - kiter - 博客园 (cnblogs.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CaptureVideo:
def net_video(self):
cap = cv2.VideoCapture('rtmp://liteavapp.qcloud.com/live/liteavdemoplayerstreamid')
while cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break


if __name__ == '__main__':
cv = CaptureVideo()
cv.net_video()

python基础

Python3 元组_w3cschool

列表

能保存不同类型数据,并能用下标访问,可以更改元素

1
2
list1 = ['Google', 'W3Cschool', 1997, 2000]
list1[2] = "1998" #直接对想要修改的数据项赋上一个新值即可修改该数据项,将指针改变

元组

能保存不同类型数据,并能用下标访问,不能更改元素

1
tup1 = ('Google', 'W3CSchool', 1997, 2020)

数组

同类型

1
list1 = [1996, 1999, 1997, 2000]

NumPy 数组相比 Python 的内置列表,在数学和科学计算中提供了更高的效率和更多的功能,比如矢量化运算、广播等。

所以一般使用numpy将列表构建为数组是为了更好的数学运算

1
2
3
4
5
6
7
8
9
10
import numpy as np

# 从列表创建 numpy 数组
my_array = np.array([1, 2, 3])

# 从列表的列表创建多维数组
multi_dim_array = np.array([[1, 2, 3], [4, 5, 6]])

# 指定数据类型创建数组
array_with_dtype = np.array([1.7, 3.4], dtype=np.int32)