最近在和同学做一个简单的猫脸识别项目,我们希望达到的最终目的是可以区分不同的猫猫。
打了三百多张数据集喂给yolov8,出来了一个能用的模型。模型跑出来大概就是下面这样
图1.可爱的猫猫被模型打了满脸标记
效果还是不错的。在yolov8中使用模型对图片进行预测的相关代码如下:
# 测试图片
from ultralytics import YOLO
import cv2
import numpy as np
import sys
# 读取命令行参数
weight_path = 'G:\\little_cat\\ultralytics\\best1.pt'
media_path = "G:\\little_cat\\ultralytics\\data\\images\\train\\Cats_Test496.png"
# 加载模型
model = YOLO(weight_path)
# 获取类别
objs_labels = model.names # get class labels
print(objs_labels)
# 类别的颜色
class_color = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),(255, 0, 0), (0, 255, 0)]
# 关键点的顺序
class_list = ["cat"]
# 关键点的颜色
keypoint_color = [(255, 0, 0), (0, 255, 0),(255, 0, 0), (0, 255, 0),(255, 0, 0), (0, 255, 0),(255, 0, 0)]
# 读取图片
frame = cv2.imread(media_path)
frame = cv2.resize(frame, (frame.shape[1] // 2, frame.shape[0] // 2))
# rotate
# 检测
result = list(model(frame, conf=0.5, stream=True))[0] # inference,如果stream=False,返回的是一个列表,如果stream=True,返回的是一个生成器
boxes = result.boxes # Boxes object for bbox outputs
boxes = boxes.cpu().numpy() # convert to numpy array
# 遍历每个框
for box in boxes.data:
l, t, r, b = box[:4].astype(np.int32) # left, top, right, bottom
conf, id = box[4:] # confidence, class
id = int(id)
# 绘制框
cv2.rectangle(frame, (l, t), (r, b), (0, 0, 255), 2)
# 绘制类别+置信度(格式:98.1%)
cv2.putText(frame, f"{objs_labels[id]} {conf * 100:.1f}", (l, t - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0, 0, 255), 1)
# 遍历keypoints
keypoints = result.keypoints # Keypoints object for pose outputs
keypoints = keypoints.cpu().numpy() # convert to numpy array
# draw keypoints, set first keypoint is red, second is blue
for keypoint in keypoints.data:
for i in range(len(keypoint)):
x, y ,_ = keypoint[i]
x, y = int(x), int(y)
cv2.circle(frame, (x, y), 3, (0, 255, 0), -1)
#cv2.putText(frame, f"{keypoint_list[i]}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, keypoint_color[i], 2)
# save image
cv2.imwrite("result.jpg", frame)
print("save result.jpg")
print(keypoints.data)
data_str=str(keypoints.data.tolist())
with open("temp1.txt","w") as f:
f.write(data_str)
最后,通过上述代码,可以提取到猫猫脸上点的坐标,存入temp1.txt文件里面。
我的主要工作就是对这些点进行一些特征提取,有两种思路:
1.对点连成的线条之间的夹角进行计算
2.对各个器官的比例进行计算
可以看到,三百张数据集训练出来的效果确实一般,使用角度进行计算会导致较大的误差,所以我们采用第二种方式。
# 打开文本文件并读取内容
import math
with open("temp1.txt", "r") as f:
data_str = f.read()
data=eval(data_str)
data=data[0]
# for i in data[0]:
# print(i)
class Features:
def __init__(self):
self.ear_face=0
self.forehead_face=0
self.eyes_face=0
self.nose_face=0
self.mouth_face=0
def get_points_middle(self,point1,point2):
return ((point1[0]+point2[0])/2,(point1[1]+point2[1])/2)
def get_points_length(self,point1,point2):
return math.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
def get_ear_length(self,data):
point1=self.get_points_middle((data[1][0],data[1][1]),(data[2][0],data[2][1]))
point2=self.get_points_middle((data[4][0],data[4][1]),(data[5][0],data[5][1]))
return self.get_points_length(point1,point2)
def get_face_length(self,data):
point1=self.get_points_middle((data[0][0],data[0][1]),(data[3][0],data[3][1]))
point2=self.get_points_middle((data[9][0],data[9][1]),(data[10][0],data[10][1]))
return self.get_points_length(point1,point2)
def get_forehead_length(self,data):
point1 = self.get_points_middle((data[1][0], data[1][1]), (data[2][0], data[2][1]))
point2 = self.get_points_middle((data[4][0], data[4][1]), (data[5][0], data[5][1]))
point3 = self.get_points_middle(point1,point2)
point4 = self.get_points_middle((data[6][0],data[6][1]),(data[7][0],data[7][1]))
return self.get_points_length(point3,point4)
def get_nose_length(self,data):
point1 = self.get_points_middle((data[6][0], data[6][1]), (data[7][0], data[7][1]))
point2=(data[8][0],data[8][1])
return self.get_points_length(point1,point2)
def get_mouth_length(self,data):
point2 = (data[8][0], data[8][1])
point1=self.get_points_middle((data[9][0], data[9][1]), (data[10][0], data[10][1]))
return self.get_points_length(point1,point2)
def get_Features(self,data):
f_ear=self.get_ear_length(data)/self.get_face_length(data)
f_forehead=self.get_forehead_length(data)/self.get_face_length(data)
f_nose=self.get_nose_length(data)/self.get_face_length(data)
f_mouth=self.get_mouth_length(data)/self.get_face_length(data)
return f_ear,f_forehead,f_nose,f_mouth
print(data)
my_loss=Features()
# my_loss.get_total_loss(data)
print(my_loss.get_face_length(data))
print(my_loss.get_Features(data))
将耳朵,额头,鼻梁,嘴的相关点进行求中心点的操作,然后分别进行各个器官在脸部的占比计算,可以粗略得到一个占比数值的列表。
(0.6925027544123905, 0.27656696682019977, 0.25617610947111147, 0.20007784862698205)
后续真正投入使用的过程中,面部器官结构可以作为一个指标对猫的品种进行评判。