enumerate(), plt drawing, save json, cv2.resize, baseline

1. The enumerate() function is used to combine a traversable data object (such as a list, tuple or string) into an index sequence, while listing the data and data subscripts. It is generally used in for loops.
enumerate(sequence, [start=0])

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))       # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
>>> tuple(enumerate(seasons, start=1))
((1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter'))

2.Drawing

import matplotlib.pyplot as plt
import numpy as np

 x = np.linspace(-2, 6, 50)
    y1 = x + 3  # 曲线 y1
    y2 = 3 - x  # 曲线 y2
    plt.figure()  # 定义一个图像窗口
    plt.plot(x, y1)  # 绘制曲线 y1
    plt.plot(x, y2)  # 绘制曲线 y2
    plt.title("test")
    plt.show()

Insert image description here

	plt.imshow(img)
    plt.title("test")
    plt.show()

Insert image description here
3. The function of json.dumps is to convert the dictionary type into a string type in json format.
Parameters: (1) sort_keys tells the encoder to output in order of dictionary keys (a to z).
(2) The indent parameter is displayed indented according to the data format, making it clearer to read. The value of indent represents the indented space
(3) To correctly output Chinese, you can specify ensure_ascii=False:

#将label写进json
def write_json(cla_dict,json_path):#要写入的label,json路径
    json_str = json.dumps(cla_dict, indent=4)  # 编码成json格式
    with open(json_path, 'w') as json_file:  # 写进去
        json_file.write(json_str)
#从json读出label
def read_json(json_path): #json路径
    assert os.path.exists(json_path), "file: '{}' dose not exist.".format(json_path)
    with open(json_path, "r") as f:
        class_indict = json.load(f)
    return class_indict

Example:

		flower_list = train_dataset.class_to_idx  # ----大概是已经通过数据集已经分类好的文件名确定的图片类别
        cla_dict = dict((val, key) for key, val in flower_list.items())  # 将key,value值反过来,已达到通过索引找到分类的目的
        # label信息写入json
        json_path = './class_indices.json'
        write_json(cla_dict,json_path)

class_indices.json

{
    
    
    "0": "daisy",
    "1": "dandelion",
    "2": "roses",
    "3": "sunflowers",
    "4": "tulips"
}

4.cv2.resize function
resize is a function in the opencv library, which mainly plays the role of scaling images.
example: The following code can convert the original image into an image with a width and length of 300 and 300 respectively. Width and height can be specified arbitrarily, regardless of size.
interpolation: This is the way to specify interpolation. After the image is scaled, the pixels must be recalculated. This parameter is used to specify the way to recalculate the pixels. There are the following: INTER_NEAREST - nearest neighbor interpolation INTER_LINEAR -
double
line Sexual interpolation, if you do not specify the last parameter, this method is used by default
INTER_CUBIC - bicubic interpolation within a 4x4 pixel neighborhood
INTER_LANCZOS4 - Lanczos interpolation within an 8x8 pixel neighborhood

The above interpolation method is used for zooming in and out. For specific interpolation methods, see Image Processing: Five Interpolation Methods
Example:

import cv2 as cv

width = 300
height = 300
img = cv.imread('E:\\both.png')# 原图224*224
img = cv.resize(img, (width, height))# 默认使用双线性插值法

cv.imshow("img", img)
cv.waitKey(0)
cv.destroyAllWindows()

So wouldn't resize be almost the same as the upsampling and downsampling functional methods? ? ? ?
5. Baseline
baseline just means "reference object". As for where the baseline system comes from and what its performance is, there is no certain standard.

Guess you like

Origin blog.csdn.net/weixin_44040169/article/details/127994791