@kidswong999 实在不会了。救救孩子吧
S
stzo
@stzo
0
声望
15
楼层
1188
资料浏览
0
粉丝
1
关注
stzo 发布的帖子
-
RE: OSError:Descriptors have different type怎么解决
@kidswong999 我刚刚又换了个F7试了下 依然存在若干bug随机出现的问题 再比如还有这个:
-
OSError:Descriptors have different type怎么解决
调试人脸识别代码时同一条语句不定时出现不同错误。还有其他奇奇怪怪的bug也都不定时出现。运气好时可以连续运行一个小时。运气不好时一打开就崩溃。而且有时不报错直接崩
# Face recognition with LBP descriptors. # See Timo Ahonen's "Face Recognition with Local Binary Patterns". # # Before running the example: # 1) Download the AT&T faces database http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip # 2) Exract and copy the orl_faces directory to the SD card root. import sensor, time, image, pyb sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others) sensor.set_windowing([0,0,127,127])#((92,112)) sensor.skip_frames(10) # Let new settings take affect. sensor.skip_frames(time = 5000) #等待5s sensor.set_contrast(1) sensor.set_gainceiling(16) face_cascade = image.HaarCascade("frontalface", stages=25) #SUB = "s1" NUM_SUBJECTS = 6 #图像库中不同人数,一共6人 NUM_SUBJECTS_IMGS = 20 #每人有20张样本图片 # 拍摄当前人脸。 #img = sensor.snapshot() ##img = image.Image("singtown/%s/1.pgm"%(SUB)) #d0 = img.find_lbp((0, 0, img.width(), img.height())) #d0为当前人脸的lbp特征 img = None pmin = 999999 num=0 def min(pmin, a, s): global num if a<pmin: pmin=a num=s return pmin def max_face(objects): face2=0 for face1 in objects: face2=face1; #if i==0: #face2=objects[0] #i=1; #elif face1.w<objects[i].w: #face2=objects[i] return face2; face=[0,0,127,127] dist = 0 while(True): img = sensor.snapshot() objects = img.find_features(face_cascade, threshold=1.3, scale=1.5) #face=max_face(objects); if len(objects)==1: for face in objects: print(objects) img.draw_rectangle(face); if face[3]<50: continue; elif face[2]>55: continue; sensor.set_windowing(face) d0 = img.find_lbp(face) #((0, 0, img.width(), img.height())) pmin=9999999; for s in range(1, NUM_SUBJECTS+1): dist = 0 for i in range(2, NUM_SUBJECTS_IMGS+1): img = image.Image("singtown/s%d/%d.pgm"%(s, i)) #pgm d1 = img.find_lbp((0, 0, img.width(), img.height())) #d1为第s文件夹中的第i张图片的lbp特征 dist += image.match_descriptor(d0, d1)#计算d0 d1即样本图像与被检测人脸的特征差异度。 print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS)) pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。 print(pmin) print(num) # num为当前最匹配的人的编号。 sensor.set_windowing([0,0,127,127])#((92,112)) sensor.skip_frames(time = 200)
-
RE: 每次运行都显示内存分配错误,能解决么?
import sensor, image, time, math from pyb import LED,Timer from struct import pack, unpack from pyb import UART #import Message,DotFollowing uart = UART(3,500000)#初始化串口 波特率 500000 class Receive(object): uart_buf = [] _data_len = 0 _data_cnt = 0 state = 0 uart_data=[5,6,7,8,9] R=Receive() def UartSendData(Data): uart.write(Data) def jiexi(data): if R.state==0: if data==0xee : R.state+=1 elif R.state<=5 : R.uart_data[R.state-1]=data R.state+=1 else : R.state=0 if R.state>5: R.state=0 #读取串口缓存 def UartReadBuffer(): i = 0 Buffer_size = uart.any() while i<Buffer_size: #ReceivePrepare(uart.readchar()) jiexi(uart.readchar()) i = i + 1 #点检测数据打包 def DotDataPack(color,flag,x,y,yaw): print("found: x=",x," y=",y) #x=-x #y=-y pack_data=bytearray([0xAA,0x29,0x05,0x41,0x00,color,flag,x>>8,x,(-y)>>8,(-y),0x00]) lens = len(pack_data)#数据包大小 pack_data[4] = 6;#有效数据个数 i = 0 sum = 0 #和校验 while i<(lens-1): sum = sum + pack_data[i] i = i+1 pack_data[lens-1] = sum; return pack_data sensor.reset() sensor.set_pixformat(sensor.RGB565) #阈值内数据(白色像素)较多时,QQVGA帧率降到5以下。线较细时可以使用(7·19测试) #QQQVGA较稳定,帧率保持高速(50以上) sensor.set_framesize(sensor.QQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000. sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds clock = time.clock() # to process a frame sometimes. sensor.set_hmirror(True) #水平方向翻转 sensor.set_vflip(True) #垂直方向翻转 #sensor.set_auto_whitebal(False) #sensor.set_auto_gain(False) sensor.skip_frames(n=10) #Prevention_go_out_up=(0,30,160,30) #Prevention_go_out_down=(0,60,160,30) #Prevention_go_out_left=(40,0,40,120) #Prevention_go_out_right=(80,0,40,120) #Prevention_go_out_up_flag=0 #Prevention_go_out_down_flag=0 #Prevention_go_out_left_flag=0 #Prevention_go_out_right_flag=0 #roi_find_line_out=(0,0,160,30) color_red_x=(52, 92, 110, 19, 86, -36) #red_x_x=0 #red_x_y=0 fly_mode=0 color_red_x_end=0 red_x_time_flag=0 class red_x(object): flag = 0 color = 0 x = 0 y = 0 red_X=red_x() ################################################################### #色块识别函数定红X #定义函数:找到画面中最大的指定色块 def FindMax(blobs): max_size=1 if blobs: max_blob = 0 for blob in blobs: blob_size = blob.w()*blob.h() if ( (blob_size > max_size) & (blob_size > 20) ) :#& (blob.density()<1.2*math.pi/4) & (blob.density()>0.8*math.pi/4) if ( math.fabs( blob.w() / blob.h() - 1 ) < 2.0 ) : max_blob=blob max_size = blob.w()*blob.h() return max_blob def LineFilter(src, dst): for i in range(0, len(dst), 1): dst[i] = src[i<<1] start_i=0 start=0 delate=0 #点检测 def find_red_x(): global start_i global start global delate img = sensor.snapshot(line_filter = LineFilter)#拍一张图像 red_blobs = img.find_blobs([color_red_x], pixels_threshold=3, area_threshold=3, merge=True, margin=5)#识别红色物体 max_blob=FindMax(red_blobs)#找到最大的那个 if max_blob: img.draw_cross(max_blob.cx(), max_blob.cy())#物体中心画十字 img.draw_rectangle(max_blob.rect())#画圈 #获取坐标并转换为+-200 red_x.x = max_blob.cx()-80 red_x.y = max_blob.cy()-60 red_x.flag = 1 #LED灯闪烁 LED(3).toggle() #LED灯闪烁 LED(2).toggle() else: red_x.flag = 0 LED(2).off() LED(3).off() #串口发送数据给飞控 #################################################################### ''' def Prevention_go_out(img): global Prevention_go_out_up_flag global Prevention_go_out_down_flag global Prevention_go_out_left_flag global Prevention_go_out_right_flag Prevention_go_out_up_flag=0 Prevention_go_out_down_flag=0 Prevention_go_out_left_flag=0 Prevention_go_out_right_flag=0 lines=img.find_lines(Prevention_go_out_up,x_stride=4,y_stride=1,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if abs(line.theta()-180)>35: Prevention_go_out_up_flag=1 else : if line.theta()>35 : Prevention_go_out_up_flag=1 #Prevention_go_out_up_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_up_flag=0 lines=img.find_lines(Prevention_go_out_down,x_stride=4,y_stride=1,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if abs(line.theta()-180)>35: Prevention_go_out_down_flag=1 else : if line.theta()>35 : Prevention_go_out_down_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_down_flag=0 lines=img.find_lines(Prevention_go_out_left,x_stride=1,y_stride=4,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if abs(line.theta()-180)<35: Prevention_go_out_left_flag=1 else : if line.theta()<35 : Prevention_go_out_left_flag=1 #Prevention_go_out_left_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_left_flag=0 lines=img.find_lines(Prevention_go_out_right,x_stride=1,y_stride=4,threshold=1000,theta_margin=45,rho_maigin=20) if len(lines)>0 : for line in lines: if line.theta()>90: if 180-line.theta()<35: Prevention_go_out_right_flag=1 else : if line.theta()<35 : Prevention_go_out_right_flag=1 print(line.theta(), line.theta()) img.draw_line(line.line(), color=(255,0,0)) #Prevention_go_out_right_flag=1 #line=max(lines,lambda x:x.magnitude()) #img.draw_line(line.line(), color=(255,0,0)) else : Prevention_go_out_right_flag=0 ''' ######################################################################### find_color_time_start=0 find_color_time_now=0 find_color_time_flag=0 color_fire=(62, 100, 8, 55, -22, 82)#(77, 100, 7, 45, -8, 79)#(84, 100, 8, 53, -50, 60)#(52, 92, 110, 19, 86, -36)#(33, 90, 7, 59, -60, 74)#(33,100,7,59,-62,74)#(59, 42, -17, 92, 16, 94)#(89, 99, 39, 3, 7, -2) (33, 100, 7, 59, -62, 74) class find_color_fire(object): x=0 y=0 flag=0 daxiao=0 find_color_fire = find_color_fire() def find_color(img): global find_color_time_start global find_color_time_now global find_color_time_flag find_color_fire.x=0 find_color_fire.y=0 find_color_fire.flag=0 find_color_fire.daxiao=0 #拍一张图像 red_blobs = img.find_blobs([color_fire], pixels_threshold=10, area_threshold=8 , merge=True, margin=5)#识别红色物体 max_blob=FindMax(red_blobs)#找到最大的那个 if max_blob: img.draw_cross(max_blob.cx(), max_blob.cy())#物体中心画十字 img.draw_rectangle(max_blob.rect())#画圈 #获取坐标并转换为+-200 find_color_fire.x = max_blob.cx()-80 find_color_fire.y = max_blob.cy()-60 find_color_fire.flag = 1 find_color_fire.daxiao=max_blob.area() #LED灯闪烁 #LED灯闪烁 LED(2).toggle() else: #Dot.flag = LED(3).off() LED(2).off() def Send_out(color,sta,x,y,yaw): send_out_1=int(color) send_out_2=int(sta) send_out_x=int(x) send_out_y=int(y) send_out_yaw=int(yaw) UartSendData(DotDataPack(send_out_1,send_out_2,send_out_x,send_out_y,send_out_yaw)) ''' fly_mode=0 find_color_mode=0 fly_x=0 fly_y=0 find_color_flag=0 fire_time_start=0 fire_time_now=0 fire_time_flag=0 fire_laser=0 fire_laser_flag=0 jiaoyan=0 back_home=0 while True : fire_laser=0 #fly_mode=2 UartReadBuffer() for i in range(0,5): print(R.uart_data[i] , i , "fly_mode=",fly_mode) #if fly_mode==0: if R.uart_data[0]==0 or fly_mode==0: find_red_x() if red_x.flag==1: if start_i==0 : start = time.ticks() start_i=1 else : delate=time.ticks() if delate-start>5000: fly_mode=1 start_i=0 start=0 delate=0 print(delate-start) Send_out(0,1,red_x.x,red_x.y) else : # LED().on() if fly_mode==0: flymode+=1 if fly_mode==1: #sensor.set_pixformat(sensor.RGB565) img = sensor.snapshot() #lines=img.find_lines((0,60,160,60),x_stride=40,y_stride=1,threshold=1000,theta_margin=40,rho_margin=20) blobs=img.find_blobs([color_fire],roi=(0,0,160,10), pixels_threshold=3, area_threshold=3, merge=True, margin=5) if len(blobs): fly_mode=2 sensor.set_pixformat(sensor.RGB565) LED(3).toggle() Send_out(0,1,0,-30) if fly_mode==2 : find_color() if find_color_fire.daxiao<300 and find_color_fire.flag==1: fly_x=find_color_fire.x/2 fly_y=find_color_fire.y/2 if abs(find_color_fire.x)<16 and abs(find_color_fire.y<12) : if(fire_time_flag==0): fire_time_start=time.ticks() fire_time_flag=1 else : fire_time_now=time.ticks() if fire_laser_flag==0: fire_laser=2 fire_laser_flag=1 jiaoyan=5 else : fire_time_start=0 fire_time_flag=0 fire_time_now=0 fire_laser=0 if fire_time_now-fire_time_start>2000: find_color_flag=1 print(find_color_flag,"start time is",fire_time_s . .. tart,"now time is",fire_time_now) else : fire_laser_flag=0 fire_time_start=0 fire_time_flag=0 fire_time_now=0 #fly_x=0 #fly_y=-30 print("jlkjlskadkkhksdh",find_color_fire.daxiao,find_color_fire.flag,) #if find_color_fire.flag==0: if find_color_flag==1: find_color_flag=0 find_color_mode+=1 if find_color_mode==0: fly_x=0 fly_y=-30 elif find_color_mode==1: fly_x=0 fly_y=-30 elif find_color_mode==2: fly_x=-30 fly_y=0 elif find_color_mode==3: fly_x=0 fly_y=30 elif find_color_mode==4: fly_x=0 fly_y=30 else: print("ajsjhdalsdlasdljasldjlasjdljalsfhakjshfkahslfdhakjshdkjashdkjahsdkjhasdjggaiuyh") if find_color_mode>=4: for line in img.find_lines(roi_find_line_out,x_stride=50,y_stride=2,threshold=2000,theta_margin=30,rho_margin=20): if line.theta()>40: back_home=1 fly_mode=3 Send_out(fire_laser,1,fly_x,fly_y,) print("alsdjlashdljassldjalskjd",jiaoyan,find_color_mode) if fly_mode==3: find_red_x() if red_x.flag==1: Send_out(1,1,red_x.x,red_x.y) else: Send_out(1,1,30,0) #else: #fly_x=find_color_fire.x #fly_y=find_color_fire.y #find_color_flag=1 #if find_color_mode==4: #for line in find_lines(roi=[(0,0,160,120)],x_stride=20,y_stride=1,threshold=1000,theta_margin=20,rho_maigin=10): #if line.theta()>35 and line.theta()<125: #fly_mode=3 #Send_out(fire_laser,1,fly_x,fly_y,) #print("alsdjlashdljassldjalskjd",jiaoyan) #if fly_mode==3 : ''' roi_jinchang=(0,60,160,60) fly_mode=0 qifei=0 jinchang=0 qianjin=0 end_fire=0 find_fire_xun=1 find_fire_xun_will=1 find_fire_xun_rorl=-30 fire_time_start=0 fire_time_now=0 fire_time_flag=0 fire_time_end=0 #fire_end_time_start=0 #fire_end_time_now=0 #fire_end_time_flag=0 class send_data(object): color=0 sta=1 x=0 y=0 yaw=0 send_data=send_data() while True: Send_out(send_data.color,send_data.sta,send_data.x,send_data.y,send_data.yaw) send_data.color=0 send_data.sta=1 send_data.x=0 send_data.y=0 send_data.yaw=0 UartReadBuffer() for i in range(0,5): print(R.uart_data[i],i,"fly_mode=",fly_mode) if qifei==0: find_red_x() send_data.x=red_x.x send_data.y=red_x.y if R.uart_data[0]==1: if red_x.flag==1: if start_i==0 : start = time.ticks() start_i=1 else : delate=time.ticks() if delate-start>5000: qifei=1 if qifei==1 and qianjin==0: img = sensor.snapshot() for line in img.find_lines(roi=roi_jinchang,x_stride=20,y_stride=1,threshold=1500,theta_margin=25,rho_margin=25): if line.theta()>40 and line.theta()<120 and line.magnitude()>100: qianjin=1 if qianjin==0: send_data.x=0 send_data.y=-30 if qianjin==1 and end_fire<4: img = sensor.snapshot(line_filter = LineFilter) find_color(img) if find_color_fire.flag==1: send_data.x=find_color_fire.x send_data.y=find_color_fire.y if abs(find_color_fire.x)<16 and abs(find_color_fire.y)<12: if fire_time_flag==0: fire_time_start = time.ticks() fire_time_flag=1 else : fire_time_now=time.ticks() #fire_end_time_start=0 #fire_end_time_now=0 #fire_end_time_flag=0 start=0 start_i=0 delate=0 else: fire_time_start=0 fire_time_now=0 fire_time_flag=0 fire_time_end=0##########################逻辑有待斟酌 if fire_time_now-fire_time_start>5000: fire_time_end=1 else: if fire_time_end==1: if start_i==0:#if fire_end_time_flag==0: start=time.ticks()#fire_end_time_start=time.ticks() else : delate=time.ticks()#fire_end_time_now=time.ticks() if delate-start>5000:#fire_end_time_now-fire_end_time_start>1000: end_fire+=1 fire_time_end=0 send_data.x=0 send_data.y=0 else: fire_time_start=0 fire_time_now=0 fire_time_flag=0 #fire_end_time_start=0 #fire_end_time_now=0 #fire_end_time_flag=0 start=0 stare_i=0 delate=0 #Prevention_go_out(img): if find_fire_xun==1: for line in img.find_lines(roi=(0,0,160,30),x_stride=20,y_stride=1,threshold=1000,theta_margin=30,rho_maigin=10): if line.theta()<120 and line.theta()>40 and line.magnitude()>100: for i in range(0,40): img = sensor.snapshot() Send_out(0,1,find_fire_xun_rorl,1,0) find_fire_xun_will=2 if find_fire_xun_rorl==-30: for l in img.find_lines(roi=(80,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=30 elif find_fire_xun_rorl==30: for l in img.find_lines(roi=(40,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=-30 for i in range(0,40): img=sensor.snpshot() Send_out(0,1,0,0,0) find_fire_xun=find_fire_xun_will if find_fire_xun==1: send_data.x=-find_fire_xun_rorl/30 send_data.y=-30 #if Prevention_go_up_flag==1: #find_fire_xun_will=1 if find_fire_xun==2: for line in img.find_lines(roi=(0,90,160,30),x_stride=20,y_stride=1,threshold=1000,theta_margin=30,rho_maigin=10): if line.theta()<120 and line.theta()>40 and line.magnitude()>100: for i in range(0,40): img = sensor.snapshot() Send_out(0,1,find_fire_xun_rorl,-1,0) find_fire_xun_will=1 if find_fire_xun_rorl==-30: for l in img.find_lines(roi=(80,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=30 elif find_fire_xun_rorl==30: for l in img.find_lines(roi=(40,0,40,120),x_stride=1,y_stride=20,threshold=1500,theta_nargin=30,rho_maigin=100): if l.theta()<30 or l.theta()>150: find_fire_xun_rorl=-30 for i in range(0,40): img=sensor.snpshot() Send_out(0,1,0,0,0) find_fire_xun=find_fire_xun_will if find_fire_xun==2: send_data.x=-find_fire_xun_rorl/30 send_data.y=30 if end_fire>=4: send_data.x=0 send_data.y=0