Embedded_game/002_B_Car/主程序/获取图片.py

86 lines
2.2 KiB
Python
Raw Permalink Normal View History

2025-01-02 12:48:11 +08:00
# Untitled - By: nian0 - 周一 11月 20 2023
import sensor, image, lcd, time
from fpioa_manager import fm
import KPU as kpu
import gc
from Maix import GPIO
from machine import Timer, PWM, UART, Timer
#初始化串口
#uart = UART(UART.UART1, 115200, read_buf_len=4096)
def Servo(angle):
'''
说明舵机控制函数
功能180度舵机angle:-90至90 表示相应的角度
360连续旋转度舵机angle:-90至90 旋转方向和速度值
duty占空比值0-100
'''
#PWM通过定时器配置接到IO17引脚
tim_pwm = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
S1 = PWM(tim_pwm, freq=50, duty=0, pin=17)
S1.duty((angle+90)/180*10+2.5)
Servo(15)
# 摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1) # 摄像头后置方式
sensor.set_pixformat(sensor.RGB565) # 设置像素格式为彩色 RGB565
#sensor.set_pixformat(sensor.GRAYSCALE) # 设置像素格式为灰色
sensor.set_framesize(sensor.QVGA) # 设置帧大小为 QVGA (320x240)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_gain(0,0)
lcd.init() # LCD初始化
# 初始化 BOOT 键
boot_pin = 16 # 根据您的板子修改对应的引脚号
fm.register(boot_pin, fm.fpioa.GPIOHS0)
key = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)
clock = time.clock()
# 拍照状态
taking_pictures = False
count = 0
while(True):
img = sensor.snapshot()
# 检查 BOOT 键是否被按下
if key.value() == 0:
# 等待按键释放
while key.value() == 0:
time.sleep_ms(20)
# 改变拍照状态
taking_pictures = not taking_pictures
if taking_pictures:
print("Start taking pictures")
else:
print("Stop taking pictures")
# 根据拍照状态决定是否保存图片
if taking_pictures:
# img = img.resize(224, 224)
image_save = "/sd/img/example{}.jpg".format(count)
img.save(image_save)
print("Image saved:", image_save)
count += 1
a = lcd.display(img)
else:
a = lcd.display(img)
# 打印FPS
print(clock.fps())
# 稍作延迟,避免过快的循环
time.sleep_ms(20)