PYTHON表情识别常用函数

python表情识别常用函数

  • 函数read_image(image: Union[str, Path]):用于实现指定目录下的文件读取
        • 参数类型:表征图片文件路径的字符串
  • 函数draw_bounding_box(face_coordinates, image_array, color):用于绘制识别框
        • 参数类型:face_coordinates包含人脸左边坐标、顶端坐标、宽度、高度的list;图片数组image_array;颜色参数color
  • 函数draw_text(coordinates, image_array, text, color, x_offset=0, y_offset=0, font_scale=2, thickness=2):用于标注表情
        • 参数类型:face_coordinates包含人脸左边坐标、顶端坐标、宽度、高度的list;图片数组image_array;颜色参数color;标注文字与识别框的相对位置x_offset=0, y_offset=0;字体大小font_scale=2;字体粗细thickness=2
  • 函数show_image(image_array):显示图像
        • 参数类型:图片数组image_array

函数read_image(image: Union[str, Path]):用于实现指定目录下的文件读取

参数类型:表征图片文件路径的字符串

从指定路径读取图片,参数为表征图片文件路径的字符串,返回文件对象:image = Path(image)
判断该路径下的文件是否存在:if not image.exists():
报错,raise函数用于抛出自定义的异常:raise FileNotFoundError('File not found: ’ + str(image))
使用OpenCV读取图像,返回值是一个nparray 多维数组:image_ndarray = cv2.imread(str(image))
返回多维数组return image_ndarray

函数draw_bounding_box(face_coordinates, image_array, color):用于绘制识别框

参数类型:face_coordinates包含人脸左边坐标、顶端坐标、宽度、高度的list;图片数组image_array;颜色参数color

读取bounding_box的位置、形状信息:x, y, w, h = face_coordinates
在图像上绘制矩形边界框,参数image_array为图片数组,(x, y)为左上角坐标,(x + w, y + h)为长宽,color为颜色,2为线条磅数:cv2.rectangle(image_array, (x, y), (x + w, y + h), color, 2)

函数draw_text(coordinates, image_array, text, color, x_offset=0, y_offset=0, font_scale=2, thickness=2):用于标注表情

参数类型:face_coordinates包含人脸左边坐标、顶端坐标、宽度、高度的list;图片数组image_array;颜色参数color;标注文字与识别框的相对位置x_offset=0, y_offset=0;字体大小font_scale=2;字体粗细thickness=2

读取识别框基准坐标:x, y = face_coordinates[:2]
添加标注,参数image_array为图片数组,text为标注文字,(x + x_offset, y + y_offset)为标注文字与识别框的相对位置,设置字体为正常大小无衬线字体 FONT_HERSHEY_PLAINcv2.putText(image_array,text,(x + x_offset, y + y_offset),字体大小font_scale,字体颜色color,字体粗细thickness,抗钜齿线cv2.LINE_AA:font_scale=2cv2.FONT_HERSHEY_SIMPLEX,font_scale,color,thickness,cv2.LINE_AA)

函数show_image(image_array):显示图像

参数类型:图片数组image_array

调整色彩空间以便opencv处理:rgb_image = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
显示图像:pyplot.imshow(rgb_image)
关闭坐标轴显示:pyplot.axis(‘off’)
图像显示:pyplot.show()

你可能感兴趣的:(PYTHON表情识别常用函数)