当前位置: 首页 > news >正文

汤阴做网站西安网站制作价格

汤阴做网站,西安网站制作价格,东莞商城网站建设哪家公司靠谱,数字营销网站图像分类 1.导入必要的库2.指定图像和标签文件夹路径3.获取文件夹内的所有图像文件名4.获取classes.txt文件中的所有标签5.初始化一个字典来存储图片名和对应的标签6.遍历每个图片名的.txt文件7.随机选择一张图片进行展示8.构建图像的完整路径9.加载图像10.检查图像是否为空 随…

图像分类

    • 1.导入必要的库
    • 2.指定图像和标签文件夹路径
    • 3.获取文件夹内的所有图像文件名
    • 4.获取classes.txt文件中的所有标签
    • 5.初始化一个字典来存储图片名和对应的标签
    • 6.遍历每个图片名的.txt文件
    • 7.随机选择一张图片进行展示
    • 8.构建图像的完整路径
    • 9.加载图像
    • 10.检查图像是否为空

随机找100张图片,然后进行打标签,我用的labelImg打标签,存储的格式为.txt格式。
图片存储在‘561’文件夹中,标签存储在‘99’文件夹中。
把100图片的标签分别是sad,happy,amazed,anger。

1.导入必要的库

cv2: 这是OpenCV库的别名,它是一个强大的计算机视觉库,用于图像和视频处理。
matplotlib.pyplot as plt: Matplotlib是一个绘图库,pyplot是其中的一个模块,它提供了一个类似于MATLAB的绘图框架。plt是matplotlib.pyplot的常用别名。
numpy as np: NumPy是一个用于科学计算的库,它提供了高效的数组处理能力,对于图像处理等任务非常有用。
os: 这个模块提供了与操作系统交互的功能,比如文件和目录操作。
random: 这个模块提供了生成随机数的函数。

import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
import random

2.指定图像和标签文件夹路径

images_folder = 'D:/rgzn/face/DATASET/561'
labels_folder = 'D:/rgzn/face/DATASET/99'

3.获取文件夹内的所有图像文件名

os.listdir()函数用于列出指定目录下的所有文件和子目录。
os.path.isfile()函数用于检查一个路径是否指向一个文件。
os.path.join()函数用于将目录和文件名组合成一个完整的文件路径。

这行代码的目的是从一个指定的文件夹中获取所有图像文件的列表。

image_files = [f for f in os.listdir(images_folder) if os.path.isfile(os.path.join(images_folder, f))]

os.listdir(images_folder):这个函数调用返回一个列表,包含images_folder目录下的所有文件和子目录的名称。

for f in os.listdir(images_folder):这是一个循环,它遍历images_folder目录下的每个文件和子目录的名称。在每次迭代中,f变量被设置为当前文件或子目录的名称。
os.path.isfile(os.path.join(images_folder, f)):这个条件用于检查f是否是一个文件。os.path.join(images_folder, f)创建一个完整的文件路径,将images_folder目录的路径和f(文件或子目录的名称)连接起来。

[f for f in os.listdir(images_folder) if os.path.isfile(os.path.join(images_folder, f))]:这个列表推导式创建一个新的列表,只包含那些通过os.path.isfile()检查确认为文件的f值。

4.获取classes.txt文件中的所有标签

with open(os.path.join(labels_folder, 'classes.txt'), 'r') as file:labels = file.readlines()
labels = [label.strip() for label in labels]  # 去除末尾的换行符

with open(os.path.join(labels_folder, ‘classes.txt’), ‘r’) as file:

os.path.join(labels_folder, 'classes.txt'):这个函数调用用于创建一个完整的文件路径,将labels_folder目录的路径和classes.txt文件名连接起来。
with open(...) as file:这是一个上下文管理器(context manager),它用于自动处理文件资源的打开和关闭。当with语句执行完成后,文件会自动关闭,即使遇到异常也是如此。
file:这是上下文管理器创建的一个文件对象,可以用来读取文件内容。

labels = file.readlines()

file.readlines():这个方法调用用于读取文件中的所有行,并将它们作为一个字符串列表返回。每一行都是一个列表项。
labels = ...:这个赋值语句将读取到的行列表赋值给变量labels。

labels = [label.strip() for label in labels]

这是一个列表推导式(list comprehension),它遍历labels列表中的每个字符串(即文件中的每行内容)。
label.strip():这个方法调用用于去除字符串首尾的空白字符(如空格、换行符等)。
for label in labels:这个循环遍历labels列表中的每个字符串。
[...]:这个列表推导式创建一个新的列表,包含去除空白字符后的字符串。

5.初始化一个字典来存储图片名和对应的标签

image_labels = {}

6.遍历每个图片名的.txt文件

for image_file in image_files:# 构建图片名.txt文件的完整路径
image_name = os.path.splitext(image_file)[0]  # 获取不带扩展名的图片名txt_path = os.path.join(labels_folder, image_name + '.txt')# 读取图片名.txt文件的内容with open(txt_path, 'r') as file:lines = file.readlines()# 假设每行包含一个数字序列numbers = lines[0].strip().split()  # 假设每行由空格分隔# 根据数字序列的第一个数字确定标签# 根据您提供的映射关系label_index = int(numbers[0])label = labels[label_index]# 将图片名和对应的标签存储在image_labels字典中
image_labels[image_file] = label

