从头认识C—枚举类型

枚举类型是一种基本数据类型,而不是一种构造类型,因为它不能再分解为任何基本类型。不要和联合混淆。 

enum weekday{ sun,mou,tue,wed,thu,fri,sat }; enum weekday a,b,c; 

枚举类型变量的赋值和使用 枚举类型在使用中有以下规定: 

1. 枚举值是常量,不是变量。不能在程序中用赋值语句再对它赋值。   

例如对枚举weekday的元素再作以下赋值:    

sun=5; mon=2; sun=mon; 都是错误的。 

2. 枚举元素本身由系统定义了一个表示序号的数值,从0开始顺序定义为0,1,2…。如在weekday中,sun值为0,mon值为1,…,sat值为6。 

如main(){ 

enum weekday { sun,mon,tue,wed,thu,fri,sat } a,b,c;     

a=sun;     b=mon;     c=tue; 

printf("%d,%d,%d",a,b,c); }  

说明: 

只能把枚举值赋予枚举变量,不能把元素的数值直接赋予枚举变量。

如:a=sum; b=mon; 是正确的。

而:a=0; b=1; 是错误的。

如一定要把数值赋予枚举变量,则必须用强制类型转换。 如: a=(enum weekday)2; 

其意义是将顺序号为2的枚举元素赋予枚举变量a,相当于:a=tue; 
还应该说明的是枚举元素不是字符常量也不是字符串常量,使用时不要加单、双引号。 
枚举变量只能取枚举说明结构中的某个标识符常量。  
例如: enum string  {x1=5,x2,x3,x4,};  

     enum strig x=x3;      

此时, 枚举变量x实际上是7。 

enum变量类型还可以给其中的常量符号赋值,如果不赋值则会从被赋初值的那个常量开始依次加1,如果都没有赋值,它们的值从0开始依次递增1。如分别用一个常数表示不同颜色 typedef enum{ GREEN = 1, RED, BLUE, GREEN_RED = 10, GREEN_BLUE }Color. 
其中各常量名代表的数值分别为: GREEN = 1 RED = 2 BLUE = 3 GREEN_RED = 10 GREEN_BLUE = 11 
所以enum定义时括号里是常量,不像struct定义时括号里是变量。enum类型变量任何时刻只能等于括号中的一个常量。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
YOLOv8 是一个不存在的模型,可能是您想说的是 YOLOv4,那么以下是一个使用 Python 编写 YOLOv4 目标检测模型的示例代码: 1. 首先安装相关依赖: ```python !pip install tensorflow-gpu==2.4.0 opencv-python ``` 2. 导入必要的库: ```python import cv2 import numpy as np import tensorflow as tf from tensorflow.keras import layers, models ``` 3. 构建模型: ```python def YOLOv4(input_shape=(608, 608, 3), num_classes=80): input_layer = layers.Input(shape=input_shape) # Backbone x = layers.Conv2D(32, (3, 3), padding='same', use_bias=False)(input_layer) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2D(64, (3, 3), strides=(2, 2), padding='same', use_bias=False)(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) # YOLOv4 的主干网络包含了大量的 SPP 和 CSP 模块,这里不一一列举,可以参考相关论文和代码实现 # Head x = layers.Conv2D(512, (1, 1), padding='same', use_bias=False)(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2D(1024, (3, 3), strides=(2, 2), padding='same', use_bias=False)(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2D(512, (1, 1), padding='same', use_bias=False)(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) x = layers.Conv2D(1024, (3, 3), strides=(2, 2), padding='same', use_bias=False)(x) x = layers.BatchNormalization()(x) x = layers.Activation('relu')(x) # 最终输出的 feature map x = layers.Conv2D(255, (1, 1), padding='same')(x) output_layer = layers.Reshape((tf.shape(x)[1], tf.shape(x)[2], 3, 85))(x) model = models.Model(input_layer, output_layer) return model ``` 4. 加载预训练权重(可选): ```python def load_weights(model, weights_file): with open(weights_file, 'rb') as f: major, minor, revision, seen, _ = np.fromfile(f, dtype=np.int32, count=5) layers = model.layers for layer in layers: if any([s in layer.name for s in ['conv2d', 'batch_norm']]): if 'conv2d' in layer.name: kernel = layer.kernel filters = kernel.shape[-1] shape = kernel.shape[::-1] bn_name = layer.name.replace('conv2d', 'batch_norm') else: filters = layer.gamma.shape[-1] shape = (1, 1, 1, filters) bn_name = layer.name bias = np.fromfile(f, dtype=np.float32, count=filters) scale = np.fromfile(f, dtype=np.float32, count=filters) mean = np.fromfile(f, dtype=np.float32, count=filters) var = np.fromfile(f, dtype=np.float32, count=filters) kernel_weights = np.fromfile(f, dtype=np.float32, count=np.product(shape)) kernel_weights = kernel_weights.reshape(shape) kernel_weights = kernel_weights.transpose([2, 3, 1, 0]) if 'conv2d' in layer.name: layer.set_weights([kernel_weights, bias]) else: layer.set_weights([scale, bias, mean, var]) return model ``` 5. 进行目标检测: ```python # 加载模型 model = YOLOv4() model = load_weights(model, 'yolov4.weights') # 加载类别标签 with open('coco.names', 'r') as f: class_names = [cname.strip() for cname in f.readlines()] # 加载测试图片 image = cv2.imread('test.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 图片预处理 image_size = image.shape[:2] image_data = cv2.resize(image, (608, 608)) image_data = image_data[np.newaxis, ...].astype(np.float32) / 255.0 # 模型推理 pred = model.predict(image_data) # 后处理 boxes, scores, classes = tf.image.combined_non_max_suppression( boxes=tf.reshape(pred[:, :, :, :, :4], (-1, 4)), scores=tf.reshape(pred[:, :, :, :, 4:5] * pred[:, :, :, :, 5:], (-1,)), max_output_size_per_class=100, max_total_size=100, iou_threshold=0.45, score_threshold=0.25 ) boxes = boxes.numpy() scores = scores.numpy() classes = classes.numpy().astype(int) # 绘制检测结果 for i in range(len(boxes)): if scores[i] > 0.25: x1, y1, x2, y2 = boxes[i] x1 = int(x1 / 608 * image_size[1]) y1 = int(y1 / 608 * image_size[0]) x2 = int(x2 / 608 * image_size[1]) y2 = int(y2 / 608 * image_size[0]) label = class_names[classes[i]] cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) cv2.imshow('result', image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 以上代码仅作为示例参考,具体实现还需要根据自己的需求进行修改。同时,由于 YOLOv4 模型相对较大,需要较强的计算资源才能进行训练和推理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

→嵌入式Linux开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值