@kidswong999 实在不会了。救救孩子吧
S
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
-
当直线是横向的时候线性回归就会乱 求解
固件是最新的
import sensor, image, time from pyb import UART # 鲁棒线性回归例程 # 可以跟踪所有指向相同的总方向但实际上没有连接的线。 在线路上使用 # find_blobs(),以便更好地过滤选项和控制。 from pid import PID rho_pid = PID(p=6.0, i=0.1, d=0.3) #控制rol theta_pid = PID(p=2.5, i=0)#控制yaw finds_blob=0 flymode=1 THRESHOLD = (0, 100) # Grayscale threshold for dark things... BINARY_VISIBLE = True # 首先二值化 up_roi = (0,0, 80, 15) down_roi = (0, 55, 80, 15) left_roi = (0, 0, 25, 60) righ_roi = (55, 0,25, 40) y=0 up_roif = (0,0, 80, 30) down_roif = (0, 30, 80, 30) left_roif = (0, 0, 40, 60) righ_roif = (40, 0,40, 60) sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) #阈值内数据(白色像素)较多时,QQVGA帧率降到5以下。线较细时可以使用(7·19测试) #QQQVGA较稳定,帧率保持高速(50以上) sensor.set_framesize(sensor.QQQVGA) # 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_vflip(True) sensor.set_hmirror(True) def find_border(img,area_roi): area=0 singleline_check = img.get_regression([(255,255)],roi=area_roi, robust = True) if (singleline_check): area=1 return area uart = UART(3,500000)#初始化串口 波特率 500000 class Receive(object): uart_buf = [] _data_len = 0 _data_cnt = 0 state = 0 R=Receive() def UartSendData(Data): uart.write(Data) def DotDataPack(color,flag,x,y): print("found: x=",x," y=",y, flymode) 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 times=0 def find_123(img): global times global flymode upflag=0 leftflag=0 rightflag=0 downflag=0 finds_blob=0 upflag=find_border(img,up_roi) downflag=find_border(img,down_roi) leftflag=find_border(img,left_roi) rightflag=find_border(img,righ_roi) if upflag: finds_blob=finds_blob|0x01 if downflag: finds_blob=finds_blob|0x02 if leftflag: finds_blob=finds_blob|0x04 if rightflag: finds_blob=finds_blob|0x08 if flymode==1 : if finds_blob&0x01==0x00: times+=1 else : times=0 if times>10: if finds_blob&0x04!=0x00: flymode=3 elif finds_blob&0x08!=0x00: flymode=4 elif flymode==2 : if finds_blob&0x02==0x00: times+=1 if times>15: if finds_blob&0x04!=0x00: flymode=3 elif finds_blob&0x08!=0x00: flymode=4 else : times=0 elif flymode==3 : if finds_blob&0x04==0x00: times+=1 if times>15: if finds_blob&0x01!=0x00: flymode=1 elif finds_blob&0x02!=0x00: flymode=2 else : times=0 elif flymode==4 : if finds_blob&0x08==0x00: times+=1 if times>15: if finds_blob&0x01!=0x00: flymode=1 elif finds_blob&0x02!=0x00: flymode=2 else : times=0 while(True): clock.tick() img = sensor.snapshot().binary([(0,100)]).lens_corr(strength = 1.8, zoom = 1.0)# if BINARY_VISIBLE else sensor.snapshot() #for i in [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]: #img[i*160:i*160+80]=255 #print(img[0]) # 返回类似于由find_lines()和find_line_segments()返回的线对象。 # 你有x1(),y1(),x2(),y2(),length(), # theta()(以度为单位的旋转),rho()和magnitude()。 # # magnitude() 表示线性回归的工作情况。这对于鲁棒的线性回归意味着不同 # 的东西。一般来说,值越大越好... # for l in img.find_lines(roi=(30,20,20,20),threshold=1000) find_123(img) #print(flymode) if flymode==1 : roi=up_roif elif flymode==2 : roi=down_roif elif flymode==3 : roi=left_roif elif flymdoe==4 : roi=righ_roif line = img.get_regression([(255,255) if BINARY_VISIBLE else THRESHOLD],roi=roi,robust = True) #line = img.get_regression([(255,255) if BINARY_VISIBLE else THRESHOLD],robust = True) if (line): rho_err = abs(line.rho())-img.width()/2 if line.theta()>90: theta_err = line.theta()-180 else: theta_err = line.theta() if line.magnitude()>8: #if -40<b_err<40 and -30<t_err<30: rho_output = rho_pid.get_pid(rho_err,1) #正值->右移 rol theta_output = theta_pid.get_pid(theta_err,1) #正值->右转 yaw #if abs(theta_output)>80: #y=0 #else : # y=200 if(flymode==1): x=rho_output y=100 elif(flymode==2): x=rho_output y=-100 elif(flymode==3): x=-100 y=rho_output elif(flymode==4): x=100 y=rho_output UartSendData(DotDataPack(0,1,int(x),int(y))) #print("rho_output="+str(rho_output)+" theta_output="+str(theta_output)) img.draw_line(line.line(), color = 127,thickness=2) print("FPS %f, mag = %s" % (clock.fps(), str(line.magnitude()) if (line) else "N/A"))
-
为什么图像的像素点少于某个值(且不为零)程序就会崩溃
import sensor, image, time from pyb import LED from pyb import UART,Timer import Message from pid import PID rho_pid = PID(p=5.0, i=0.1, d=0.3) #控制rol theta_pid = PID(p=3.0, i=0.2)#控制yaw sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.QQQVGA) sensor.set_vflip(False) sensor.set_hmirror(False) sensor.skip_frames(time = 2000) # let auto-gain/auto-white-balance run before using images yanse=(0,100) class zhixian(): angle=0 yaw=0 pitch=0 magnitude=0 a1=0 a2=0 b1=0 b2=0 c1=0 c2=0 last_x=0 last_y=0 rho=0 angle=0 yaw=0 rho_err=0 angle_err=0 yaw_err=0 clock = time.clock() lx=zhixian() i=0 while True: clock.tick() img = sensor.snapshot().binary([yanse])#二值化 i=0 last_x=0 last_y=0 rho=0 angle=0 x=0 y=0 maxbo=0 #blobs = img.find_blobs([(250,255)],merge=True) #for b in blobs : #if maxbo<b.pixels(): #maxbo=b.pixels() #if maxbo>20 : for l in img.find_lines(threshold = 1000, theta_margin = 50, rho_margin = 50) : i=i+1 #if(min_degree <= l.theta()) and (l.theta() <= max_degree): if i==1: #img.draw_line(l.line(), color = (255, 0, 0)) a1=l[3]-l[1]; b1=l[0]-l[2]; c1=l[2]*l[1]-l[0]*l[3]; elif i==2 : a2=l[3]-l[1]; b2=l[0]-l[2]; c2=l[2]*l[1]-l[0]*l[3]; else: img.draw_line(l.line(), color = (255, 0, 0)); if i>1 : if((a1*b2-a2*b1)!=0 and (a1*b2-a2*b1)!=0): img.draw_cross(int((b1*c2-b2*c1)/(a1*b2-a2*b1)),int((c1*a2-c2*a1)/(a1*b2-a2*b1)),10,color=[255,0,0]); last_x=int((b1*c2-b2*c1)/(a1*b2-a2*b1)); last_y=int((c1*a2-c2*a1)/(a1*b2-a2*b1)); Message.UartSendData(Message.DotDataPack(last_y,last_x,0)) print(2, last_x, last_y ) elif i==1 : lines = img.get_regression([(255,255)],robust = True) # print(lines.line()) #lx.yaw=lines.theta() #lx.pitch=lines.rho() #lx.magnitude=lines.magnitude() #rho_err = abs(lines.rho())-img.width()/2 #if lines.theta()>90: #angle_err = lines.theta()-180 #else: #angle_err = lines.theta() #if lines.magnitude()>8: ##if -40<b_err<40 and -30<t_err<30: #rho = rho_pid.get_pid(lines.rho(),1) #正值->右移 rol #angle = theta_pid.get_pid(angle_err,1) #正值->右转 yaw ##for ls in lines: rho=lines.rho() angle=lines.theta() Message.UartSendData(Message.DotDataPack(20,int(rho),int(angle))) #print(1, rho, angle ) print(clock.fps())
-
例程上改的(算是大改)报错AttributeError怎么解决
AttributeError:'module' object has no attribute 'draw_circle'
识别自己在直线例程
这个例子展示了如何在图像中查找线条。对于在图像中找到的每个线对象,
都会返回一个包含线条旋转的线对象。
注意:线条检测是通过使用霍夫变换完成的:
http://en.wikipedia.org/wiki/Hough_transform
请阅读以上关于“theta”和“rho”的更多信息。
find_lines()找到无限长度的线。使用find_line_segments()
来查找非无限线。
enable_lens_corr = False # turn on for straighter lines...
import sensor, image, time
lim1=[1,2,3,4]
lim2=[1,2,3,4]
sum=0
flag=0
i=0
li=(0,1,2,3,)
x=0
y=0
zan=()
sensor.reset()sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
所有的线对象都有一个
theta()
方法来获取它们的旋转角度。您可以根据旋转角度来过滤线条。
min_degree = 0
max_degree = 179
所有线段都有
x1()
,y1()
,x2()
, andy2()
方法来获得他们的终点一个
line()
方法来获得所有上述的四个元组值,可用于draw_line()
.while(True):
clock.tick() img = sensor.snapshot() if enable_lens_corr: img.lens_corr(1.8) # for 2.8mm lens... # `threshold` controls how many lines in the image are found. Only lines with # edge difference magnitude sums greater than `threshold` are detected... # `threshold`控制从霍夫变换中监测到的直线。只返回大于或等于阈值的 # 直线。应用程序的阈值正确值取决于图像。注意:一条直线的大小是组成 # 直线所有索贝尔滤波像素大小的总和。 # `theta_margin`和`rho_margin`控件合并相似的直线。如果两直线的 # theta和ρ值差异小于边际,则它们合并。 sum=0 flag=0; for l in img.find_lines(threshold = 3000, y_stride=2,y_stride=2,theta_margin = 25, rho_margin = 25): if (min_degree <= l.theta()) and (l.theta() <= max_degree): img.draw_line(l.line(), color = (255, 0, 0)) # print(l) if flag==0: sum=l.theta() flag=1 lim1=l y= lim1[1] else : sum-=l.theta() lim2=l x= lim2[0] if sum<110 and sum>70: for i in range(0,3): print(lim1[i],lim2[i]) #x=0 #y=0 if x!=0 or y!=0: image.draw_cross(x=100, y=100, size=5, color=White) #image.draw_circle(x=100,y=100, radius=100,color=(0,255,0)) print("FPS %f" % clock.fps()) print(sum)
About negative rho values:
A [theta+0:-rho] tuple is the same as [theta+180:+rho].
之前在看了几个帖子要升级固件,特地看了下固件应该是没啥问题
求大佬指点迷津