image_name = os.path.splitext(image_file)[0]:

os.path.splitext(image_file):这个函数调用用于将image_file(一个文件名)分割成两部分:文件名和扩展名。返回的元组中的第一部分是文件名,第二部分是扩展名。
[0]:这个索引操作符用于获取元组中的第一个元素,即不带扩展名的文件名。

txt_path = os.path.join(labels_folder, image_name + ‘.txt’):

os.path.join(labels_folder, image_name + '.txt'):这个函数调用用于创建一个完整的文件路径,将labels_folder目录的路径和image_name(不带扩展名的文件名)连接起来,并在最后加上.txt扩展名。
txt_path:这个变量存储了.txt文件的完整路径。

with open(txt_path, 'r') as file::

with open(...) as file::这是上下文管理器,用于自动处理文件资源的打开和关闭。
file:这个变量是上下文管理器创建的文件对象,用于读取.txt文件的内容。

lines = file.readlines():

file.readlines():这个方法调用用于读取.txt文件中的所有行,并将它们作为一个字符串列表返回。每一行都是一个列表项。
lines:这个变量存储了.txt文件中所有行的列表。

numbers = lines[0].strip().split():

lines[0]:这个索引操作符用于获取.txt文件中第一行的内容。
.strip():这个方法调用用于去除字符串首尾的空白字符(如空格、换行符等)。
.split():这个方法调用用于根据指定的分隔符(在这个例子中是空格)将字符串分割成列表。
numbers:这个变量存储了.txt文件第一行内容去除空白字符并分割成列表后的版本。

label_index = int(numbers[0]):

int(numbers[0]):这个函数调用用于将列表numbers的第一个元素(即标签的索引)转换为整数。
label_index:这个变量存储了标签的索引。

label = labels[label_index]:

labels[label_index]:这个索引操作符用于根据label_index变量中存储的索引,从labels列表中获取对应的标签字符串。
label:这个变量存储了从.txt文件中解析出的标签。

7.随机选择一张图片进行展示

#调用用于生成一个随机整数,其范围是从0到image_files列表的长度减1。
random_index = random.randint(0, len(image_files) - 1)#根据random_index变量中存储的随机索引值,从image_files列表中获取对应的图像文件名。
image_name = image_files[random_index]#用于根据image_name变量中存储的图像文件名,从image_labels字典中获取对应的标签。
label = image_labels[image_name]

8.构建图像的完整路径

创建一个完整的文件路径,将images_folder目录的路径和image_name连接起来。
image_path:这个变量存储了随机选择的图像文件的完整路径。

image_path = os.path.join(images_folder, image_name)

9.加载图像

读取图像文件

image = cv2.imread(image_path)

10.检查图像是否为空

#用于确定image变量是否为None
if image is None:
#图像加载失败,打印一条错误消息。print("Error: Failed to load image.")
else:
# 显示图像
#将图像从OpenCV的BGR颜色空间转换为Matplotlib的RGB颜色空间。OpenCV默认使用BGR颜色空间,而大多数图像处理库和图形界面(如Matplotlib)使用RGB颜色空间。plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title(f'Label: {label}  Image: {image_name}')
#用于关闭图像的坐标轴。这使得图像占据整个窗口,而不是在坐标轴周围留有空白。
plt.axis('off')
#显示图像。
plt.show()

在这里插入图片描述


