6
6pcn
@6pcn
0
声望
6
楼层
347
资料浏览
0
粉丝
0
关注
6pcn 发布的帖子
-
请问怎么设置每隔10s清除一下全局变量的数据
请问怎么设置每10s清除一下全局变量的数据,也就是我的List_score01,我使用定时器会报错
import sensor, image, time, os, tf, uos, gc, lcd , cpufreq from pyb import UART,LED,Timer uart = UART(3, 115200, timeout_char=1000) u_start=bytearray([0x2C,0x12]) u_over=bytearray([0x5B]) LED(2).on() sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QQVGA) # Set frame size to QVGA (320x240) sensor.set_windowing((240, 240)) # Set 240x240 window. #sensor.skip_frames(time=2000) # Let the camera adjust. lcd.init() net = None labels = None List_score01 = [0]*7 flag = 0 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) + ')') clock = time.clock() flag_stop = 0 number = 1 #label = "in","line","out","stop","wandao1","wandao2","word" #label = "0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" #def test(timer): #global List_score01 #List_score01 = [0]*7 #print("11111111") #tim = Timer(2, freq=1) # 使用定时器2创建定时器对象-以1Hz触发 #tim.callback(test) # 将回调设置为tick函数 def problem_1(predictions_list): global flag_stop global List_score01 uart.write(u_start) uart.write(bytearray([3])) uart.write(u_over) print("problem_1 was OK") if(predictions_list[3][1]>=0.4): #'stop' print(List_score01) flag_stop +=1 LED(3).on() List_score01 = [0]*7 #print(flag_stop) while flag_stop >=2: number = 1 while number < 5: #发送"5" uart.write(u_start) uart.write(bytearray([5])) uart.write(u_over) print(5) number +=1 time.sleep_ms(100) flag_stop=0 number = 1 List_score01 = [0]*7 time.sleep_ms(1000) LED(3).off() def problem_3(predictions_list): number3=1 global List_score01 uart.write(u_start) uart.write(bytearray([3])) uart.write(u_over) if(predictions_list[0][1]>=0.4): #'in' List_score01[int(predictions_list[0][0])] += 1 #print(List_score01) if List_score01[0]>1: while number3 < 5: #发送10个"2" uart.write(u_start) uart.write(bytearray([2])) uart.write(u_over) number3 +=1 time.sleep_ms(100) number3 = 1 #number清除再发送"3" while number3 < 10: #发送"3" uart.write(u_start) uart.write(bytearray([3])) uart.write(u_over) #print(3) number3 +=1 time.sleep_ms(100) List_score01 = [0]*7 #退出初始化循环 while(True): clock.tick() cpufreq.set_frequency(480) img = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.0) 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())) #for i in range(len(predictions_list)): #print("%s = %f" % (predictions_list[i][0], predictions_list[i][1])) if uart.any(): ##收到单片机发送来的数据 a=uart.readline().decode() if(a[0]=='1'): flag = 1 if(a[0]=='3'): flag = 3 if flag == 1: problem_1(predictions_list) if flag == 3: problem_3(predictions_list) print(clock.fps(), "fps") lcd.display(img)
-
全局变量报错,错误为SyntaxError: identifier redefined as global
import sensor, image, time, os, tf, uos, gc, lcd , cpufreq from pyb import UART,LED,Timer uart = UART(3, 115200, timeout_char=1000) u_start=bytearray([0x2C,0x12]) u_over=bytearray([0x5B]) LED(2).on() sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QQVGA) # Set frame size to QVGA (320x240) sensor.set_windowing((240, 240)) # Set 240x240 window. #sensor.skip_frames(time=2000) # Let the camera adjust. lcd.init() net = None labels = None List_score01 = [0]*7 #global List_score01 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) + ')') clock = time.clock() #label = "in","line","out","stop","wandao1","wandao2","word" #label = "0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" while(True): #flag=3 number = 1 clock.tick() cpufreq.set_frequency(480) img = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.0) global List_score01 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()) #print(obj.output()) predictions_list = list(zip(labels, obj.output())) #print(predictions_list) #print(predictions_list[0][1]) print(List_score01) if(predictions_list[0][1]>=0.7): #'in' List_score01[int(predictions_list[0][0])] += 1 print(List_score01) if List_score01[0]>1: while number < 5: #发送10个"2" uart.write(u_start) uart.write(bytearray([2])) uart.write(u_over) #img.draw_string(0, 0, "flag:%s" %(flag), scale=2, color=(255, 0, 0)) print(2) number +=1 time.sleep_ms(100) number = 1 #number清除再发送"3" while number < 10: #发送"3" uart.write(u_start) uart.write(bytearray([3])) uart.write(u_over) #img.draw_string(0, 0, "flag:%s" %(flag), scale=2, color=(255, 0, 0)) print(3) number +=1 time.sleep_ms(100) List_score01 = [0]*7 #退出初始化循环 if(predictions_list[3][1]>=0.9): #'stop' List_score01[int(predictions_list[3][0])] += 1 #print(List_score01) if List_score01[3]>20: while number < 7: #发送"2" uart.write(u_start) uart.write(bytearray([5])) uart.write(u_over) #img.draw_string(0, 0, "flag:%s" %(flag), scale=2, color=(255, 0, 0)) print(5) number +=1 time.sleep_ms(100) List_score01 = [0]*7 for i in range(len(predictions_list)): print("%s = %f" % (predictions_list[i][0], predictions_list[i][1])) #img.draw_string(10,10,str(flag),color=(0,0,255)) uart_buf = bytearray([3]) uart.write(u_start) uart.write(uart_buf) uart.write(u_over) #print(clock.fps(), "fps") lcd.display(img) def test(timer): global List_score01 List_score01 = [0]*7 print("11111111") tim = Timer(4, freq=1) # 使用定时器2创建定时器对象-以1Hz触发 tim.callback(test) # 将回调设置为tick函数
主函数中global List_score01报错
-
请问神经网络检测到了目标怎么计数
我正在坐一个环形小车,每跑一圈都会经过一次停止线,我想在检测到第三次停止线的时候给单片机发送信号停止,但是我不会怎么每经过一次记圈数+1,因为目标是从远到近逐渐出现,如果累加的话一秒就会记很多次,中间也存在漏检,请问怎么解决这个问题
mport sensor, image, time, os, tf, uos, gc, lcd , cpufreq from pyb import UART,LED uart = UART(3, 115200, timeout_char=1000) u_start=bytearray([0x2C,0x12]) u_over=bytearray([0x5B]) LED(2).on() sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QQVGA) # Set frame size to QVGA (320x240) sensor.set_windowing((240, 240)) # Set 240x240 window. sensor.skip_frames(time=2000) # Let the camera adjust. lcd.init() 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) + ')') clock = time.clock() while(True): flag=3 clock.tick() cpufreq.set_frequency(480) img = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.0) for obj in net.classify(img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5): #print("**********\nPredictions at [x=%d,y=%d,w=%d,h=%d]" % obj.rect()) img.draw_rectangle(obj.rect()) predictions_list = list(zip(labels, obj.output())) #print(predictions_list[0][1]) if(predictions_list[0][1]>=0.7): flag=2 elif(predictions_list[3][1]>=0.95): flag=4 else: flag=3 #for i in range(len(predictions_list)): #print("%s = %f" % (predictions_list[i][0], predictions_list[i][1])) uart_buf = bytearray([flag]) img.draw_string(10,10,str(flag),color=(0,0,255)) uart.write(u_start) uart.write(uart_buf) uart.write(u_over) print(clock.fps(), "fps") lcd.display(img)
-
如何在检测到某一物体后在接下来的2s内一直向单片机传输同一个信息,不会不同的信息被打断
我通过神经网络进行目标分类,由于是高速移动状态,故误检率较高。且与单片机通信需要时间,我想在第一次检测到特征物体后,一直向单片机传输我需要给它的信息,例如0和1,因为存在误检。故传输信息可能像...0011101110100000...这样子,而我想要...0011111111111111100000....这样子