import sensor, image, time,image, os, tf, uos, gc
from pyb import UART
uart = UART(3, 9600)
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.VGA)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_auto_whitebal(True)
sensor.set_auto_gain(False) # 必须关闭此功能,以防止图像冲洗…
sensor.set_windowing((240, 240)) # Set 240x240 window.
sensor.skip_frames(time=2000) # Let the camera adjust.
net = None
labels = None
try:
# load the model, alloc the model file on the heap if we have at least 64K free after loading
net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
except Exception as e:
print(e)
raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')
try:
labels = [line.rstrip('\n') for line in open("labels.txt")]
except Exception as e:
raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')
# 加载Haar算子
# 默认情况下,这将使用所有阶段,更低的satges更快,但不太准确。
face_cascade = image.HaarCascade("frontalface", stages=25)
#image.HaarCascade(path, stages=Auto)加载一个haar模型。haar模型是二进制文件,
#这个模型如果是自定义的,则引号内为模型文件的路径;也可以使用内置的haar模型,
#比如“frontalface” 人脸模型或者“eye”人眼模型。
#stages值未传入时使用默认的stages。stages值设置的小一些可以加速匹配,但会降低准确率。
# FPS clock
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
img.lens_corr(1.8) # 1.8的强度参数对于2.8mm镜头来说是不错的。
objects = img.find_features(face_cascade, threshold=0.75, scale=1.35)
QR_Code=img.find_qrcodes()
if objects:
for r in objects:
img.draw_rectangle(r)
for obj in net.classify(img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
img.draw_rectangle(obj.rect())
predictions_list = list(zip(labels, obj.output()))
radio=predictions_list[1][1]*100
print("%s = %.2f%%" % (predictions_list[1][0],radio ))
radio=str(radio)
uart.write('@'+radio+'\r\n')
elif QR_Code:
for code in QR_Code:
img.draw_rectangle(code.rect(), color = (255, 0, 0))
print(code.payload())
uart.write('@'+code.payload()+'\r\n')
#else:
#print("-----------error------------")

并且转换角度之后不能及时调整亮度,先照人的话再照平板就会很亮,这该怎么解决