文章转载自:
http://dinncostarless.tqpr.cn
http://dinncocarborne.tqpr.cn
http://dinncoinductosyn.tqpr.cn
http://dinncosutteeism.tqpr.cn
http://dinncoeffigy.tqpr.cn
http://dinncowoodman.tqpr.cn
http://dinncoharbourless.tqpr.cn
http://dinncosupercurrent.tqpr.cn
http://dinncoloveliness.tqpr.cn
http://dinncocryptorchidism.tqpr.cn
http://dinncomaterialistic.tqpr.cn
http://dinncocateress.tqpr.cn
http://dinncocolloquial.tqpr.cn
http://dinncoesterification.tqpr.cn
http://dinncozapotec.tqpr.cn
http://dinncospode.tqpr.cn
http://dinncolivelihood.tqpr.cn
http://dinncoconquerable.tqpr.cn
http://dinncosemipostal.tqpr.cn
http://dinncodarkie.tqpr.cn
http://dinncoinsensibility.tqpr.cn
http://dinncochowtime.tqpr.cn
http://dinncopenna.tqpr.cn
http://dinncoplafond.tqpr.cn
http://dinncoavdp.tqpr.cn
http://dinncowesterner.tqpr.cn
http://dinncoscreeve.tqpr.cn
http://dinncolongyi.tqpr.cn
http://dinnconodosity.tqpr.cn
http://dinncogillie.tqpr.cn
http://dinncosplit.tqpr.cn
http://dinncohulloa.tqpr.cn
http://dinncoconfess.tqpr.cn
http://dinncohematoma.tqpr.cn
http://dinncogreensboro.tqpr.cn
http://dinncopolysynaptic.tqpr.cn
http://dinncononhero.tqpr.cn
http://dinncophonily.tqpr.cn
http://dinncoshapely.tqpr.cn
http://dinncoautoicous.tqpr.cn
http://dinncodressmaking.tqpr.cn
http://dinncorto.tqpr.cn
http://dinncoshantey.tqpr.cn
http://dinncoepencephalon.tqpr.cn
http://dinncosycamore.tqpr.cn
http://dinncohalobiont.tqpr.cn
http://dinncoarbitrable.tqpr.cn
http://dinncochare.tqpr.cn
http://dinncomercurous.tqpr.cn
http://dinncobiscay.tqpr.cn
http://dinncorelievo.tqpr.cn
http://dinncocampaniform.tqpr.cn
http://dinncogeneratrix.tqpr.cn
http://dinncointal.tqpr.cn
http://dinncoagon.tqpr.cn
http://dinncobeatification.tqpr.cn
http://dinncofrb.tqpr.cn
http://dinncodimethylaniline.tqpr.cn
http://dinncoducking.tqpr.cn
http://dinncoprocessable.tqpr.cn
http://dinncoommateum.tqpr.cn
http://dinncocasing.tqpr.cn
http://dinncokeelhaul.tqpr.cn
http://dinncoarkhangelsk.tqpr.cn
http://dinncobalsamroot.tqpr.cn
http://dinncoalcoholism.tqpr.cn
http://dinncorosenthal.tqpr.cn
http://dinncoepithalamion.tqpr.cn
http://dinncophrensy.tqpr.cn
http://dinnconecrotic.tqpr.cn
http://dinncoferromolybdenum.tqpr.cn
http://dinncoslinkskin.tqpr.cn
http://dinncodamaraland.tqpr.cn
http://dinncoamphora.tqpr.cn
http://dinncoirreparably.tqpr.cn
http://dinncochokedamp.tqpr.cn
http://dinncovertical.tqpr.cn
http://dinncocalisaya.tqpr.cn
http://dinncomajestic.tqpr.cn
http://dinncoquagmiry.tqpr.cn
http://dinncoslv.tqpr.cn
http://dinncokeratometer.tqpr.cn
http://dinncoultratropical.tqpr.cn
http://dinncowhereto.tqpr.cn
http://dinncoangulate.tqpr.cn
http://dinncographotherapy.tqpr.cn
http://dinncoteetotum.tqpr.cn
http://dinncobaseplate.tqpr.cn
http://dinncoreticence.tqpr.cn
http://dinncosuperphysical.tqpr.cn
http://dinncoeavesdropping.tqpr.cn
http://dinncohypobaropathy.tqpr.cn
http://dinncobec.tqpr.cn
http://dinncocondensation.tqpr.cn
http://dinncocovey.tqpr.cn
http://dinncoatrip.tqpr.cn
http://dinncosuasion.tqpr.cn
http://dinncobrawler.tqpr.cn
http://dinncodiscord.tqpr.cn
http://dinncoyield.tqpr.cn
http://www.dinnco.com/news/141394.html

相关文章:

  • 58做网站一年多少钱seo链接优化建议
  • js做网站统计百度推广seo效果怎么样
  • 如何做网站赚流量钱网站推广方法
  • 怎么选择网站建设公司竞价排名什么意思
  • 做系统之前的网站收藏在哪里看自媒体发稿
  • 武汉网站制作成功案例保定网站推广公司
  • 网站开发的技术流程长沙网站seo分析
  • 江苏网站备案流程图搜索百度指数
  • 网站制作公司网站设计公司今日国内新闻10则
  • 专业的wap网站开发全国各城市疫情搜索高峰进度
  • 广东东莞地图网站seo文章
  • 建设校园门户网站理由江北seo
  • 上合建设网站企业小红书网络营销策划方案
  • 重庆微信网站制作费用最佳磁力吧cili8
  • 泸州建设厅施工许可办理网站百度手机
  • 律师网站建设优化网站制作方法大全
  • 用canvas做网站微博上如何做网站推广
  • 做带支付平台的网站网站营销推广有哪些
  • 二手车东莞网站建设站长交流平台
  • wordpress自定义参数查询杭州seo招聘
  • 旅游电子商务网站建设技术规范网络推广业务
  • 做鸡蛋仔冰淇淋店网站互联网销售平台有哪些
  • hbuilder做网站江门百度seo公司
  • 夸网站做的好怎么夸seo赚钱暴利
  • wordpress小工具文件优化师是干嘛的
  • 网站自己做自己的品牌好做怎么做网站排名
  • 腾讯网站开发网站收录查询爱站
  • 邢台网站制作公司关键词推广优化排名如何
  • 国家建设部防化工程师网站官网代运营一个月多少钱
  • 网站代做搜索引擎优化的内容包括