0%

OpenCV(十二)-机器学习

image-20241124162624834

image-20241124162803287

一、Haar人脸识别方法

image-20241124162833967

image-20241124162856952

image-20241124162945362

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cv2
import numpy as np

# 第一步、创建级联器
faser = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')

# 第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('p3.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 第三步,进行人脸识别
# [[x,y,w,h]]
faces = faser.detectMultiScale(gray, 1.1, 5)

for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 0, 255), 2)

cv2.imshow('img', img)
cv2.waitKey()

image-20241124164125359

二、Haar识别眼鼻口

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
import numpy as np

#第一步,创建Haar级联器
facer = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')
eye = cv2.CascadeClassifier('./haarcascades/haarcascade_eye.xml')
mouth = cv2.CascadeClassifier('./haarcascades/haarcascade_mcs_mouth.xml')
nose = cv2.CascadeClassifier('./haarcascades/haarcascade_mcs_nose.xml')

#第二步,导入人脸识别的图片并将其灰度化
img = cv2.imread('./p3.png')

#第三步,进行人脸识别
#[[x,y,w,h]]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#检测出的人脸上再检测眼睛
faces = facer.detectMultiScale(gray, 1.1, 3)
i = 0
j = 0
for (x,y,w,h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
roi_img = img[y:y+h, x:x+w]
eyes = eye.detectMultiScale(roi_img, 1.1, 3)
for (x,y,w,h) in eyes:
cv2.rectangle(roi_img, (x, y), (x+w, y+h), (0, 255, 0), 2)
roi_eye=roi_img[y:y+h, x:x+w]
eyename = 'eye' + str(j)
j = j+1
cv2.imshow(eyename, roi_eye)

i = i+1
winname = 'face' + str(i)
cv2.imshow(winname, roi_img)


# mouths = mouth.detectMultiScale(gray, 1.1, 3)
# for (x,y,w,h) in mouths:
# cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

# noses = nose.detectMultiScale(gray, 1.1, 3)
# for (x,y,w,h) in noses:
# cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 0), 2)

cv2.imshow('img', img)

cv2.waitKey()

image-20241124171623655

三、Haar+Tesseract进行车牌识别

image-20241124171449117

image-20241124171528468

image-20241124171730134

四、深度学习基础知识

image-20241124172710040

image-20241124172720603

image-20241124172731228

image-20241124172740490

image-20241124172809677

image-20241124172824072

image-20241124172938312

image-20241124173013402

image-20241124173041757

image-20241124173145330

image-20241124173213308

image-20241124173306912

image-20241124173343995

.

五、dnn实现图像分类

image-20241124173559520

image-20241124173617944

image-20241124173709770

image-20241124173809213

image-20241124173828190

image-20241124173847091

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

import numpy as np
import argparse
import time
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-p", "--prototxt", required=True,
help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-l", "--labels", required=True,
help="path to ImageNet labels (i.e., syn-sets)")
args = vars(ap.parse_args())

# load the input image from disk
image = cv2.imread(args["image"])
# load the class labels from disk
# rows = open(args["labels"]).read().strip().split("\n")
# classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]

# our CNN requires fixed spatial dimensions for our input image(s)
# so we need to ensure it is resized to 224x224 pixels while
# performing mean subtraction (104, 117, 123) to normalize the input;
# after executing this command our "blob" now has the shape:
# (1, 3, 224, 224)
blob = cv2.dnn.blobFromImage(image, 1, (224, 224), (104, 117, 123))

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

# set the blob as input to the network and perform a forward-pass to
# obtain our output classification
net.setInput(blob)
start = time.time()
preds = net.forward()
end = time.time()
print("[INFO] classification took {:.5} seconds".format(end - start))

# sort the indexes of the probabilities in descending order (higher
# probabilitiy first) and grab the top-5 predictions
idxs = np.argsort(preds[0])[::-1][:5]

# loop over the top-5 predictions and display them
for (i, idx) in enumerate(idxs):
# draw the top prediction on the input image
if i == 0:
text = "Label: {}, {:.2f}%".format(classes[idx],
preds[0][idx] * 100)
cv2.putText(image, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 0, 255), 2)
# display the predicted label + associated probability to the
# console
print("[INFO] {}. label: {}, probability: {:.5}".format(i + 1,
classes[idx], preds[0][idx]))
# display the output image
cv2.imshow("Image", image)
cv2.waitKey(0)

image-20241124174737791

-------------本文结束感谢您的阅读-------------