python编写的储存单位转换代码(以字节(B)为单位)

代码:

def bytes(bytes):
    if bytes < 1024:  #比特
        bytes = str(round(bytes, 2)) + ' B' #字节
    elif bytes >= 1024 and bytes < 1024 * 1024:
        bytes = str(round(bytes / 1024, 2)) + ' KB' #千字节
    elif bytes >= 1024 * 1024 and bytes < 1024 * 1024 * 1024:
        bytes = str(round(bytes / 1024 / 1024, 2)) + ' MB' #兆字节
    elif bytes >= 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024:
        bytes = str(round(bytes / 1024 / 1024 / 1024, 2)) + ' GB' #千兆字节
    elif bytes >= 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024:
        bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024, 2)) + ' TB' #太字节
    elif bytes >= 1024 * 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024 * 1024:
        bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024 / 1024, 2)) + ' PB' #拍字节
    elif bytes >= 1024 * 1024 * 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024:
        bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024 / 1024 /1024, 2)) + ' EB' #艾字节
    return bytes

if __name__ == '__main__':
    print('0:' + bytes(0))
    print('1:' + bytes(1))
    print('2:' + bytes(10))
    print('3:' + bytes(100))
    print('4:' + bytes(1000))
    print('5:' + bytes(10000))
    print('6:' + bytes(100000))
    print('7:' + bytes(1000000))
    print('8:' + bytes(10000000))
    print('9:' + bytes(100000000))
    print('10:' + bytes(1000000000))
    print('11:' + bytes(10000000000))
    print('12:' + bytes(100000000000))
    print('13:' + bytes(1000000000000))
    print('14:' + bytes(10000000000000))
    print('15:' + bytes(100000000000000))
    print('16:' + bytes(1000000000000000))
    print('17:' + bytes(10000000000000000))
    print('18:' + bytes(100000000000000000))
    print('19:' + bytes(1000000000000000000))
    print('20:' + bytes(10000000000000000000))
    print('20:' + bytes(100000000000000000000))
    print('20:' + bytes(1000000000000000000000))

测试:

"D:\Program Files\Python\Python36\python.exe" C:/Users/Jochen/PycharmProjects/mysite/bytes.py
0:0 B
1:1 B
2:10 B
3:100 B
4:1000 B
5:9.77 KB
6:97.66 KB
7:976.56 KB
8:9.54 MB
9:95.37 MB
10:953.67 MB
11:9.31 GB
12:93.13 GB
13:931.32 GB
14:9.09 TB
15:90.95 TB
16:909.49 TB
17:8.88 PB
18:88.82 PB
19:888.18 PB
20:8.67 EB
20:86.74 EB
20:867.36 EB

Process finished with exit code 0

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个基于Python和K210设备(使用MaixPy固件)实现的人脸识别代码示例,包括了人脸数据的储存和识别: ```python import sensor import image import lcd import KPU as kpu import time # 初始化LCD显示器 lcd.init() # 初始化摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_hmirror(True) sensor.run(1) # 加载人脸识别模型 task = kpu.load("/sd/model.kmodel") # 初始化人脸数据库 database = {} # 主循环 while True: # 摄像头捕获人脸 img = sensor.snapshot() # 使用人脸识别模型检测人脸 faces = kpu.run(task, img) # 遍历检测到的人脸 for face in faces: # 绘制矩形框 img.draw_rectangle(face.rect()) # 提取人脸ROI并缩放为固定大小 roi = img.crop(face.x(), face.y(), face.w(), face.h()).resize(200, 200) # 显示人脸ROI lcd.display(roi) # 询问用户是否要保存人脸 print("Do you want to save this face? (y/n)") while True: key = input() if key == 'y': # 保存人脸 print("Please enter the name of the person:") name = input() database[name] = roi.to_bytes() print("Face saved successfully!") break elif key == 'n': # 不保存人脸 break # 将人脸ROI与数据库中的人脸进行比对 match = None for name, face_data in database.items(): # 将字节数组转换为图像 face = image.Image(img.width(), img.height(), img.bpp(), face_data) # 使用L2距离计算相似度 distance = image.match_descriptor(roi, face) if match is None or distance < match[0]: match = (distance, name) # 如果匹配到人脸,则在矩形框上方显示名字 if match is not None: img.draw_string(face.x(), face.y() - 20, match[1], scale=2) # 显示帧 lcd.display(img) # 等待一段时间 time.sleep_ms(10) ``` 这段代码需要在K210设备上运行,并且需要将人脸数据储存在设备的SD卡中。在储存人脸时,会询问用户输入人名,并将人脸ROI转换字节数组并储存在数据库中。在识别人脸时,会将数据库中的人脸数据转换为图像进行比对,以获得更高的准确率。 需要注意的是,这是一个简单的示例代码,实际上人脸识别需要进行更加复杂的算法和处理。建议在实际应用中使用更加精细的算法和人脸数据库进行识别,以获得更高的准确率。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值